usb_transfer.c revision 190181
1/* $FreeBSD: head/sys/dev/usb/usb_transfer.c 190181 2009-03-20 21:50:54Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/usb/usb_mfunc.h>
28#include <dev/usb/usb_error.h>
29#include <dev/usb/usb.h>
30
31#define	USB_DEBUG_VAR usb2_debug
32
33#include <dev/usb/usb_core.h>
34#include <dev/usb/usb_busdma.h>
35#include <dev/usb/usb_process.h>
36#include <dev/usb/usb_transfer.h>
37#include <dev/usb/usb_device.h>
38#include <dev/usb/usb_debug.h>
39#include <dev/usb/usb_util.h>
40
41#include <dev/usb/usb_controller.h>
42#include <dev/usb/usb_bus.h>
43
44struct usb2_std_packet_size {
45	struct {
46		uint16_t min;		/* inclusive */
47		uint16_t max;		/* inclusive */
48	}	range;
49
50	uint16_t fixed[4];
51};
52
53/*
54 * This table stores the all the allowed packet sizes based on
55 * endpoint type and USB speed:
56 */
57static const struct usb2_std_packet_size
58	usb2_std_packet_size[4][USB_SPEED_MAX] = {
59
60	[UE_INTERRUPT] = {
61		[USB_SPEED_LOW] = {.range = {0, 8}},
62		[USB_SPEED_FULL] = {.range = {0, 64}},
63		[USB_SPEED_HIGH] = {.range = {0, 1024}},
64		[USB_SPEED_VARIABLE] = {.range = {0, 1024}},
65		[USB_SPEED_SUPER] = {.range = {0, 1024}},
66	},
67
68	[UE_CONTROL] = {
69		[USB_SPEED_LOW] = {.fixed = {8, 8, 8, 8}},
70		[USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
71		[USB_SPEED_HIGH] = {.fixed = {64, 64, 64, 64}},
72		[USB_SPEED_VARIABLE] = {.fixed = {512, 512, 512, 512}},
73		[USB_SPEED_SUPER] = {.fixed = {512, 512, 512, 512}},
74	},
75
76	[UE_BULK] = {
77		[USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}},	/* invalid */
78		[USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
79		[USB_SPEED_HIGH] = {.fixed = {512, 512, 512, 512}},
80		[USB_SPEED_VARIABLE] = {.fixed = {512, 512, 1024, 1536}},
81		[USB_SPEED_SUPER] = {.fixed = {1024, 1024, 1024, 1024}},
82	},
83
84	[UE_ISOCHRONOUS] = {
85		[USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}},	/* invalid */
86		[USB_SPEED_FULL] = {.range = {0, 1023}},
87		[USB_SPEED_HIGH] = {.range = {0, 1024}},
88		[USB_SPEED_VARIABLE] = {.range = {0, 3584}},
89		[USB_SPEED_SUPER] = {.range = {0, 1024}},
90	},
91};
92
93static const struct usb2_config usb2_control_ep_cfg[USB_DEFAULT_XFER_MAX] = {
94
95	/* This transfer is used for generic control endpoint transfers */
96
97	[0] = {
98		.type = UE_CONTROL,
99		.endpoint = 0x00,	/* Control endpoint */
100		.direction = UE_DIR_ANY,
101		.mh.bufsize = USB_EP0_BUFSIZE,	/* bytes */
102		.mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
103		.mh.callback = &usb2_do_request_callback,
104		.md.bufsize = USB_EP0_BUFSIZE,	/* bytes */
105		.md.flags = {.proxy_buffer = 1,.short_xfer_ok = 0,},
106		.md.callback = &usb2_handle_request_callback,
107	},
108
109	/* This transfer is used for generic clear stall only */
110
111	[1] = {
112		.type = UE_CONTROL,
113		.endpoint = 0x00,	/* Control pipe */
114		.direction = UE_DIR_ANY,
115		.mh.bufsize = sizeof(struct usb2_device_request),
116		.mh.flags = {},
117		.mh.callback = &usb2_do_clear_stall_callback,
118		.mh.timeout = 1000,	/* 1 second */
119		.mh.interval = 50,	/* 50ms */
120	},
121};
122
123/* function prototypes */
124
125static void	usb2_update_max_frame_size(struct usb2_xfer *);
126static void	usb2_transfer_unsetup_sub(struct usb2_xfer_root *, uint8_t);
127static void	usb2_control_transfer_init(struct usb2_xfer *);
128static uint8_t	usb2_start_hardware_sub(struct usb2_xfer *);
129static void	usb2_callback_proc(struct usb2_proc_msg *);
130static void	usb2_callback_ss_done_defer(struct usb2_xfer *);
131static void	usb2_callback_wrapper(struct usb2_xfer_queue *);
132static void	usb2_dma_delay_done_cb(void *);
133static void	usb2_transfer_start_cb(void *);
134static uint8_t	usb2_callback_wrapper_sub(struct usb2_xfer *);
135
136/*------------------------------------------------------------------------*
137 *	usb2_update_max_frame_size
138 *
139 * This function updates the maximum frame size, hence high speed USB
140 * can transfer multiple consecutive packets.
141 *------------------------------------------------------------------------*/
142static void
143usb2_update_max_frame_size(struct usb2_xfer *xfer)
144{
145	/* compute maximum frame size */
146
147	if (xfer->max_packet_count == 2) {
148		xfer->max_frame_size = 2 * xfer->max_packet_size;
149	} else if (xfer->max_packet_count == 3) {
150		xfer->max_frame_size = 3 * xfer->max_packet_size;
151	} else {
152		xfer->max_frame_size = xfer->max_packet_size;
153	}
154}
155
156/*------------------------------------------------------------------------*
157 *	usb2_get_dma_delay
158 *
159 * The following function is called when we need to
160 * synchronize with DMA hardware.
161 *
162 * Returns:
163 *    0: no DMA delay required
164 * Else: milliseconds of DMA delay
165 *------------------------------------------------------------------------*/
166usb2_timeout_t
167usb2_get_dma_delay(struct usb2_bus *bus)
168{
169	uint32_t temp = 0;
170
171	if (bus->methods->get_dma_delay) {
172		(bus->methods->get_dma_delay) (bus, &temp);
173		/*
174		 * Round up and convert to milliseconds. Note that we use
175		 * 1024 milliseconds per second. to save a division.
176		 */
177		temp += 0x3FF;
178		temp /= 0x400;
179	}
180	return (temp);
181}
182
183/*------------------------------------------------------------------------*
184 *	usb2_transfer_setup_sub_malloc
185 *
186 * This function will allocate one or more DMA'able memory chunks
187 * according to "size", "align" and "count" arguments. "ppc" is
188 * pointed to a linear array of USB page caches afterwards.
189 *
190 * Returns:
191 *    0: Success
192 * Else: Failure
193 *------------------------------------------------------------------------*/
194#if USB_HAVE_BUSDMA
195uint8_t
196usb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm,
197    struct usb2_page_cache **ppc, usb2_size_t size, usb2_size_t align,
198    usb2_size_t count)
199{
200	struct usb2_page_cache *pc;
201	struct usb2_page *pg;
202	void *buf;
203	usb2_size_t n_dma_pc;
204	usb2_size_t n_obj;
205	usb2_size_t x;
206	usb2_size_t y;
207	usb2_size_t r;
208	usb2_size_t z;
209
210	USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n",
211	    align));
212	USB_ASSERT(size > 0, ("Invalid size = 0!\n"));
213
214	if (count == 0) {
215		return (0);		/* nothing to allocate */
216	}
217	/*
218	 * Make sure that the size is aligned properly.
219	 */
220	size = -((-size) & (-align));
221
222	/*
223	 * Try multi-allocation chunks to reduce the number of DMA
224	 * allocations, hence DMA allocations are slow.
225	 */
226	if (size >= PAGE_SIZE) {
227		n_dma_pc = count;
228		n_obj = 1;
229	} else {
230		/* compute number of objects per page */
231		n_obj = (PAGE_SIZE / size);
232		/*
233		 * Compute number of DMA chunks, rounded up
234		 * to nearest one:
235		 */
236		n_dma_pc = ((count + n_obj - 1) / n_obj);
237	}
238
239	if (parm->buf == NULL) {
240		/* for the future */
241		parm->dma_page_ptr += n_dma_pc;
242		parm->dma_page_cache_ptr += n_dma_pc;
243		parm->dma_page_ptr += count;
244		parm->xfer_page_cache_ptr += count;
245		return (0);
246	}
247	for (x = 0; x != n_dma_pc; x++) {
248		/* need to initialize the page cache */
249		parm->dma_page_cache_ptr[x].tag_parent =
250		    &parm->curr_xfer->xroot->dma_parent_tag;
251	}
252	for (x = 0; x != count; x++) {
253		/* need to initialize the page cache */
254		parm->xfer_page_cache_ptr[x].tag_parent =
255		    &parm->curr_xfer->xroot->dma_parent_tag;
256	}
257
258	if (ppc) {
259		*ppc = parm->xfer_page_cache_ptr;
260	}
261	r = count;			/* set remainder count */
262	z = n_obj * size;		/* set allocation size */
263	pc = parm->xfer_page_cache_ptr;
264	pg = parm->dma_page_ptr;
265
266	for (x = 0; x != n_dma_pc; x++) {
267
268		if (r < n_obj) {
269			/* compute last remainder */
270			z = r * size;
271			n_obj = r;
272		}
273		if (usb2_pc_alloc_mem(parm->dma_page_cache_ptr,
274		    pg, z, align)) {
275			return (1);	/* failure */
276		}
277		/* Set beginning of current buffer */
278		buf = parm->dma_page_cache_ptr->buffer;
279		/* Make room for one DMA page cache and one page */
280		parm->dma_page_cache_ptr++;
281		pg++;
282
283		for (y = 0; (y != n_obj); y++, r--, pc++, pg++) {
284
285			/* Load sub-chunk into DMA */
286			if (usb2_pc_dmamap_create(pc, size)) {
287				return (1);	/* failure */
288			}
289			pc->buffer = USB_ADD_BYTES(buf, y * size);
290			pc->page_start = pg;
291
292			mtx_lock(pc->tag_parent->mtx);
293			if (usb2_pc_load_mem(pc, size, 1 /* synchronous */ )) {
294				mtx_unlock(pc->tag_parent->mtx);
295				return (1);	/* failure */
296			}
297			mtx_unlock(pc->tag_parent->mtx);
298		}
299	}
300
301	parm->xfer_page_cache_ptr = pc;
302	parm->dma_page_ptr = pg;
303	return (0);
304}
305#endif
306
307/*------------------------------------------------------------------------*
308 *	usb2_transfer_setup_sub - transfer setup subroutine
309 *
310 * This function must be called from the "xfer_setup" callback of the
311 * USB Host or Device controller driver when setting up an USB
312 * transfer. This function will setup correct packet sizes, buffer
313 * sizes, flags and more, that are stored in the "usb2_xfer"
314 * structure.
315 *------------------------------------------------------------------------*/
316void
317usb2_transfer_setup_sub(struct usb2_setup_params *parm)
318{
319	enum {
320		REQ_SIZE = 8,
321		MIN_PKT = 8,
322	};
323	struct usb2_xfer *xfer = parm->curr_xfer;
324	const struct usb2_config_sub *setup_sub = parm->curr_setup_sub;
325	struct usb2_endpoint_descriptor *edesc;
326	struct usb2_std_packet_size std_size;
327	usb2_frcount_t n_frlengths;
328	usb2_frcount_t n_frbuffers;
329	usb2_frcount_t x;
330	uint8_t type;
331	uint8_t zmps;
332
333	/*
334	 * Sanity check. The following parameters must be initialized before
335	 * calling this function.
336	 */
337	if ((parm->hc_max_packet_size == 0) ||
338	    (parm->hc_max_packet_count == 0) ||
339	    (parm->hc_max_frame_size == 0)) {
340		parm->err = USB_ERR_INVAL;
341		goto done;
342	}
343	edesc = xfer->pipe->edesc;
344
345	type = (edesc->bmAttributes & UE_XFERTYPE);
346
347	xfer->flags = setup_sub->flags;
348	xfer->nframes = setup_sub->frames;
349	xfer->timeout = setup_sub->timeout;
350	xfer->callback = setup_sub->callback;
351	xfer->interval = setup_sub->interval;
352	xfer->endpoint = edesc->bEndpointAddress;
353	xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
354	xfer->max_packet_count = 1;
355	/* make a shadow copy: */
356	xfer->flags_int.usb2_mode = parm->udev->flags.usb2_mode;
357
358	parm->bufsize = setup_sub->bufsize;
359
360	if (parm->speed == USB_SPEED_HIGH) {
361		xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
362		xfer->max_packet_size &= 0x7FF;
363	}
364	/* range check "max_packet_count" */
365
366	if (xfer->max_packet_count > parm->hc_max_packet_count) {
367		xfer->max_packet_count = parm->hc_max_packet_count;
368	}
369	/* filter "wMaxPacketSize" according to HC capabilities */
370
371	if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
372	    (xfer->max_packet_size == 0)) {
373		xfer->max_packet_size = parm->hc_max_packet_size;
374	}
375	/* filter "wMaxPacketSize" according to standard sizes */
376
377	std_size = usb2_std_packet_size[type][parm->speed];
378
379	if (std_size.range.min || std_size.range.max) {
380
381		if (xfer->max_packet_size < std_size.range.min) {
382			xfer->max_packet_size = std_size.range.min;
383		}
384		if (xfer->max_packet_size > std_size.range.max) {
385			xfer->max_packet_size = std_size.range.max;
386		}
387	} else {
388
389		if (xfer->max_packet_size >= std_size.fixed[3]) {
390			xfer->max_packet_size = std_size.fixed[3];
391		} else if (xfer->max_packet_size >= std_size.fixed[2]) {
392			xfer->max_packet_size = std_size.fixed[2];
393		} else if (xfer->max_packet_size >= std_size.fixed[1]) {
394			xfer->max_packet_size = std_size.fixed[1];
395		} else {
396			/* only one possibility left */
397			xfer->max_packet_size = std_size.fixed[0];
398		}
399	}
400
401	/* compute "max_frame_size" */
402
403	usb2_update_max_frame_size(xfer);
404
405	/* check interrupt interval and transfer pre-delay */
406
407	if (type == UE_ISOCHRONOUS) {
408
409		uint16_t frame_limit;
410
411		xfer->interval = 0;	/* not used, must be zero */
412		xfer->flags_int.isochronous_xfr = 1;	/* set flag */
413
414		if (xfer->timeout == 0) {
415			/*
416			 * set a default timeout in
417			 * case something goes wrong!
418			 */
419			xfer->timeout = 1000 / 4;
420		}
421		switch (parm->speed) {
422		case USB_SPEED_LOW:
423		case USB_SPEED_FULL:
424			frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
425			break;
426		default:
427			frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
428			break;
429		}
430
431		if (xfer->nframes > frame_limit) {
432			/*
433			 * this is not going to work
434			 * cross hardware
435			 */
436			parm->err = USB_ERR_INVAL;
437			goto done;
438		}
439		if (xfer->nframes == 0) {
440			/*
441			 * this is not a valid value
442			 */
443			parm->err = USB_ERR_ZERO_NFRAMES;
444			goto done;
445		}
446	} else {
447
448		/*
449		 * if a value is specified use that else check the endpoint
450		 * descriptor
451		 */
452		if (xfer->interval == 0) {
453
454			if (type == UE_INTERRUPT) {
455
456				xfer->interval = edesc->bInterval;
457
458				switch (parm->speed) {
459				case USB_SPEED_SUPER:
460				case USB_SPEED_VARIABLE:
461					/* 125us -> 1ms */
462					if (xfer->interval < 4)
463						xfer->interval = 1;
464					else if (xfer->interval > 16)
465						xfer->interval = (1<<(16-4));
466					else
467						xfer->interval =
468						    (1 << (xfer->interval-4));
469					break;
470				case USB_SPEED_HIGH:
471					/* 125us -> 1ms */
472					xfer->interval /= 8;
473					break;
474				default:
475					break;
476				}
477				if (xfer->interval == 0) {
478					/*
479					 * One millisecond is the smallest
480					 * interval we support:
481					 */
482					xfer->interval = 1;
483				}
484			}
485		}
486	}
487
488	/*
489	 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
490	 * to be equal to zero when setting up USB transfers, hence
491	 * this leads to alot of extra code in the USB kernel.
492	 */
493
494	if ((xfer->max_frame_size == 0) ||
495	    (xfer->max_packet_size == 0)) {
496
497		zmps = 1;
498
499		if ((parm->bufsize <= MIN_PKT) &&
500		    (type != UE_CONTROL) &&
501		    (type != UE_BULK)) {
502
503			/* workaround */
504			xfer->max_packet_size = MIN_PKT;
505			xfer->max_packet_count = 1;
506			parm->bufsize = 0;	/* automatic setup length */
507			usb2_update_max_frame_size(xfer);
508
509		} else {
510			parm->err = USB_ERR_ZERO_MAXP;
511			goto done;
512		}
513
514	} else {
515		zmps = 0;
516	}
517
518	/*
519	 * check if we should setup a default
520	 * length:
521	 */
522
523	if (parm->bufsize == 0) {
524
525		parm->bufsize = xfer->max_frame_size;
526
527		if (type == UE_ISOCHRONOUS) {
528			parm->bufsize *= xfer->nframes;
529		}
530	}
531	/*
532	 * check if we are about to setup a proxy
533	 * type of buffer:
534	 */
535
536	if (xfer->flags.proxy_buffer) {
537
538		/* round bufsize up */
539
540		parm->bufsize += (xfer->max_frame_size - 1);
541
542		if (parm->bufsize < xfer->max_frame_size) {
543			/* length wrapped around */
544			parm->err = USB_ERR_INVAL;
545			goto done;
546		}
547		/* subtract remainder */
548
549		parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
550
551		/* add length of USB device request structure, if any */
552
553		if (type == UE_CONTROL) {
554			parm->bufsize += REQ_SIZE;	/* SETUP message */
555		}
556	}
557	xfer->max_data_length = parm->bufsize;
558
559	/* Setup "n_frlengths" and "n_frbuffers" */
560
561	if (type == UE_ISOCHRONOUS) {
562		n_frlengths = xfer->nframes;
563		n_frbuffers = 1;
564	} else {
565
566		if (type == UE_CONTROL) {
567			xfer->flags_int.control_xfr = 1;
568			if (xfer->nframes == 0) {
569				if (parm->bufsize <= REQ_SIZE) {
570					/*
571					 * there will never be any data
572					 * stage
573					 */
574					xfer->nframes = 1;
575				} else {
576					xfer->nframes = 2;
577				}
578			}
579		} else {
580			if (xfer->nframes == 0) {
581				xfer->nframes = 1;
582			}
583		}
584
585		n_frlengths = xfer->nframes;
586		n_frbuffers = xfer->nframes;
587	}
588
589	/*
590	 * check if we have room for the
591	 * USB device request structure:
592	 */
593
594	if (type == UE_CONTROL) {
595
596		if (xfer->max_data_length < REQ_SIZE) {
597			/* length wrapped around or too small bufsize */
598			parm->err = USB_ERR_INVAL;
599			goto done;
600		}
601		xfer->max_data_length -= REQ_SIZE;
602	}
603	/* setup "frlengths" */
604
605	xfer->frlengths = parm->xfer_length_ptr;
606
607	parm->xfer_length_ptr += n_frlengths;
608
609	/* setup "frbuffers" */
610
611	xfer->frbuffers = parm->xfer_page_cache_ptr;
612
613	parm->xfer_page_cache_ptr += n_frbuffers;
614
615	/*
616	 * check if we need to setup
617	 * a local buffer:
618	 */
619
620	if (!xfer->flags.ext_buffer) {
621
622		/* align data */
623		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
624
625		if (parm->buf) {
626
627			xfer->local_buffer =
628			    USB_ADD_BYTES(parm->buf, parm->size[0]);
629
630			usb2_set_frame_offset(xfer, 0, 0);
631
632			if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
633				usb2_set_frame_offset(xfer, REQ_SIZE, 1);
634			}
635		}
636		parm->size[0] += parm->bufsize;
637
638		/* align data again */
639		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
640	}
641	/*
642	 * Compute maximum buffer size
643	 */
644
645	if (parm->bufsize_max < parm->bufsize) {
646		parm->bufsize_max = parm->bufsize;
647	}
648#if USB_HAVE_BUSDMA
649	if (xfer->flags_int.bdma_enable) {
650		/*
651		 * Setup "dma_page_ptr".
652		 *
653		 * Proof for formula below:
654		 *
655		 * Assume there are three USB frames having length "a", "b" and
656		 * "c". These USB frames will at maximum need "z"
657		 * "usb2_page" structures. "z" is given by:
658		 *
659		 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
660		 * ((c / USB_PAGE_SIZE) + 2);
661		 *
662		 * Constraining "a", "b" and "c" like this:
663		 *
664		 * (a + b + c) <= parm->bufsize
665		 *
666		 * We know that:
667		 *
668		 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
669		 *
670		 * Here is the general formula:
671		 */
672		xfer->dma_page_ptr = parm->dma_page_ptr;
673		parm->dma_page_ptr += (2 * n_frbuffers);
674		parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
675	}
676#endif
677	if (zmps) {
678		/* correct maximum data length */
679		xfer->max_data_length = 0;
680	}
681	/* subtract USB frame remainder from "hc_max_frame_size" */
682
683	xfer->max_hc_frame_size =
684	    (parm->hc_max_frame_size -
685	    (parm->hc_max_frame_size % xfer->max_frame_size));
686
687	if (xfer->max_hc_frame_size == 0) {
688		parm->err = USB_ERR_INVAL;
689		goto done;
690	}
691	/* initialize max frame count */
692
693	xfer->max_frame_count = xfer->nframes;
694
695	/* initialize frame buffers */
696
697	if (parm->buf) {
698		for (x = 0; x != n_frbuffers; x++) {
699			xfer->frbuffers[x].tag_parent =
700			    &xfer->xroot->dma_parent_tag;
701#if USB_HAVE_BUSDMA
702			if (xfer->flags_int.bdma_enable &&
703			    (parm->bufsize_max > 0)) {
704
705				if (usb2_pc_dmamap_create(
706				    xfer->frbuffers + x,
707				    parm->bufsize_max)) {
708					parm->err = USB_ERR_NOMEM;
709					goto done;
710				}
711			}
712#endif
713		}
714	}
715done:
716	if (parm->err) {
717		/*
718		 * Set some dummy values so that we avoid division by zero:
719		 */
720		xfer->max_hc_frame_size = 1;
721		xfer->max_frame_size = 1;
722		xfer->max_packet_size = 1;
723		xfer->max_data_length = 0;
724		xfer->nframes = 0;
725		xfer->max_frame_count = 0;
726	}
727}
728
729/*------------------------------------------------------------------------*
730 *	usb2_transfer_setup - setup an array of USB transfers
731 *
732 * NOTE: You must always call "usb2_transfer_unsetup" after calling
733 * "usb2_transfer_setup" if success was returned.
734 *
735 * The idea is that the USB device driver should pre-allocate all its
736 * transfers by one call to this function.
737 *
738 * Return values:
739 *    0: Success
740 * Else: Failure
741 *------------------------------------------------------------------------*/
742usb2_error_t
743usb2_transfer_setup(struct usb2_device *udev,
744    const uint8_t *ifaces, struct usb2_xfer **ppxfer,
745    const struct usb2_config *setup_start, uint16_t n_setup,
746    void *priv_sc, struct mtx *xfer_mtx)
747{
748	struct usb2_xfer dummy;
749	struct usb2_setup_params parm;
750	const struct usb2_config *setup_end = setup_start + n_setup;
751	const struct usb2_config *setup;
752	struct usb2_pipe *pipe;
753	struct usb2_xfer_root *info;
754	struct usb2_xfer *xfer;
755	void *buf = NULL;
756	uint16_t n;
757	uint16_t refcount;
758
759	parm.err = 0;
760	refcount = 0;
761	info = NULL;
762
763	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
764	    "usb2_transfer_setup can sleep!");
765
766	/* do some checking first */
767
768	if (n_setup == 0) {
769		DPRINTFN(6, "setup array has zero length!\n");
770		return (USB_ERR_INVAL);
771	}
772	if (ifaces == 0) {
773		DPRINTFN(6, "ifaces array is NULL!\n");
774		return (USB_ERR_INVAL);
775	}
776	if (xfer_mtx == NULL) {
777		DPRINTFN(6, "using global lock\n");
778		xfer_mtx = &Giant;
779	}
780	/* sanity checks */
781	for (setup = setup_start, n = 0;
782	    setup != setup_end; setup++, n++) {
783		if ((setup->mh.bufsize == (usb2_frlength_t)-1) ||
784		    (setup->md.bufsize == (usb2_frlength_t)-1)) {
785			parm.err = USB_ERR_BAD_BUFSIZE;
786			DPRINTF("invalid bufsize\n");
787		}
788		if ((setup->mh.callback == NULL) &&
789		    (setup->md.callback == NULL)) {
790			parm.err = USB_ERR_NO_CALLBACK;
791			DPRINTF("no callback\n");
792		}
793		ppxfer[n] = NULL;
794	}
795
796	if (parm.err) {
797		goto done;
798	}
799	bzero(&parm, sizeof(parm));
800
801	parm.udev = udev;
802	parm.speed = usb2_get_speed(udev);
803	parm.hc_max_packet_count = 1;
804
805	if (parm.speed >= USB_SPEED_MAX) {
806		parm.err = USB_ERR_INVAL;
807		goto done;
808	}
809	/* setup all transfers */
810
811	while (1) {
812
813		if (buf) {
814			/*
815			 * Initialize the "usb2_xfer_root" structure,
816			 * which is common for all our USB transfers.
817			 */
818			info = USB_ADD_BYTES(buf, 0);
819
820			info->memory_base = buf;
821			info->memory_size = parm.size[0];
822
823#if USB_HAVE_BUSDMA
824			info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
825			info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
826#endif
827			info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
828			info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
829
830			usb2_cv_init(&info->cv_drain, "WDRAIN");
831
832			info->xfer_mtx = xfer_mtx;
833#if USB_HAVE_BUSDMA
834			usb2_dma_tag_setup(&info->dma_parent_tag,
835			    parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
836			    xfer_mtx, &usb2_bdma_done_event, 32, parm.dma_tag_max);
837#endif
838
839			info->bus = udev->bus;
840			info->udev = udev;
841
842			TAILQ_INIT(&info->done_q.head);
843			info->done_q.command = &usb2_callback_wrapper;
844#if USB_HAVE_BUSDMA
845			TAILQ_INIT(&info->dma_q.head);
846			info->dma_q.command = &usb2_bdma_work_loop;
847#endif
848			info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
849			info->done_m[0].xroot = info;
850			info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
851			info->done_m[1].xroot = info;
852
853			if (xfer_mtx == &Giant)
854				info->done_p =
855				    &udev->bus->giant_callback_proc;
856			else
857				info->done_p =
858				    &udev->bus->non_giant_callback_proc;
859		}
860		/* reset sizes */
861
862		parm.size[0] = 0;
863		parm.buf = buf;
864		parm.size[0] += sizeof(info[0]);
865
866		for (setup = setup_start, n = 0;
867		    setup != setup_end; setup++, n++) {
868
869			/* select mode specific structure */
870			if (udev->flags.usb2_mode == USB_MODE_HOST) {
871				parm.curr_setup_sub = &setup->mh;
872			} else {
873				parm.curr_setup_sub = &setup->md;
874			}
875			/* skip USB transfers without callbacks: */
876			if (parm.curr_setup_sub->callback == NULL) {
877				continue;
878			}
879			/* see if there is a matching endpoint */
880			pipe = usb2_get_pipe(udev,
881			    ifaces[setup->if_index], setup);
882
883			if (!pipe) {
884				if (parm.curr_setup_sub->flags.no_pipe_ok) {
885					continue;
886				}
887				parm.err = USB_ERR_NO_PIPE;
888				goto done;
889			}
890			/* store current setup pointer */
891			parm.curr_setup = setup;
892
893			/* align data properly */
894			parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
895
896			if (buf) {
897				/*
898				 * Common initialization of the
899				 * "usb2_xfer" structure.
900				 */
901				xfer = USB_ADD_BYTES(buf, parm.size[0]);
902				xfer->address = udev->address;
903				xfer->priv_sc = priv_sc;
904				xfer->xroot = info;
905
906				usb2_callout_init_mtx(&xfer->timeout_handle,
907				    &udev->bus->bus_mtx, 0);
908			} else {
909				/*
910				 * Setup a dummy xfer, hence we are
911				 * writing to the "usb2_xfer"
912				 * structure pointed to by "xfer"
913				 * before we have allocated any
914				 * memory:
915				 */
916				xfer = &dummy;
917				bzero(&dummy, sizeof(dummy));
918				refcount++;
919			}
920
921			/* set transfer pipe pointer */
922			xfer->pipe = pipe;
923
924			parm.size[0] += sizeof(xfer[0]);
925			parm.methods = xfer->pipe->methods;
926			parm.curr_xfer = xfer;
927
928			/*
929			 * Call the Host or Device controller transfer
930			 * setup routine:
931			 */
932			(udev->bus->methods->xfer_setup) (&parm);
933
934			/* check for error */
935			if (parm.err)
936				goto done;
937
938			if (buf) {
939				/*
940				 * Increment the pipe refcount. This
941				 * basically prevents setting a new
942				 * configuration and alternate setting
943				 * when USB transfers are in use on
944				 * the given interface. Search the USB
945				 * code for "pipe->refcount" if you
946				 * want more information.
947				 */
948				xfer->pipe->refcount++;
949
950				/*
951				 * Whenever we set ppxfer[] then we
952				 * also need to increment the
953				 * "setup_refcount":
954				 */
955				info->setup_refcount++;
956
957				/*
958				 * Transfer is successfully setup and
959				 * can be used:
960				 */
961				ppxfer[n] = xfer;
962			}
963		}
964
965		if (buf || parm.err) {
966			goto done;
967		}
968		if (refcount == 0) {
969			/* no transfers - nothing to do ! */
970			goto done;
971		}
972		/* align data properly */
973		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
974
975		/* store offset temporarily */
976		parm.size[1] = parm.size[0];
977
978		/*
979		 * The number of DMA tags required depends on
980		 * the number of endpoints. The current estimate
981		 * for maximum number of DMA tags per endpoint
982		 * is two.
983		 */
984		parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX);
985
986		/*
987		 * DMA tags for QH, TD, Data and more.
988		 */
989		parm.dma_tag_max += 8;
990
991		parm.dma_tag_p += parm.dma_tag_max;
992
993		parm.size[0] += ((uint8_t *)parm.dma_tag_p) -
994		    ((uint8_t *)0);
995
996		/* align data properly */
997		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
998
999		/* store offset temporarily */
1000		parm.size[3] = parm.size[0];
1001
1002		parm.size[0] += ((uint8_t *)parm.dma_page_ptr) -
1003		    ((uint8_t *)0);
1004
1005		/* align data properly */
1006		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1007
1008		/* store offset temporarily */
1009		parm.size[4] = parm.size[0];
1010
1011		parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) -
1012		    ((uint8_t *)0);
1013
1014		/* store end offset temporarily */
1015		parm.size[5] = parm.size[0];
1016
1017		parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) -
1018		    ((uint8_t *)0);
1019
1020		/* store end offset temporarily */
1021
1022		parm.size[2] = parm.size[0];
1023
1024		/* align data properly */
1025		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1026
1027		parm.size[6] = parm.size[0];
1028
1029		parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) -
1030		    ((uint8_t *)0);
1031
1032		/* align data properly */
1033		parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1034
1035		/* allocate zeroed memory */
1036		buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO);
1037
1038		if (buf == NULL) {
1039			parm.err = USB_ERR_NOMEM;
1040			DPRINTFN(0, "cannot allocate memory block for "
1041			    "configuration (%d bytes)\n",
1042			    parm.size[0]);
1043			goto done;
1044		}
1045		parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]);
1046		parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]);
1047		parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]);
1048		parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]);
1049		parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1050	}
1051
1052done:
1053	if (buf) {
1054		if (info->setup_refcount == 0) {
1055			/*
1056			 * "usb2_transfer_unsetup_sub" will unlock
1057			 * the bus mutex before returning !
1058			 */
1059			USB_BUS_LOCK(info->bus);
1060
1061			/* something went wrong */
1062			usb2_transfer_unsetup_sub(info, 0);
1063		}
1064	}
1065	if (parm.err) {
1066		usb2_transfer_unsetup(ppxfer, n_setup);
1067	}
1068	return (parm.err);
1069}
1070
1071/*------------------------------------------------------------------------*
1072 *	usb2_transfer_unsetup_sub - factored out code
1073 *------------------------------------------------------------------------*/
1074static void
1075usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay)
1076{
1077	struct usb2_page_cache *pc;
1078
1079	USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1080
1081	/* wait for any outstanding DMA operations */
1082
1083	if (needs_delay) {
1084		usb2_timeout_t temp;
1085		temp = usb2_get_dma_delay(info->bus);
1086		usb2_pause_mtx(&info->bus->bus_mtx,
1087		    USB_MS_TO_TICKS(temp));
1088	}
1089
1090	/* make sure that our done messages are not queued anywhere */
1091	usb2_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1092
1093	USB_BUS_UNLOCK(info->bus);
1094
1095#if USB_HAVE_BUSDMA
1096	/* free DMA'able memory, if any */
1097	pc = info->dma_page_cache_start;
1098	while (pc != info->dma_page_cache_end) {
1099		usb2_pc_free_mem(pc);
1100		pc++;
1101	}
1102
1103	/* free DMA maps in all "xfer->frbuffers" */
1104	pc = info->xfer_page_cache_start;
1105	while (pc != info->xfer_page_cache_end) {
1106		usb2_pc_dmamap_destroy(pc);
1107		pc++;
1108	}
1109
1110	/* free all DMA tags */
1111	usb2_dma_tag_unsetup(&info->dma_parent_tag);
1112#endif
1113
1114	usb2_cv_destroy(&info->cv_drain);
1115
1116	/*
1117	 * free the "memory_base" last, hence the "info" structure is
1118	 * contained within the "memory_base"!
1119	 */
1120	free(info->memory_base, M_USB);
1121}
1122
1123/*------------------------------------------------------------------------*
1124 *	usb2_transfer_unsetup - unsetup/free an array of USB transfers
1125 *
1126 * NOTE: All USB transfers in progress will get called back passing
1127 * the error code "USB_ERR_CANCELLED" before this function
1128 * returns.
1129 *------------------------------------------------------------------------*/
1130void
1131usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup)
1132{
1133	struct usb2_xfer *xfer;
1134	struct usb2_xfer_root *info;
1135	uint8_t needs_delay = 0;
1136
1137	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1138	    "usb2_transfer_unsetup can sleep!");
1139
1140	while (n_setup--) {
1141		xfer = pxfer[n_setup];
1142
1143		if (xfer == NULL)
1144			continue;
1145
1146		info = xfer->xroot;
1147
1148		USB_XFER_LOCK(xfer);
1149		USB_BUS_LOCK(info->bus);
1150
1151		/*
1152		 * HINT: when you start/stop a transfer, it might be a
1153		 * good idea to directly use the "pxfer[]" structure:
1154		 *
1155		 * usb2_transfer_start(sc->pxfer[0]);
1156		 * usb2_transfer_stop(sc->pxfer[0]);
1157		 *
1158		 * That way, if your code has many parts that will not
1159		 * stop running under the same lock, in other words
1160		 * "xfer_mtx", the usb2_transfer_start and
1161		 * usb2_transfer_stop functions will simply return
1162		 * when they detect a NULL pointer argument.
1163		 *
1164		 * To avoid any races we clear the "pxfer[]" pointer
1165		 * while holding the private mutex of the driver:
1166		 */
1167		pxfer[n_setup] = NULL;
1168
1169		USB_BUS_UNLOCK(info->bus);
1170		USB_XFER_UNLOCK(xfer);
1171
1172		usb2_transfer_drain(xfer);
1173
1174#if USB_HAVE_BUSDMA
1175		if (xfer->flags_int.bdma_enable)
1176			needs_delay = 1;
1177#endif
1178		/*
1179		 * NOTE: default pipe does not have an
1180		 * interface, even if pipe->iface_index == 0
1181		 */
1182		xfer->pipe->refcount--;
1183
1184		usb2_callout_drain(&xfer->timeout_handle);
1185
1186		USB_BUS_LOCK(info->bus);
1187
1188		USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1189		    "reference count!\n"));
1190
1191		info->setup_refcount--;
1192
1193		if (info->setup_refcount == 0) {
1194			usb2_transfer_unsetup_sub(info,
1195			    needs_delay);
1196		} else {
1197			USB_BUS_UNLOCK(info->bus);
1198		}
1199	}
1200}
1201
1202/*------------------------------------------------------------------------*
1203 *	usb2_control_transfer_init - factored out code
1204 *
1205 * In USB Device Mode we have to wait for the SETUP packet which
1206 * containst the "struct usb2_device_request" structure, before we can
1207 * transfer any data. In USB Host Mode we already have the SETUP
1208 * packet at the moment the USB transfer is started. This leads us to
1209 * having to setup the USB transfer at two different places in
1210 * time. This function just contains factored out control transfer
1211 * initialisation code, so that we don't duplicate the code.
1212 *------------------------------------------------------------------------*/
1213static void
1214usb2_control_transfer_init(struct usb2_xfer *xfer)
1215{
1216	struct usb2_device_request req;
1217
1218	/* copy out the USB request header */
1219
1220	usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1221
1222	/* setup remainder */
1223
1224	xfer->flags_int.control_rem = UGETW(req.wLength);
1225
1226	/* copy direction to endpoint variable */
1227
1228	xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT);
1229	xfer->endpoint |=
1230	    (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1231}
1232
1233/*------------------------------------------------------------------------*
1234 *	usb2_start_hardware_sub
1235 *
1236 * This function handles initialisation of control transfers. Control
1237 * transfers are special in that regard that they can both transmit
1238 * and receive data.
1239 *
1240 * Return values:
1241 *    0: Success
1242 * Else: Failure
1243 *------------------------------------------------------------------------*/
1244static uint8_t
1245usb2_start_hardware_sub(struct usb2_xfer *xfer)
1246{
1247	usb2_frlength_t len;
1248
1249	/* Check for control endpoint stall */
1250	if (xfer->flags.stall_pipe) {
1251		/* no longer active */
1252		xfer->flags_int.control_act = 0;
1253	}
1254	/*
1255         * Check if there is a control
1256         * transfer in progress:
1257         */
1258	if (xfer->flags_int.control_act) {
1259
1260		if (xfer->flags_int.control_hdr) {
1261
1262			/* clear send header flag */
1263
1264			xfer->flags_int.control_hdr = 0;
1265
1266			/* setup control transfer */
1267			if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
1268				usb2_control_transfer_init(xfer);
1269			}
1270		}
1271		/* get data length */
1272
1273		len = xfer->sumlen;
1274
1275	} else {
1276
1277		/* the size of the SETUP structure is hardcoded ! */
1278
1279		if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) {
1280			DPRINTFN(0, "Wrong framelength %u != %zu\n",
1281			    xfer->frlengths[0], sizeof(struct
1282			    usb2_device_request));
1283			goto error;
1284		}
1285		/* check USB mode */
1286		if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
1287
1288			/* check number of frames */
1289			if (xfer->nframes != 1) {
1290				/*
1291			         * We need to receive the setup
1292			         * message first so that we know the
1293			         * data direction!
1294			         */
1295				DPRINTF("Misconfigured transfer\n");
1296				goto error;
1297			}
1298			/*
1299			 * Set a dummy "control_rem" value.  This
1300			 * variable will be overwritten later by a
1301			 * call to "usb2_control_transfer_init()" !
1302			 */
1303			xfer->flags_int.control_rem = 0xFFFF;
1304		} else {
1305
1306			/* setup "endpoint" and "control_rem" */
1307
1308			usb2_control_transfer_init(xfer);
1309		}
1310
1311		/* set transfer-header flag */
1312
1313		xfer->flags_int.control_hdr = 1;
1314
1315		/* get data length */
1316
1317		len = (xfer->sumlen - sizeof(struct usb2_device_request));
1318	}
1319
1320	/* check if there is a length mismatch */
1321
1322	if (len > xfer->flags_int.control_rem) {
1323		DPRINTFN(0, "Length greater than remaining length!\n");
1324		goto error;
1325	}
1326	/* check if we are doing a short transfer */
1327
1328	if (xfer->flags.force_short_xfer) {
1329		xfer->flags_int.control_rem = 0;
1330	} else {
1331		if ((len != xfer->max_data_length) &&
1332		    (len != xfer->flags_int.control_rem) &&
1333		    (xfer->nframes != 1)) {
1334			DPRINTFN(0, "Short control transfer without "
1335			    "force_short_xfer set!\n");
1336			goto error;
1337		}
1338		xfer->flags_int.control_rem -= len;
1339	}
1340
1341	/* the status part is executed when "control_act" is 0 */
1342
1343	if ((xfer->flags_int.control_rem > 0) ||
1344	    (xfer->flags.manual_status)) {
1345		/* don't execute the STATUS stage yet */
1346		xfer->flags_int.control_act = 1;
1347
1348		/* sanity check */
1349		if ((!xfer->flags_int.control_hdr) &&
1350		    (xfer->nframes == 1)) {
1351			/*
1352		         * This is not a valid operation!
1353		         */
1354			DPRINTFN(0, "Invalid parameter "
1355			    "combination\n");
1356			goto error;
1357		}
1358	} else {
1359		/* time to execute the STATUS stage */
1360		xfer->flags_int.control_act = 0;
1361	}
1362	return (0);			/* success */
1363
1364error:
1365	return (1);			/* failure */
1366}
1367
1368/*------------------------------------------------------------------------*
1369 *	usb2_start_hardware - start USB hardware for the given transfer
1370 *
1371 * This function should only be called from the USB callback.
1372 *------------------------------------------------------------------------*/
1373void
1374usb2_start_hardware(struct usb2_xfer *xfer)
1375{
1376	usb2_frcount_t x;
1377
1378	DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
1379	    xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1380	    "read" : "write");
1381
1382#if USB_DEBUG
1383	if (USB_DEBUG_VAR > 0) {
1384		USB_BUS_LOCK(xfer->xroot->bus);
1385
1386		usb2_dump_pipe(xfer->pipe);
1387
1388		USB_BUS_UNLOCK(xfer->xroot->bus);
1389	}
1390#endif
1391
1392	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1393	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
1394
1395	/* Only open the USB transfer once! */
1396	if (!xfer->flags_int.open) {
1397		xfer->flags_int.open = 1;
1398
1399		DPRINTF("open\n");
1400
1401		USB_BUS_LOCK(xfer->xroot->bus);
1402		(xfer->pipe->methods->open) (xfer);
1403		USB_BUS_UNLOCK(xfer->xroot->bus);
1404	}
1405	/* set "transferring" flag */
1406	xfer->flags_int.transferring = 1;
1407
1408	/* increment power reference */
1409	usb2_transfer_power_ref(xfer, 1);
1410
1411	/*
1412	 * Check if the transfer is waiting on a queue, most
1413	 * frequently the "done_q":
1414	 */
1415	if (xfer->wait_queue) {
1416		USB_BUS_LOCK(xfer->xroot->bus);
1417		usb2_transfer_dequeue(xfer);
1418		USB_BUS_UNLOCK(xfer->xroot->bus);
1419	}
1420	/* clear "did_dma_delay" flag */
1421	xfer->flags_int.did_dma_delay = 0;
1422
1423	/* clear "did_close" flag */
1424	xfer->flags_int.did_close = 0;
1425
1426#if USB_HAVE_BUSDMA
1427	/* clear "bdma_setup" flag */
1428	xfer->flags_int.bdma_setup = 0;
1429#endif
1430	/* by default we cannot cancel any USB transfer immediately */
1431	xfer->flags_int.can_cancel_immed = 0;
1432
1433	/* clear lengths and frame counts by default */
1434	xfer->sumlen = 0;
1435	xfer->actlen = 0;
1436	xfer->aframes = 0;
1437
1438	/* clear any previous errors */
1439	xfer->error = 0;
1440
1441	/* sanity check */
1442
1443	if (xfer->nframes == 0) {
1444		if (xfer->flags.stall_pipe) {
1445			/*
1446			 * Special case - want to stall without transferring
1447			 * any data:
1448			 */
1449			DPRINTF("xfer=%p nframes=0: stall "
1450			    "or clear stall!\n", xfer);
1451			USB_BUS_LOCK(xfer->xroot->bus);
1452			xfer->flags_int.can_cancel_immed = 1;
1453			/* start the transfer */
1454			usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
1455			USB_BUS_UNLOCK(xfer->xroot->bus);
1456			return;
1457		}
1458		USB_BUS_LOCK(xfer->xroot->bus);
1459		usb2_transfer_done(xfer, USB_ERR_INVAL);
1460		USB_BUS_UNLOCK(xfer->xroot->bus);
1461		return;
1462	}
1463	/* compute total transfer length */
1464
1465	for (x = 0; x != xfer->nframes; x++) {
1466		xfer->sumlen += xfer->frlengths[x];
1467		if (xfer->sumlen < xfer->frlengths[x]) {
1468			/* length wrapped around */
1469			USB_BUS_LOCK(xfer->xroot->bus);
1470			usb2_transfer_done(xfer, USB_ERR_INVAL);
1471			USB_BUS_UNLOCK(xfer->xroot->bus);
1472			return;
1473		}
1474	}
1475
1476	/* clear some internal flags */
1477
1478	xfer->flags_int.short_xfer_ok = 0;
1479	xfer->flags_int.short_frames_ok = 0;
1480
1481	/* check if this is a control transfer */
1482
1483	if (xfer->flags_int.control_xfr) {
1484
1485		if (usb2_start_hardware_sub(xfer)) {
1486			USB_BUS_LOCK(xfer->xroot->bus);
1487			usb2_transfer_done(xfer, USB_ERR_STALLED);
1488			USB_BUS_UNLOCK(xfer->xroot->bus);
1489			return;
1490		}
1491	}
1492	/*
1493	 * Setup filtered version of some transfer flags,
1494	 * in case of data read direction
1495	 */
1496	if (USB_GET_DATA_ISREAD(xfer)) {
1497
1498		if (xfer->flags_int.control_xfr) {
1499
1500			/*
1501			 * Control transfers do not support reception
1502			 * of multiple short USB frames !
1503			 */
1504
1505			if (xfer->flags.short_xfer_ok) {
1506				xfer->flags_int.short_xfer_ok = 1;
1507			}
1508		} else {
1509
1510			if (xfer->flags.short_frames_ok) {
1511				xfer->flags_int.short_xfer_ok = 1;
1512				xfer->flags_int.short_frames_ok = 1;
1513			} else if (xfer->flags.short_xfer_ok) {
1514				xfer->flags_int.short_xfer_ok = 1;
1515			}
1516		}
1517	}
1518	/*
1519	 * Check if BUS-DMA support is enabled and try to load virtual
1520	 * buffers into DMA, if any:
1521	 */
1522#if USB_HAVE_BUSDMA
1523	if (xfer->flags_int.bdma_enable) {
1524		/* insert the USB transfer last in the BUS-DMA queue */
1525		usb2_command_wrapper(&xfer->xroot->dma_q, xfer);
1526		return;
1527	}
1528#endif
1529	/*
1530	 * Enter the USB transfer into the Host Controller or
1531	 * Device Controller schedule:
1532	 */
1533	usb2_pipe_enter(xfer);
1534}
1535
1536/*------------------------------------------------------------------------*
1537 *	usb2_pipe_enter - factored out code
1538 *------------------------------------------------------------------------*/
1539void
1540usb2_pipe_enter(struct usb2_xfer *xfer)
1541{
1542	struct usb2_pipe *pipe;
1543
1544	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1545
1546	USB_BUS_LOCK(xfer->xroot->bus);
1547
1548	pipe = xfer->pipe;
1549
1550	DPRINTF("enter\n");
1551
1552	/* enter the transfer */
1553	(pipe->methods->enter) (xfer);
1554
1555	/* check cancelability */
1556	if (pipe->methods->enter_is_cancelable) {
1557		xfer->flags_int.can_cancel_immed = 1;
1558		/* check for transfer error */
1559		if (xfer->error) {
1560			/* some error has happened */
1561			usb2_transfer_done(xfer, 0);
1562			USB_BUS_UNLOCK(xfer->xroot->bus);
1563			return;
1564		}
1565	} else {
1566		xfer->flags_int.can_cancel_immed = 0;
1567	}
1568
1569	/* start the transfer */
1570	usb2_command_wrapper(&pipe->pipe_q, xfer);
1571	USB_BUS_UNLOCK(xfer->xroot->bus);
1572}
1573
1574/*------------------------------------------------------------------------*
1575 *	usb2_transfer_start - start an USB transfer
1576 *
1577 * NOTE: Calling this function more than one time will only
1578 *       result in a single transfer start, until the USB transfer
1579 *       completes.
1580 *------------------------------------------------------------------------*/
1581void
1582usb2_transfer_start(struct usb2_xfer *xfer)
1583{
1584	if (xfer == NULL) {
1585		/* transfer is gone */
1586		return;
1587	}
1588	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1589
1590	/* mark the USB transfer started */
1591
1592	if (!xfer->flags_int.started) {
1593		xfer->flags_int.started = 1;
1594	}
1595	/* check if the USB transfer callback is already transferring */
1596
1597	if (xfer->flags_int.transferring) {
1598		return;
1599	}
1600	USB_BUS_LOCK(xfer->xroot->bus);
1601	/* call the USB transfer callback */
1602	usb2_callback_ss_done_defer(xfer);
1603	USB_BUS_UNLOCK(xfer->xroot->bus);
1604}
1605
1606/*------------------------------------------------------------------------*
1607 *	usb2_transfer_stop - stop an USB transfer
1608 *
1609 * NOTE: Calling this function more than one time will only
1610 *       result in a single transfer stop.
1611 * NOTE: When this function returns it is not safe to free nor
1612 *       reuse any DMA buffers. See "usb2_transfer_drain()".
1613 *------------------------------------------------------------------------*/
1614void
1615usb2_transfer_stop(struct usb2_xfer *xfer)
1616{
1617	struct usb2_pipe *pipe;
1618
1619	if (xfer == NULL) {
1620		/* transfer is gone */
1621		return;
1622	}
1623	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1624
1625	/* check if the USB transfer was ever opened */
1626
1627	if (!xfer->flags_int.open) {
1628		/* nothing to do except clearing the "started" flag */
1629		xfer->flags_int.started = 0;
1630		return;
1631	}
1632	/* try to stop the current USB transfer */
1633
1634	USB_BUS_LOCK(xfer->xroot->bus);
1635	xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1636	/*
1637	 * Clear "open" and "started" when both private and USB lock
1638	 * is locked so that we don't get a race updating "flags_int"
1639	 */
1640	xfer->flags_int.open = 0;
1641	xfer->flags_int.started = 0;
1642
1643	/*
1644	 * Check if we can cancel the USB transfer immediately.
1645	 */
1646	if (xfer->flags_int.transferring) {
1647		if (xfer->flags_int.can_cancel_immed &&
1648		    (!xfer->flags_int.did_close)) {
1649			DPRINTF("close\n");
1650			/*
1651			 * The following will lead to an USB_ERR_CANCELLED
1652			 * error code being passed to the USB callback.
1653			 */
1654			(xfer->pipe->methods->close) (xfer);
1655			/* only close once */
1656			xfer->flags_int.did_close = 1;
1657		} else {
1658			/* need to wait for the next done callback */
1659		}
1660	} else {
1661		DPRINTF("close\n");
1662
1663		/* close here and now */
1664		(xfer->pipe->methods->close) (xfer);
1665
1666		/*
1667		 * Any additional DMA delay is done by
1668		 * "usb2_transfer_unsetup()".
1669		 */
1670
1671		/*
1672		 * Special case. Check if we need to restart a blocked
1673		 * pipe.
1674		 */
1675		pipe = xfer->pipe;
1676
1677		/*
1678		 * If the current USB transfer is completing we need
1679		 * to start the next one:
1680		 */
1681		if (pipe->pipe_q.curr == xfer) {
1682			usb2_command_wrapper(&pipe->pipe_q, NULL);
1683		}
1684	}
1685
1686	USB_BUS_UNLOCK(xfer->xroot->bus);
1687}
1688
1689/*------------------------------------------------------------------------*
1690 *	usb2_transfer_pending
1691 *
1692 * This function will check if an USB transfer is pending which is a
1693 * little bit complicated!
1694 * Return values:
1695 * 0: Not pending
1696 * 1: Pending: The USB transfer will receive a callback in the future.
1697 *------------------------------------------------------------------------*/
1698uint8_t
1699usb2_transfer_pending(struct usb2_xfer *xfer)
1700{
1701	struct usb2_xfer_root *info;
1702	struct usb2_xfer_queue *pq;
1703
1704	if (xfer == NULL) {
1705		/* transfer is gone */
1706		return (0);
1707	}
1708	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1709
1710	if (xfer->flags_int.transferring) {
1711		/* trivial case */
1712		return (1);
1713	}
1714	USB_BUS_LOCK(xfer->xroot->bus);
1715	if (xfer->wait_queue) {
1716		/* we are waiting on a queue somewhere */
1717		USB_BUS_UNLOCK(xfer->xroot->bus);
1718		return (1);
1719	}
1720	info = xfer->xroot;
1721	pq = &info->done_q;
1722
1723	if (pq->curr == xfer) {
1724		/* we are currently scheduled for callback */
1725		USB_BUS_UNLOCK(xfer->xroot->bus);
1726		return (1);
1727	}
1728	/* we are not pending */
1729	USB_BUS_UNLOCK(xfer->xroot->bus);
1730	return (0);
1731}
1732
1733/*------------------------------------------------------------------------*
1734 *	usb2_transfer_drain
1735 *
1736 * This function will stop the USB transfer and wait for any
1737 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
1738 * are loaded into DMA can safely be freed or reused after that this
1739 * function has returned.
1740 *------------------------------------------------------------------------*/
1741void
1742usb2_transfer_drain(struct usb2_xfer *xfer)
1743{
1744	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1745	    "usb2_transfer_drain can sleep!");
1746
1747	if (xfer == NULL) {
1748		/* transfer is gone */
1749		return;
1750	}
1751	if (xfer->xroot->xfer_mtx != &Giant) {
1752		USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1753	}
1754	USB_XFER_LOCK(xfer);
1755
1756	usb2_transfer_stop(xfer);
1757
1758	while (usb2_transfer_pending(xfer)) {
1759		xfer->flags_int.draining = 1;
1760		/*
1761		 * Wait until the current outstanding USB
1762		 * transfer is complete !
1763		 */
1764		usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
1765	}
1766	USB_XFER_UNLOCK(xfer);
1767}
1768
1769/*------------------------------------------------------------------------*
1770 *	usb2_set_frame_data
1771 *
1772 * This function sets the pointer of the buffer that should
1773 * loaded directly into DMA for the given USB frame. Passing "ptr"
1774 * equal to NULL while the corresponding "frlength" is greater
1775 * than zero gives undefined results!
1776 *------------------------------------------------------------------------*/
1777void
1778usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, usb2_frcount_t frindex)
1779{
1780	/* set virtual address to load and length */
1781	xfer->frbuffers[frindex].buffer = ptr;
1782}
1783
1784/*------------------------------------------------------------------------*
1785 *	usb2_set_frame_offset
1786 *
1787 * This function sets the frame data buffer offset relative to the beginning
1788 * of the USB DMA buffer allocated for this USB transfer.
1789 *------------------------------------------------------------------------*/
1790void
1791usb2_set_frame_offset(struct usb2_xfer *xfer, usb2_frlength_t offset,
1792    usb2_frcount_t frindex)
1793{
1794	USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
1795	    "when the USB buffer is external!\n"));
1796
1797	/* set virtual address to load */
1798	xfer->frbuffers[frindex].buffer =
1799	    USB_ADD_BYTES(xfer->local_buffer, offset);
1800}
1801
1802/*------------------------------------------------------------------------*
1803 *	usb2_callback_proc - factored out code
1804 *
1805 * This function performs USB callbacks.
1806 *------------------------------------------------------------------------*/
1807static void
1808usb2_callback_proc(struct usb2_proc_msg *_pm)
1809{
1810	struct usb2_done_msg *pm = (void *)_pm;
1811	struct usb2_xfer_root *info = pm->xroot;
1812
1813	/* Change locking order */
1814	USB_BUS_UNLOCK(info->bus);
1815
1816	/*
1817	 * We exploit the fact that the mutex is the same for all
1818	 * callbacks that will be called from this thread:
1819	 */
1820	mtx_lock(info->xfer_mtx);
1821	USB_BUS_LOCK(info->bus);
1822
1823	/* Continue where we lost track */
1824	usb2_command_wrapper(&info->done_q,
1825	    info->done_q.curr);
1826
1827	mtx_unlock(info->xfer_mtx);
1828}
1829
1830/*------------------------------------------------------------------------*
1831 *	usb2_callback_ss_done_defer
1832 *
1833 * This function will defer the start, stop and done callback to the
1834 * correct thread.
1835 *------------------------------------------------------------------------*/
1836static void
1837usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1838{
1839	struct usb2_xfer_root *info = xfer->xroot;
1840	struct usb2_xfer_queue *pq = &info->done_q;
1841
1842	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1843
1844	if (pq->curr != xfer) {
1845		usb2_transfer_enqueue(pq, xfer);
1846	}
1847	if (!pq->recurse_1) {
1848
1849		/*
1850	         * We have to postpone the callback due to the fact we
1851	         * will have a Lock Order Reversal, LOR, if we try to
1852	         * proceed !
1853	         */
1854		if (usb2_proc_msignal(info->done_p,
1855		    &info->done_m[0], &info->done_m[1])) {
1856			/* ignore */
1857		}
1858	} else {
1859		/* clear second recurse flag */
1860		pq->recurse_2 = 0;
1861	}
1862	return;
1863
1864}
1865
1866/*------------------------------------------------------------------------*
1867 *	usb2_callback_wrapper
1868 *
1869 * This is a wrapper for USB callbacks. This wrapper does some
1870 * auto-magic things like figuring out if we can call the callback
1871 * directly from the current context or if we need to wakeup the
1872 * interrupt process.
1873 *------------------------------------------------------------------------*/
1874static void
1875usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1876{
1877	struct usb2_xfer *xfer = pq->curr;
1878	struct usb2_xfer_root *info = xfer->xroot;
1879
1880	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1881	if (!mtx_owned(xfer->xroot->xfer_mtx)) {
1882		/*
1883	       	 * Cases that end up here:
1884		 *
1885		 * 5) HW interrupt done callback or other source.
1886		 */
1887		DPRINTFN(3, "case 5\n");
1888
1889		/*
1890	         * We have to postpone the callback due to the fact we
1891	         * will have a Lock Order Reversal, LOR, if we try to
1892	         * proceed !
1893	         */
1894		if (usb2_proc_msignal(info->done_p,
1895		    &info->done_m[0], &info->done_m[1])) {
1896			/* ignore */
1897		}
1898		return;
1899	}
1900	/*
1901	 * Cases that end up here:
1902	 *
1903	 * 1) We are starting a transfer
1904	 * 2) We are prematurely calling back a transfer
1905	 * 3) We are stopping a transfer
1906	 * 4) We are doing an ordinary callback
1907	 */
1908	DPRINTFN(3, "case 1-4\n");
1909	/* get next USB transfer in the queue */
1910	info->done_q.curr = NULL;
1911
1912	USB_BUS_UNLOCK(xfer->xroot->bus);
1913	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
1914
1915	/* set correct USB state for callback */
1916	if (!xfer->flags_int.transferring) {
1917		xfer->usb2_state = USB_ST_SETUP;
1918		if (!xfer->flags_int.started) {
1919			/* we got stopped before we even got started */
1920			USB_BUS_LOCK(xfer->xroot->bus);
1921			goto done;
1922		}
1923	} else {
1924
1925		if (usb2_callback_wrapper_sub(xfer)) {
1926			/* the callback has been deferred */
1927			USB_BUS_LOCK(xfer->xroot->bus);
1928			goto done;
1929		}
1930		/* decrement power reference */
1931		usb2_transfer_power_ref(xfer, -1);
1932
1933		xfer->flags_int.transferring = 0;
1934
1935		if (xfer->error) {
1936			xfer->usb2_state = USB_ST_ERROR;
1937		} else {
1938			/* set transferred state */
1939			xfer->usb2_state = USB_ST_TRANSFERRED;
1940#if USB_HAVE_BUSDMA
1941			/* sync DMA memory, if any */
1942			if (xfer->flags_int.bdma_enable &&
1943			    (!xfer->flags_int.bdma_no_post_sync)) {
1944				usb2_bdma_post_sync(xfer);
1945			}
1946#endif
1947		}
1948	}
1949
1950	/* call processing routine */
1951	(xfer->callback) (xfer);
1952
1953	/* pickup the USB mutex again */
1954	USB_BUS_LOCK(xfer->xroot->bus);
1955
1956	/*
1957	 * Check if we got started after that we got cancelled, but
1958	 * before we managed to do the callback.
1959	 */
1960	if ((!xfer->flags_int.open) &&
1961	    (xfer->flags_int.started) &&
1962	    (xfer->usb2_state == USB_ST_ERROR)) {
1963		/* try to loop, but not recursivly */
1964		usb2_command_wrapper(&info->done_q, xfer);
1965		return;
1966	}
1967
1968done:
1969	/*
1970	 * Check if we are draining.
1971	 */
1972	if (xfer->flags_int.draining &&
1973	    (!xfer->flags_int.transferring)) {
1974		/* "usb2_transfer_drain()" is waiting for end of transfer */
1975		xfer->flags_int.draining = 0;
1976		usb2_cv_broadcast(&xfer->xroot->cv_drain);
1977	}
1978
1979	/* do the next callback, if any */
1980	usb2_command_wrapper(&info->done_q,
1981	    info->done_q.curr);
1982}
1983
1984/*------------------------------------------------------------------------*
1985 *	usb2_dma_delay_done_cb
1986 *
1987 * This function is called when the DMA delay has been exectuded, and
1988 * will make sure that the callback is called to complete the USB
1989 * transfer. This code path is ususally only used when there is an USB
1990 * error like USB_ERR_CANCELLED.
1991 *------------------------------------------------------------------------*/
1992static void
1993usb2_dma_delay_done_cb(void *arg)
1994{
1995	struct usb2_xfer *xfer = arg;
1996
1997	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1998
1999	DPRINTFN(3, "Completed %p\n", xfer);
2000
2001	/* queue callback for execution, again */
2002	usb2_transfer_done(xfer, 0);
2003}
2004
2005/*------------------------------------------------------------------------*
2006 *	usb2_transfer_dequeue
2007 *
2008 *  - This function is used to remove an USB transfer from a USB
2009 *  transfer queue.
2010 *
2011 *  - This function can be called multiple times in a row.
2012 *------------------------------------------------------------------------*/
2013void
2014usb2_transfer_dequeue(struct usb2_xfer *xfer)
2015{
2016	struct usb2_xfer_queue *pq;
2017
2018	pq = xfer->wait_queue;
2019	if (pq) {
2020		TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2021		xfer->wait_queue = NULL;
2022	}
2023}
2024
2025/*------------------------------------------------------------------------*
2026 *	usb2_transfer_enqueue
2027 *
2028 *  - This function is used to insert an USB transfer into a USB *
2029 *  transfer queue.
2030 *
2031 *  - This function can be called multiple times in a row.
2032 *------------------------------------------------------------------------*/
2033void
2034usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
2035{
2036	/*
2037	 * Insert the USB transfer into the queue, if it is not
2038	 * already on a USB transfer queue:
2039	 */
2040	if (xfer->wait_queue == NULL) {
2041		xfer->wait_queue = pq;
2042		TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2043	}
2044}
2045
2046/*------------------------------------------------------------------------*
2047 *	usb2_transfer_done
2048 *
2049 *  - This function is used to remove an USB transfer from the busdma,
2050 *  pipe or interrupt queue.
2051 *
2052 *  - This function is used to queue the USB transfer on the done
2053 *  queue.
2054 *
2055 *  - This function is used to stop any USB transfer timeouts.
2056 *------------------------------------------------------------------------*/
2057void
2058usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2059{
2060	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2061
2062	DPRINTF("err=%s\n", usb2_errstr(error));
2063
2064	/*
2065	 * If we are not transferring then just return.
2066	 * This can happen during transfer cancel.
2067	 */
2068	if (!xfer->flags_int.transferring) {
2069		DPRINTF("not transferring\n");
2070		return;
2071	}
2072	/* only set transfer error if not already set */
2073	if (!xfer->error) {
2074		xfer->error = error;
2075	}
2076	/* stop any callouts */
2077	usb2_callout_stop(&xfer->timeout_handle);
2078
2079	/*
2080	 * If we are waiting on a queue, just remove the USB transfer
2081	 * from the queue, if any. We should have the required locks
2082	 * locked to do the remove when this function is called.
2083	 */
2084	usb2_transfer_dequeue(xfer);
2085
2086#if USB_HAVE_BUSDMA
2087	if (mtx_owned(xfer->xroot->xfer_mtx)) {
2088		struct usb2_xfer_queue *pq;
2089
2090		/*
2091		 * If the private USB lock is not locked, then we assume
2092		 * that the BUS-DMA load stage has been passed:
2093		 */
2094		pq = &xfer->xroot->dma_q;
2095
2096		if (pq->curr == xfer) {
2097			/* start the next BUS-DMA load, if any */
2098			usb2_command_wrapper(pq, NULL);
2099		}
2100	}
2101#endif
2102	/* keep some statistics */
2103	if (xfer->error) {
2104		xfer->xroot->bus->stats_err.uds_requests
2105		    [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2106	} else {
2107		xfer->xroot->bus->stats_ok.uds_requests
2108		    [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2109	}
2110
2111	/* call the USB transfer callback */
2112	usb2_callback_ss_done_defer(xfer);
2113}
2114
2115/*------------------------------------------------------------------------*
2116 *	usb2_transfer_start_cb
2117 *
2118 * This function is called to start the USB transfer when
2119 * "xfer->interval" is greater than zero, and and the endpoint type is
2120 * BULK or CONTROL.
2121 *------------------------------------------------------------------------*/
2122static void
2123usb2_transfer_start_cb(void *arg)
2124{
2125	struct usb2_xfer *xfer = arg;
2126	struct usb2_pipe *pipe = xfer->pipe;
2127
2128	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2129
2130	DPRINTF("start\n");
2131
2132	/* start the transfer */
2133	(pipe->methods->start) (xfer);
2134
2135	/* check cancelability */
2136	if (pipe->methods->start_is_cancelable) {
2137		xfer->flags_int.can_cancel_immed = 1;
2138		if (xfer->error) {
2139			/* some error has happened */
2140			usb2_transfer_done(xfer, 0);
2141		}
2142	} else {
2143		xfer->flags_int.can_cancel_immed = 0;
2144	}
2145}
2146
2147/*------------------------------------------------------------------------*
2148 *	usb2_transfer_set_stall
2149 *
2150 * This function is used to set the stall flag outside the
2151 * callback. This function is NULL safe.
2152 *------------------------------------------------------------------------*/
2153void
2154usb2_transfer_set_stall(struct usb2_xfer *xfer)
2155{
2156	if (xfer == NULL) {
2157		/* tearing down */
2158		return;
2159	}
2160	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2161
2162	/* avoid any races by locking the USB mutex */
2163	USB_BUS_LOCK(xfer->xroot->bus);
2164
2165	xfer->flags.stall_pipe = 1;
2166
2167	USB_BUS_UNLOCK(xfer->xroot->bus);
2168}
2169
2170/*------------------------------------------------------------------------*
2171 *	usb2_transfer_clear_stall
2172 *
2173 * This function is used to clear the stall flag outside the
2174 * callback. This function is NULL safe.
2175 *------------------------------------------------------------------------*/
2176void
2177usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2178{
2179	if (xfer == NULL) {
2180		/* tearing down */
2181		return;
2182	}
2183	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2184
2185	/* avoid any races by locking the USB mutex */
2186	USB_BUS_LOCK(xfer->xroot->bus);
2187
2188	xfer->flags.stall_pipe = 0;
2189
2190	USB_BUS_UNLOCK(xfer->xroot->bus);
2191}
2192
2193/*------------------------------------------------------------------------*
2194 *	usb2_pipe_start
2195 *
2196 * This function is used to add an USB transfer to the pipe transfer list.
2197 *------------------------------------------------------------------------*/
2198void
2199usb2_pipe_start(struct usb2_xfer_queue *pq)
2200{
2201	struct usb2_pipe *pipe;
2202	struct usb2_xfer *xfer;
2203	uint8_t type;
2204
2205	xfer = pq->curr;
2206	pipe = xfer->pipe;
2207
2208	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2209
2210	/*
2211	 * If the pipe is already stalled we do nothing !
2212	 */
2213	if (pipe->is_stalled) {
2214		return;
2215	}
2216	/*
2217	 * Check if we are supposed to stall the pipe:
2218	 */
2219	if (xfer->flags.stall_pipe) {
2220		/* clear stall command */
2221		xfer->flags.stall_pipe = 0;
2222
2223		/*
2224		 * Only stall BULK and INTERRUPT endpoints.
2225		 */
2226		type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2227		if ((type == UE_BULK) ||
2228		    (type == UE_INTERRUPT)) {
2229			struct usb2_device *udev;
2230			struct usb2_xfer_root *info;
2231
2232			info = xfer->xroot;
2233			udev = info->udev;
2234			pipe->is_stalled = 1;
2235
2236			if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2237				(udev->bus->methods->set_stall) (
2238				    udev, NULL, pipe);
2239			} else if (udev->default_xfer[1]) {
2240				info = udev->default_xfer[1]->xroot;
2241				if (usb2_proc_msignal(
2242				    &info->bus->non_giant_callback_proc,
2243				    &udev->cs_msg[0], &udev->cs_msg[1])) {
2244					/* ignore */
2245				}
2246			} else {
2247				/* should not happen */
2248				DPRINTFN(0, "No stall handler!\n");
2249			}
2250			/*
2251			 * We get started again when the stall is cleared!
2252			 */
2253			return;
2254		}
2255	}
2256	/* Set or clear stall complete - special case */
2257	if (xfer->nframes == 0) {
2258		/* we are complete */
2259		xfer->aframes = 0;
2260		usb2_transfer_done(xfer, 0);
2261		return;
2262	}
2263	/*
2264	 * Handled cases:
2265	 *
2266	 * 1) Start the first transfer queued.
2267	 *
2268	 * 2) Re-start the current USB transfer.
2269	 */
2270	/*
2271	 * Check if there should be any
2272	 * pre transfer start delay:
2273	 */
2274	if (xfer->interval > 0) {
2275		type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2276		if ((type == UE_BULK) ||
2277		    (type == UE_CONTROL)) {
2278			usb2_transfer_timeout_ms(xfer,
2279			    &usb2_transfer_start_cb,
2280			    xfer->interval);
2281			return;
2282		}
2283	}
2284	DPRINTF("start\n");
2285
2286	/* start USB transfer */
2287	(pipe->methods->start) (xfer);
2288
2289	/* check cancelability */
2290	if (pipe->methods->start_is_cancelable) {
2291		xfer->flags_int.can_cancel_immed = 1;
2292		if (xfer->error) {
2293			/* some error has happened */
2294			usb2_transfer_done(xfer, 0);
2295		}
2296	} else {
2297		xfer->flags_int.can_cancel_immed = 0;
2298	}
2299}
2300
2301/*------------------------------------------------------------------------*
2302 *	usb2_transfer_timeout_ms
2303 *
2304 * This function is used to setup a timeout on the given USB
2305 * transfer. If the timeout has been deferred the callback given by
2306 * "cb" will get called after "ms" milliseconds.
2307 *------------------------------------------------------------------------*/
2308void
2309usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2310    void (*cb) (void *arg), usb2_timeout_t ms)
2311{
2312	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2313
2314	/* defer delay */
2315	usb2_callout_reset(&xfer->timeout_handle,
2316	    USB_MS_TO_TICKS(ms), cb, xfer);
2317}
2318
2319/*------------------------------------------------------------------------*
2320 *	usb2_callback_wrapper_sub
2321 *
2322 *  - This function will update variables in an USB transfer after
2323 *  that the USB transfer is complete.
2324 *
2325 *  - This function is used to start the next USB transfer on the
2326 *  pipe transfer queue, if any.
2327 *
2328 * NOTE: In some special cases the USB transfer will not be removed from
2329 * the pipe queue, but remain first. To enforce USB transfer removal call
2330 * this function passing the error code "USB_ERR_CANCELLED".
2331 *
2332 * Return values:
2333 * 0: Success.
2334 * Else: The callback has been deferred.
2335 *------------------------------------------------------------------------*/
2336static uint8_t
2337usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2338{
2339	struct usb2_pipe *pipe;
2340	usb2_frcount_t x;
2341
2342	if ((!xfer->flags_int.open) &&
2343	    (!xfer->flags_int.did_close)) {
2344		DPRINTF("close\n");
2345		USB_BUS_LOCK(xfer->xroot->bus);
2346		(xfer->pipe->methods->close) (xfer);
2347		USB_BUS_UNLOCK(xfer->xroot->bus);
2348		/* only close once */
2349		xfer->flags_int.did_close = 1;
2350		return (1);		/* wait for new callback */
2351	}
2352	/*
2353	 * If we have a non-hardware induced error we
2354	 * need to do the DMA delay!
2355	 */
2356	if (((xfer->error == USB_ERR_CANCELLED) ||
2357	    (xfer->error == USB_ERR_TIMEOUT)) &&
2358	    (!xfer->flags_int.did_dma_delay)) {
2359
2360		usb2_timeout_t temp;
2361
2362		/* only delay once */
2363		xfer->flags_int.did_dma_delay = 1;
2364
2365		/* we can not cancel this delay */
2366		xfer->flags_int.can_cancel_immed = 0;
2367
2368		temp = usb2_get_dma_delay(xfer->xroot->bus);
2369
2370		DPRINTFN(3, "DMA delay, %u ms, "
2371		    "on %p\n", temp, xfer);
2372
2373		if (temp != 0) {
2374			USB_BUS_LOCK(xfer->xroot->bus);
2375			usb2_transfer_timeout_ms(xfer,
2376			    &usb2_dma_delay_done_cb, temp);
2377			USB_BUS_UNLOCK(xfer->xroot->bus);
2378			return (1);	/* wait for new callback */
2379		}
2380	}
2381	/* check actual number of frames */
2382	if (xfer->aframes > xfer->nframes) {
2383		if (xfer->error == 0) {
2384			panic("%s: actual number of frames, %d, is "
2385			    "greater than initial number of frames, %d!\n",
2386			    __FUNCTION__, xfer->aframes, xfer->nframes);
2387		} else {
2388			/* just set some valid value */
2389			xfer->aframes = xfer->nframes;
2390		}
2391	}
2392	/* compute actual length */
2393	xfer->actlen = 0;
2394
2395	for (x = 0; x != xfer->aframes; x++) {
2396		xfer->actlen += xfer->frlengths[x];
2397	}
2398
2399	/*
2400	 * Frames that were not transferred get zero actual length in
2401	 * case the USB device driver does not check the actual number
2402	 * of frames transferred, "xfer->aframes":
2403	 */
2404	for (; x < xfer->nframes; x++) {
2405		xfer->frlengths[x] = 0;
2406	}
2407
2408	/* check actual length */
2409	if (xfer->actlen > xfer->sumlen) {
2410		if (xfer->error == 0) {
2411			panic("%s: actual length, %d, is greater than "
2412			    "initial length, %d!\n",
2413			    __FUNCTION__, xfer->actlen, xfer->sumlen);
2414		} else {
2415			/* just set some valid value */
2416			xfer->actlen = xfer->sumlen;
2417		}
2418	}
2419	DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2420	    xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen,
2421	    xfer->aframes, xfer->nframes);
2422
2423	if (xfer->error) {
2424		/* end of control transfer, if any */
2425		xfer->flags_int.control_act = 0;
2426
2427		/* check if we should block the execution queue */
2428		if ((xfer->error != USB_ERR_CANCELLED) &&
2429		    (xfer->flags.pipe_bof)) {
2430			DPRINTFN(2, "xfer=%p: Block On Failure "
2431			    "on pipe=%p\n", xfer, xfer->pipe);
2432			goto done;
2433		}
2434	} else {
2435		/* check for short transfers */
2436		if (xfer->actlen < xfer->sumlen) {
2437
2438			/* end of control transfer, if any */
2439			xfer->flags_int.control_act = 0;
2440
2441			if (!xfer->flags_int.short_xfer_ok) {
2442				xfer->error = USB_ERR_SHORT_XFER;
2443				if (xfer->flags.pipe_bof) {
2444					DPRINTFN(2, "xfer=%p: Block On Failure on "
2445					    "Short Transfer on pipe %p.\n",
2446					    xfer, xfer->pipe);
2447					goto done;
2448				}
2449			}
2450		} else {
2451			/*
2452			 * Check if we are in the middle of a
2453			 * control transfer:
2454			 */
2455			if (xfer->flags_int.control_act) {
2456				DPRINTFN(5, "xfer=%p: Control transfer "
2457				    "active on pipe=%p\n", xfer, xfer->pipe);
2458				goto done;
2459			}
2460		}
2461	}
2462
2463	pipe = xfer->pipe;
2464
2465	/*
2466	 * If the current USB transfer is completing we need to start the
2467	 * next one:
2468	 */
2469	USB_BUS_LOCK(xfer->xroot->bus);
2470	if (pipe->pipe_q.curr == xfer) {
2471		usb2_command_wrapper(&pipe->pipe_q, NULL);
2472
2473		if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2474			/* there is another USB transfer waiting */
2475		} else {
2476			/* this is the last USB transfer */
2477			/* clear isochronous sync flag */
2478			xfer->pipe->is_synced = 0;
2479		}
2480	}
2481	USB_BUS_UNLOCK(xfer->xroot->bus);
2482done:
2483	return (0);
2484}
2485
2486/*------------------------------------------------------------------------*
2487 *	usb2_command_wrapper
2488 *
2489 * This function is used to execute commands non-recursivly on an USB
2490 * transfer.
2491 *------------------------------------------------------------------------*/
2492void
2493usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
2494{
2495	if (xfer) {
2496		/*
2497		 * If the transfer is not already processing,
2498		 * queue it!
2499		 */
2500		if (pq->curr != xfer) {
2501			usb2_transfer_enqueue(pq, xfer);
2502			if (pq->curr != NULL) {
2503				/* something is already processing */
2504				DPRINTFN(6, "busy %p\n", pq->curr);
2505				return;
2506			}
2507		}
2508	} else {
2509		/* Get next element in queue */
2510		pq->curr = NULL;
2511	}
2512
2513	if (!pq->recurse_1) {
2514
2515		do {
2516
2517			/* set both recurse flags */
2518			pq->recurse_1 = 1;
2519			pq->recurse_2 = 1;
2520
2521			if (pq->curr == NULL) {
2522				xfer = TAILQ_FIRST(&pq->head);
2523				if (xfer) {
2524					TAILQ_REMOVE(&pq->head, xfer,
2525					    wait_entry);
2526					xfer->wait_queue = NULL;
2527					pq->curr = xfer;
2528				} else {
2529					break;
2530				}
2531			}
2532			DPRINTFN(6, "cb %p (enter)\n", pq->curr);
2533			(pq->command) (pq);
2534			DPRINTFN(6, "cb %p (leave)\n", pq->curr);
2535
2536		} while (!pq->recurse_2);
2537
2538		/* clear first recurse flag */
2539		pq->recurse_1 = 0;
2540
2541	} else {
2542		/* clear second recurse flag */
2543		pq->recurse_2 = 0;
2544	}
2545}
2546
2547/*------------------------------------------------------------------------*
2548 *	usb2_default_transfer_setup
2549 *
2550 * This function is used to setup the default USB control endpoint
2551 * transfer.
2552 *------------------------------------------------------------------------*/
2553void
2554usb2_default_transfer_setup(struct usb2_device *udev)
2555{
2556	struct usb2_xfer *xfer;
2557	uint8_t no_resetup;
2558	uint8_t iface_index;
2559
2560repeat:
2561
2562	xfer = udev->default_xfer[0];
2563	if (xfer) {
2564		USB_XFER_LOCK(xfer);
2565		no_resetup =
2566		    ((xfer->address == udev->address) &&
2567		    (udev->default_ep_desc.wMaxPacketSize[0] ==
2568		    udev->ddesc.bMaxPacketSize));
2569		if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2570			if (no_resetup) {
2571				/*
2572				 * NOTE: checking "xfer->address" and
2573				 * starting the USB transfer must be
2574				 * atomic!
2575				 */
2576				usb2_transfer_start(xfer);
2577			}
2578		}
2579		USB_XFER_UNLOCK(xfer);
2580	} else {
2581		no_resetup = 0;
2582	}
2583
2584	if (no_resetup) {
2585		/*
2586	         * All parameters are exactly the same like before.
2587	         * Just return.
2588	         */
2589		return;
2590	}
2591	/*
2592	 * Update wMaxPacketSize for the default control endpoint:
2593	 */
2594	udev->default_ep_desc.wMaxPacketSize[0] =
2595	    udev->ddesc.bMaxPacketSize;
2596
2597	/*
2598	 * Unsetup any existing USB transfer:
2599	 */
2600	usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
2601
2602	/*
2603	 * Try to setup a new USB transfer for the
2604	 * default control endpoint:
2605	 */
2606	iface_index = 0;
2607	if (usb2_transfer_setup(udev, &iface_index,
2608	    udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL,
2609	    udev->default_mtx)) {
2610		DPRINTFN(0, "could not setup default "
2611		    "USB transfer!\n");
2612	} else {
2613		goto repeat;
2614	}
2615}
2616
2617/*------------------------------------------------------------------------*
2618 *	usb2_clear_data_toggle - factored out code
2619 *
2620 * NOTE: the intention of this function is not to reset the hardware
2621 * data toggle.
2622 *------------------------------------------------------------------------*/
2623void
2624usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe)
2625{
2626	DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe);
2627
2628	USB_BUS_LOCK(udev->bus);
2629	pipe->toggle_next = 0;
2630	USB_BUS_UNLOCK(udev->bus);
2631}
2632
2633/*------------------------------------------------------------------------*
2634 *	usb2_clear_stall_callback - factored out clear stall callback
2635 *
2636 * Input parameters:
2637 *  xfer1: Clear Stall Control Transfer
2638 *  xfer2: Stalled USB Transfer
2639 *
2640 * This function is NULL safe.
2641 *
2642 * Return values:
2643 *   0: In progress
2644 *   Else: Finished
2645 *
2646 * Clear stall config example:
2647 *
2648 * static const struct usb2_config my_clearstall =  {
2649 *	.type = UE_CONTROL,
2650 *	.endpoint = 0,
2651 *	.direction = UE_DIR_ANY,
2652 *	.interval = 50, //50 milliseconds
2653 *	.bufsize = sizeof(struct usb2_device_request),
2654 *	.mh.timeout = 1000, //1.000 seconds
2655 *	.mh.flags = { },
2656 *	.mh.callback = &my_clear_stall_callback, // **
2657 * };
2658 *
2659 * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback"
2660 * passing the correct parameters.
2661 *------------------------------------------------------------------------*/
2662uint8_t
2663usb2_clear_stall_callback(struct usb2_xfer *xfer1,
2664    struct usb2_xfer *xfer2)
2665{
2666	struct usb2_device_request req;
2667
2668	if (xfer2 == NULL) {
2669		/* looks like we are tearing down */
2670		DPRINTF("NULL input parameter\n");
2671		return (0);
2672	}
2673	USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
2674	USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
2675
2676	switch (USB_GET_STATE(xfer1)) {
2677	case USB_ST_SETUP:
2678
2679		/*
2680		 * pre-clear the data toggle to DATA0 ("umass.c" and
2681		 * "ata-usb.c" depends on this)
2682		 */
2683
2684		usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe);
2685
2686		/* setup a clear-stall packet */
2687
2688		req.bmRequestType = UT_WRITE_ENDPOINT;
2689		req.bRequest = UR_CLEAR_FEATURE;
2690		USETW(req.wValue, UF_ENDPOINT_HALT);
2691		req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress;
2692		req.wIndex[1] = 0;
2693		USETW(req.wLength, 0);
2694
2695		/*
2696		 * "usb2_transfer_setup_sub()" will ensure that
2697		 * we have sufficient room in the buffer for
2698		 * the request structure!
2699		 */
2700
2701		/* copy in the transfer */
2702
2703		usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
2704
2705		/* set length */
2706		xfer1->frlengths[0] = sizeof(req);
2707		xfer1->nframes = 1;
2708
2709		usb2_start_hardware(xfer1);
2710		return (0);
2711
2712	case USB_ST_TRANSFERRED:
2713		break;
2714
2715	default:			/* Error */
2716		if (xfer1->error == USB_ERR_CANCELLED) {
2717			return (0);
2718		}
2719		break;
2720	}
2721	return (1);			/* Clear Stall Finished */
2722}
2723
2724void
2725usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2726{
2727	static uint8_t once = 0;
2728	/* polling is currently not supported */
2729	if (!once) {
2730		once = 1;
2731		printf("usb2_do_poll: USB polling is "
2732		    "not supported!\n");
2733	}
2734}
2735