xref: /freebsd/sys/dev/usb/usb_transfer.c (revision bce32d7bc4ed80cc0de92e82b86daddce08071b6)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
2702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
2802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
2902ac6454SAndrew Thompson #include <dev/usb/usb.h>
3002ac6454SAndrew Thompson #include <dev/usb/usb_defs.h>
3102ac6454SAndrew Thompson 
3202ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_debug
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3502ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
3702ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
3802ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
4002ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
4402ac6454SAndrew Thompson 
4502ac6454SAndrew Thompson struct usb2_std_packet_size {
4602ac6454SAndrew Thompson 	struct {
4702ac6454SAndrew Thompson 		uint16_t min;		/* inclusive */
4802ac6454SAndrew Thompson 		uint16_t max;		/* inclusive */
4902ac6454SAndrew Thompson 	}	range;
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson 	uint16_t fixed[4];
5202ac6454SAndrew Thompson };
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson /*
5502ac6454SAndrew Thompson  * This table stores the all the allowed packet sizes based on
5602ac6454SAndrew Thompson  * endpoint type and USB speed:
5702ac6454SAndrew Thompson  */
5802ac6454SAndrew Thompson static const struct usb2_std_packet_size
5902ac6454SAndrew Thompson 	usb2_std_packet_size[4][USB_SPEED_MAX] = {
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson 	[UE_INTERRUPT] = {
6202ac6454SAndrew Thompson 		[USB_SPEED_LOW] = {.range = {0, 8}},
6302ac6454SAndrew Thompson 		[USB_SPEED_FULL] = {.range = {0, 64}},
6402ac6454SAndrew Thompson 		[USB_SPEED_HIGH] = {.range = {0, 1024}},
6502ac6454SAndrew Thompson 		[USB_SPEED_VARIABLE] = {.range = {0, 1024}},
6602ac6454SAndrew Thompson 		[USB_SPEED_SUPER] = {.range = {0, 1024}},
6702ac6454SAndrew Thompson 	},
6802ac6454SAndrew Thompson 
6902ac6454SAndrew Thompson 	[UE_CONTROL] = {
7002ac6454SAndrew Thompson 		[USB_SPEED_LOW] = {.fixed = {8, 8, 8, 8}},
7102ac6454SAndrew Thompson 		[USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
7202ac6454SAndrew Thompson 		[USB_SPEED_HIGH] = {.fixed = {64, 64, 64, 64}},
7302ac6454SAndrew Thompson 		[USB_SPEED_VARIABLE] = {.fixed = {512, 512, 512, 512}},
7402ac6454SAndrew Thompson 		[USB_SPEED_SUPER] = {.fixed = {512, 512, 512, 512}},
7502ac6454SAndrew Thompson 	},
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson 	[UE_BULK] = {
7802ac6454SAndrew Thompson 		[USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}},	/* invalid */
7902ac6454SAndrew Thompson 		[USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
8002ac6454SAndrew Thompson 		[USB_SPEED_HIGH] = {.fixed = {512, 512, 512, 512}},
8102ac6454SAndrew Thompson 		[USB_SPEED_VARIABLE] = {.fixed = {512, 512, 1024, 1536}},
8202ac6454SAndrew Thompson 		[USB_SPEED_SUPER] = {.fixed = {1024, 1024, 1024, 1024}},
8302ac6454SAndrew Thompson 	},
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson 	[UE_ISOCHRONOUS] = {
8602ac6454SAndrew Thompson 		[USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}},	/* invalid */
8702ac6454SAndrew Thompson 		[USB_SPEED_FULL] = {.range = {0, 1023}},
8802ac6454SAndrew Thompson 		[USB_SPEED_HIGH] = {.range = {0, 1024}},
8902ac6454SAndrew Thompson 		[USB_SPEED_VARIABLE] = {.range = {0, 3584}},
9002ac6454SAndrew Thompson 		[USB_SPEED_SUPER] = {.range = {0, 1024}},
9102ac6454SAndrew Thompson 	},
9202ac6454SAndrew Thompson };
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson static const struct usb2_config usb2_control_ep_cfg[USB_DEFAULT_XFER_MAX] = {
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson 	/* This transfer is used for generic control endpoint transfers */
9702ac6454SAndrew Thompson 
9802ac6454SAndrew Thompson 	[0] = {
9902ac6454SAndrew Thompson 		.type = UE_CONTROL,
10002ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control endpoint */
10102ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
10202ac6454SAndrew Thompson 		.mh.bufsize = 1024,	/* bytes */
10302ac6454SAndrew Thompson 		.mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
10402ac6454SAndrew Thompson 		.mh.callback = &usb2_do_request_callback,
10502ac6454SAndrew Thompson 		.md.bufsize = 1024,	/* bytes */
10602ac6454SAndrew Thompson 		.md.flags = {.proxy_buffer = 1,.short_xfer_ok = 0,},
10702ac6454SAndrew Thompson 		.md.callback = &usb2_handle_request_callback,
10802ac6454SAndrew Thompson 	},
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 	/* This transfer is used for generic clear stall only */
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson 	[1] = {
11302ac6454SAndrew Thompson 		.type = UE_CONTROL,
11402ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
11502ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
11602ac6454SAndrew Thompson 		.mh.bufsize = sizeof(struct usb2_device_request),
11702ac6454SAndrew Thompson 		.mh.flags = {},
11802ac6454SAndrew Thompson 		.mh.callback = &usb2_do_clear_stall_callback,
11902ac6454SAndrew Thompson 		.mh.timeout = 1000,	/* 1 second */
12002ac6454SAndrew Thompson 		.mh.interval = 50,	/* 50ms */
12102ac6454SAndrew Thompson 	},
12202ac6454SAndrew Thompson };
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson /* function prototypes */
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson static void	usb2_update_max_frame_size(struct usb2_xfer *);
12702ac6454SAndrew Thompson static void	usb2_transfer_unsetup_sub(struct usb2_xfer_root *, uint8_t);
12802ac6454SAndrew Thompson static void	usb2_control_transfer_init(struct usb2_xfer *);
12902ac6454SAndrew Thompson static uint8_t	usb2_start_hardware_sub(struct usb2_xfer *);
13002ac6454SAndrew Thompson static void	usb2_callback_proc(struct usb2_proc_msg *);
13102ac6454SAndrew Thompson static void	usb2_callback_ss_done_defer(struct usb2_xfer *);
13202ac6454SAndrew Thompson static void	usb2_callback_wrapper(struct usb2_xfer_queue *);
13302ac6454SAndrew Thompson static void	usb2_dma_delay_done_cb(void *);
13402ac6454SAndrew Thompson static void	usb2_transfer_start_cb(void *);
13502ac6454SAndrew Thompson static uint8_t	usb2_callback_wrapper_sub(struct usb2_xfer *);
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
13802ac6454SAndrew Thompson  *	usb2_update_max_frame_size
13902ac6454SAndrew Thompson  *
14002ac6454SAndrew Thompson  * This function updates the maximum frame size, hence high speed USB
14102ac6454SAndrew Thompson  * can transfer multiple consecutive packets.
14202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
14302ac6454SAndrew Thompson static void
14402ac6454SAndrew Thompson usb2_update_max_frame_size(struct usb2_xfer *xfer)
14502ac6454SAndrew Thompson {
14602ac6454SAndrew Thompson 	/* compute maximum frame size */
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson 	if (xfer->max_packet_count == 2) {
14902ac6454SAndrew Thompson 		xfer->max_frame_size = 2 * xfer->max_packet_size;
15002ac6454SAndrew Thompson 	} else if (xfer->max_packet_count == 3) {
15102ac6454SAndrew Thompson 		xfer->max_frame_size = 3 * xfer->max_packet_size;
15202ac6454SAndrew Thompson 	} else {
15302ac6454SAndrew Thompson 		xfer->max_frame_size = xfer->max_packet_size;
15402ac6454SAndrew Thompson 	}
15502ac6454SAndrew Thompson }
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
15802ac6454SAndrew Thompson  *	usb2_get_dma_delay
15902ac6454SAndrew Thompson  *
16002ac6454SAndrew Thompson  * The following function is called when we need to
16102ac6454SAndrew Thompson  * synchronize with DMA hardware.
16202ac6454SAndrew Thompson  *
16302ac6454SAndrew Thompson  * Returns:
16402ac6454SAndrew Thompson  *    0: no DMA delay required
16502ac6454SAndrew Thompson  * Else: milliseconds of DMA delay
16602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
16702ac6454SAndrew Thompson uint32_t
16802ac6454SAndrew Thompson usb2_get_dma_delay(struct usb2_bus *bus)
16902ac6454SAndrew Thompson {
17002ac6454SAndrew Thompson 	uint32_t temp = 0;
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson 	if (bus->methods->get_dma_delay) {
17302ac6454SAndrew Thompson 		(bus->methods->get_dma_delay) (bus, &temp);
17402ac6454SAndrew Thompson 		/*
17502ac6454SAndrew Thompson 		 * Round up and convert to milliseconds. Note that we use
17602ac6454SAndrew Thompson 		 * 1024 milliseconds per second. to save a division.
17702ac6454SAndrew Thompson 		 */
17802ac6454SAndrew Thompson 		temp += 0x3FF;
17902ac6454SAndrew Thompson 		temp /= 0x400;
18002ac6454SAndrew Thompson 	}
18102ac6454SAndrew Thompson 	return (temp);
18202ac6454SAndrew Thompson }
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
18502ac6454SAndrew Thompson  *	usb2_transfer_setup_sub_malloc
18602ac6454SAndrew Thompson  *
18702ac6454SAndrew Thompson  * This function will allocate one or more DMA'able memory chunks
18802ac6454SAndrew Thompson  * according to "size", "align" and "count" arguments. "ppc" is
18902ac6454SAndrew Thompson  * pointed to a linear array of USB page caches afterwards.
19002ac6454SAndrew Thompson  *
19102ac6454SAndrew Thompson  * Returns:
19202ac6454SAndrew Thompson  *    0: Success
19302ac6454SAndrew Thompson  * Else: Failure
19402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
19502ac6454SAndrew Thompson uint8_t
19602ac6454SAndrew Thompson usb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm,
19702ac6454SAndrew Thompson     struct usb2_page_cache **ppc, uint32_t size, uint32_t align,
19802ac6454SAndrew Thompson     uint32_t count)
19902ac6454SAndrew Thompson {
20002ac6454SAndrew Thompson 	struct usb2_page_cache *pc;
20102ac6454SAndrew Thompson 	struct usb2_page *pg;
20202ac6454SAndrew Thompson 	void *buf;
20302ac6454SAndrew Thompson 	uint32_t n_dma_pc;
20402ac6454SAndrew Thompson 	uint32_t n_obj;
20502ac6454SAndrew Thompson 	uint32_t x;
20602ac6454SAndrew Thompson 	uint32_t y;
20702ac6454SAndrew Thompson 	uint32_t r;
20802ac6454SAndrew Thompson 	uint32_t z;
20902ac6454SAndrew Thompson 
21002ac6454SAndrew Thompson 	USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n",
21102ac6454SAndrew Thompson 	    align));
21202ac6454SAndrew Thompson 	USB_ASSERT(size > 0, ("Invalid size = 0!\n"));
21302ac6454SAndrew Thompson 
21402ac6454SAndrew Thompson 	if (count == 0) {
21502ac6454SAndrew Thompson 		return (0);		/* nothing to allocate */
21602ac6454SAndrew Thompson 	}
21702ac6454SAndrew Thompson 	/*
21802ac6454SAndrew Thompson 	 * Make sure that the size is aligned properly.
21902ac6454SAndrew Thompson 	 */
22002ac6454SAndrew Thompson 	size = -((-size) & (-align));
22102ac6454SAndrew Thompson 
22202ac6454SAndrew Thompson 	/*
22302ac6454SAndrew Thompson 	 * Try multi-allocation chunks to reduce the number of DMA
22402ac6454SAndrew Thompson 	 * allocations, hence DMA allocations are slow.
22502ac6454SAndrew Thompson 	 */
22602ac6454SAndrew Thompson 	if (size >= PAGE_SIZE) {
22702ac6454SAndrew Thompson 		n_dma_pc = count;
22802ac6454SAndrew Thompson 		n_obj = 1;
22902ac6454SAndrew Thompson 	} else {
23002ac6454SAndrew Thompson 		/* compute number of objects per page */
23102ac6454SAndrew Thompson 		n_obj = (PAGE_SIZE / size);
23202ac6454SAndrew Thompson 		/*
23302ac6454SAndrew Thompson 		 * Compute number of DMA chunks, rounded up
23402ac6454SAndrew Thompson 		 * to nearest one:
23502ac6454SAndrew Thompson 		 */
23602ac6454SAndrew Thompson 		n_dma_pc = ((count + n_obj - 1) / n_obj);
23702ac6454SAndrew Thompson 	}
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 	if (parm->buf == NULL) {
24002ac6454SAndrew Thompson 		/* for the future */
24102ac6454SAndrew Thompson 		parm->dma_page_ptr += n_dma_pc;
24202ac6454SAndrew Thompson 		parm->dma_page_cache_ptr += n_dma_pc;
24302ac6454SAndrew Thompson 		parm->dma_page_ptr += count;
24402ac6454SAndrew Thompson 		parm->xfer_page_cache_ptr += count;
24502ac6454SAndrew Thompson 		return (0);
24602ac6454SAndrew Thompson 	}
24702ac6454SAndrew Thompson 	for (x = 0; x != n_dma_pc; x++) {
24802ac6454SAndrew Thompson 		/* need to initialize the page cache */
24902ac6454SAndrew Thompson 		parm->dma_page_cache_ptr[x].tag_parent =
25002ac6454SAndrew Thompson 		    &parm->curr_xfer->xroot->dma_parent_tag;
25102ac6454SAndrew Thompson 	}
25202ac6454SAndrew Thompson 	for (x = 0; x != count; x++) {
25302ac6454SAndrew Thompson 		/* need to initialize the page cache */
25402ac6454SAndrew Thompson 		parm->xfer_page_cache_ptr[x].tag_parent =
25502ac6454SAndrew Thompson 		    &parm->curr_xfer->xroot->dma_parent_tag;
25602ac6454SAndrew Thompson 	}
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	if (ppc) {
25902ac6454SAndrew Thompson 		*ppc = parm->xfer_page_cache_ptr;
26002ac6454SAndrew Thompson 	}
26102ac6454SAndrew Thompson 	r = count;			/* set remainder count */
26202ac6454SAndrew Thompson 	z = n_obj * size;		/* set allocation size */
26302ac6454SAndrew Thompson 	pc = parm->xfer_page_cache_ptr;
26402ac6454SAndrew Thompson 	pg = parm->dma_page_ptr;
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson 	for (x = 0; x != n_dma_pc; x++) {
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 		if (r < n_obj) {
26902ac6454SAndrew Thompson 			/* compute last remainder */
27002ac6454SAndrew Thompson 			z = r * size;
27102ac6454SAndrew Thompson 			n_obj = r;
27202ac6454SAndrew Thompson 		}
27302ac6454SAndrew Thompson 		if (usb2_pc_alloc_mem(parm->dma_page_cache_ptr,
27402ac6454SAndrew Thompson 		    pg, z, align)) {
27502ac6454SAndrew Thompson 			return (1);	/* failure */
27602ac6454SAndrew Thompson 		}
27702ac6454SAndrew Thompson 		/* Set beginning of current buffer */
27802ac6454SAndrew Thompson 		buf = parm->dma_page_cache_ptr->buffer;
27902ac6454SAndrew Thompson 		/* Make room for one DMA page cache and one page */
28002ac6454SAndrew Thompson 		parm->dma_page_cache_ptr++;
28102ac6454SAndrew Thompson 		pg++;
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson 		for (y = 0; (y != n_obj); y++, r--, pc++, pg++) {
28402ac6454SAndrew Thompson 
28502ac6454SAndrew Thompson 			/* Load sub-chunk into DMA */
28602ac6454SAndrew Thompson 			if (usb2_pc_dmamap_create(pc, size)) {
28702ac6454SAndrew Thompson 				return (1);	/* failure */
28802ac6454SAndrew Thompson 			}
28902ac6454SAndrew Thompson 			pc->buffer = USB_ADD_BYTES(buf, y * size);
29002ac6454SAndrew Thompson 			pc->page_start = pg;
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson 			mtx_lock(pc->tag_parent->mtx);
29302ac6454SAndrew Thompson 			if (usb2_pc_load_mem(pc, size, 1 /* synchronous */ )) {
29402ac6454SAndrew Thompson 				mtx_unlock(pc->tag_parent->mtx);
29502ac6454SAndrew Thompson 				return (1);	/* failure */
29602ac6454SAndrew Thompson 			}
29702ac6454SAndrew Thompson 			mtx_unlock(pc->tag_parent->mtx);
29802ac6454SAndrew Thompson 		}
29902ac6454SAndrew Thompson 	}
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 	parm->xfer_page_cache_ptr = pc;
30202ac6454SAndrew Thompson 	parm->dma_page_ptr = pg;
30302ac6454SAndrew Thompson 	return (0);
30402ac6454SAndrew Thompson }
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
30702ac6454SAndrew Thompson  *	usb2_transfer_setup_sub - transfer setup subroutine
30802ac6454SAndrew Thompson  *
30902ac6454SAndrew Thompson  * This function must be called from the "xfer_setup" callback of the
31002ac6454SAndrew Thompson  * USB Host or Device controller driver when setting up an USB
31102ac6454SAndrew Thompson  * transfer. This function will setup correct packet sizes, buffer
31202ac6454SAndrew Thompson  * sizes, flags and more, that are stored in the "usb2_xfer"
31302ac6454SAndrew Thompson  * structure.
31402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
31502ac6454SAndrew Thompson void
31602ac6454SAndrew Thompson usb2_transfer_setup_sub(struct usb2_setup_params *parm)
31702ac6454SAndrew Thompson {
31802ac6454SAndrew Thompson 	enum {
31902ac6454SAndrew Thompson 		REQ_SIZE = 8,
32002ac6454SAndrew Thompson 		MIN_PKT = 8,
32102ac6454SAndrew Thompson 	};
32202ac6454SAndrew Thompson 	struct usb2_xfer *xfer = parm->curr_xfer;
32302ac6454SAndrew Thompson 	const struct usb2_config_sub *setup_sub = parm->curr_setup_sub;
32402ac6454SAndrew Thompson 	struct usb2_endpoint_descriptor *edesc;
32502ac6454SAndrew Thompson 	struct usb2_std_packet_size std_size;
32602ac6454SAndrew Thompson 	uint32_t n_frlengths;
32702ac6454SAndrew Thompson 	uint32_t n_frbuffers;
32802ac6454SAndrew Thompson 	uint32_t x;
32902ac6454SAndrew Thompson 	uint8_t type;
33002ac6454SAndrew Thompson 	uint8_t zmps;
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	/*
33302ac6454SAndrew Thompson 	 * Sanity check. The following parameters must be initialized before
33402ac6454SAndrew Thompson 	 * calling this function.
33502ac6454SAndrew Thompson 	 */
33602ac6454SAndrew Thompson 	if ((parm->hc_max_packet_size == 0) ||
33702ac6454SAndrew Thompson 	    (parm->hc_max_packet_count == 0) ||
33802ac6454SAndrew Thompson 	    (parm->hc_max_frame_size == 0)) {
33902ac6454SAndrew Thompson 		parm->err = USB_ERR_INVAL;
34002ac6454SAndrew Thompson 		goto done;
34102ac6454SAndrew Thompson 	}
34202ac6454SAndrew Thompson 	edesc = xfer->pipe->edesc;
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 	type = (edesc->bmAttributes & UE_XFERTYPE);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	xfer->flags = setup_sub->flags;
34702ac6454SAndrew Thompson 	xfer->nframes = setup_sub->frames;
34802ac6454SAndrew Thompson 	xfer->timeout = setup_sub->timeout;
34902ac6454SAndrew Thompson 	xfer->callback = setup_sub->callback;
35002ac6454SAndrew Thompson 	xfer->interval = setup_sub->interval;
35102ac6454SAndrew Thompson 	xfer->endpoint = edesc->bEndpointAddress;
35202ac6454SAndrew Thompson 	xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
35302ac6454SAndrew Thompson 	xfer->max_packet_count = 1;
35402ac6454SAndrew Thompson 	/* make a shadow copy: */
35502ac6454SAndrew Thompson 	xfer->flags_int.usb2_mode = parm->udev->flags.usb2_mode;
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson 	parm->bufsize = setup_sub->bufsize;
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 	if (parm->speed == USB_SPEED_HIGH) {
36002ac6454SAndrew Thompson 		xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
36102ac6454SAndrew Thompson 		xfer->max_packet_size &= 0x7FF;
36202ac6454SAndrew Thompson 	}
36302ac6454SAndrew Thompson 	/* range check "max_packet_count" */
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	if (xfer->max_packet_count > parm->hc_max_packet_count) {
36602ac6454SAndrew Thompson 		xfer->max_packet_count = parm->hc_max_packet_count;
36702ac6454SAndrew Thompson 	}
36802ac6454SAndrew Thompson 	/* filter "wMaxPacketSize" according to HC capabilities */
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
37102ac6454SAndrew Thompson 	    (xfer->max_packet_size == 0)) {
37202ac6454SAndrew Thompson 		xfer->max_packet_size = parm->hc_max_packet_size;
37302ac6454SAndrew Thompson 	}
37402ac6454SAndrew Thompson 	/* filter "wMaxPacketSize" according to standard sizes */
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	std_size = usb2_std_packet_size[type][parm->speed];
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	if (std_size.range.min || std_size.range.max) {
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson 		if (xfer->max_packet_size < std_size.range.min) {
38102ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.range.min;
38202ac6454SAndrew Thompson 		}
38302ac6454SAndrew Thompson 		if (xfer->max_packet_size > std_size.range.max) {
38402ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.range.max;
38502ac6454SAndrew Thompson 		}
38602ac6454SAndrew Thompson 	} else {
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 		if (xfer->max_packet_size >= std_size.fixed[3]) {
38902ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.fixed[3];
39002ac6454SAndrew Thompson 		} else if (xfer->max_packet_size >= std_size.fixed[2]) {
39102ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.fixed[2];
39202ac6454SAndrew Thompson 		} else if (xfer->max_packet_size >= std_size.fixed[1]) {
39302ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.fixed[1];
39402ac6454SAndrew Thompson 		} else {
39502ac6454SAndrew Thompson 			/* only one possibility left */
39602ac6454SAndrew Thompson 			xfer->max_packet_size = std_size.fixed[0];
39702ac6454SAndrew Thompson 		}
39802ac6454SAndrew Thompson 	}
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 	/* compute "max_frame_size" */
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 	usb2_update_max_frame_size(xfer);
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson 	/* check interrupt interval and transfer pre-delay */
40502ac6454SAndrew Thompson 
40602ac6454SAndrew Thompson 	if (type == UE_ISOCHRONOUS) {
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 		uint32_t frame_limit;
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson 		xfer->interval = 0;	/* not used, must be zero */
41102ac6454SAndrew Thompson 		xfer->flags_int.isochronous_xfr = 1;	/* set flag */
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson 		if (xfer->timeout == 0) {
41402ac6454SAndrew Thompson 			/*
41502ac6454SAndrew Thompson 			 * set a default timeout in
41602ac6454SAndrew Thompson 			 * case something goes wrong!
41702ac6454SAndrew Thompson 			 */
41802ac6454SAndrew Thompson 			xfer->timeout = 1000 / 4;
41902ac6454SAndrew Thompson 		}
42002ac6454SAndrew Thompson 		switch (parm->speed) {
42102ac6454SAndrew Thompson 		case USB_SPEED_LOW:
42202ac6454SAndrew Thompson 		case USB_SPEED_FULL:
42302ac6454SAndrew Thompson 			frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
42402ac6454SAndrew Thompson 			break;
42502ac6454SAndrew Thompson 		default:
42602ac6454SAndrew Thompson 			frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
42702ac6454SAndrew Thompson 			break;
42802ac6454SAndrew Thompson 		}
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 		if (xfer->nframes > frame_limit) {
43102ac6454SAndrew Thompson 			/*
43202ac6454SAndrew Thompson 			 * this is not going to work
43302ac6454SAndrew Thompson 			 * cross hardware
43402ac6454SAndrew Thompson 			 */
43502ac6454SAndrew Thompson 			parm->err = USB_ERR_INVAL;
43602ac6454SAndrew Thompson 			goto done;
43702ac6454SAndrew Thompson 		}
43802ac6454SAndrew Thompson 		if (xfer->nframes == 0) {
43902ac6454SAndrew Thompson 			/*
44002ac6454SAndrew Thompson 			 * this is not a valid value
44102ac6454SAndrew Thompson 			 */
44202ac6454SAndrew Thompson 			parm->err = USB_ERR_ZERO_NFRAMES;
44302ac6454SAndrew Thompson 			goto done;
44402ac6454SAndrew Thompson 		}
44502ac6454SAndrew Thompson 	} else {
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 		/*
44802ac6454SAndrew Thompson 		 * if a value is specified use that else check the endpoint
44902ac6454SAndrew Thompson 		 * descriptor
45002ac6454SAndrew Thompson 		 */
45102ac6454SAndrew Thompson 		if (xfer->interval == 0) {
45202ac6454SAndrew Thompson 
45302ac6454SAndrew Thompson 			if (type == UE_INTERRUPT) {
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 				xfer->interval = edesc->bInterval;
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 				switch (parm->speed) {
45802ac6454SAndrew Thompson 				case USB_SPEED_SUPER:
45902ac6454SAndrew Thompson 				case USB_SPEED_VARIABLE:
46002ac6454SAndrew Thompson 					/* 125us -> 1ms */
46102ac6454SAndrew Thompson 					if (xfer->interval < 4)
46202ac6454SAndrew Thompson 						xfer->interval = 1;
46302ac6454SAndrew Thompson 					else if (xfer->interval > 16)
46402ac6454SAndrew Thompson 						xfer->interval = (1<<(16-4));
46502ac6454SAndrew Thompson 					else
46602ac6454SAndrew Thompson 						xfer->interval =
46702ac6454SAndrew Thompson 						    (1 << (xfer->interval-4));
46802ac6454SAndrew Thompson 					break;
46902ac6454SAndrew Thompson 				case USB_SPEED_HIGH:
47002ac6454SAndrew Thompson 					/* 125us -> 1ms */
47102ac6454SAndrew Thompson 					xfer->interval /= 8;
47202ac6454SAndrew Thompson 					break;
47302ac6454SAndrew Thompson 				default:
47402ac6454SAndrew Thompson 					break;
47502ac6454SAndrew Thompson 				}
47602ac6454SAndrew Thompson 				if (xfer->interval == 0) {
47702ac6454SAndrew Thompson 					/*
47802ac6454SAndrew Thompson 					 * One millisecond is the smallest
47902ac6454SAndrew Thompson 					 * interval we support:
48002ac6454SAndrew Thompson 					 */
48102ac6454SAndrew Thompson 					xfer->interval = 1;
48202ac6454SAndrew Thompson 				}
48302ac6454SAndrew Thompson 			}
48402ac6454SAndrew Thompson 		}
48502ac6454SAndrew Thompson 	}
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	/*
48802ac6454SAndrew Thompson 	 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
48902ac6454SAndrew Thompson 	 * to be equal to zero when setting up USB transfers, hence
49002ac6454SAndrew Thompson 	 * this leads to alot of extra code in the USB kernel.
49102ac6454SAndrew Thompson 	 */
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	if ((xfer->max_frame_size == 0) ||
49402ac6454SAndrew Thompson 	    (xfer->max_packet_size == 0)) {
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 		zmps = 1;
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 		if ((parm->bufsize <= MIN_PKT) &&
49902ac6454SAndrew Thompson 		    (type != UE_CONTROL) &&
50002ac6454SAndrew Thompson 		    (type != UE_BULK)) {
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 			/* workaround */
50302ac6454SAndrew Thompson 			xfer->max_packet_size = MIN_PKT;
50402ac6454SAndrew Thompson 			xfer->max_packet_count = 1;
50502ac6454SAndrew Thompson 			parm->bufsize = 0;	/* automatic setup length */
50602ac6454SAndrew Thompson 			usb2_update_max_frame_size(xfer);
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 		} else {
50902ac6454SAndrew Thompson 			parm->err = USB_ERR_ZERO_MAXP;
51002ac6454SAndrew Thompson 			goto done;
51102ac6454SAndrew Thompson 		}
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	} else {
51402ac6454SAndrew Thompson 		zmps = 0;
51502ac6454SAndrew Thompson 	}
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	/*
51802ac6454SAndrew Thompson 	 * check if we should setup a default
51902ac6454SAndrew Thompson 	 * length:
52002ac6454SAndrew Thompson 	 */
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 	if (parm->bufsize == 0) {
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 		parm->bufsize = xfer->max_frame_size;
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 		if (type == UE_ISOCHRONOUS) {
52702ac6454SAndrew Thompson 			parm->bufsize *= xfer->nframes;
52802ac6454SAndrew Thompson 		}
52902ac6454SAndrew Thompson 	}
53002ac6454SAndrew Thompson 	/*
53102ac6454SAndrew Thompson 	 * check if we are about to setup a proxy
53202ac6454SAndrew Thompson 	 * type of buffer:
53302ac6454SAndrew Thompson 	 */
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	if (xfer->flags.proxy_buffer) {
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 		/* round bufsize up */
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 		parm->bufsize += (xfer->max_frame_size - 1);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 		if (parm->bufsize < xfer->max_frame_size) {
54202ac6454SAndrew Thompson 			/* length wrapped around */
54302ac6454SAndrew Thompson 			parm->err = USB_ERR_INVAL;
54402ac6454SAndrew Thompson 			goto done;
54502ac6454SAndrew Thompson 		}
54602ac6454SAndrew Thompson 		/* subtract remainder */
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 		parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson 		/* add length of USB device request structure, if any */
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 		if (type == UE_CONTROL) {
55302ac6454SAndrew Thompson 			parm->bufsize += REQ_SIZE;	/* SETUP message */
55402ac6454SAndrew Thompson 		}
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson 	xfer->max_data_length = parm->bufsize;
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson 	/* Setup "n_frlengths" and "n_frbuffers" */
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	if (type == UE_ISOCHRONOUS) {
56102ac6454SAndrew Thompson 		n_frlengths = xfer->nframes;
56202ac6454SAndrew Thompson 		n_frbuffers = 1;
56302ac6454SAndrew Thompson 	} else {
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 		if (type == UE_CONTROL) {
56602ac6454SAndrew Thompson 			xfer->flags_int.control_xfr = 1;
56702ac6454SAndrew Thompson 			if (xfer->nframes == 0) {
56802ac6454SAndrew Thompson 				if (parm->bufsize <= REQ_SIZE) {
56902ac6454SAndrew Thompson 					/*
57002ac6454SAndrew Thompson 					 * there will never be any data
57102ac6454SAndrew Thompson 					 * stage
57202ac6454SAndrew Thompson 					 */
57302ac6454SAndrew Thompson 					xfer->nframes = 1;
57402ac6454SAndrew Thompson 				} else {
57502ac6454SAndrew Thompson 					xfer->nframes = 2;
57602ac6454SAndrew Thompson 				}
57702ac6454SAndrew Thompson 			}
57802ac6454SAndrew Thompson 		} else {
57902ac6454SAndrew Thompson 			if (xfer->nframes == 0) {
58002ac6454SAndrew Thompson 				xfer->nframes = 1;
58102ac6454SAndrew Thompson 			}
58202ac6454SAndrew Thompson 		}
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 		n_frlengths = xfer->nframes;
58502ac6454SAndrew Thompson 		n_frbuffers = xfer->nframes;
58602ac6454SAndrew Thompson 	}
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	/*
58902ac6454SAndrew Thompson 	 * check if we have room for the
59002ac6454SAndrew Thompson 	 * USB device request structure:
59102ac6454SAndrew Thompson 	 */
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	if (type == UE_CONTROL) {
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 		if (xfer->max_data_length < REQ_SIZE) {
59602ac6454SAndrew Thompson 			/* length wrapped around or too small bufsize */
59702ac6454SAndrew Thompson 			parm->err = USB_ERR_INVAL;
59802ac6454SAndrew Thompson 			goto done;
59902ac6454SAndrew Thompson 		}
60002ac6454SAndrew Thompson 		xfer->max_data_length -= REQ_SIZE;
60102ac6454SAndrew Thompson 	}
60202ac6454SAndrew Thompson 	/* setup "frlengths" */
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	xfer->frlengths = parm->xfer_length_ptr;
60502ac6454SAndrew Thompson 
60602ac6454SAndrew Thompson 	parm->xfer_length_ptr += n_frlengths;
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson 	/* setup "frbuffers" */
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	xfer->frbuffers = parm->xfer_page_cache_ptr;
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson 	parm->xfer_page_cache_ptr += n_frbuffers;
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	/*
61502ac6454SAndrew Thompson 	 * check if we need to setup
61602ac6454SAndrew Thompson 	 * a local buffer:
61702ac6454SAndrew Thompson 	 */
61802ac6454SAndrew Thompson 
61902ac6454SAndrew Thompson 	if (!xfer->flags.ext_buffer) {
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 		/* align data */
62202ac6454SAndrew Thompson 		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
62302ac6454SAndrew Thompson 
62402ac6454SAndrew Thompson 		if (parm->buf) {
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 			xfer->local_buffer =
62702ac6454SAndrew Thompson 			    USB_ADD_BYTES(parm->buf, parm->size[0]);
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 			usb2_set_frame_offset(xfer, 0, 0);
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson 			if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
63202ac6454SAndrew Thompson 				usb2_set_frame_offset(xfer, REQ_SIZE, 1);
63302ac6454SAndrew Thompson 			}
63402ac6454SAndrew Thompson 		}
63502ac6454SAndrew Thompson 		parm->size[0] += parm->bufsize;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 		/* align data again */
63802ac6454SAndrew Thompson 		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
63902ac6454SAndrew Thompson 	}
64002ac6454SAndrew Thompson 	/*
64102ac6454SAndrew Thompson 	 * Compute maximum buffer size
64202ac6454SAndrew Thompson 	 */
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson 	if (parm->bufsize_max < parm->bufsize) {
64502ac6454SAndrew Thompson 		parm->bufsize_max = parm->bufsize;
64602ac6454SAndrew Thompson 	}
64702ac6454SAndrew Thompson 	if (xfer->flags_int.bdma_enable) {
64802ac6454SAndrew Thompson 		/*
64902ac6454SAndrew Thompson 		 * Setup "dma_page_ptr".
65002ac6454SAndrew Thompson 		 *
65102ac6454SAndrew Thompson 		 * Proof for formula below:
65202ac6454SAndrew Thompson 		 *
65302ac6454SAndrew Thompson 		 * Assume there are three USB frames having length "a", "b" and
65402ac6454SAndrew Thompson 		 * "c". These USB frames will at maximum need "z"
65502ac6454SAndrew Thompson 		 * "usb2_page" structures. "z" is given by:
65602ac6454SAndrew Thompson 		 *
65702ac6454SAndrew Thompson 		 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
65802ac6454SAndrew Thompson 		 * ((c / USB_PAGE_SIZE) + 2);
65902ac6454SAndrew Thompson 		 *
66002ac6454SAndrew Thompson 		 * Constraining "a", "b" and "c" like this:
66102ac6454SAndrew Thompson 		 *
66202ac6454SAndrew Thompson 		 * (a + b + c) <= parm->bufsize
66302ac6454SAndrew Thompson 		 *
66402ac6454SAndrew Thompson 		 * We know that:
66502ac6454SAndrew Thompson 		 *
66602ac6454SAndrew Thompson 		 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
66702ac6454SAndrew Thompson 		 *
66802ac6454SAndrew Thompson 		 * Here is the general formula:
66902ac6454SAndrew Thompson 		 */
67002ac6454SAndrew Thompson 		xfer->dma_page_ptr = parm->dma_page_ptr;
67102ac6454SAndrew Thompson 		parm->dma_page_ptr += (2 * n_frbuffers);
67202ac6454SAndrew Thompson 		parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
67302ac6454SAndrew Thompson 	}
67402ac6454SAndrew Thompson 	if (zmps) {
67502ac6454SAndrew Thompson 		/* correct maximum data length */
67602ac6454SAndrew Thompson 		xfer->max_data_length = 0;
67702ac6454SAndrew Thompson 	}
67802ac6454SAndrew Thompson 	/* subtract USB frame remainder from "hc_max_frame_size" */
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 	xfer->max_usb2_frame_size =
68102ac6454SAndrew Thompson 	    (parm->hc_max_frame_size -
68202ac6454SAndrew Thompson 	    (parm->hc_max_frame_size % xfer->max_frame_size));
68302ac6454SAndrew Thompson 
68402ac6454SAndrew Thompson 	if (xfer->max_usb2_frame_size == 0) {
68502ac6454SAndrew Thompson 		parm->err = USB_ERR_INVAL;
68602ac6454SAndrew Thompson 		goto done;
68702ac6454SAndrew Thompson 	}
68802ac6454SAndrew Thompson 	/* initialize max frame count */
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 	xfer->max_frame_count = xfer->nframes;
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	/* initialize frame buffers */
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	if (parm->buf) {
69502ac6454SAndrew Thompson 		for (x = 0; x != n_frbuffers; x++) {
69602ac6454SAndrew Thompson 			xfer->frbuffers[x].tag_parent =
69702ac6454SAndrew Thompson 			    &xfer->xroot->dma_parent_tag;
69802ac6454SAndrew Thompson 
69902ac6454SAndrew Thompson 			if (xfer->flags_int.bdma_enable &&
70002ac6454SAndrew Thompson 			    (parm->bufsize_max > 0)) {
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson 				if (usb2_pc_dmamap_create(
70302ac6454SAndrew Thompson 				    xfer->frbuffers + x,
70402ac6454SAndrew Thompson 				    parm->bufsize_max)) {
70502ac6454SAndrew Thompson 					parm->err = USB_ERR_NOMEM;
70602ac6454SAndrew Thompson 					goto done;
70702ac6454SAndrew Thompson 				}
70802ac6454SAndrew Thompson 			}
70902ac6454SAndrew Thompson 		}
71002ac6454SAndrew Thompson 	}
71102ac6454SAndrew Thompson done:
71202ac6454SAndrew Thompson 	if (parm->err) {
71302ac6454SAndrew Thompson 		/*
71402ac6454SAndrew Thompson 		 * Set some dummy values so that we avoid division by zero:
71502ac6454SAndrew Thompson 		 */
71602ac6454SAndrew Thompson 		xfer->max_usb2_frame_size = 1;
71702ac6454SAndrew Thompson 		xfer->max_frame_size = 1;
71802ac6454SAndrew Thompson 		xfer->max_packet_size = 1;
71902ac6454SAndrew Thompson 		xfer->max_data_length = 0;
72002ac6454SAndrew Thompson 		xfer->nframes = 0;
72102ac6454SAndrew Thompson 		xfer->max_frame_count = 0;
72202ac6454SAndrew Thompson 	}
72302ac6454SAndrew Thompson }
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
72602ac6454SAndrew Thompson  *	usb2_transfer_setup - setup an array of USB transfers
72702ac6454SAndrew Thompson  *
72802ac6454SAndrew Thompson  * NOTE: You must always call "usb2_transfer_unsetup" after calling
72902ac6454SAndrew Thompson  * "usb2_transfer_setup" if success was returned.
73002ac6454SAndrew Thompson  *
73102ac6454SAndrew Thompson  * The idea is that the USB device driver should pre-allocate all its
73202ac6454SAndrew Thompson  * transfers by one call to this function.
73302ac6454SAndrew Thompson  *
73402ac6454SAndrew Thompson  * Return values:
73502ac6454SAndrew Thompson  *    0: Success
73602ac6454SAndrew Thompson  * Else: Failure
73702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
73802ac6454SAndrew Thompson usb2_error_t
73902ac6454SAndrew Thompson usb2_transfer_setup(struct usb2_device *udev,
74002ac6454SAndrew Thompson     const uint8_t *ifaces, struct usb2_xfer **ppxfer,
74102ac6454SAndrew Thompson     const struct usb2_config *setup_start, uint16_t n_setup,
74202ac6454SAndrew Thompson     void *priv_sc, struct mtx *xfer_mtx)
74302ac6454SAndrew Thompson {
74402ac6454SAndrew Thompson 	struct usb2_xfer dummy;
74502ac6454SAndrew Thompson 	struct usb2_setup_params parm;
74602ac6454SAndrew Thompson 	const struct usb2_config *setup_end = setup_start + n_setup;
74702ac6454SAndrew Thompson 	const struct usb2_config *setup;
74802ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
74902ac6454SAndrew Thompson 	struct usb2_xfer_root *info;
75002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
75102ac6454SAndrew Thompson 	void *buf = NULL;
75202ac6454SAndrew Thompson 	uint16_t n;
75302ac6454SAndrew Thompson 	uint16_t refcount;
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson 	parm.err = 0;
75602ac6454SAndrew Thompson 	refcount = 0;
75702ac6454SAndrew Thompson 	info = NULL;
75802ac6454SAndrew Thompson 
75902ac6454SAndrew Thompson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
76002ac6454SAndrew Thompson 	    "usb2_transfer_setup can sleep!");
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 	/* do some checking first */
76302ac6454SAndrew Thompson 
76402ac6454SAndrew Thompson 	if (n_setup == 0) {
76502ac6454SAndrew Thompson 		DPRINTFN(6, "setup array has zero length!\n");
76602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
76702ac6454SAndrew Thompson 	}
76802ac6454SAndrew Thompson 	if (ifaces == 0) {
76902ac6454SAndrew Thompson 		DPRINTFN(6, "ifaces array is NULL!\n");
77002ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
77102ac6454SAndrew Thompson 	}
77202ac6454SAndrew Thompson 	if (xfer_mtx == NULL) {
77302ac6454SAndrew Thompson 		DPRINTFN(6, "using global lock\n");
77402ac6454SAndrew Thompson 		xfer_mtx = &Giant;
77502ac6454SAndrew Thompson 	}
77602ac6454SAndrew Thompson 	/* sanity checks */
77702ac6454SAndrew Thompson 	for (setup = setup_start, n = 0;
77802ac6454SAndrew Thompson 	    setup != setup_end; setup++, n++) {
77902ac6454SAndrew Thompson 		if ((setup->mh.bufsize == 0xffffffff) ||
78002ac6454SAndrew Thompson 		    (setup->md.bufsize == 0xffffffff)) {
78102ac6454SAndrew Thompson 			parm.err = USB_ERR_BAD_BUFSIZE;
78202ac6454SAndrew Thompson 			DPRINTF("invalid bufsize\n");
78302ac6454SAndrew Thompson 		}
78402ac6454SAndrew Thompson 		if ((setup->mh.callback == NULL) &&
78502ac6454SAndrew Thompson 		    (setup->md.callback == NULL)) {
78602ac6454SAndrew Thompson 			parm.err = USB_ERR_NO_CALLBACK;
78702ac6454SAndrew Thompson 			DPRINTF("no callback\n");
78802ac6454SAndrew Thompson 		}
78902ac6454SAndrew Thompson 		ppxfer[n] = NULL;
79002ac6454SAndrew Thompson 	}
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 	if (parm.err) {
79302ac6454SAndrew Thompson 		goto done;
79402ac6454SAndrew Thompson 	}
79502ac6454SAndrew Thompson 	bzero(&parm, sizeof(parm));
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 	parm.udev = udev;
79802ac6454SAndrew Thompson 	parm.speed = usb2_get_speed(udev);
79902ac6454SAndrew Thompson 	parm.hc_max_packet_count = 1;
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	if (parm.speed >= USB_SPEED_MAX) {
80202ac6454SAndrew Thompson 		parm.err = USB_ERR_INVAL;
80302ac6454SAndrew Thompson 		goto done;
80402ac6454SAndrew Thompson 	}
80502ac6454SAndrew Thompson 	/* setup all transfers */
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	while (1) {
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 		if (buf) {
81002ac6454SAndrew Thompson 			/*
81102ac6454SAndrew Thompson 			 * Initialize the "usb2_xfer_root" structure,
81202ac6454SAndrew Thompson 			 * which is common for all our USB transfers.
81302ac6454SAndrew Thompson 			 */
81402ac6454SAndrew Thompson 			info = USB_ADD_BYTES(buf, 0);
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 			info->memory_base = buf;
81702ac6454SAndrew Thompson 			info->memory_size = parm.size[0];
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 			info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
82002ac6454SAndrew Thompson 			info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
82102ac6454SAndrew Thompson 			info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
82202ac6454SAndrew Thompson 			info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 			usb2_cv_init(&info->cv_drain, "WDRAIN");
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson 			info->xfer_mtx = xfer_mtx;
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 			usb2_dma_tag_setup(&info->dma_parent_tag,
82902ac6454SAndrew Thompson 			    parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
83002ac6454SAndrew Thompson 			    xfer_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 			info->bus = udev->bus;
83302ac6454SAndrew Thompson 			info->udev = udev;
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 			TAILQ_INIT(&info->done_q.head);
83602ac6454SAndrew Thompson 			info->done_q.command = &usb2_callback_wrapper;
83702ac6454SAndrew Thompson 
83802ac6454SAndrew Thompson 			TAILQ_INIT(&info->dma_q.head);
83902ac6454SAndrew Thompson 			info->dma_q.command = &usb2_bdma_work_loop;
84002ac6454SAndrew Thompson 
84102ac6454SAndrew Thompson 			info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
84202ac6454SAndrew Thompson 			info->done_m[0].xroot = info;
84302ac6454SAndrew Thompson 			info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
84402ac6454SAndrew Thompson 			info->done_m[1].xroot = info;
84502ac6454SAndrew Thompson 
84602ac6454SAndrew Thompson 			if (xfer_mtx == &Giant)
84702ac6454SAndrew Thompson 				info->done_p =
84802ac6454SAndrew Thompson 				    &udev->bus->giant_callback_proc;
84902ac6454SAndrew Thompson 			else
85002ac6454SAndrew Thompson 				info->done_p =
85102ac6454SAndrew Thompson 				    &udev->bus->non_giant_callback_proc;
85202ac6454SAndrew Thompson 		}
85302ac6454SAndrew Thompson 		/* reset sizes */
85402ac6454SAndrew Thompson 
85502ac6454SAndrew Thompson 		parm.size[0] = 0;
85602ac6454SAndrew Thompson 		parm.buf = buf;
85702ac6454SAndrew Thompson 		parm.size[0] += sizeof(info[0]);
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 		for (setup = setup_start, n = 0;
86002ac6454SAndrew Thompson 		    setup != setup_end; setup++, n++) {
86102ac6454SAndrew Thompson 
86202ac6454SAndrew Thompson 			/* select mode specific structure */
86302ac6454SAndrew Thompson 			if (udev->flags.usb2_mode == USB_MODE_HOST) {
86402ac6454SAndrew Thompson 				parm.curr_setup_sub = &setup->mh;
86502ac6454SAndrew Thompson 			} else {
86602ac6454SAndrew Thompson 				parm.curr_setup_sub = &setup->md;
86702ac6454SAndrew Thompson 			}
86802ac6454SAndrew Thompson 			/* skip USB transfers without callbacks: */
86902ac6454SAndrew Thompson 			if (parm.curr_setup_sub->callback == NULL) {
87002ac6454SAndrew Thompson 				continue;
87102ac6454SAndrew Thompson 			}
87202ac6454SAndrew Thompson 			/* see if there is a matching endpoint */
87302ac6454SAndrew Thompson 			pipe = usb2_get_pipe(udev,
87402ac6454SAndrew Thompson 			    ifaces[setup->if_index], setup);
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 			if (!pipe) {
87702ac6454SAndrew Thompson 				if (parm.curr_setup_sub->flags.no_pipe_ok) {
87802ac6454SAndrew Thompson 					continue;
87902ac6454SAndrew Thompson 				}
88002ac6454SAndrew Thompson 				parm.err = USB_ERR_NO_PIPE;
88102ac6454SAndrew Thompson 				goto done;
88202ac6454SAndrew Thompson 			}
88302ac6454SAndrew Thompson 			/* store current setup pointer */
88402ac6454SAndrew Thompson 			parm.curr_setup = setup;
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 			/* align data properly */
88702ac6454SAndrew Thompson 			parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
88802ac6454SAndrew Thompson 
88902ac6454SAndrew Thompson 			if (buf) {
89002ac6454SAndrew Thompson 				/*
89102ac6454SAndrew Thompson 				 * Common initialization of the
89202ac6454SAndrew Thompson 				 * "usb2_xfer" structure.
89302ac6454SAndrew Thompson 				 */
89402ac6454SAndrew Thompson 				xfer = USB_ADD_BYTES(buf, parm.size[0]);
89502ac6454SAndrew Thompson 				xfer->address = udev->address;
89602ac6454SAndrew Thompson 				xfer->priv_sc = priv_sc;
89702ac6454SAndrew Thompson 				xfer->xroot = info;
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 				usb2_callout_init_mtx(&xfer->timeout_handle,
90002ac6454SAndrew Thompson 				    &udev->bus->bus_mtx, 0);
90102ac6454SAndrew Thompson 			} else {
90202ac6454SAndrew Thompson 				/*
90302ac6454SAndrew Thompson 				 * Setup a dummy xfer, hence we are
90402ac6454SAndrew Thompson 				 * writing to the "usb2_xfer"
90502ac6454SAndrew Thompson 				 * structure pointed to by "xfer"
90602ac6454SAndrew Thompson 				 * before we have allocated any
90702ac6454SAndrew Thompson 				 * memory:
90802ac6454SAndrew Thompson 				 */
90902ac6454SAndrew Thompson 				xfer = &dummy;
91002ac6454SAndrew Thompson 				bzero(&dummy, sizeof(dummy));
91102ac6454SAndrew Thompson 				refcount++;
91202ac6454SAndrew Thompson 			}
91302ac6454SAndrew Thompson 
914bce32d7bSAndrew Thompson 			/* set transfer pipe pointer */
91502ac6454SAndrew Thompson 			xfer->pipe = pipe;
91602ac6454SAndrew Thompson 
917bce32d7bSAndrew Thompson 			parm.size[0] += sizeof(xfer[0]);
918bce32d7bSAndrew Thompson 			parm.methods = xfer->pipe->methods;
919bce32d7bSAndrew Thompson 			parm.curr_xfer = xfer;
920bce32d7bSAndrew Thompson 
921bce32d7bSAndrew Thompson 			/*
922bce32d7bSAndrew Thompson 			 * Call the Host or Device controller transfer
923bce32d7bSAndrew Thompson 			 * setup routine:
924bce32d7bSAndrew Thompson 			 */
925bce32d7bSAndrew Thompson 			(udev->bus->methods->xfer_setup) (&parm);
926bce32d7bSAndrew Thompson 
927bce32d7bSAndrew Thompson 			/* check for error */
928bce32d7bSAndrew Thompson 			if (parm.err)
929bce32d7bSAndrew Thompson 				goto done;
930bce32d7bSAndrew Thompson 
93102ac6454SAndrew Thompson 			if (buf) {
93202ac6454SAndrew Thompson 				/*
93302ac6454SAndrew Thompson 				 * Increment the pipe refcount. This
93402ac6454SAndrew Thompson 				 * basically prevents setting a new
93502ac6454SAndrew Thompson 				 * configuration and alternate setting
93602ac6454SAndrew Thompson 				 * when USB transfers are in use on
93702ac6454SAndrew Thompson 				 * the given interface. Search the USB
93802ac6454SAndrew Thompson 				 * code for "pipe->refcount" if you
93902ac6454SAndrew Thompson 				 * want more information.
94002ac6454SAndrew Thompson 				 */
94102ac6454SAndrew Thompson 				xfer->pipe->refcount++;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 				/*
944bce32d7bSAndrew Thompson 				 * Whenever we set ppxfer[] then we
945bce32d7bSAndrew Thompson 				 * also need to increment the
946bce32d7bSAndrew Thompson 				 * "setup_refcount":
94702ac6454SAndrew Thompson 				 */
948bce32d7bSAndrew Thompson 				info->setup_refcount++;
94902ac6454SAndrew Thompson 
950bce32d7bSAndrew Thompson 				/*
951bce32d7bSAndrew Thompson 				 * Transfer is successfully setup and
952bce32d7bSAndrew Thompson 				 * can be used:
953bce32d7bSAndrew Thompson 				 */
954bce32d7bSAndrew Thompson 				ppxfer[n] = xfer;
95502ac6454SAndrew Thompson 			}
95602ac6454SAndrew Thompson 		}
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 		if (buf || parm.err) {
95902ac6454SAndrew Thompson 			goto done;
96002ac6454SAndrew Thompson 		}
96102ac6454SAndrew Thompson 		if (refcount == 0) {
96202ac6454SAndrew Thompson 			/* no transfers - nothing to do ! */
96302ac6454SAndrew Thompson 			goto done;
96402ac6454SAndrew Thompson 		}
96502ac6454SAndrew Thompson 		/* align data properly */
96602ac6454SAndrew Thompson 		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
96702ac6454SAndrew Thompson 
96802ac6454SAndrew Thompson 		/* store offset temporarily */
96902ac6454SAndrew Thompson 		parm.size[1] = parm.size[0];
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson 		/*
97202ac6454SAndrew Thompson 		 * The number of DMA tags required depends on
97302ac6454SAndrew Thompson 		 * the number of endpoints. The current estimate
97402ac6454SAndrew Thompson 		 * for maximum number of DMA tags per endpoint
97502ac6454SAndrew Thompson 		 * is two.
97602ac6454SAndrew Thompson 		 */
97702ac6454SAndrew Thompson 		parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX);
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson 		/*
98002ac6454SAndrew Thompson 		 * DMA tags for QH, TD, Data and more.
98102ac6454SAndrew Thompson 		 */
98202ac6454SAndrew Thompson 		parm.dma_tag_max += 8;
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 		parm.dma_tag_p += parm.dma_tag_max;
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 		parm.size[0] += ((uint8_t *)parm.dma_tag_p) -
98702ac6454SAndrew Thompson 		    ((uint8_t *)0);
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 		/* align data properly */
99002ac6454SAndrew Thompson 		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
99102ac6454SAndrew Thompson 
99202ac6454SAndrew Thompson 		/* store offset temporarily */
99302ac6454SAndrew Thompson 		parm.size[3] = parm.size[0];
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 		parm.size[0] += ((uint8_t *)parm.dma_page_ptr) -
99602ac6454SAndrew Thompson 		    ((uint8_t *)0);
99702ac6454SAndrew Thompson 
99802ac6454SAndrew Thompson 		/* align data properly */
99902ac6454SAndrew Thompson 		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 		/* store offset temporarily */
100202ac6454SAndrew Thompson 		parm.size[4] = parm.size[0];
100302ac6454SAndrew Thompson 
100402ac6454SAndrew Thompson 		parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) -
100502ac6454SAndrew Thompson 		    ((uint8_t *)0);
100602ac6454SAndrew Thompson 
100702ac6454SAndrew Thompson 		/* store end offset temporarily */
100802ac6454SAndrew Thompson 		parm.size[5] = parm.size[0];
100902ac6454SAndrew Thompson 
101002ac6454SAndrew Thompson 		parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) -
101102ac6454SAndrew Thompson 		    ((uint8_t *)0);
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 		/* store end offset temporarily */
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 		parm.size[2] = parm.size[0];
101602ac6454SAndrew Thompson 
101702ac6454SAndrew Thompson 		/* align data properly */
101802ac6454SAndrew Thompson 		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
101902ac6454SAndrew Thompson 
102002ac6454SAndrew Thompson 		parm.size[6] = parm.size[0];
102102ac6454SAndrew Thompson 
102202ac6454SAndrew Thompson 		parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) -
102302ac6454SAndrew Thompson 		    ((uint8_t *)0);
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson 		/* align data properly */
102602ac6454SAndrew Thompson 		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 		/* allocate zeroed memory */
102902ac6454SAndrew Thompson 		buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO);
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson 		if (buf == NULL) {
103202ac6454SAndrew Thompson 			parm.err = USB_ERR_NOMEM;
103302ac6454SAndrew Thompson 			DPRINTFN(0, "cannot allocate memory block for "
103402ac6454SAndrew Thompson 			    "configuration (%d bytes)\n",
103502ac6454SAndrew Thompson 			    parm.size[0]);
103602ac6454SAndrew Thompson 			goto done;
103702ac6454SAndrew Thompson 		}
103802ac6454SAndrew Thompson 		parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]);
103902ac6454SAndrew Thompson 		parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]);
104002ac6454SAndrew Thompson 		parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]);
104102ac6454SAndrew Thompson 		parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]);
104202ac6454SAndrew Thompson 		parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
104302ac6454SAndrew Thompson 	}
104402ac6454SAndrew Thompson 
104502ac6454SAndrew Thompson done:
104602ac6454SAndrew Thompson 	if (buf) {
104702ac6454SAndrew Thompson 		if (info->setup_refcount == 0) {
104802ac6454SAndrew Thompson 			/*
104902ac6454SAndrew Thompson 			 * "usb2_transfer_unsetup_sub" will unlock
105002ac6454SAndrew Thompson 			 * the bus mutex before returning !
105102ac6454SAndrew Thompson 			 */
105202ac6454SAndrew Thompson 			USB_BUS_LOCK(info->bus);
105302ac6454SAndrew Thompson 
105402ac6454SAndrew Thompson 			/* something went wrong */
105502ac6454SAndrew Thompson 			usb2_transfer_unsetup_sub(info, 0);
105602ac6454SAndrew Thompson 		}
105702ac6454SAndrew Thompson 	}
105802ac6454SAndrew Thompson 	if (parm.err) {
105902ac6454SAndrew Thompson 		usb2_transfer_unsetup(ppxfer, n_setup);
106002ac6454SAndrew Thompson 	}
106102ac6454SAndrew Thompson 	return (parm.err);
106202ac6454SAndrew Thompson }
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
106502ac6454SAndrew Thompson  *	usb2_transfer_unsetup_sub - factored out code
106602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
106702ac6454SAndrew Thompson static void
106802ac6454SAndrew Thompson usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay)
106902ac6454SAndrew Thompson {
107002ac6454SAndrew Thompson 	struct usb2_page_cache *pc;
107102ac6454SAndrew Thompson 	uint32_t temp;
107202ac6454SAndrew Thompson 
107302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson 	/* wait for any outstanding DMA operations */
107602ac6454SAndrew Thompson 
107702ac6454SAndrew Thompson 	if (needs_delay) {
107802ac6454SAndrew Thompson 		temp = usb2_get_dma_delay(info->bus);
107902ac6454SAndrew Thompson 		usb2_pause_mtx(&info->bus->bus_mtx,
108002ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(temp));
108102ac6454SAndrew Thompson 	}
108202ac6454SAndrew Thompson 
108302ac6454SAndrew Thompson 	/* make sure that our done messages are not queued anywhere */
108402ac6454SAndrew Thompson 	usb2_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
108502ac6454SAndrew Thompson 
108602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(info->bus);
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	/* free DMA'able memory, if any */
108902ac6454SAndrew Thompson 	pc = info->dma_page_cache_start;
109002ac6454SAndrew Thompson 	while (pc != info->dma_page_cache_end) {
109102ac6454SAndrew Thompson 		usb2_pc_free_mem(pc);
109202ac6454SAndrew Thompson 		pc++;
109302ac6454SAndrew Thompson 	}
109402ac6454SAndrew Thompson 
109502ac6454SAndrew Thompson 	/* free DMA maps in all "xfer->frbuffers" */
109602ac6454SAndrew Thompson 	pc = info->xfer_page_cache_start;
109702ac6454SAndrew Thompson 	while (pc != info->xfer_page_cache_end) {
109802ac6454SAndrew Thompson 		usb2_pc_dmamap_destroy(pc);
109902ac6454SAndrew Thompson 		pc++;
110002ac6454SAndrew Thompson 	}
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	/* free all DMA tags */
110302ac6454SAndrew Thompson 	usb2_dma_tag_unsetup(&info->dma_parent_tag);
110402ac6454SAndrew Thompson 
110502ac6454SAndrew Thompson 	usb2_cv_destroy(&info->cv_drain);
110602ac6454SAndrew Thompson 
110702ac6454SAndrew Thompson 	/*
110802ac6454SAndrew Thompson 	 * free the "memory_base" last, hence the "info" structure is
110902ac6454SAndrew Thompson 	 * contained within the "memory_base"!
111002ac6454SAndrew Thompson 	 */
111102ac6454SAndrew Thompson 	free(info->memory_base, M_USB);
111202ac6454SAndrew Thompson }
111302ac6454SAndrew Thompson 
111402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
111502ac6454SAndrew Thompson  *	usb2_transfer_unsetup - unsetup/free an array of USB transfers
111602ac6454SAndrew Thompson  *
111702ac6454SAndrew Thompson  * NOTE: All USB transfers in progress will get called back passing
111802ac6454SAndrew Thompson  * the error code "USB_ERR_CANCELLED" before this function
111902ac6454SAndrew Thompson  * returns.
112002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
112102ac6454SAndrew Thompson void
112202ac6454SAndrew Thompson usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup)
112302ac6454SAndrew Thompson {
112402ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
112502ac6454SAndrew Thompson 	struct usb2_xfer_root *info;
112602ac6454SAndrew Thompson 	uint8_t needs_delay = 0;
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
112902ac6454SAndrew Thompson 	    "usb2_transfer_unsetup can sleep!");
113002ac6454SAndrew Thompson 
113102ac6454SAndrew Thompson 	while (n_setup--) {
113202ac6454SAndrew Thompson 		xfer = pxfer[n_setup];
113302ac6454SAndrew Thompson 
1134bce32d7bSAndrew Thompson 		if (xfer == NULL)
1135bce32d7bSAndrew Thompson 			continue;
1136bce32d7bSAndrew Thompson 
1137bce32d7bSAndrew Thompson 		info = xfer->xroot;
1138bce32d7bSAndrew Thompson 
113902ac6454SAndrew Thompson 		USB_XFER_LOCK(xfer);
1140bce32d7bSAndrew Thompson 		USB_BUS_LOCK(info->bus);
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson 		/*
1143bce32d7bSAndrew Thompson 		 * HINT: when you start/stop a transfer, it might be a
1144bce32d7bSAndrew Thompson 		 * good idea to directly use the "pxfer[]" structure:
114502ac6454SAndrew Thompson 		 *
114602ac6454SAndrew Thompson 		 * usb2_transfer_start(sc->pxfer[0]);
114702ac6454SAndrew Thompson 		 * usb2_transfer_stop(sc->pxfer[0]);
114802ac6454SAndrew Thompson 		 *
1149bce32d7bSAndrew Thompson 		 * That way, if your code has many parts that will not
1150bce32d7bSAndrew Thompson 		 * stop running under the same lock, in other words
1151bce32d7bSAndrew Thompson 		 * "xfer_mtx", the usb2_transfer_start and
1152bce32d7bSAndrew Thompson 		 * usb2_transfer_stop functions will simply return
1153bce32d7bSAndrew Thompson 		 * when they detect a NULL pointer argument.
115402ac6454SAndrew Thompson 		 *
1155bce32d7bSAndrew Thompson 		 * To avoid any races we clear the "pxfer[]" pointer
1156bce32d7bSAndrew Thompson 		 * while holding the private mutex of the driver:
115702ac6454SAndrew Thompson 		 */
115802ac6454SAndrew Thompson 		pxfer[n_setup] = NULL;
115902ac6454SAndrew Thompson 
1160bce32d7bSAndrew Thompson 		USB_BUS_UNLOCK(info->bus);
116102ac6454SAndrew Thompson 		USB_XFER_UNLOCK(xfer);
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 		usb2_transfer_drain(xfer);
116402ac6454SAndrew Thompson 
1165bce32d7bSAndrew Thompson 		if (xfer->flags_int.bdma_enable)
116602ac6454SAndrew Thompson 			needs_delay = 1;
1167bce32d7bSAndrew Thompson 
116802ac6454SAndrew Thompson 		/*
116902ac6454SAndrew Thompson 		 * NOTE: default pipe does not have an
117002ac6454SAndrew Thompson 		 * interface, even if pipe->iface_index == 0
117102ac6454SAndrew Thompson 		 */
117202ac6454SAndrew Thompson 		xfer->pipe->refcount--;
117302ac6454SAndrew Thompson 
117402ac6454SAndrew Thompson 		usb2_callout_drain(&xfer->timeout_handle);
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson 		USB_BUS_LOCK(info->bus);
117702ac6454SAndrew Thompson 
1178bce32d7bSAndrew Thompson 		USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
117902ac6454SAndrew Thompson 		    "reference count!\n"));
118002ac6454SAndrew Thompson 
118102ac6454SAndrew Thompson 		info->setup_refcount--;
118202ac6454SAndrew Thompson 
118302ac6454SAndrew Thompson 		if (info->setup_refcount == 0) {
118402ac6454SAndrew Thompson 			usb2_transfer_unsetup_sub(info,
118502ac6454SAndrew Thompson 			    needs_delay);
118602ac6454SAndrew Thompson 		} else {
118702ac6454SAndrew Thompson 			USB_BUS_UNLOCK(info->bus);
118802ac6454SAndrew Thompson 		}
118902ac6454SAndrew Thompson 	}
119002ac6454SAndrew Thompson }
119102ac6454SAndrew Thompson 
119202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
119302ac6454SAndrew Thompson  *	usb2_control_transfer_init - factored out code
119402ac6454SAndrew Thompson  *
119502ac6454SAndrew Thompson  * In USB Device Mode we have to wait for the SETUP packet which
119602ac6454SAndrew Thompson  * containst the "struct usb2_device_request" structure, before we can
119702ac6454SAndrew Thompson  * transfer any data. In USB Host Mode we already have the SETUP
119802ac6454SAndrew Thompson  * packet at the moment the USB transfer is started. This leads us to
119902ac6454SAndrew Thompson  * having to setup the USB transfer at two different places in
120002ac6454SAndrew Thompson  * time. This function just contains factored out control transfer
120102ac6454SAndrew Thompson  * initialisation code, so that we don't duplicate the code.
120202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
120302ac6454SAndrew Thompson static void
120402ac6454SAndrew Thompson usb2_control_transfer_init(struct usb2_xfer *xfer)
120502ac6454SAndrew Thompson {
120602ac6454SAndrew Thompson 	struct usb2_device_request req;
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 	/* copy out the USB request header */
120902ac6454SAndrew Thompson 
121002ac6454SAndrew Thompson 	usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
121102ac6454SAndrew Thompson 
121202ac6454SAndrew Thompson 	/* setup remainder */
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 	xfer->flags_int.control_rem = UGETW(req.wLength);
121502ac6454SAndrew Thompson 
121602ac6454SAndrew Thompson 	/* copy direction to endpoint variable */
121702ac6454SAndrew Thompson 
121802ac6454SAndrew Thompson 	xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT);
121902ac6454SAndrew Thompson 	xfer->endpoint |=
122002ac6454SAndrew Thompson 	    (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
122102ac6454SAndrew Thompson }
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
122402ac6454SAndrew Thompson  *	usb2_start_hardware_sub
122502ac6454SAndrew Thompson  *
122602ac6454SAndrew Thompson  * This function handles initialisation of control transfers. Control
122702ac6454SAndrew Thompson  * transfers are special in that regard that they can both transmit
122802ac6454SAndrew Thompson  * and receive data.
122902ac6454SAndrew Thompson  *
123002ac6454SAndrew Thompson  * Return values:
123102ac6454SAndrew Thompson  *    0: Success
123202ac6454SAndrew Thompson  * Else: Failure
123302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
123402ac6454SAndrew Thompson static uint8_t
123502ac6454SAndrew Thompson usb2_start_hardware_sub(struct usb2_xfer *xfer)
123602ac6454SAndrew Thompson {
123702ac6454SAndrew Thompson 	uint32_t len;
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson 	/* Check for control endpoint stall */
124002ac6454SAndrew Thompson 	if (xfer->flags.stall_pipe) {
124102ac6454SAndrew Thompson 		/* no longer active */
124202ac6454SAndrew Thompson 		xfer->flags_int.control_act = 0;
124302ac6454SAndrew Thompson 	}
124402ac6454SAndrew Thompson 	/*
124502ac6454SAndrew Thompson          * Check if there is a control
124602ac6454SAndrew Thompson          * transfer in progress:
124702ac6454SAndrew Thompson          */
124802ac6454SAndrew Thompson 	if (xfer->flags_int.control_act) {
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson 		if (xfer->flags_int.control_hdr) {
125102ac6454SAndrew Thompson 
125202ac6454SAndrew Thompson 			/* clear send header flag */
125302ac6454SAndrew Thompson 
125402ac6454SAndrew Thompson 			xfer->flags_int.control_hdr = 0;
125502ac6454SAndrew Thompson 
125602ac6454SAndrew Thompson 			/* setup control transfer */
125702ac6454SAndrew Thompson 			if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
125802ac6454SAndrew Thompson 				usb2_control_transfer_init(xfer);
125902ac6454SAndrew Thompson 			}
126002ac6454SAndrew Thompson 		}
126102ac6454SAndrew Thompson 		/* get data length */
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 		len = xfer->sumlen;
126402ac6454SAndrew Thompson 
126502ac6454SAndrew Thompson 	} else {
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 		/* the size of the SETUP structure is hardcoded ! */
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson 		if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) {
127002ac6454SAndrew Thompson 			DPRINTFN(0, "Wrong framelength %u != %zu\n",
127102ac6454SAndrew Thompson 			    xfer->frlengths[0], sizeof(struct
127202ac6454SAndrew Thompson 			    usb2_device_request));
127302ac6454SAndrew Thompson 			goto error;
127402ac6454SAndrew Thompson 		}
127502ac6454SAndrew Thompson 		/* check USB mode */
127602ac6454SAndrew Thompson 		if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
127702ac6454SAndrew Thompson 
127802ac6454SAndrew Thompson 			/* check number of frames */
127902ac6454SAndrew Thompson 			if (xfer->nframes != 1) {
128002ac6454SAndrew Thompson 				/*
128102ac6454SAndrew Thompson 			         * We need to receive the setup
128202ac6454SAndrew Thompson 			         * message first so that we know the
128302ac6454SAndrew Thompson 			         * data direction!
128402ac6454SAndrew Thompson 			         */
128502ac6454SAndrew Thompson 				DPRINTF("Misconfigured transfer\n");
128602ac6454SAndrew Thompson 				goto error;
128702ac6454SAndrew Thompson 			}
128802ac6454SAndrew Thompson 			/*
128902ac6454SAndrew Thompson 			 * Set a dummy "control_rem" value.  This
129002ac6454SAndrew Thompson 			 * variable will be overwritten later by a
129102ac6454SAndrew Thompson 			 * call to "usb2_control_transfer_init()" !
129202ac6454SAndrew Thompson 			 */
129302ac6454SAndrew Thompson 			xfer->flags_int.control_rem = 0xFFFF;
129402ac6454SAndrew Thompson 		} else {
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 			/* setup "endpoint" and "control_rem" */
129702ac6454SAndrew Thompson 
129802ac6454SAndrew Thompson 			usb2_control_transfer_init(xfer);
129902ac6454SAndrew Thompson 		}
130002ac6454SAndrew Thompson 
130102ac6454SAndrew Thompson 		/* set transfer-header flag */
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson 		xfer->flags_int.control_hdr = 1;
130402ac6454SAndrew Thompson 
130502ac6454SAndrew Thompson 		/* get data length */
130602ac6454SAndrew Thompson 
130702ac6454SAndrew Thompson 		len = (xfer->sumlen - sizeof(struct usb2_device_request));
130802ac6454SAndrew Thompson 	}
130902ac6454SAndrew Thompson 
131002ac6454SAndrew Thompson 	/* check if there is a length mismatch */
131102ac6454SAndrew Thompson 
131202ac6454SAndrew Thompson 	if (len > xfer->flags_int.control_rem) {
131302ac6454SAndrew Thompson 		DPRINTFN(0, "Length greater than remaining length!\n");
131402ac6454SAndrew Thompson 		goto error;
131502ac6454SAndrew Thompson 	}
131602ac6454SAndrew Thompson 	/* check if we are doing a short transfer */
131702ac6454SAndrew Thompson 
131802ac6454SAndrew Thompson 	if (xfer->flags.force_short_xfer) {
131902ac6454SAndrew Thompson 		xfer->flags_int.control_rem = 0;
132002ac6454SAndrew Thompson 	} else {
132102ac6454SAndrew Thompson 		if ((len != xfer->max_data_length) &&
132202ac6454SAndrew Thompson 		    (len != xfer->flags_int.control_rem) &&
132302ac6454SAndrew Thompson 		    (xfer->nframes != 1)) {
132402ac6454SAndrew Thompson 			DPRINTFN(0, "Short control transfer without "
132502ac6454SAndrew Thompson 			    "force_short_xfer set!\n");
132602ac6454SAndrew Thompson 			goto error;
132702ac6454SAndrew Thompson 		}
132802ac6454SAndrew Thompson 		xfer->flags_int.control_rem -= len;
132902ac6454SAndrew Thompson 	}
133002ac6454SAndrew Thompson 
133102ac6454SAndrew Thompson 	/* the status part is executed when "control_act" is 0 */
133202ac6454SAndrew Thompson 
133302ac6454SAndrew Thompson 	if ((xfer->flags_int.control_rem > 0) ||
133402ac6454SAndrew Thompson 	    (xfer->flags.manual_status)) {
133502ac6454SAndrew Thompson 		/* don't execute the STATUS stage yet */
133602ac6454SAndrew Thompson 		xfer->flags_int.control_act = 1;
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson 		/* sanity check */
133902ac6454SAndrew Thompson 		if ((!xfer->flags_int.control_hdr) &&
134002ac6454SAndrew Thompson 		    (xfer->nframes == 1)) {
134102ac6454SAndrew Thompson 			/*
134202ac6454SAndrew Thompson 		         * This is not a valid operation!
134302ac6454SAndrew Thompson 		         */
134402ac6454SAndrew Thompson 			DPRINTFN(0, "Invalid parameter "
134502ac6454SAndrew Thompson 			    "combination\n");
134602ac6454SAndrew Thompson 			goto error;
134702ac6454SAndrew Thompson 		}
134802ac6454SAndrew Thompson 	} else {
134902ac6454SAndrew Thompson 		/* time to execute the STATUS stage */
135002ac6454SAndrew Thompson 		xfer->flags_int.control_act = 0;
135102ac6454SAndrew Thompson 	}
135202ac6454SAndrew Thompson 	return (0);			/* success */
135302ac6454SAndrew Thompson 
135402ac6454SAndrew Thompson error:
135502ac6454SAndrew Thompson 	return (1);			/* failure */
135602ac6454SAndrew Thompson }
135702ac6454SAndrew Thompson 
135802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
135902ac6454SAndrew Thompson  *	usb2_start_hardware - start USB hardware for the given transfer
136002ac6454SAndrew Thompson  *
136102ac6454SAndrew Thompson  * This function should only be called from the USB callback.
136202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
136302ac6454SAndrew Thompson void
136402ac6454SAndrew Thompson usb2_start_hardware(struct usb2_xfer *xfer)
136502ac6454SAndrew Thompson {
136602ac6454SAndrew Thompson 	uint32_t x;
136702ac6454SAndrew Thompson 
136802ac6454SAndrew Thompson 	DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
136902ac6454SAndrew Thompson 	    xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
137002ac6454SAndrew Thompson 	    "read" : "write");
137102ac6454SAndrew Thompson 
137202ac6454SAndrew Thompson #if USB_DEBUG
137302ac6454SAndrew Thompson 	if (USB_DEBUG_VAR > 0) {
137402ac6454SAndrew Thompson 		USB_BUS_LOCK(xfer->xroot->bus);
137502ac6454SAndrew Thompson 
137602ac6454SAndrew Thompson 		usb2_dump_pipe(xfer->pipe);
137702ac6454SAndrew Thompson 
137802ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
137902ac6454SAndrew Thompson 	}
138002ac6454SAndrew Thompson #endif
138102ac6454SAndrew Thompson 
138202ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
138302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson 	/* Only open the USB transfer once! */
138602ac6454SAndrew Thompson 	if (!xfer->flags_int.open) {
138702ac6454SAndrew Thompson 		xfer->flags_int.open = 1;
138802ac6454SAndrew Thompson 
138902ac6454SAndrew Thompson 		DPRINTF("open\n");
139002ac6454SAndrew Thompson 
139102ac6454SAndrew Thompson 		USB_BUS_LOCK(xfer->xroot->bus);
139202ac6454SAndrew Thompson 		(xfer->pipe->methods->open) (xfer);
139302ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
139402ac6454SAndrew Thompson 	}
139502ac6454SAndrew Thompson 	/* set "transferring" flag */
139602ac6454SAndrew Thompson 	xfer->flags_int.transferring = 1;
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson 	/* increment power reference */
139902ac6454SAndrew Thompson 	usb2_transfer_power_ref(xfer, 1);
140002ac6454SAndrew Thompson 
140102ac6454SAndrew Thompson 	/*
140202ac6454SAndrew Thompson 	 * Check if the transfer is waiting on a queue, most
140302ac6454SAndrew Thompson 	 * frequently the "done_q":
140402ac6454SAndrew Thompson 	 */
140502ac6454SAndrew Thompson 	if (xfer->wait_queue) {
140602ac6454SAndrew Thompson 		USB_BUS_LOCK(xfer->xroot->bus);
140702ac6454SAndrew Thompson 		usb2_transfer_dequeue(xfer);
140802ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
140902ac6454SAndrew Thompson 	}
141002ac6454SAndrew Thompson 	/* clear "did_dma_delay" flag */
141102ac6454SAndrew Thompson 	xfer->flags_int.did_dma_delay = 0;
141202ac6454SAndrew Thompson 
141302ac6454SAndrew Thompson 	/* clear "did_close" flag */
141402ac6454SAndrew Thompson 	xfer->flags_int.did_close = 0;
141502ac6454SAndrew Thompson 
141602ac6454SAndrew Thompson 	/* clear "bdma_setup" flag */
141702ac6454SAndrew Thompson 	xfer->flags_int.bdma_setup = 0;
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson 	/* by default we cannot cancel any USB transfer immediately */
142002ac6454SAndrew Thompson 	xfer->flags_int.can_cancel_immed = 0;
142102ac6454SAndrew Thompson 
142202ac6454SAndrew Thompson 	/* clear lengths and frame counts by default */
142302ac6454SAndrew Thompson 	xfer->sumlen = 0;
142402ac6454SAndrew Thompson 	xfer->actlen = 0;
142502ac6454SAndrew Thompson 	xfer->aframes = 0;
142602ac6454SAndrew Thompson 
142702ac6454SAndrew Thompson 	/* clear any previous errors */
142802ac6454SAndrew Thompson 	xfer->error = 0;
142902ac6454SAndrew Thompson 
143002ac6454SAndrew Thompson 	/* sanity check */
143102ac6454SAndrew Thompson 
143202ac6454SAndrew Thompson 	if (xfer->nframes == 0) {
143302ac6454SAndrew Thompson 		if (xfer->flags.stall_pipe) {
143402ac6454SAndrew Thompson 			/*
143502ac6454SAndrew Thompson 			 * Special case - want to stall without transferring
143602ac6454SAndrew Thompson 			 * any data:
143702ac6454SAndrew Thompson 			 */
143802ac6454SAndrew Thompson 			DPRINTF("xfer=%p nframes=0: stall "
143902ac6454SAndrew Thompson 			    "or clear stall!\n", xfer);
144002ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
144102ac6454SAndrew Thompson 			xfer->flags_int.can_cancel_immed = 1;
144202ac6454SAndrew Thompson 			/* start the transfer */
144302ac6454SAndrew Thompson 			usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
144402ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
144502ac6454SAndrew Thompson 			return;
144602ac6454SAndrew Thompson 		}
144702ac6454SAndrew Thompson 		USB_BUS_LOCK(xfer->xroot->bus);
144802ac6454SAndrew Thompson 		usb2_transfer_done(xfer, USB_ERR_INVAL);
144902ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
145002ac6454SAndrew Thompson 		return;
145102ac6454SAndrew Thompson 	}
145202ac6454SAndrew Thompson 	/* compute total transfer length */
145302ac6454SAndrew Thompson 
145402ac6454SAndrew Thompson 	for (x = 0; x != xfer->nframes; x++) {
145502ac6454SAndrew Thompson 		xfer->sumlen += xfer->frlengths[x];
145602ac6454SAndrew Thompson 		if (xfer->sumlen < xfer->frlengths[x]) {
145702ac6454SAndrew Thompson 			/* length wrapped around */
145802ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
145902ac6454SAndrew Thompson 			usb2_transfer_done(xfer, USB_ERR_INVAL);
146002ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
146102ac6454SAndrew Thompson 			return;
146202ac6454SAndrew Thompson 		}
146302ac6454SAndrew Thompson 	}
146402ac6454SAndrew Thompson 
146502ac6454SAndrew Thompson 	/* clear some internal flags */
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 	xfer->flags_int.short_xfer_ok = 0;
146802ac6454SAndrew Thompson 	xfer->flags_int.short_frames_ok = 0;
146902ac6454SAndrew Thompson 
147002ac6454SAndrew Thompson 	/* check if this is a control transfer */
147102ac6454SAndrew Thompson 
147202ac6454SAndrew Thompson 	if (xfer->flags_int.control_xfr) {
147302ac6454SAndrew Thompson 
147402ac6454SAndrew Thompson 		if (usb2_start_hardware_sub(xfer)) {
147502ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
147602ac6454SAndrew Thompson 			usb2_transfer_done(xfer, USB_ERR_STALLED);
147702ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
147802ac6454SAndrew Thompson 			return;
147902ac6454SAndrew Thompson 		}
148002ac6454SAndrew Thompson 	}
148102ac6454SAndrew Thompson 	/*
148202ac6454SAndrew Thompson 	 * Setup filtered version of some transfer flags,
148302ac6454SAndrew Thompson 	 * in case of data read direction
148402ac6454SAndrew Thompson 	 */
148502ac6454SAndrew Thompson 	if (USB_GET_DATA_ISREAD(xfer)) {
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 		if (xfer->flags_int.control_xfr) {
148802ac6454SAndrew Thompson 
148902ac6454SAndrew Thompson 			/*
149002ac6454SAndrew Thompson 			 * Control transfers do not support reception
149102ac6454SAndrew Thompson 			 * of multiple short USB frames !
149202ac6454SAndrew Thompson 			 */
149302ac6454SAndrew Thompson 
149402ac6454SAndrew Thompson 			if (xfer->flags.short_xfer_ok) {
149502ac6454SAndrew Thompson 				xfer->flags_int.short_xfer_ok = 1;
149602ac6454SAndrew Thompson 			}
149702ac6454SAndrew Thompson 		} else {
149802ac6454SAndrew Thompson 
149902ac6454SAndrew Thompson 			if (xfer->flags.short_frames_ok) {
150002ac6454SAndrew Thompson 				xfer->flags_int.short_xfer_ok = 1;
150102ac6454SAndrew Thompson 				xfer->flags_int.short_frames_ok = 1;
150202ac6454SAndrew Thompson 			} else if (xfer->flags.short_xfer_ok) {
150302ac6454SAndrew Thompson 				xfer->flags_int.short_xfer_ok = 1;
150402ac6454SAndrew Thompson 			}
150502ac6454SAndrew Thompson 		}
150602ac6454SAndrew Thompson 	}
150702ac6454SAndrew Thompson 	/*
150802ac6454SAndrew Thompson 	 * Check if BUS-DMA support is enabled and try to load virtual
150902ac6454SAndrew Thompson 	 * buffers into DMA, if any:
151002ac6454SAndrew Thompson 	 */
151102ac6454SAndrew Thompson 	if (xfer->flags_int.bdma_enable) {
151202ac6454SAndrew Thompson 		/* insert the USB transfer last in the BUS-DMA queue */
151302ac6454SAndrew Thompson 		usb2_command_wrapper(&xfer->xroot->dma_q, xfer);
151402ac6454SAndrew Thompson 		return;
151502ac6454SAndrew Thompson 	}
151602ac6454SAndrew Thompson 	/*
151702ac6454SAndrew Thompson 	 * Enter the USB transfer into the Host Controller or
151802ac6454SAndrew Thompson 	 * Device Controller schedule:
151902ac6454SAndrew Thompson 	 */
152002ac6454SAndrew Thompson 	usb2_pipe_enter(xfer);
152102ac6454SAndrew Thompson }
152202ac6454SAndrew Thompson 
152302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
152402ac6454SAndrew Thompson  *	usb2_pipe_enter - factored out code
152502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
152602ac6454SAndrew Thompson void
152702ac6454SAndrew Thompson usb2_pipe_enter(struct usb2_xfer *xfer)
152802ac6454SAndrew Thompson {
152902ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
153202ac6454SAndrew Thompson 
153302ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
153402ac6454SAndrew Thompson 
153502ac6454SAndrew Thompson 	pipe = xfer->pipe;
153602ac6454SAndrew Thompson 
153702ac6454SAndrew Thompson 	DPRINTF("enter\n");
153802ac6454SAndrew Thompson 
153902ac6454SAndrew Thompson 	/* enter the transfer */
154002ac6454SAndrew Thompson 	(pipe->methods->enter) (xfer);
154102ac6454SAndrew Thompson 
154202ac6454SAndrew Thompson 	/* check cancelability */
154302ac6454SAndrew Thompson 	if (pipe->methods->enter_is_cancelable) {
154402ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 1;
154502ac6454SAndrew Thompson 		/* check for transfer error */
154602ac6454SAndrew Thompson 		if (xfer->error) {
154702ac6454SAndrew Thompson 			/* some error has happened */
154802ac6454SAndrew Thompson 			usb2_transfer_done(xfer, 0);
154902ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
155002ac6454SAndrew Thompson 			return;
155102ac6454SAndrew Thompson 		}
155202ac6454SAndrew Thompson 	} else {
155302ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 0;
155402ac6454SAndrew Thompson 	}
155502ac6454SAndrew Thompson 
155602ac6454SAndrew Thompson 	/* start the transfer */
155702ac6454SAndrew Thompson 	usb2_command_wrapper(&pipe->pipe_q, xfer);
155802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
155902ac6454SAndrew Thompson }
156002ac6454SAndrew Thompson 
156102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
156202ac6454SAndrew Thompson  *	usb2_transfer_start - start an USB transfer
156302ac6454SAndrew Thompson  *
156402ac6454SAndrew Thompson  * NOTE: Calling this function more than one time will only
156502ac6454SAndrew Thompson  *       result in a single transfer start, until the USB transfer
156602ac6454SAndrew Thompson  *       completes.
156702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
156802ac6454SAndrew Thompson void
156902ac6454SAndrew Thompson usb2_transfer_start(struct usb2_xfer *xfer)
157002ac6454SAndrew Thompson {
157102ac6454SAndrew Thompson 	if (xfer == NULL) {
157202ac6454SAndrew Thompson 		/* transfer is gone */
157302ac6454SAndrew Thompson 		return;
157402ac6454SAndrew Thompson 	}
157502ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
157602ac6454SAndrew Thompson 
157702ac6454SAndrew Thompson 	/* mark the USB transfer started */
157802ac6454SAndrew Thompson 
157902ac6454SAndrew Thompson 	if (!xfer->flags_int.started) {
158002ac6454SAndrew Thompson 		xfer->flags_int.started = 1;
158102ac6454SAndrew Thompson 	}
158202ac6454SAndrew Thompson 	/* check if the USB transfer callback is already transferring */
158302ac6454SAndrew Thompson 
158402ac6454SAndrew Thompson 	if (xfer->flags_int.transferring) {
158502ac6454SAndrew Thompson 		return;
158602ac6454SAndrew Thompson 	}
158702ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
158802ac6454SAndrew Thompson 	/* call the USB transfer callback */
158902ac6454SAndrew Thompson 	usb2_callback_ss_done_defer(xfer);
159002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
159102ac6454SAndrew Thompson }
159202ac6454SAndrew Thompson 
159302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
159402ac6454SAndrew Thompson  *	usb2_transfer_stop - stop an USB transfer
159502ac6454SAndrew Thompson  *
159602ac6454SAndrew Thompson  * NOTE: Calling this function more than one time will only
159702ac6454SAndrew Thompson  *       result in a single transfer stop.
159802ac6454SAndrew Thompson  * NOTE: When this function returns it is not safe to free nor
159902ac6454SAndrew Thompson  *       reuse any DMA buffers. See "usb2_transfer_drain()".
160002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
160102ac6454SAndrew Thompson void
160202ac6454SAndrew Thompson usb2_transfer_stop(struct usb2_xfer *xfer)
160302ac6454SAndrew Thompson {
160402ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson 	if (xfer == NULL) {
160702ac6454SAndrew Thompson 		/* transfer is gone */
160802ac6454SAndrew Thompson 		return;
160902ac6454SAndrew Thompson 	}
161002ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 	/* check if the USB transfer was ever opened */
161302ac6454SAndrew Thompson 
161402ac6454SAndrew Thompson 	if (!xfer->flags_int.open) {
161502ac6454SAndrew Thompson 		/* nothing to do except clearing the "started" flag */
161602ac6454SAndrew Thompson 		xfer->flags_int.started = 0;
161702ac6454SAndrew Thompson 		return;
161802ac6454SAndrew Thompson 	}
161902ac6454SAndrew Thompson 	/* try to stop the current USB transfer */
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
162202ac6454SAndrew Thompson 	xfer->error = USB_ERR_CANCELLED;/* override any previous error */
162302ac6454SAndrew Thompson 	/*
162402ac6454SAndrew Thompson 	 * Clear "open" and "started" when both private and USB lock
162502ac6454SAndrew Thompson 	 * is locked so that we don't get a race updating "flags_int"
162602ac6454SAndrew Thompson 	 */
162702ac6454SAndrew Thompson 	xfer->flags_int.open = 0;
162802ac6454SAndrew Thompson 	xfer->flags_int.started = 0;
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	/*
163102ac6454SAndrew Thompson 	 * Check if we can cancel the USB transfer immediately.
163202ac6454SAndrew Thompson 	 */
163302ac6454SAndrew Thompson 	if (xfer->flags_int.transferring) {
163402ac6454SAndrew Thompson 		if (xfer->flags_int.can_cancel_immed &&
163502ac6454SAndrew Thompson 		    (!xfer->flags_int.did_close)) {
163602ac6454SAndrew Thompson 			DPRINTF("close\n");
163702ac6454SAndrew Thompson 			/*
163802ac6454SAndrew Thompson 			 * The following will lead to an USB_ERR_CANCELLED
163902ac6454SAndrew Thompson 			 * error code being passed to the USB callback.
164002ac6454SAndrew Thompson 			 */
164102ac6454SAndrew Thompson 			(xfer->pipe->methods->close) (xfer);
164202ac6454SAndrew Thompson 			/* only close once */
164302ac6454SAndrew Thompson 			xfer->flags_int.did_close = 1;
164402ac6454SAndrew Thompson 		} else {
164502ac6454SAndrew Thompson 			/* need to wait for the next done callback */
164602ac6454SAndrew Thompson 		}
164702ac6454SAndrew Thompson 	} else {
164802ac6454SAndrew Thompson 		DPRINTF("close\n");
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson 		/* close here and now */
165102ac6454SAndrew Thompson 		(xfer->pipe->methods->close) (xfer);
165202ac6454SAndrew Thompson 
165302ac6454SAndrew Thompson 		/*
165402ac6454SAndrew Thompson 		 * Any additional DMA delay is done by
165502ac6454SAndrew Thompson 		 * "usb2_transfer_unsetup()".
165602ac6454SAndrew Thompson 		 */
165702ac6454SAndrew Thompson 
165802ac6454SAndrew Thompson 		/*
165902ac6454SAndrew Thompson 		 * Special case. Check if we need to restart a blocked
166002ac6454SAndrew Thompson 		 * pipe.
166102ac6454SAndrew Thompson 		 */
166202ac6454SAndrew Thompson 		pipe = xfer->pipe;
166302ac6454SAndrew Thompson 
166402ac6454SAndrew Thompson 		/*
166502ac6454SAndrew Thompson 		 * If the current USB transfer is completing we need
166602ac6454SAndrew Thompson 		 * to start the next one:
166702ac6454SAndrew Thompson 		 */
166802ac6454SAndrew Thompson 		if (pipe->pipe_q.curr == xfer) {
166902ac6454SAndrew Thompson 			usb2_command_wrapper(&pipe->pipe_q, NULL);
167002ac6454SAndrew Thompson 		}
167102ac6454SAndrew Thompson 	}
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
167402ac6454SAndrew Thompson }
167502ac6454SAndrew Thompson 
167602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
167702ac6454SAndrew Thompson  *	usb2_transfer_pending
167802ac6454SAndrew Thompson  *
167902ac6454SAndrew Thompson  * This function will check if an USB transfer is pending which is a
168002ac6454SAndrew Thompson  * little bit complicated!
168102ac6454SAndrew Thompson  * Return values:
168202ac6454SAndrew Thompson  * 0: Not pending
168302ac6454SAndrew Thompson  * 1: Pending: The USB transfer will receive a callback in the future.
168402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
168502ac6454SAndrew Thompson uint8_t
168602ac6454SAndrew Thompson usb2_transfer_pending(struct usb2_xfer *xfer)
168702ac6454SAndrew Thompson {
168802ac6454SAndrew Thompson 	struct usb2_xfer_root *info;
168902ac6454SAndrew Thompson 	struct usb2_xfer_queue *pq;
169002ac6454SAndrew Thompson 
169102ac6454SAndrew Thompson 	if (xfer == NULL) {
169202ac6454SAndrew Thompson 		/* transfer is gone */
169302ac6454SAndrew Thompson 		return (0);
169402ac6454SAndrew Thompson 	}
169502ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
169602ac6454SAndrew Thompson 
169702ac6454SAndrew Thompson 	if (xfer->flags_int.transferring) {
169802ac6454SAndrew Thompson 		/* trivial case */
169902ac6454SAndrew Thompson 		return (1);
170002ac6454SAndrew Thompson 	}
170102ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
170202ac6454SAndrew Thompson 	if (xfer->wait_queue) {
170302ac6454SAndrew Thompson 		/* we are waiting on a queue somewhere */
170402ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
170502ac6454SAndrew Thompson 		return (1);
170602ac6454SAndrew Thompson 	}
170702ac6454SAndrew Thompson 	info = xfer->xroot;
170802ac6454SAndrew Thompson 	pq = &info->done_q;
170902ac6454SAndrew Thompson 
171002ac6454SAndrew Thompson 	if (pq->curr == xfer) {
171102ac6454SAndrew Thompson 		/* we are currently scheduled for callback */
171202ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
171302ac6454SAndrew Thompson 		return (1);
171402ac6454SAndrew Thompson 	}
171502ac6454SAndrew Thompson 	/* we are not pending */
171602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
171702ac6454SAndrew Thompson 	return (0);
171802ac6454SAndrew Thompson }
171902ac6454SAndrew Thompson 
172002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
172102ac6454SAndrew Thompson  *	usb2_transfer_drain
172202ac6454SAndrew Thompson  *
172302ac6454SAndrew Thompson  * This function will stop the USB transfer and wait for any
172402ac6454SAndrew Thompson  * additional BUS-DMA and HW-DMA operations to complete. Buffers that
172502ac6454SAndrew Thompson  * are loaded into DMA can safely be freed or reused after that this
172602ac6454SAndrew Thompson  * function has returned.
172702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
172802ac6454SAndrew Thompson void
172902ac6454SAndrew Thompson usb2_transfer_drain(struct usb2_xfer *xfer)
173002ac6454SAndrew Thompson {
173102ac6454SAndrew Thompson 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
173202ac6454SAndrew Thompson 	    "usb2_transfer_drain can sleep!");
173302ac6454SAndrew Thompson 
173402ac6454SAndrew Thompson 	if (xfer == NULL) {
173502ac6454SAndrew Thompson 		/* transfer is gone */
173602ac6454SAndrew Thompson 		return;
173702ac6454SAndrew Thompson 	}
173802ac6454SAndrew Thompson 	if (xfer->xroot->xfer_mtx != &Giant) {
173902ac6454SAndrew Thompson 		USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
174002ac6454SAndrew Thompson 	}
174102ac6454SAndrew Thompson 	USB_XFER_LOCK(xfer);
174202ac6454SAndrew Thompson 
174302ac6454SAndrew Thompson 	usb2_transfer_stop(xfer);
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 	while (usb2_transfer_pending(xfer)) {
174602ac6454SAndrew Thompson 		xfer->flags_int.draining = 1;
174702ac6454SAndrew Thompson 		/*
174802ac6454SAndrew Thompson 		 * Wait until the current outstanding USB
174902ac6454SAndrew Thompson 		 * transfer is complete !
175002ac6454SAndrew Thompson 		 */
175102ac6454SAndrew Thompson 		usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
175202ac6454SAndrew Thompson 	}
175302ac6454SAndrew Thompson 	USB_XFER_UNLOCK(xfer);
175402ac6454SAndrew Thompson }
175502ac6454SAndrew Thompson 
175602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
175702ac6454SAndrew Thompson  *	usb2_set_frame_data
175802ac6454SAndrew Thompson  *
175902ac6454SAndrew Thompson  * This function sets the pointer of the buffer that should
176002ac6454SAndrew Thompson  * loaded directly into DMA for the given USB frame. Passing "ptr"
176102ac6454SAndrew Thompson  * equal to NULL while the corresponding "frlength" is greater
176202ac6454SAndrew Thompson  * than zero gives undefined results!
176302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
176402ac6454SAndrew Thompson void
176502ac6454SAndrew Thompson usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, uint32_t frindex)
176602ac6454SAndrew Thompson {
176702ac6454SAndrew Thompson 	/* set virtual address to load and length */
176802ac6454SAndrew Thompson 	xfer->frbuffers[frindex].buffer = ptr;
176902ac6454SAndrew Thompson }
177002ac6454SAndrew Thompson 
177102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
177202ac6454SAndrew Thompson  *	usb2_set_frame_offset
177302ac6454SAndrew Thompson  *
177402ac6454SAndrew Thompson  * This function sets the frame data buffer offset relative to the beginning
177502ac6454SAndrew Thompson  * of the USB DMA buffer allocated for this USB transfer.
177602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
177702ac6454SAndrew Thompson void
177802ac6454SAndrew Thompson usb2_set_frame_offset(struct usb2_xfer *xfer, uint32_t offset,
177902ac6454SAndrew Thompson     uint32_t frindex)
178002ac6454SAndrew Thompson {
178102ac6454SAndrew Thompson 	USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
178202ac6454SAndrew Thompson 	    "when the USB buffer is external!\n"));
178302ac6454SAndrew Thompson 
178402ac6454SAndrew Thompson 	/* set virtual address to load */
178502ac6454SAndrew Thompson 	xfer->frbuffers[frindex].buffer =
178602ac6454SAndrew Thompson 	    USB_ADD_BYTES(xfer->local_buffer, offset);
178702ac6454SAndrew Thompson }
178802ac6454SAndrew Thompson 
178902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
179002ac6454SAndrew Thompson  *	usb2_callback_proc - factored out code
179102ac6454SAndrew Thompson  *
179202ac6454SAndrew Thompson  * This function performs USB callbacks.
179302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
179402ac6454SAndrew Thompson static void
179502ac6454SAndrew Thompson usb2_callback_proc(struct usb2_proc_msg *_pm)
179602ac6454SAndrew Thompson {
179702ac6454SAndrew Thompson 	struct usb2_done_msg *pm = (void *)_pm;
179802ac6454SAndrew Thompson 	struct usb2_xfer_root *info = pm->xroot;
179902ac6454SAndrew Thompson 
180002ac6454SAndrew Thompson 	/* Change locking order */
180102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(info->bus);
180202ac6454SAndrew Thompson 
180302ac6454SAndrew Thompson 	/*
180402ac6454SAndrew Thompson 	 * We exploit the fact that the mutex is the same for all
180502ac6454SAndrew Thompson 	 * callbacks that will be called from this thread:
180602ac6454SAndrew Thompson 	 */
180702ac6454SAndrew Thompson 	mtx_lock(info->xfer_mtx);
180802ac6454SAndrew Thompson 	USB_BUS_LOCK(info->bus);
180902ac6454SAndrew Thompson 
181002ac6454SAndrew Thompson 	/* Continue where we lost track */
181102ac6454SAndrew Thompson 	usb2_command_wrapper(&info->done_q,
181202ac6454SAndrew Thompson 	    info->done_q.curr);
181302ac6454SAndrew Thompson 
181402ac6454SAndrew Thompson 	mtx_unlock(info->xfer_mtx);
181502ac6454SAndrew Thompson }
181602ac6454SAndrew Thompson 
181702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
181802ac6454SAndrew Thompson  *	usb2_callback_ss_done_defer
181902ac6454SAndrew Thompson  *
182002ac6454SAndrew Thompson  * This function will defer the start, stop and done callback to the
182102ac6454SAndrew Thompson  * correct thread.
182202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
182302ac6454SAndrew Thompson static void
182402ac6454SAndrew Thompson usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
182502ac6454SAndrew Thompson {
182602ac6454SAndrew Thompson 	struct usb2_xfer_root *info = xfer->xroot;
182702ac6454SAndrew Thompson 	struct usb2_xfer_queue *pq = &info->done_q;
182802ac6454SAndrew Thompson 
182902ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
183002ac6454SAndrew Thompson 
183102ac6454SAndrew Thompson 	if (pq->curr != xfer) {
183202ac6454SAndrew Thompson 		usb2_transfer_enqueue(pq, xfer);
183302ac6454SAndrew Thompson 	}
183402ac6454SAndrew Thompson 	if (!pq->recurse_1) {
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 		/*
183702ac6454SAndrew Thompson 	         * We have to postpone the callback due to the fact we
183802ac6454SAndrew Thompson 	         * will have a Lock Order Reversal, LOR, if we try to
183902ac6454SAndrew Thompson 	         * proceed !
184002ac6454SAndrew Thompson 	         */
184102ac6454SAndrew Thompson 		if (usb2_proc_msignal(info->done_p,
184202ac6454SAndrew Thompson 		    &info->done_m[0], &info->done_m[1])) {
184302ac6454SAndrew Thompson 			/* ignore */
184402ac6454SAndrew Thompson 		}
184502ac6454SAndrew Thompson 	} else {
184602ac6454SAndrew Thompson 		/* clear second recurse flag */
184702ac6454SAndrew Thompson 		pq->recurse_2 = 0;
184802ac6454SAndrew Thompson 	}
184902ac6454SAndrew Thompson 	return;
185002ac6454SAndrew Thompson 
185102ac6454SAndrew Thompson }
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
185402ac6454SAndrew Thompson  *	usb2_callback_wrapper
185502ac6454SAndrew Thompson  *
185602ac6454SAndrew Thompson  * This is a wrapper for USB callbacks. This wrapper does some
185702ac6454SAndrew Thompson  * auto-magic things like figuring out if we can call the callback
185802ac6454SAndrew Thompson  * directly from the current context or if we need to wakeup the
185902ac6454SAndrew Thompson  * interrupt process.
186002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
186102ac6454SAndrew Thompson static void
186202ac6454SAndrew Thompson usb2_callback_wrapper(struct usb2_xfer_queue *pq)
186302ac6454SAndrew Thompson {
186402ac6454SAndrew Thompson 	struct usb2_xfer *xfer = pq->curr;
186502ac6454SAndrew Thompson 	struct usb2_xfer_root *info = xfer->xroot;
186602ac6454SAndrew Thompson 
186702ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
186802ac6454SAndrew Thompson 	if (!mtx_owned(xfer->xroot->xfer_mtx)) {
186902ac6454SAndrew Thompson 		/*
187002ac6454SAndrew Thompson 	       	 * Cases that end up here:
187102ac6454SAndrew Thompson 		 *
187202ac6454SAndrew Thompson 		 * 5) HW interrupt done callback or other source.
187302ac6454SAndrew Thompson 		 */
187402ac6454SAndrew Thompson 		DPRINTFN(3, "case 5\n");
187502ac6454SAndrew Thompson 
187602ac6454SAndrew Thompson 		/*
187702ac6454SAndrew Thompson 	         * We have to postpone the callback due to the fact we
187802ac6454SAndrew Thompson 	         * will have a Lock Order Reversal, LOR, if we try to
187902ac6454SAndrew Thompson 	         * proceed !
188002ac6454SAndrew Thompson 	         */
188102ac6454SAndrew Thompson 		if (usb2_proc_msignal(info->done_p,
188202ac6454SAndrew Thompson 		    &info->done_m[0], &info->done_m[1])) {
188302ac6454SAndrew Thompson 			/* ignore */
188402ac6454SAndrew Thompson 		}
188502ac6454SAndrew Thompson 		return;
188602ac6454SAndrew Thompson 	}
188702ac6454SAndrew Thompson 	/*
188802ac6454SAndrew Thompson 	 * Cases that end up here:
188902ac6454SAndrew Thompson 	 *
189002ac6454SAndrew Thompson 	 * 1) We are starting a transfer
189102ac6454SAndrew Thompson 	 * 2) We are prematurely calling back a transfer
189202ac6454SAndrew Thompson 	 * 3) We are stopping a transfer
189302ac6454SAndrew Thompson 	 * 4) We are doing an ordinary callback
189402ac6454SAndrew Thompson 	 */
189502ac6454SAndrew Thompson 	DPRINTFN(3, "case 1-4\n");
189602ac6454SAndrew Thompson 	/* get next USB transfer in the queue */
189702ac6454SAndrew Thompson 	info->done_q.curr = NULL;
189802ac6454SAndrew Thompson 
189902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
190002ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
190102ac6454SAndrew Thompson 
190202ac6454SAndrew Thompson 	/* set correct USB state for callback */
190302ac6454SAndrew Thompson 	if (!xfer->flags_int.transferring) {
190402ac6454SAndrew Thompson 		xfer->usb2_state = USB_ST_SETUP;
190502ac6454SAndrew Thompson 		if (!xfer->flags_int.started) {
190602ac6454SAndrew Thompson 			/* we got stopped before we even got started */
190702ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
190802ac6454SAndrew Thompson 			goto done;
190902ac6454SAndrew Thompson 		}
191002ac6454SAndrew Thompson 	} else {
191102ac6454SAndrew Thompson 
191202ac6454SAndrew Thompson 		if (usb2_callback_wrapper_sub(xfer)) {
191302ac6454SAndrew Thompson 			/* the callback has been deferred */
191402ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
191502ac6454SAndrew Thompson 			goto done;
191602ac6454SAndrew Thompson 		}
191702ac6454SAndrew Thompson 		/* decrement power reference */
191802ac6454SAndrew Thompson 		usb2_transfer_power_ref(xfer, -1);
191902ac6454SAndrew Thompson 
192002ac6454SAndrew Thompson 		xfer->flags_int.transferring = 0;
192102ac6454SAndrew Thompson 
192202ac6454SAndrew Thompson 		if (xfer->error) {
192302ac6454SAndrew Thompson 			xfer->usb2_state = USB_ST_ERROR;
192402ac6454SAndrew Thompson 		} else {
192502ac6454SAndrew Thompson 			/* set transferred state */
192602ac6454SAndrew Thompson 			xfer->usb2_state = USB_ST_TRANSFERRED;
192702ac6454SAndrew Thompson 
192802ac6454SAndrew Thompson 			/* sync DMA memory, if any */
192902ac6454SAndrew Thompson 			if (xfer->flags_int.bdma_enable &&
193002ac6454SAndrew Thompson 			    (!xfer->flags_int.bdma_no_post_sync)) {
193102ac6454SAndrew Thompson 				usb2_bdma_post_sync(xfer);
193202ac6454SAndrew Thompson 			}
193302ac6454SAndrew Thompson 		}
193402ac6454SAndrew Thompson 	}
193502ac6454SAndrew Thompson 
193602ac6454SAndrew Thompson 	/* call processing routine */
193702ac6454SAndrew Thompson 	(xfer->callback) (xfer);
193802ac6454SAndrew Thompson 
193902ac6454SAndrew Thompson 	/* pickup the USB mutex again */
194002ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
194102ac6454SAndrew Thompson 
194202ac6454SAndrew Thompson 	/*
194302ac6454SAndrew Thompson 	 * Check if we got started after that we got cancelled, but
194402ac6454SAndrew Thompson 	 * before we managed to do the callback.
194502ac6454SAndrew Thompson 	 */
194602ac6454SAndrew Thompson 	if ((!xfer->flags_int.open) &&
194702ac6454SAndrew Thompson 	    (xfer->flags_int.started) &&
194802ac6454SAndrew Thompson 	    (xfer->usb2_state == USB_ST_ERROR)) {
194902ac6454SAndrew Thompson 		/* try to loop, but not recursivly */
195002ac6454SAndrew Thompson 		usb2_command_wrapper(&info->done_q, xfer);
195102ac6454SAndrew Thompson 		return;
195202ac6454SAndrew Thompson 	}
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson done:
195502ac6454SAndrew Thompson 	/*
195602ac6454SAndrew Thompson 	 * Check if we are draining.
195702ac6454SAndrew Thompson 	 */
195802ac6454SAndrew Thompson 	if (xfer->flags_int.draining &&
195902ac6454SAndrew Thompson 	    (!xfer->flags_int.transferring)) {
196002ac6454SAndrew Thompson 		/* "usb2_transfer_drain()" is waiting for end of transfer */
196102ac6454SAndrew Thompson 		xfer->flags_int.draining = 0;
196202ac6454SAndrew Thompson 		usb2_cv_broadcast(&xfer->xroot->cv_drain);
196302ac6454SAndrew Thompson 	}
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson 	/* do the next callback, if any */
196602ac6454SAndrew Thompson 	usb2_command_wrapper(&info->done_q,
196702ac6454SAndrew Thompson 	    info->done_q.curr);
196802ac6454SAndrew Thompson }
196902ac6454SAndrew Thompson 
197002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
197102ac6454SAndrew Thompson  *	usb2_dma_delay_done_cb
197202ac6454SAndrew Thompson  *
197302ac6454SAndrew Thompson  * This function is called when the DMA delay has been exectuded, and
197402ac6454SAndrew Thompson  * will make sure that the callback is called to complete the USB
197502ac6454SAndrew Thompson  * transfer. This code path is ususally only used when there is an USB
197602ac6454SAndrew Thompson  * error like USB_ERR_CANCELLED.
197702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
197802ac6454SAndrew Thompson static void
197902ac6454SAndrew Thompson usb2_dma_delay_done_cb(void *arg)
198002ac6454SAndrew Thompson {
198102ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
198202ac6454SAndrew Thompson 
198302ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
198402ac6454SAndrew Thompson 
198502ac6454SAndrew Thompson 	DPRINTFN(3, "Completed %p\n", xfer);
198602ac6454SAndrew Thompson 
198702ac6454SAndrew Thompson 	/* queue callback for execution, again */
198802ac6454SAndrew Thompson 	usb2_transfer_done(xfer, 0);
198902ac6454SAndrew Thompson }
199002ac6454SAndrew Thompson 
199102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
199202ac6454SAndrew Thompson  *	usb2_transfer_dequeue
199302ac6454SAndrew Thompson  *
199402ac6454SAndrew Thompson  *  - This function is used to remove an USB transfer from a USB
199502ac6454SAndrew Thompson  *  transfer queue.
199602ac6454SAndrew Thompson  *
199702ac6454SAndrew Thompson  *  - This function can be called multiple times in a row.
199802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
199902ac6454SAndrew Thompson void
200002ac6454SAndrew Thompson usb2_transfer_dequeue(struct usb2_xfer *xfer)
200102ac6454SAndrew Thompson {
200202ac6454SAndrew Thompson 	struct usb2_xfer_queue *pq;
200302ac6454SAndrew Thompson 
200402ac6454SAndrew Thompson 	pq = xfer->wait_queue;
200502ac6454SAndrew Thompson 	if (pq) {
200602ac6454SAndrew Thompson 		TAILQ_REMOVE(&pq->head, xfer, wait_entry);
200702ac6454SAndrew Thompson 		xfer->wait_queue = NULL;
200802ac6454SAndrew Thompson 	}
200902ac6454SAndrew Thompson }
201002ac6454SAndrew Thompson 
201102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
201202ac6454SAndrew Thompson  *	usb2_transfer_enqueue
201302ac6454SAndrew Thompson  *
201402ac6454SAndrew Thompson  *  - This function is used to insert an USB transfer into a USB *
201502ac6454SAndrew Thompson  *  transfer queue.
201602ac6454SAndrew Thompson  *
201702ac6454SAndrew Thompson  *  - This function can be called multiple times in a row.
201802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
201902ac6454SAndrew Thompson void
202002ac6454SAndrew Thompson usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
202102ac6454SAndrew Thompson {
202202ac6454SAndrew Thompson 	/*
202302ac6454SAndrew Thompson 	 * Insert the USB transfer into the queue, if it is not
202402ac6454SAndrew Thompson 	 * already on a USB transfer queue:
202502ac6454SAndrew Thompson 	 */
202602ac6454SAndrew Thompson 	if (xfer->wait_queue == NULL) {
202702ac6454SAndrew Thompson 		xfer->wait_queue = pq;
202802ac6454SAndrew Thompson 		TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
202902ac6454SAndrew Thompson 	}
203002ac6454SAndrew Thompson }
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
203302ac6454SAndrew Thompson  *	usb2_transfer_done
203402ac6454SAndrew Thompson  *
203502ac6454SAndrew Thompson  *  - This function is used to remove an USB transfer from the busdma,
203602ac6454SAndrew Thompson  *  pipe or interrupt queue.
203702ac6454SAndrew Thompson  *
203802ac6454SAndrew Thompson  *  - This function is used to queue the USB transfer on the done
203902ac6454SAndrew Thompson  *  queue.
204002ac6454SAndrew Thompson  *
204102ac6454SAndrew Thompson  *  - This function is used to stop any USB transfer timeouts.
204202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
204302ac6454SAndrew Thompson void
204402ac6454SAndrew Thompson usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
204502ac6454SAndrew Thompson {
204602ac6454SAndrew Thompson 	struct usb2_xfer_queue *pq;
204702ac6454SAndrew Thompson 
204802ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
204902ac6454SAndrew Thompson 
205002ac6454SAndrew Thompson 	DPRINTF("err=%s\n", usb2_errstr(error));
205102ac6454SAndrew Thompson 
205202ac6454SAndrew Thompson 	/*
205302ac6454SAndrew Thompson 	 * If we are not transferring then just return.
205402ac6454SAndrew Thompson 	 * This can happen during transfer cancel.
205502ac6454SAndrew Thompson 	 */
205602ac6454SAndrew Thompson 	if (!xfer->flags_int.transferring) {
205702ac6454SAndrew Thompson 		DPRINTF("not transferring\n");
205802ac6454SAndrew Thompson 		return;
205902ac6454SAndrew Thompson 	}
206002ac6454SAndrew Thompson 	/* only set transfer error if not already set */
206102ac6454SAndrew Thompson 	if (!xfer->error) {
206202ac6454SAndrew Thompson 		xfer->error = error;
206302ac6454SAndrew Thompson 	}
206402ac6454SAndrew Thompson 	/* stop any callouts */
206502ac6454SAndrew Thompson 	usb2_callout_stop(&xfer->timeout_handle);
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson 	/*
206802ac6454SAndrew Thompson 	 * If we are waiting on a queue, just remove the USB transfer
206902ac6454SAndrew Thompson 	 * from the queue, if any. We should have the required locks
207002ac6454SAndrew Thompson 	 * locked to do the remove when this function is called.
207102ac6454SAndrew Thompson 	 */
207202ac6454SAndrew Thompson 	usb2_transfer_dequeue(xfer);
207302ac6454SAndrew Thompson 
207402ac6454SAndrew Thompson 	if (mtx_owned(xfer->xroot->xfer_mtx)) {
207502ac6454SAndrew Thompson 		/*
207602ac6454SAndrew Thompson 		 * If the private USB lock is not locked, then we assume
207702ac6454SAndrew Thompson 		 * that the BUS-DMA load stage has been passed:
207802ac6454SAndrew Thompson 		 */
207902ac6454SAndrew Thompson 		pq = &xfer->xroot->dma_q;
208002ac6454SAndrew Thompson 
208102ac6454SAndrew Thompson 		if (pq->curr == xfer) {
208202ac6454SAndrew Thompson 			/* start the next BUS-DMA load, if any */
208302ac6454SAndrew Thompson 			usb2_command_wrapper(pq, NULL);
208402ac6454SAndrew Thompson 		}
208502ac6454SAndrew Thompson 	}
208602ac6454SAndrew Thompson 	/* keep some statistics */
208702ac6454SAndrew Thompson 	if (xfer->error) {
208802ac6454SAndrew Thompson 		xfer->xroot->bus->stats_err.uds_requests
208902ac6454SAndrew Thompson 		    [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
209002ac6454SAndrew Thompson 	} else {
209102ac6454SAndrew Thompson 		xfer->xroot->bus->stats_ok.uds_requests
209202ac6454SAndrew Thompson 		    [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
209302ac6454SAndrew Thompson 	}
209402ac6454SAndrew Thompson 
209502ac6454SAndrew Thompson 	/* call the USB transfer callback */
209602ac6454SAndrew Thompson 	usb2_callback_ss_done_defer(xfer);
209702ac6454SAndrew Thompson }
209802ac6454SAndrew Thompson 
209902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
210002ac6454SAndrew Thompson  *	usb2_transfer_start_cb
210102ac6454SAndrew Thompson  *
210202ac6454SAndrew Thompson  * This function is called to start the USB transfer when
210302ac6454SAndrew Thompson  * "xfer->interval" is greater than zero, and and the endpoint type is
210402ac6454SAndrew Thompson  * BULK or CONTROL.
210502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
210602ac6454SAndrew Thompson static void
210702ac6454SAndrew Thompson usb2_transfer_start_cb(void *arg)
210802ac6454SAndrew Thompson {
210902ac6454SAndrew Thompson 	struct usb2_xfer *xfer = arg;
211002ac6454SAndrew Thompson 	struct usb2_pipe *pipe = xfer->pipe;
211102ac6454SAndrew Thompson 
211202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
211302ac6454SAndrew Thompson 
211402ac6454SAndrew Thompson 	DPRINTF("start\n");
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson 	/* start the transfer */
211702ac6454SAndrew Thompson 	(pipe->methods->start) (xfer);
211802ac6454SAndrew Thompson 
211902ac6454SAndrew Thompson 	/* check cancelability */
212002ac6454SAndrew Thompson 	if (pipe->methods->start_is_cancelable) {
212102ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 1;
212202ac6454SAndrew Thompson 		if (xfer->error) {
212302ac6454SAndrew Thompson 			/* some error has happened */
212402ac6454SAndrew Thompson 			usb2_transfer_done(xfer, 0);
212502ac6454SAndrew Thompson 		}
212602ac6454SAndrew Thompson 	} else {
212702ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 0;
212802ac6454SAndrew Thompson 	}
212902ac6454SAndrew Thompson }
213002ac6454SAndrew Thompson 
213102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
213202ac6454SAndrew Thompson  *	usb2_transfer_set_stall
213302ac6454SAndrew Thompson  *
213402ac6454SAndrew Thompson  * This function is used to set the stall flag outside the
213502ac6454SAndrew Thompson  * callback. This function is NULL safe.
213602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
213702ac6454SAndrew Thompson void
213802ac6454SAndrew Thompson usb2_transfer_set_stall(struct usb2_xfer *xfer)
213902ac6454SAndrew Thompson {
214002ac6454SAndrew Thompson 	if (xfer == NULL) {
214102ac6454SAndrew Thompson 		/* tearing down */
214202ac6454SAndrew Thompson 		return;
214302ac6454SAndrew Thompson 	}
214402ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
214502ac6454SAndrew Thompson 
214602ac6454SAndrew Thompson 	/* avoid any races by locking the USB mutex */
214702ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
214802ac6454SAndrew Thompson 
214902ac6454SAndrew Thompson 	xfer->flags.stall_pipe = 1;
215002ac6454SAndrew Thompson 
215102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
215202ac6454SAndrew Thompson }
215302ac6454SAndrew Thompson 
215402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
215502ac6454SAndrew Thompson  *	usb2_transfer_clear_stall
215602ac6454SAndrew Thompson  *
215702ac6454SAndrew Thompson  * This function is used to clear the stall flag outside the
215802ac6454SAndrew Thompson  * callback. This function is NULL safe.
215902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
216002ac6454SAndrew Thompson void
216102ac6454SAndrew Thompson usb2_transfer_clear_stall(struct usb2_xfer *xfer)
216202ac6454SAndrew Thompson {
216302ac6454SAndrew Thompson 	if (xfer == NULL) {
216402ac6454SAndrew Thompson 		/* tearing down */
216502ac6454SAndrew Thompson 		return;
216602ac6454SAndrew Thompson 	}
216702ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
216802ac6454SAndrew Thompson 
216902ac6454SAndrew Thompson 	/* avoid any races by locking the USB mutex */
217002ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
217102ac6454SAndrew Thompson 
217202ac6454SAndrew Thompson 	xfer->flags.stall_pipe = 0;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
217502ac6454SAndrew Thompson }
217602ac6454SAndrew Thompson 
217702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
217802ac6454SAndrew Thompson  *	usb2_pipe_start
217902ac6454SAndrew Thompson  *
218002ac6454SAndrew Thompson  * This function is used to add an USB transfer to the pipe transfer list.
218102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
218202ac6454SAndrew Thompson void
218302ac6454SAndrew Thompson usb2_pipe_start(struct usb2_xfer_queue *pq)
218402ac6454SAndrew Thompson {
218502ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
218602ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
218702ac6454SAndrew Thompson 	uint8_t type;
218802ac6454SAndrew Thompson 
218902ac6454SAndrew Thompson 	xfer = pq->curr;
219002ac6454SAndrew Thompson 	pipe = xfer->pipe;
219102ac6454SAndrew Thompson 
219202ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
219302ac6454SAndrew Thompson 
219402ac6454SAndrew Thompson 	/*
219502ac6454SAndrew Thompson 	 * If the pipe is already stalled we do nothing !
219602ac6454SAndrew Thompson 	 */
219702ac6454SAndrew Thompson 	if (pipe->is_stalled) {
219802ac6454SAndrew Thompson 		return;
219902ac6454SAndrew Thompson 	}
220002ac6454SAndrew Thompson 	/*
220102ac6454SAndrew Thompson 	 * Check if we are supposed to stall the pipe:
220202ac6454SAndrew Thompson 	 */
220302ac6454SAndrew Thompson 	if (xfer->flags.stall_pipe) {
220402ac6454SAndrew Thompson 		/* clear stall command */
220502ac6454SAndrew Thompson 		xfer->flags.stall_pipe = 0;
220602ac6454SAndrew Thompson 
220702ac6454SAndrew Thompson 		/*
220802ac6454SAndrew Thompson 		 * Only stall BULK and INTERRUPT endpoints.
220902ac6454SAndrew Thompson 		 */
221002ac6454SAndrew Thompson 		type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
221102ac6454SAndrew Thompson 		if ((type == UE_BULK) ||
221202ac6454SAndrew Thompson 		    (type == UE_INTERRUPT)) {
221302ac6454SAndrew Thompson 			struct usb2_device *udev;
221402ac6454SAndrew Thompson 			struct usb2_xfer_root *info;
221502ac6454SAndrew Thompson 
221602ac6454SAndrew Thompson 			info = xfer->xroot;
221702ac6454SAndrew Thompson 			udev = info->udev;
221802ac6454SAndrew Thompson 			pipe->is_stalled = 1;
221902ac6454SAndrew Thompson 
222002ac6454SAndrew Thompson 			if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
222102ac6454SAndrew Thompson 				(udev->bus->methods->set_stall) (
222202ac6454SAndrew Thompson 				    udev, NULL, pipe);
222302ac6454SAndrew Thompson 			} else if (udev->default_xfer[1]) {
222402ac6454SAndrew Thompson 				info = udev->default_xfer[1]->xroot;
222502ac6454SAndrew Thompson 				if (usb2_proc_msignal(
222602ac6454SAndrew Thompson 				    &info->bus->non_giant_callback_proc,
222702ac6454SAndrew Thompson 				    &udev->cs_msg[0], &udev->cs_msg[1])) {
222802ac6454SAndrew Thompson 					/* ignore */
222902ac6454SAndrew Thompson 				}
223002ac6454SAndrew Thompson 			} else {
223102ac6454SAndrew Thompson 				/* should not happen */
223202ac6454SAndrew Thompson 				DPRINTFN(0, "No stall handler!\n");
223302ac6454SAndrew Thompson 			}
223402ac6454SAndrew Thompson 			/*
223502ac6454SAndrew Thompson 			 * We get started again when the stall is cleared!
223602ac6454SAndrew Thompson 			 */
223702ac6454SAndrew Thompson 			return;
223802ac6454SAndrew Thompson 		}
223902ac6454SAndrew Thompson 	}
224002ac6454SAndrew Thompson 	/* Set or clear stall complete - special case */
224102ac6454SAndrew Thompson 	if (xfer->nframes == 0) {
224202ac6454SAndrew Thompson 		/* we are complete */
224302ac6454SAndrew Thompson 		xfer->aframes = 0;
224402ac6454SAndrew Thompson 		usb2_transfer_done(xfer, 0);
224502ac6454SAndrew Thompson 		return;
224602ac6454SAndrew Thompson 	}
224702ac6454SAndrew Thompson 	/*
224802ac6454SAndrew Thompson 	 * Handled cases:
224902ac6454SAndrew Thompson 	 *
225002ac6454SAndrew Thompson 	 * 1) Start the first transfer queued.
225102ac6454SAndrew Thompson 	 *
225202ac6454SAndrew Thompson 	 * 2) Re-start the current USB transfer.
225302ac6454SAndrew Thompson 	 */
225402ac6454SAndrew Thompson 	/*
225502ac6454SAndrew Thompson 	 * Check if there should be any
225602ac6454SAndrew Thompson 	 * pre transfer start delay:
225702ac6454SAndrew Thompson 	 */
225802ac6454SAndrew Thompson 	if (xfer->interval > 0) {
225902ac6454SAndrew Thompson 		type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
226002ac6454SAndrew Thompson 		if ((type == UE_BULK) ||
226102ac6454SAndrew Thompson 		    (type == UE_CONTROL)) {
226202ac6454SAndrew Thompson 			usb2_transfer_timeout_ms(xfer,
226302ac6454SAndrew Thompson 			    &usb2_transfer_start_cb,
226402ac6454SAndrew Thompson 			    xfer->interval);
226502ac6454SAndrew Thompson 			return;
226602ac6454SAndrew Thompson 		}
226702ac6454SAndrew Thompson 	}
226802ac6454SAndrew Thompson 	DPRINTF("start\n");
226902ac6454SAndrew Thompson 
227002ac6454SAndrew Thompson 	/* start USB transfer */
227102ac6454SAndrew Thompson 	(pipe->methods->start) (xfer);
227202ac6454SAndrew Thompson 
227302ac6454SAndrew Thompson 	/* check cancelability */
227402ac6454SAndrew Thompson 	if (pipe->methods->start_is_cancelable) {
227502ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 1;
227602ac6454SAndrew Thompson 		if (xfer->error) {
227702ac6454SAndrew Thompson 			/* some error has happened */
227802ac6454SAndrew Thompson 			usb2_transfer_done(xfer, 0);
227902ac6454SAndrew Thompson 		}
228002ac6454SAndrew Thompson 	} else {
228102ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 0;
228202ac6454SAndrew Thompson 	}
228302ac6454SAndrew Thompson }
228402ac6454SAndrew Thompson 
228502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
228602ac6454SAndrew Thompson  *	usb2_transfer_timeout_ms
228702ac6454SAndrew Thompson  *
228802ac6454SAndrew Thompson  * This function is used to setup a timeout on the given USB
228902ac6454SAndrew Thompson  * transfer. If the timeout has been deferred the callback given by
229002ac6454SAndrew Thompson  * "cb" will get called after "ms" milliseconds.
229102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229202ac6454SAndrew Thompson void
229302ac6454SAndrew Thompson usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
229402ac6454SAndrew Thompson     void (*cb) (void *arg), uint32_t ms)
229502ac6454SAndrew Thompson {
229602ac6454SAndrew Thompson 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
229702ac6454SAndrew Thompson 
229802ac6454SAndrew Thompson 	/* defer delay */
229902ac6454SAndrew Thompson 	usb2_callout_reset(&xfer->timeout_handle,
230002ac6454SAndrew Thompson 	    USB_MS_TO_TICKS(ms), cb, xfer);
230102ac6454SAndrew Thompson }
230202ac6454SAndrew Thompson 
230302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
230402ac6454SAndrew Thompson  *	usb2_callback_wrapper_sub
230502ac6454SAndrew Thompson  *
230602ac6454SAndrew Thompson  *  - This function will update variables in an USB transfer after
230702ac6454SAndrew Thompson  *  that the USB transfer is complete.
230802ac6454SAndrew Thompson  *
230902ac6454SAndrew Thompson  *  - This function is used to start the next USB transfer on the
231002ac6454SAndrew Thompson  *  pipe transfer queue, if any.
231102ac6454SAndrew Thompson  *
231202ac6454SAndrew Thompson  * NOTE: In some special cases the USB transfer will not be removed from
231302ac6454SAndrew Thompson  * the pipe queue, but remain first. To enforce USB transfer removal call
231402ac6454SAndrew Thompson  * this function passing the error code "USB_ERR_CANCELLED".
231502ac6454SAndrew Thompson  *
231602ac6454SAndrew Thompson  * Return values:
231702ac6454SAndrew Thompson  * 0: Success.
231802ac6454SAndrew Thompson  * Else: The callback has been deferred.
231902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
232002ac6454SAndrew Thompson static uint8_t
232102ac6454SAndrew Thompson usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
232202ac6454SAndrew Thompson {
232302ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
232402ac6454SAndrew Thompson 	uint32_t x;
232502ac6454SAndrew Thompson 
232602ac6454SAndrew Thompson 	if ((!xfer->flags_int.open) &&
232702ac6454SAndrew Thompson 	    (!xfer->flags_int.did_close)) {
232802ac6454SAndrew Thompson 		DPRINTF("close\n");
232902ac6454SAndrew Thompson 		USB_BUS_LOCK(xfer->xroot->bus);
233002ac6454SAndrew Thompson 		(xfer->pipe->methods->close) (xfer);
233102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(xfer->xroot->bus);
233202ac6454SAndrew Thompson 		/* only close once */
233302ac6454SAndrew Thompson 		xfer->flags_int.did_close = 1;
233402ac6454SAndrew Thompson 		return (1);		/* wait for new callback */
233502ac6454SAndrew Thompson 	}
233602ac6454SAndrew Thompson 	/*
233702ac6454SAndrew Thompson 	 * If we have a non-hardware induced error we
233802ac6454SAndrew Thompson 	 * need to do the DMA delay!
233902ac6454SAndrew Thompson 	 */
234002ac6454SAndrew Thompson 	if (((xfer->error == USB_ERR_CANCELLED) ||
234102ac6454SAndrew Thompson 	    (xfer->error == USB_ERR_TIMEOUT)) &&
234202ac6454SAndrew Thompson 	    (!xfer->flags_int.did_dma_delay)) {
234302ac6454SAndrew Thompson 
234402ac6454SAndrew Thompson 		uint32_t temp;
234502ac6454SAndrew Thompson 
234602ac6454SAndrew Thompson 		/* only delay once */
234702ac6454SAndrew Thompson 		xfer->flags_int.did_dma_delay = 1;
234802ac6454SAndrew Thompson 
234902ac6454SAndrew Thompson 		/* we can not cancel this delay */
235002ac6454SAndrew Thompson 		xfer->flags_int.can_cancel_immed = 0;
235102ac6454SAndrew Thompson 
235202ac6454SAndrew Thompson 		temp = usb2_get_dma_delay(xfer->xroot->bus);
235302ac6454SAndrew Thompson 
235402ac6454SAndrew Thompson 		DPRINTFN(3, "DMA delay, %u ms, "
235502ac6454SAndrew Thompson 		    "on %p\n", temp, xfer);
235602ac6454SAndrew Thompson 
235702ac6454SAndrew Thompson 		if (temp != 0) {
235802ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
235902ac6454SAndrew Thompson 			usb2_transfer_timeout_ms(xfer,
236002ac6454SAndrew Thompson 			    &usb2_dma_delay_done_cb, temp);
236102ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
236202ac6454SAndrew Thompson 			return (1);	/* wait for new callback */
236302ac6454SAndrew Thompson 		}
236402ac6454SAndrew Thompson 	}
236502ac6454SAndrew Thompson 	/* check actual number of frames */
236602ac6454SAndrew Thompson 	if (xfer->aframes > xfer->nframes) {
236702ac6454SAndrew Thompson 		if (xfer->error == 0) {
236802ac6454SAndrew Thompson 			panic("%s: actual number of frames, %d, is "
236902ac6454SAndrew Thompson 			    "greater than initial number of frames, %d!\n",
237002ac6454SAndrew Thompson 			    __FUNCTION__, xfer->aframes, xfer->nframes);
237102ac6454SAndrew Thompson 		} else {
237202ac6454SAndrew Thompson 			/* just set some valid value */
237302ac6454SAndrew Thompson 			xfer->aframes = xfer->nframes;
237402ac6454SAndrew Thompson 		}
237502ac6454SAndrew Thompson 	}
237602ac6454SAndrew Thompson 	/* compute actual length */
237702ac6454SAndrew Thompson 	xfer->actlen = 0;
237802ac6454SAndrew Thompson 
237902ac6454SAndrew Thompson 	for (x = 0; x != xfer->aframes; x++) {
238002ac6454SAndrew Thompson 		xfer->actlen += xfer->frlengths[x];
238102ac6454SAndrew Thompson 	}
238202ac6454SAndrew Thompson 
238302ac6454SAndrew Thompson 	/*
238402ac6454SAndrew Thompson 	 * Frames that were not transferred get zero actual length in
238502ac6454SAndrew Thompson 	 * case the USB device driver does not check the actual number
238602ac6454SAndrew Thompson 	 * of frames transferred, "xfer->aframes":
238702ac6454SAndrew Thompson 	 */
238802ac6454SAndrew Thompson 	for (; x < xfer->nframes; x++) {
238902ac6454SAndrew Thompson 		xfer->frlengths[x] = 0;
239002ac6454SAndrew Thompson 	}
239102ac6454SAndrew Thompson 
239202ac6454SAndrew Thompson 	/* check actual length */
239302ac6454SAndrew Thompson 	if (xfer->actlen > xfer->sumlen) {
239402ac6454SAndrew Thompson 		if (xfer->error == 0) {
239502ac6454SAndrew Thompson 			panic("%s: actual length, %d, is greater than "
239602ac6454SAndrew Thompson 			    "initial length, %d!\n",
239702ac6454SAndrew Thompson 			    __FUNCTION__, xfer->actlen, xfer->sumlen);
239802ac6454SAndrew Thompson 		} else {
239902ac6454SAndrew Thompson 			/* just set some valid value */
240002ac6454SAndrew Thompson 			xfer->actlen = xfer->sumlen;
240102ac6454SAndrew Thompson 		}
240202ac6454SAndrew Thompson 	}
240302ac6454SAndrew Thompson 	DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
240402ac6454SAndrew Thompson 	    xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen,
240502ac6454SAndrew Thompson 	    xfer->aframes, xfer->nframes);
240602ac6454SAndrew Thompson 
240702ac6454SAndrew Thompson 	if (xfer->error) {
240802ac6454SAndrew Thompson 		/* end of control transfer, if any */
240902ac6454SAndrew Thompson 		xfer->flags_int.control_act = 0;
241002ac6454SAndrew Thompson 
241102ac6454SAndrew Thompson 		/* check if we should block the execution queue */
241202ac6454SAndrew Thompson 		if ((xfer->error != USB_ERR_CANCELLED) &&
241302ac6454SAndrew Thompson 		    (xfer->flags.pipe_bof)) {
241402ac6454SAndrew Thompson 			DPRINTFN(2, "xfer=%p: Block On Failure "
241502ac6454SAndrew Thompson 			    "on pipe=%p\n", xfer, xfer->pipe);
241602ac6454SAndrew Thompson 			goto done;
241702ac6454SAndrew Thompson 		}
241802ac6454SAndrew Thompson 	} else {
241902ac6454SAndrew Thompson 		/* check for short transfers */
242002ac6454SAndrew Thompson 		if (xfer->actlen < xfer->sumlen) {
242102ac6454SAndrew Thompson 
242202ac6454SAndrew Thompson 			/* end of control transfer, if any */
242302ac6454SAndrew Thompson 			xfer->flags_int.control_act = 0;
242402ac6454SAndrew Thompson 
242502ac6454SAndrew Thompson 			if (!xfer->flags_int.short_xfer_ok) {
242602ac6454SAndrew Thompson 				xfer->error = USB_ERR_SHORT_XFER;
242702ac6454SAndrew Thompson 				if (xfer->flags.pipe_bof) {
242802ac6454SAndrew Thompson 					DPRINTFN(2, "xfer=%p: Block On Failure on "
242902ac6454SAndrew Thompson 					    "Short Transfer on pipe %p.\n",
243002ac6454SAndrew Thompson 					    xfer, xfer->pipe);
243102ac6454SAndrew Thompson 					goto done;
243202ac6454SAndrew Thompson 				}
243302ac6454SAndrew Thompson 			}
243402ac6454SAndrew Thompson 		} else {
243502ac6454SAndrew Thompson 			/*
243602ac6454SAndrew Thompson 			 * Check if we are in the middle of a
243702ac6454SAndrew Thompson 			 * control transfer:
243802ac6454SAndrew Thompson 			 */
243902ac6454SAndrew Thompson 			if (xfer->flags_int.control_act) {
244002ac6454SAndrew Thompson 				DPRINTFN(5, "xfer=%p: Control transfer "
244102ac6454SAndrew Thompson 				    "active on pipe=%p\n", xfer, xfer->pipe);
244202ac6454SAndrew Thompson 				goto done;
244302ac6454SAndrew Thompson 			}
244402ac6454SAndrew Thompson 		}
244502ac6454SAndrew Thompson 	}
244602ac6454SAndrew Thompson 
244702ac6454SAndrew Thompson 	pipe = xfer->pipe;
244802ac6454SAndrew Thompson 
244902ac6454SAndrew Thompson 	/*
245002ac6454SAndrew Thompson 	 * If the current USB transfer is completing we need to start the
245102ac6454SAndrew Thompson 	 * next one:
245202ac6454SAndrew Thompson 	 */
245302ac6454SAndrew Thompson 	USB_BUS_LOCK(xfer->xroot->bus);
245402ac6454SAndrew Thompson 	if (pipe->pipe_q.curr == xfer) {
245502ac6454SAndrew Thompson 		usb2_command_wrapper(&pipe->pipe_q, NULL);
245602ac6454SAndrew Thompson 
245702ac6454SAndrew Thompson 		if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
245802ac6454SAndrew Thompson 			/* there is another USB transfer waiting */
245902ac6454SAndrew Thompson 		} else {
246002ac6454SAndrew Thompson 			/* this is the last USB transfer */
246102ac6454SAndrew Thompson 			/* clear isochronous sync flag */
246202ac6454SAndrew Thompson 			xfer->pipe->is_synced = 0;
246302ac6454SAndrew Thompson 		}
246402ac6454SAndrew Thompson 	}
246502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(xfer->xroot->bus);
246602ac6454SAndrew Thompson done:
246702ac6454SAndrew Thompson 	return (0);
246802ac6454SAndrew Thompson }
246902ac6454SAndrew Thompson 
247002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
247102ac6454SAndrew Thompson  *	usb2_command_wrapper
247202ac6454SAndrew Thompson  *
247302ac6454SAndrew Thompson  * This function is used to execute commands non-recursivly on an USB
247402ac6454SAndrew Thompson  * transfer.
247502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
247602ac6454SAndrew Thompson void
247702ac6454SAndrew Thompson usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
247802ac6454SAndrew Thompson {
247902ac6454SAndrew Thompson 	if (xfer) {
248002ac6454SAndrew Thompson 		/*
248102ac6454SAndrew Thompson 		 * If the transfer is not already processing,
248202ac6454SAndrew Thompson 		 * queue it!
248302ac6454SAndrew Thompson 		 */
248402ac6454SAndrew Thompson 		if (pq->curr != xfer) {
248502ac6454SAndrew Thompson 			usb2_transfer_enqueue(pq, xfer);
248602ac6454SAndrew Thompson 			if (pq->curr != NULL) {
248702ac6454SAndrew Thompson 				/* something is already processing */
248802ac6454SAndrew Thompson 				DPRINTFN(6, "busy %p\n", pq->curr);
248902ac6454SAndrew Thompson 				return;
249002ac6454SAndrew Thompson 			}
249102ac6454SAndrew Thompson 		}
249202ac6454SAndrew Thompson 	} else {
249302ac6454SAndrew Thompson 		/* Get next element in queue */
249402ac6454SAndrew Thompson 		pq->curr = NULL;
249502ac6454SAndrew Thompson 	}
249602ac6454SAndrew Thompson 
249702ac6454SAndrew Thompson 	if (!pq->recurse_1) {
249802ac6454SAndrew Thompson 
249902ac6454SAndrew Thompson 		do {
250002ac6454SAndrew Thompson 
250102ac6454SAndrew Thompson 			/* set both recurse flags */
250202ac6454SAndrew Thompson 			pq->recurse_1 = 1;
250302ac6454SAndrew Thompson 			pq->recurse_2 = 1;
250402ac6454SAndrew Thompson 
250502ac6454SAndrew Thompson 			if (pq->curr == NULL) {
250602ac6454SAndrew Thompson 				xfer = TAILQ_FIRST(&pq->head);
250702ac6454SAndrew Thompson 				if (xfer) {
250802ac6454SAndrew Thompson 					TAILQ_REMOVE(&pq->head, xfer,
250902ac6454SAndrew Thompson 					    wait_entry);
251002ac6454SAndrew Thompson 					xfer->wait_queue = NULL;
251102ac6454SAndrew Thompson 					pq->curr = xfer;
251202ac6454SAndrew Thompson 				} else {
251302ac6454SAndrew Thompson 					break;
251402ac6454SAndrew Thompson 				}
251502ac6454SAndrew Thompson 			}
251602ac6454SAndrew Thompson 			DPRINTFN(6, "cb %p (enter)\n", pq->curr);
251702ac6454SAndrew Thompson 			(pq->command) (pq);
251802ac6454SAndrew Thompson 			DPRINTFN(6, "cb %p (leave)\n", pq->curr);
251902ac6454SAndrew Thompson 
252002ac6454SAndrew Thompson 		} while (!pq->recurse_2);
252102ac6454SAndrew Thompson 
252202ac6454SAndrew Thompson 		/* clear first recurse flag */
252302ac6454SAndrew Thompson 		pq->recurse_1 = 0;
252402ac6454SAndrew Thompson 
252502ac6454SAndrew Thompson 	} else {
252602ac6454SAndrew Thompson 		/* clear second recurse flag */
252702ac6454SAndrew Thompson 		pq->recurse_2 = 0;
252802ac6454SAndrew Thompson 	}
252902ac6454SAndrew Thompson }
253002ac6454SAndrew Thompson 
253102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
253202ac6454SAndrew Thompson  *	usb2_default_transfer_setup
253302ac6454SAndrew Thompson  *
253402ac6454SAndrew Thompson  * This function is used to setup the default USB control endpoint
253502ac6454SAndrew Thompson  * transfer.
253602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
253702ac6454SAndrew Thompson void
253802ac6454SAndrew Thompson usb2_default_transfer_setup(struct usb2_device *udev)
253902ac6454SAndrew Thompson {
254002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
254102ac6454SAndrew Thompson 	uint8_t no_resetup;
254202ac6454SAndrew Thompson 	uint8_t iface_index;
254302ac6454SAndrew Thompson 
254402ac6454SAndrew Thompson repeat:
254502ac6454SAndrew Thompson 
254602ac6454SAndrew Thompson 	xfer = udev->default_xfer[0];
254702ac6454SAndrew Thompson 	if (xfer) {
254802ac6454SAndrew Thompson 		USB_XFER_LOCK(xfer);
254902ac6454SAndrew Thompson 		no_resetup =
255002ac6454SAndrew Thompson 		    ((xfer->address == udev->address) &&
255102ac6454SAndrew Thompson 		    (udev->default_ep_desc.wMaxPacketSize[0] ==
255202ac6454SAndrew Thompson 		    udev->ddesc.bMaxPacketSize));
255302ac6454SAndrew Thompson 		if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
255402ac6454SAndrew Thompson 			if (no_resetup) {
255502ac6454SAndrew Thompson 				/*
255602ac6454SAndrew Thompson 				 * NOTE: checking "xfer->address" and
255702ac6454SAndrew Thompson 				 * starting the USB transfer must be
255802ac6454SAndrew Thompson 				 * atomic!
255902ac6454SAndrew Thompson 				 */
256002ac6454SAndrew Thompson 				usb2_transfer_start(xfer);
256102ac6454SAndrew Thompson 			}
256202ac6454SAndrew Thompson 		}
256302ac6454SAndrew Thompson 		USB_XFER_UNLOCK(xfer);
256402ac6454SAndrew Thompson 	} else {
256502ac6454SAndrew Thompson 		no_resetup = 0;
256602ac6454SAndrew Thompson 	}
256702ac6454SAndrew Thompson 
256802ac6454SAndrew Thompson 	if (no_resetup) {
256902ac6454SAndrew Thompson 		/*
257002ac6454SAndrew Thompson 	         * All parameters are exactly the same like before.
257102ac6454SAndrew Thompson 	         * Just return.
257202ac6454SAndrew Thompson 	         */
257302ac6454SAndrew Thompson 		return;
257402ac6454SAndrew Thompson 	}
257502ac6454SAndrew Thompson 	/*
257602ac6454SAndrew Thompson 	 * Update wMaxPacketSize for the default control endpoint:
257702ac6454SAndrew Thompson 	 */
257802ac6454SAndrew Thompson 	udev->default_ep_desc.wMaxPacketSize[0] =
257902ac6454SAndrew Thompson 	    udev->ddesc.bMaxPacketSize;
258002ac6454SAndrew Thompson 
258102ac6454SAndrew Thompson 	/*
258202ac6454SAndrew Thompson 	 * Unsetup any existing USB transfer:
258302ac6454SAndrew Thompson 	 */
258402ac6454SAndrew Thompson 	usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
258502ac6454SAndrew Thompson 
258602ac6454SAndrew Thompson 	/*
258702ac6454SAndrew Thompson 	 * Try to setup a new USB transfer for the
258802ac6454SAndrew Thompson 	 * default control endpoint:
258902ac6454SAndrew Thompson 	 */
259002ac6454SAndrew Thompson 	iface_index = 0;
259102ac6454SAndrew Thompson 	if (usb2_transfer_setup(udev, &iface_index,
259202ac6454SAndrew Thompson 	    udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL,
259302ac6454SAndrew Thompson 	    udev->default_mtx)) {
259402ac6454SAndrew Thompson 		DPRINTFN(0, "could not setup default "
259502ac6454SAndrew Thompson 		    "USB transfer!\n");
259602ac6454SAndrew Thompson 	} else {
259702ac6454SAndrew Thompson 		goto repeat;
259802ac6454SAndrew Thompson 	}
259902ac6454SAndrew Thompson }
260002ac6454SAndrew Thompson 
260102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
260202ac6454SAndrew Thompson  *	usb2_clear_data_toggle - factored out code
260302ac6454SAndrew Thompson  *
260402ac6454SAndrew Thompson  * NOTE: the intention of this function is not to reset the hardware
260502ac6454SAndrew Thompson  * data toggle.
260602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
260702ac6454SAndrew Thompson void
260802ac6454SAndrew Thompson usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe)
260902ac6454SAndrew Thompson {
261002ac6454SAndrew Thompson 	DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe);
261102ac6454SAndrew Thompson 
261202ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
261302ac6454SAndrew Thompson 	pipe->toggle_next = 0;
261402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
261502ac6454SAndrew Thompson }
261602ac6454SAndrew Thompson 
261702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
261802ac6454SAndrew Thompson  *	usb2_clear_stall_callback - factored out clear stall callback
261902ac6454SAndrew Thompson  *
262002ac6454SAndrew Thompson  * Input parameters:
262102ac6454SAndrew Thompson  *  xfer1: Clear Stall Control Transfer
262202ac6454SAndrew Thompson  *  xfer2: Stalled USB Transfer
262302ac6454SAndrew Thompson  *
262402ac6454SAndrew Thompson  * This function is NULL safe.
262502ac6454SAndrew Thompson  *
262602ac6454SAndrew Thompson  * Return values:
262702ac6454SAndrew Thompson  *   0: In progress
262802ac6454SAndrew Thompson  *   Else: Finished
262902ac6454SAndrew Thompson  *
263002ac6454SAndrew Thompson  * Clear stall config example:
263102ac6454SAndrew Thompson  *
263202ac6454SAndrew Thompson  * static const struct usb2_config my_clearstall =  {
263302ac6454SAndrew Thompson  *	.type = UE_CONTROL,
263402ac6454SAndrew Thompson  *	.endpoint = 0,
263502ac6454SAndrew Thompson  *	.direction = UE_DIR_ANY,
263602ac6454SAndrew Thompson  *	.interval = 50, //50 milliseconds
263702ac6454SAndrew Thompson  *	.bufsize = sizeof(struct usb2_device_request),
263802ac6454SAndrew Thompson  *	.mh.timeout = 1000, //1.000 seconds
263902ac6454SAndrew Thompson  *	.mh.flags = { },
264002ac6454SAndrew Thompson  *	.mh.callback = &my_clear_stall_callback, // **
264102ac6454SAndrew Thompson  * };
264202ac6454SAndrew Thompson  *
264302ac6454SAndrew Thompson  * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback"
264402ac6454SAndrew Thompson  * passing the correct parameters.
264502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
264602ac6454SAndrew Thompson uint8_t
264702ac6454SAndrew Thompson usb2_clear_stall_callback(struct usb2_xfer *xfer1,
264802ac6454SAndrew Thompson     struct usb2_xfer *xfer2)
264902ac6454SAndrew Thompson {
265002ac6454SAndrew Thompson 	struct usb2_device_request req;
265102ac6454SAndrew Thompson 
265202ac6454SAndrew Thompson 	if (xfer2 == NULL) {
265302ac6454SAndrew Thompson 		/* looks like we are tearing down */
265402ac6454SAndrew Thompson 		DPRINTF("NULL input parameter\n");
265502ac6454SAndrew Thompson 		return (0);
265602ac6454SAndrew Thompson 	}
265702ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
265802ac6454SAndrew Thompson 	USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
265902ac6454SAndrew Thompson 
266002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer1)) {
266102ac6454SAndrew Thompson 	case USB_ST_SETUP:
266202ac6454SAndrew Thompson 
266302ac6454SAndrew Thompson 		/*
266402ac6454SAndrew Thompson 		 * pre-clear the data toggle to DATA0 ("umass.c" and
266502ac6454SAndrew Thompson 		 * "ata-usb.c" depends on this)
266602ac6454SAndrew Thompson 		 */
266702ac6454SAndrew Thompson 
266802ac6454SAndrew Thompson 		usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe);
266902ac6454SAndrew Thompson 
267002ac6454SAndrew Thompson 		/* setup a clear-stall packet */
267102ac6454SAndrew Thompson 
267202ac6454SAndrew Thompson 		req.bmRequestType = UT_WRITE_ENDPOINT;
267302ac6454SAndrew Thompson 		req.bRequest = UR_CLEAR_FEATURE;
267402ac6454SAndrew Thompson 		USETW(req.wValue, UF_ENDPOINT_HALT);
267502ac6454SAndrew Thompson 		req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress;
267602ac6454SAndrew Thompson 		req.wIndex[1] = 0;
267702ac6454SAndrew Thompson 		USETW(req.wLength, 0);
267802ac6454SAndrew Thompson 
267902ac6454SAndrew Thompson 		/*
268002ac6454SAndrew Thompson 		 * "usb2_transfer_setup_sub()" will ensure that
268102ac6454SAndrew Thompson 		 * we have sufficient room in the buffer for
268202ac6454SAndrew Thompson 		 * the request structure!
268302ac6454SAndrew Thompson 		 */
268402ac6454SAndrew Thompson 
268502ac6454SAndrew Thompson 		/* copy in the transfer */
268602ac6454SAndrew Thompson 
268702ac6454SAndrew Thompson 		usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
268802ac6454SAndrew Thompson 
268902ac6454SAndrew Thompson 		/* set length */
269002ac6454SAndrew Thompson 		xfer1->frlengths[0] = sizeof(req);
269102ac6454SAndrew Thompson 		xfer1->nframes = 1;
269202ac6454SAndrew Thompson 
269302ac6454SAndrew Thompson 		usb2_start_hardware(xfer1);
269402ac6454SAndrew Thompson 		return (0);
269502ac6454SAndrew Thompson 
269602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
269702ac6454SAndrew Thompson 		break;
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson 	default:			/* Error */
270002ac6454SAndrew Thompson 		if (xfer1->error == USB_ERR_CANCELLED) {
270102ac6454SAndrew Thompson 			return (0);
270202ac6454SAndrew Thompson 		}
270302ac6454SAndrew Thompson 		break;
270402ac6454SAndrew Thompson 	}
270502ac6454SAndrew Thompson 	return (1);			/* Clear Stall Finished */
270602ac6454SAndrew Thompson }
270702ac6454SAndrew Thompson 
270802ac6454SAndrew Thompson #if (USB_NO_POLL == 0)
270902ac6454SAndrew Thompson 
271002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
271102ac6454SAndrew Thompson  *	usb2_callout_poll
271202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
271302ac6454SAndrew Thompson static void
271402ac6454SAndrew Thompson usb2_callout_poll(struct usb2_xfer *xfer)
271502ac6454SAndrew Thompson {
271602ac6454SAndrew Thompson 	struct usb2_callout *co;
271702ac6454SAndrew Thompson 	void (*cb) (void *);
271802ac6454SAndrew Thompson 	void *arg;
271902ac6454SAndrew Thompson 	struct mtx *mtx;
272002ac6454SAndrew Thompson 	uint32_t delta;
272102ac6454SAndrew Thompson 
272202ac6454SAndrew Thompson 	if (xfer == NULL) {
272302ac6454SAndrew Thompson 		return;
272402ac6454SAndrew Thompson 	}
272502ac6454SAndrew Thompson 	co = &xfer->timeout_handle;
272602ac6454SAndrew Thompson 
272702ac6454SAndrew Thompson #if __FreeBSD_version >= 800000
272802ac6454SAndrew Thompson 	mtx = (void *)(co->co.c_lock);
272902ac6454SAndrew Thompson #else
273002ac6454SAndrew Thompson 	mtx = co->co.c_mtx;
273102ac6454SAndrew Thompson #endif
273202ac6454SAndrew Thompson 	mtx_lock(mtx);
273302ac6454SAndrew Thompson 
273402ac6454SAndrew Thompson 	if (usb2_callout_pending(co)) {
273502ac6454SAndrew Thompson 		delta = ticks - co->co.c_time;
273602ac6454SAndrew Thompson 		if (!(delta & 0x80000000)) {
273702ac6454SAndrew Thompson 
273802ac6454SAndrew Thompson 			cb = co->co.c_func;
273902ac6454SAndrew Thompson 			arg = co->co.c_arg;
274002ac6454SAndrew Thompson 
274102ac6454SAndrew Thompson 			/* timed out */
274202ac6454SAndrew Thompson 			usb2_callout_stop(co);
274302ac6454SAndrew Thompson 
274402ac6454SAndrew Thompson 			(cb) (arg);
274502ac6454SAndrew Thompson 		}
274602ac6454SAndrew Thompson 	}
274702ac6454SAndrew Thompson 	mtx_unlock(mtx);
274802ac6454SAndrew Thompson }
274902ac6454SAndrew Thompson 
275002ac6454SAndrew Thompson 
275102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
275202ac6454SAndrew Thompson  *	usb2_do_poll
275302ac6454SAndrew Thompson  *
275402ac6454SAndrew Thompson  * This function is called from keyboard driver when in polling
275502ac6454SAndrew Thompson  * mode.
275602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
275702ac6454SAndrew Thompson void
275802ac6454SAndrew Thompson usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
275902ac6454SAndrew Thompson {
276002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
276102ac6454SAndrew Thompson 	struct usb2_xfer_root *xroot;
276202ac6454SAndrew Thompson 	struct usb2_device *udev;
276302ac6454SAndrew Thompson 	struct usb2_proc_msg *pm;
276402ac6454SAndrew Thompson 	uint32_t to;
276502ac6454SAndrew Thompson 	uint16_t n;
276602ac6454SAndrew Thompson 
276702ac6454SAndrew Thompson 	/* compute system tick delay */
276802ac6454SAndrew Thompson 	to = ((uint32_t)(1000000)) / ((uint32_t)(hz));
276902ac6454SAndrew Thompson 	DELAY(to);
277002ac6454SAndrew Thompson 	atomic_add_int((volatile int *)&ticks, 1);
277102ac6454SAndrew Thompson 
277202ac6454SAndrew Thompson 	for (n = 0; n != max; n++) {
277302ac6454SAndrew Thompson 		xfer = ppxfer[n];
277402ac6454SAndrew Thompson 		if (xfer) {
277502ac6454SAndrew Thompson 			xroot = xfer->xroot;
277602ac6454SAndrew Thompson 			udev = xroot->udev;
277702ac6454SAndrew Thompson 
277802ac6454SAndrew Thompson 			/*
277902ac6454SAndrew Thompson 			 * Poll hardware - signal that we are polling by
278002ac6454SAndrew Thompson 			 * locking the private mutex:
278102ac6454SAndrew Thompson 			 */
278202ac6454SAndrew Thompson 			USB_XFER_LOCK(xfer);
278302ac6454SAndrew Thompson 			(udev->bus->methods->do_poll) (udev->bus);
278402ac6454SAndrew Thompson 			USB_XFER_UNLOCK(xfer);
278502ac6454SAndrew Thompson 
278602ac6454SAndrew Thompson 			/* poll clear stall start */
278702ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
278802ac6454SAndrew Thompson 			pm = &udev->cs_msg[0].hdr;
278902ac6454SAndrew Thompson 			(pm->pm_callback) (pm);
279002ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
279102ac6454SAndrew Thompson 
279202ac6454SAndrew Thompson 			if (udev->default_xfer[1]) {
279302ac6454SAndrew Thompson 
279402ac6454SAndrew Thompson 				/* poll timeout */
279502ac6454SAndrew Thompson 				usb2_callout_poll(udev->default_xfer[1]);
279602ac6454SAndrew Thompson 
279702ac6454SAndrew Thompson 				/* poll clear stall done thread */
279802ac6454SAndrew Thompson 				USB_BUS_LOCK(xfer->xroot->bus);
279902ac6454SAndrew Thompson 				pm = &udev->default_xfer[1]->
280002ac6454SAndrew Thompson 				    xroot->done_m[0].hdr;
280102ac6454SAndrew Thompson 				(pm->pm_callback) (pm);
280202ac6454SAndrew Thompson 				USB_BUS_UNLOCK(xfer->xroot->bus);
280302ac6454SAndrew Thompson 			}
280402ac6454SAndrew Thompson 			/* poll timeout */
280502ac6454SAndrew Thompson 			usb2_callout_poll(xfer);
280602ac6454SAndrew Thompson 
280702ac6454SAndrew Thompson 			/* poll done thread */
280802ac6454SAndrew Thompson 			USB_BUS_LOCK(xfer->xroot->bus);
280902ac6454SAndrew Thompson 			pm = &xroot->done_m[0].hdr;
281002ac6454SAndrew Thompson 			(pm->pm_callback) (pm);
281102ac6454SAndrew Thompson 			USB_BUS_UNLOCK(xfer->xroot->bus);
281202ac6454SAndrew Thompson 		}
281302ac6454SAndrew Thompson 	}
281402ac6454SAndrew Thompson }
281502ac6454SAndrew Thompson 
281602ac6454SAndrew Thompson #else
281702ac6454SAndrew Thompson 
281802ac6454SAndrew Thompson void
281902ac6454SAndrew Thompson usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
282002ac6454SAndrew Thompson {
282102ac6454SAndrew Thompson 	/* polling not supported */
282202ac6454SAndrew Thompson }
282302ac6454SAndrew Thompson 
282402ac6454SAndrew Thompson #endif
2825