3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_transfer.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_util.h>
64 #include <dev/usb/usb_controller.h>
65 #include <dev/usb/usb_bus.h>
66 #include <dev/usb/usb_pf.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
69 struct usb_std_packet_size {
71 uint16_t min; /* inclusive */
72 uint16_t max; /* inclusive */
78 static usb_callback_t usb_request_callback;
80 static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
82 /* This transfer is used for generic control endpoint transfers */
86 .endpoint = 0x00, /* Control endpoint */
87 .direction = UE_DIR_ANY,
88 .bufsize = USB_EP0_BUFSIZE, /* bytes */
89 .flags = {.proxy_buffer = 1,},
90 .callback = &usb_request_callback,
91 .usb_mode = USB_MODE_DUAL, /* both modes */
94 /* This transfer is used for generic clear stall only */
98 .endpoint = 0x00, /* Control pipe */
99 .direction = UE_DIR_ANY,
100 .bufsize = sizeof(struct usb_device_request),
101 .callback = &usb_do_clear_stall_callback,
102 .timeout = 1000, /* 1 second */
103 .interval = 50, /* 50ms */
104 .usb_mode = USB_MODE_HOST,
108 static const struct usb_config usb_control_ep_quirk_cfg[USB_CTRL_XFER_MAX] = {
110 /* This transfer is used for generic control endpoint transfers */
114 .endpoint = 0x00, /* Control endpoint */
115 .direction = UE_DIR_ANY,
116 .bufsize = 65535, /* bytes */
117 .callback = &usb_request_callback,
118 .usb_mode = USB_MODE_DUAL, /* both modes */
121 /* This transfer is used for generic clear stall only */
125 .endpoint = 0x00, /* Control pipe */
126 .direction = UE_DIR_ANY,
127 .bufsize = sizeof(struct usb_device_request),
128 .callback = &usb_do_clear_stall_callback,
129 .timeout = 1000, /* 1 second */
130 .interval = 50, /* 50ms */
131 .usb_mode = USB_MODE_HOST,
135 /* function prototypes */
137 static void usbd_update_max_frame_size(struct usb_xfer *);
138 static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
139 static void usbd_control_transfer_init(struct usb_xfer *);
140 static int usbd_setup_ctrl_transfer(struct usb_xfer *);
141 static void usb_callback_proc(struct usb_proc_msg *);
142 static void usbd_callback_ss_done_defer(struct usb_xfer *);
143 static void usbd_callback_wrapper(struct usb_xfer_queue *);
144 static void usbd_transfer_start_cb(void *);
145 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
146 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
147 uint8_t type, enum usb_dev_speed speed);
149 /*------------------------------------------------------------------------*
150 * usb_request_callback
151 *------------------------------------------------------------------------*/
153 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
155 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
156 usb_handle_request_callback(xfer, error);
158 usbd_do_request_callback(xfer, error);
161 /*------------------------------------------------------------------------*
162 * usbd_update_max_frame_size
164 * This function updates the maximum frame size, hence high speed USB
165 * can transfer multiple consecutive packets.
166 *------------------------------------------------------------------------*/
168 usbd_update_max_frame_size(struct usb_xfer *xfer)
170 /* compute maximum frame size */
171 /* this computation should not overflow 16-bit */
172 /* max = 15 * 1024 */
174 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
177 /*------------------------------------------------------------------------*
180 * The following function is called when we need to
181 * synchronize with DMA hardware.
184 * 0: no DMA delay required
185 * Else: milliseconds of DMA delay
186 *------------------------------------------------------------------------*/
188 usbd_get_dma_delay(struct usb_device *udev)
190 struct usb_bus_methods *mtod;
193 mtod = udev->bus->methods;
196 if (mtod->get_dma_delay) {
197 (mtod->get_dma_delay) (udev, &temp);
199 * Round up and convert to milliseconds. Note that we use
200 * 1024 milliseconds per second. to save a division.
208 /*------------------------------------------------------------------------*
209 * usbd_transfer_setup_sub_malloc
211 * This function will allocate one or more DMA'able memory chunks
212 * according to "size", "align" and "count" arguments. "ppc" is
213 * pointed to a linear array of USB page caches afterwards.
215 * If the "align" argument is equal to "1" a non-contiguous allocation
216 * can happen. Else if the "align" argument is greater than "1", the
217 * allocation will always be contiguous in memory.
222 *------------------------------------------------------------------------*/
225 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
226 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
229 struct usb_page_cache *pc;
240 USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
242 USB_ASSERT(size > 0, ("Invalid size = 0\n"));
245 return (0); /* nothing to allocate */
248 * Make sure that the size is aligned properly.
250 size = -((-size) & (-align));
253 * Try multi-allocation chunks to reduce the number of DMA
254 * allocations, hence DMA allocations are slow.
257 /* special case - non-cached multi page DMA memory */
259 n_dma_pg = (2 + (size / USB_PAGE_SIZE));
261 } else if (size >= USB_PAGE_SIZE) {
266 /* compute number of objects per page */
267 #ifdef USB_DMA_SINGLE_ALLOC
270 n_obj = (USB_PAGE_SIZE / size);
273 * Compute number of DMA chunks, rounded up
276 n_dma_pc = ((count + n_obj - 1) / n_obj);
281 * DMA memory is allocated once, but mapped twice. That's why
282 * there is one list for auto-free and another list for
283 * non-auto-free which only holds the mapping and not the
286 if (parm->buf == NULL) {
287 /* reserve memory (auto-free) */
288 parm->dma_page_ptr += n_dma_pc * n_dma_pg;
289 parm->dma_page_cache_ptr += n_dma_pc;
291 /* reserve memory (no-auto-free) */
292 parm->dma_page_ptr += count * n_dma_pg;
293 parm->xfer_page_cache_ptr += count;
296 for (x = 0; x != n_dma_pc; x++) {
297 /* need to initialize the page cache */
298 parm->dma_page_cache_ptr[x].tag_parent =
299 &parm->curr_xfer->xroot->dma_parent_tag;
301 for (x = 0; x != count; x++) {
302 /* need to initialize the page cache */
303 parm->xfer_page_cache_ptr[x].tag_parent =
304 &parm->curr_xfer->xroot->dma_parent_tag;
309 *ppc = parm->xfer_page_cache_ptr;
311 *ppc = parm->dma_page_cache_ptr;
313 r = count; /* set remainder count */
314 z = n_obj * size; /* set allocation size */
315 pc = parm->xfer_page_cache_ptr;
316 pg = parm->dma_page_ptr;
320 * Avoid mapping memory twice if only a single object
321 * should be allocated per page cache:
323 for (x = 0; x != n_dma_pc; x++) {
324 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
326 return (1); /* failure */
328 /* Make room for one DMA page cache and "n_dma_pg" pages */
329 parm->dma_page_cache_ptr++;
333 for (x = 0; x != n_dma_pc; x++) {
336 /* compute last remainder */
340 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
342 return (1); /* failure */
344 /* Set beginning of current buffer */
345 buf = parm->dma_page_cache_ptr->buffer;
346 /* Make room for one DMA page cache and "n_dma_pg" pages */
347 parm->dma_page_cache_ptr++;
350 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
352 /* Load sub-chunk into DMA */
353 if (usb_pc_dmamap_create(pc, size)) {
354 return (1); /* failure */
356 pc->buffer = USB_ADD_BYTES(buf, y * size);
359 mtx_lock(pc->tag_parent->mtx);
360 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
361 mtx_unlock(pc->tag_parent->mtx);
362 return (1); /* failure */
364 mtx_unlock(pc->tag_parent->mtx);
369 parm->xfer_page_cache_ptr = pc;
370 parm->dma_page_ptr = pg;
375 /*------------------------------------------------------------------------*
376 * usbd_get_max_frame_length
378 * This function returns the maximum single frame length as computed by
379 * usbd_transfer_setup(). It is useful when computing buffer sizes for
380 * devices having multiple alternate settings. The SuperSpeed endpoint
381 * companion pointer is allowed to be NULL.
382 *------------------------------------------------------------------------*/
384 usbd_get_max_frame_length(const struct usb_endpoint_descriptor *edesc,
385 const struct usb_endpoint_ss_comp_descriptor *ecomp,
386 enum usb_dev_speed speed)
388 uint32_t max_packet_size;
389 uint32_t max_packet_count;
392 max_packet_size = UGETW(edesc->wMaxPacketSize);
393 max_packet_count = 1;
394 type = (edesc->bmAttributes & UE_XFERTYPE);
402 (max_packet_size >> 11) & 3;
404 /* check for invalid max packet count */
405 if (max_packet_count > 3)
406 max_packet_count = 3;
411 max_packet_size &= 0x7FF;
413 case USB_SPEED_SUPER:
414 max_packet_count += (max_packet_size >> 11) & 3;
417 max_packet_count += ecomp->bMaxBurst;
419 if ((max_packet_count == 0) ||
420 (max_packet_count > 16))
421 max_packet_count = 16;
425 max_packet_count = 1;
431 mult = UE_GET_SS_ISO_MULT(
432 ecomp->bmAttributes) + 1;
436 max_packet_count *= mult;
442 max_packet_size &= 0x7FF;
447 return (max_packet_size * max_packet_count);
450 /*------------------------------------------------------------------------*
451 * usbd_transfer_setup_sub - transfer setup subroutine
453 * This function must be called from the "xfer_setup" callback of the
454 * USB Host or Device controller driver when setting up an USB
455 * transfer. This function will setup correct packet sizes, buffer
456 * sizes, flags and more, that are stored in the "usb_xfer"
458 *------------------------------------------------------------------------*/
460 usbd_transfer_setup_sub(struct usb_setup_params *parm)
466 struct usb_xfer *xfer = parm->curr_xfer;
467 const struct usb_config *setup = parm->curr_setup;
468 struct usb_endpoint_ss_comp_descriptor *ecomp;
469 struct usb_endpoint_descriptor *edesc;
470 struct usb_std_packet_size std_size;
471 usb_frcount_t n_frlengths;
472 usb_frcount_t n_frbuffers;
479 * Sanity check. The following parameters must be initialized before
480 * calling this function.
482 if ((parm->hc_max_packet_size == 0) ||
483 (parm->hc_max_packet_count == 0) ||
484 (parm->hc_max_frame_size == 0)) {
485 parm->err = USB_ERR_INVAL;
488 edesc = xfer->endpoint->edesc;
489 ecomp = xfer->endpoint->ecomp;
491 type = (edesc->bmAttributes & UE_XFERTYPE);
493 xfer->flags = setup->flags;
494 xfer->nframes = setup->frames;
495 xfer->timeout = setup->timeout;
496 xfer->callback = setup->callback;
497 xfer->interval = setup->interval;
498 xfer->endpointno = edesc->bEndpointAddress;
499 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
500 xfer->max_packet_count = 1;
501 /* make a shadow copy: */
502 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
504 parm->bufsize = setup->bufsize;
506 switch (parm->speed) {
511 xfer->max_packet_count +=
512 (xfer->max_packet_size >> 11) & 3;
514 /* check for invalid max packet count */
515 if (xfer->max_packet_count > 3)
516 xfer->max_packet_count = 3;
521 xfer->max_packet_size &= 0x7FF;
523 case USB_SPEED_SUPER:
524 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
527 xfer->max_packet_count += ecomp->bMaxBurst;
529 if ((xfer->max_packet_count == 0) ||
530 (xfer->max_packet_count > 16))
531 xfer->max_packet_count = 16;
535 xfer->max_packet_count = 1;
541 mult = UE_GET_SS_ISO_MULT(
542 ecomp->bmAttributes) + 1;
546 xfer->max_packet_count *= mult;
552 xfer->max_packet_size &= 0x7FF;
557 /* range check "max_packet_count" */
559 if (xfer->max_packet_count > parm->hc_max_packet_count) {
560 xfer->max_packet_count = parm->hc_max_packet_count;
563 /* store max packet size value before filtering */
565 maxp_old = xfer->max_packet_size;
567 /* filter "wMaxPacketSize" according to HC capabilities */
569 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
570 (xfer->max_packet_size == 0)) {
571 xfer->max_packet_size = parm->hc_max_packet_size;
573 /* filter "wMaxPacketSize" according to standard sizes */
575 usbd_get_std_packet_size(&std_size, type, parm->speed);
577 if (std_size.range.min || std_size.range.max) {
579 if (xfer->max_packet_size < std_size.range.min) {
580 xfer->max_packet_size = std_size.range.min;
582 if (xfer->max_packet_size > std_size.range.max) {
583 xfer->max_packet_size = std_size.range.max;
587 if (xfer->max_packet_size >= std_size.fixed[3]) {
588 xfer->max_packet_size = std_size.fixed[3];
589 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
590 xfer->max_packet_size = std_size.fixed[2];
591 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
592 xfer->max_packet_size = std_size.fixed[1];
594 /* only one possibility left */
595 xfer->max_packet_size = std_size.fixed[0];
600 * Check if the max packet size was outside its allowed range
601 * and clamped to a valid value:
603 if (maxp_old != xfer->max_packet_size)
604 xfer->flags_int.maxp_was_clamped = 1;
606 /* compute "max_frame_size" */
608 usbd_update_max_frame_size(xfer);
610 /* check interrupt interval and transfer pre-delay */
612 if (type == UE_ISOCHRONOUS) {
614 uint16_t frame_limit;
616 xfer->interval = 0; /* not used, must be zero */
617 xfer->flags_int.isochronous_xfr = 1; /* set flag */
619 if (xfer->timeout == 0) {
621 * set a default timeout in
622 * case something goes wrong!
624 xfer->timeout = 1000 / 4;
626 switch (parm->speed) {
629 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
633 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
634 xfer->fps_shift = edesc->bInterval;
635 if (xfer->fps_shift > 0)
637 if (xfer->fps_shift > 3)
639 if (xfer->flags.pre_scale_frames != 0)
640 xfer->nframes <<= (3 - xfer->fps_shift);
644 if (xfer->nframes > frame_limit) {
646 * this is not going to work
649 parm->err = USB_ERR_INVAL;
652 if (xfer->nframes == 0) {
654 * this is not a valid value
656 parm->err = USB_ERR_ZERO_NFRAMES;
662 * If a value is specified use that else check the
663 * endpoint descriptor!
665 if (type == UE_INTERRUPT) {
669 if (xfer->interval == 0) {
671 xfer->interval = edesc->bInterval;
673 switch (parm->speed) {
679 if (xfer->interval < 4)
681 else if (xfer->interval > 16)
682 xfer->interval = (1 << (16 - 4));
685 (1 << (xfer->interval - 4));
690 if (xfer->interval == 0) {
692 * One millisecond is the smallest
693 * interval we support:
701 while ((temp != 0) && (temp < xfer->interval)) {
706 switch (parm->speed) {
711 xfer->fps_shift += 3;
718 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
719 * to be equal to zero when setting up USB transfers, hence
720 * this leads to alot of extra code in the USB kernel.
723 if ((xfer->max_frame_size == 0) ||
724 (xfer->max_packet_size == 0)) {
728 if ((parm->bufsize <= MIN_PKT) &&
729 (type != UE_CONTROL) &&
733 xfer->max_packet_size = MIN_PKT;
734 xfer->max_packet_count = 1;
735 parm->bufsize = 0; /* automatic setup length */
736 usbd_update_max_frame_size(xfer);
739 parm->err = USB_ERR_ZERO_MAXP;
748 * check if we should setup a default
752 if (parm->bufsize == 0) {
754 parm->bufsize = xfer->max_frame_size;
756 if (type == UE_ISOCHRONOUS) {
757 parm->bufsize *= xfer->nframes;
761 * check if we are about to setup a proxy
765 if (xfer->flags.proxy_buffer) {
767 /* round bufsize up */
769 parm->bufsize += (xfer->max_frame_size - 1);
771 if (parm->bufsize < xfer->max_frame_size) {
772 /* length wrapped around */
773 parm->err = USB_ERR_INVAL;
776 /* subtract remainder */
778 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
780 /* add length of USB device request structure, if any */
782 if (type == UE_CONTROL) {
783 parm->bufsize += REQ_SIZE; /* SETUP message */
786 xfer->max_data_length = parm->bufsize;
788 /* Setup "n_frlengths" and "n_frbuffers" */
790 if (type == UE_ISOCHRONOUS) {
791 n_frlengths = xfer->nframes;
795 if (type == UE_CONTROL) {
796 xfer->flags_int.control_xfr = 1;
797 if (xfer->nframes == 0) {
798 if (parm->bufsize <= REQ_SIZE) {
800 * there will never be any data
809 if (xfer->nframes == 0) {
814 n_frlengths = xfer->nframes;
815 n_frbuffers = xfer->nframes;
819 * check if we have room for the
820 * USB device request structure:
823 if (type == UE_CONTROL) {
825 if (xfer->max_data_length < REQ_SIZE) {
826 /* length wrapped around or too small bufsize */
827 parm->err = USB_ERR_INVAL;
830 xfer->max_data_length -= REQ_SIZE;
833 * Setup "frlengths" and shadow "frlengths" for keeping the
834 * initial frame lengths when a USB transfer is complete. This
835 * information is useful when computing isochronous offsets.
837 xfer->frlengths = parm->xfer_length_ptr;
838 parm->xfer_length_ptr += 2 * n_frlengths;
840 /* setup "frbuffers" */
841 xfer->frbuffers = parm->xfer_page_cache_ptr;
842 parm->xfer_page_cache_ptr += n_frbuffers;
844 /* initialize max frame count */
845 xfer->max_frame_count = xfer->nframes;
848 * check if we need to setup
852 if (!xfer->flags.ext_buffer) {
854 struct usb_page_search page_info;
855 struct usb_page_cache *pc;
857 if (usbd_transfer_setup_sub_malloc(parm,
858 &pc, parm->bufsize, 1, 1)) {
859 parm->err = USB_ERR_NOMEM;
860 } else if (parm->buf != NULL) {
862 usbd_get_page(pc, 0, &page_info);
864 xfer->local_buffer = page_info.buffer;
866 usbd_xfer_set_frame_offset(xfer, 0, 0);
868 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
869 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
874 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
876 if (parm->buf != NULL) {
878 USB_ADD_BYTES(parm->buf, parm->size[0]);
880 usbd_xfer_set_frame_offset(xfer, 0, 0);
882 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
883 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
886 parm->size[0] += parm->bufsize;
888 /* align data again */
889 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
893 * Compute maximum buffer size
896 if (parm->bufsize_max < parm->bufsize) {
897 parm->bufsize_max = parm->bufsize;
900 if (xfer->flags_int.bdma_enable) {
902 * Setup "dma_page_ptr".
904 * Proof for formula below:
906 * Assume there are three USB frames having length "a", "b" and
907 * "c". These USB frames will at maximum need "z"
908 * "usb_page" structures. "z" is given by:
910 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
911 * ((c / USB_PAGE_SIZE) + 2);
913 * Constraining "a", "b" and "c" like this:
915 * (a + b + c) <= parm->bufsize
919 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
921 * Here is the general formula:
923 xfer->dma_page_ptr = parm->dma_page_ptr;
924 parm->dma_page_ptr += (2 * n_frbuffers);
925 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
929 /* correct maximum data length */
930 xfer->max_data_length = 0;
932 /* subtract USB frame remainder from "hc_max_frame_size" */
934 xfer->max_hc_frame_size =
935 (parm->hc_max_frame_size -
936 (parm->hc_max_frame_size % xfer->max_frame_size));
938 if (xfer->max_hc_frame_size == 0) {
939 parm->err = USB_ERR_INVAL;
943 /* initialize frame buffers */
946 for (x = 0; x != n_frbuffers; x++) {
947 xfer->frbuffers[x].tag_parent =
948 &xfer->xroot->dma_parent_tag;
950 if (xfer->flags_int.bdma_enable &&
951 (parm->bufsize_max > 0)) {
953 if (usb_pc_dmamap_create(
955 parm->bufsize_max)) {
956 parm->err = USB_ERR_NOMEM;
966 * Set some dummy values so that we avoid division by zero:
968 xfer->max_hc_frame_size = 1;
969 xfer->max_frame_size = 1;
970 xfer->max_packet_size = 1;
971 xfer->max_data_length = 0;
973 xfer->max_frame_count = 0;
978 usbd_transfer_setup_has_bulk(const struct usb_config *setup_start,
982 uint8_t type = setup_start[n_setup].type;
983 if (type == UE_BULK || type == UE_BULK_INTR ||
990 /*------------------------------------------------------------------------*
991 * usbd_transfer_setup - setup an array of USB transfers
993 * NOTE: You must always call "usbd_transfer_unsetup" after calling
994 * "usbd_transfer_setup" if success was returned.
996 * The idea is that the USB device driver should pre-allocate all its
997 * transfers by one call to this function.
1002 *------------------------------------------------------------------------*/
1004 usbd_transfer_setup(struct usb_device *udev,
1005 const uint8_t *ifaces, struct usb_xfer **ppxfer,
1006 const struct usb_config *setup_start, uint16_t n_setup,
1007 void *priv_sc, struct mtx *xfer_mtx)
1009 const struct usb_config *setup_end = setup_start + n_setup;
1010 const struct usb_config *setup;
1011 struct usb_setup_params *parm;
1012 struct usb_endpoint *ep;
1013 struct usb_xfer_root *info;
1014 struct usb_xfer *xfer;
1016 usb_error_t error = 0;
1021 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1022 "usbd_transfer_setup can sleep!");
1024 /* do some checking first */
1027 DPRINTFN(6, "setup array has zero length!\n");
1028 return (USB_ERR_INVAL);
1031 DPRINTFN(6, "ifaces array is NULL!\n");
1032 return (USB_ERR_INVAL);
1034 if (xfer_mtx == NULL) {
1035 DPRINTFN(6, "using global lock\n");
1039 /* more sanity checks */
1041 for (setup = setup_start, n = 0;
1042 setup != setup_end; setup++, n++) {
1043 if (setup->bufsize == (usb_frlength_t)-1) {
1044 error = USB_ERR_BAD_BUFSIZE;
1045 DPRINTF("invalid bufsize\n");
1047 if (setup->callback == NULL) {
1048 error = USB_ERR_NO_CALLBACK;
1049 DPRINTF("no callback\n");
1057 /* Protect scratch area */
1058 do_unlock = usbd_ctrl_lock(udev);
1063 parm = &udev->scratch.xfer_setup[0].parm;
1064 memset(parm, 0, sizeof(*parm));
1067 parm->speed = usbd_get_speed(udev);
1068 parm->hc_max_packet_count = 1;
1070 if (parm->speed >= USB_SPEED_MAX) {
1071 parm->err = USB_ERR_INVAL;
1074 /* setup all transfers */
1080 * Initialize the "usb_xfer_root" structure,
1081 * which is common for all our USB transfers.
1083 info = USB_ADD_BYTES(buf, 0);
1085 info->memory_base = buf;
1086 info->memory_size = parm->size[0];
1089 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
1090 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
1092 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
1093 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
1095 cv_init(&info->cv_drain, "WDRAIN");
1097 info->xfer_mtx = xfer_mtx;
1099 usb_dma_tag_setup(&info->dma_parent_tag,
1100 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
1101 xfer_mtx, &usb_bdma_done_event, udev->bus->dma_bits,
1105 info->bus = udev->bus;
1108 TAILQ_INIT(&info->done_q.head);
1109 info->done_q.command = &usbd_callback_wrapper;
1111 TAILQ_INIT(&info->dma_q.head);
1112 info->dma_q.command = &usb_bdma_work_loop;
1114 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
1115 info->done_m[0].xroot = info;
1116 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1117 info->done_m[1].xroot = info;
1120 * In device side mode control endpoint
1121 * requests need to run from a separate
1122 * context, else there is a chance of
1125 if (setup_start == usb_control_ep_cfg ||
1126 setup_start == usb_control_ep_quirk_cfg)
1128 USB_BUS_CONTROL_XFER_PROC(udev->bus);
1129 else if (xfer_mtx == &Giant)
1131 USB_BUS_GIANT_PROC(udev->bus);
1132 else if (usbd_transfer_setup_has_bulk(setup_start, n_setup))
1134 USB_BUS_NON_GIANT_BULK_PROC(udev->bus);
1137 USB_BUS_NON_GIANT_ISOC_PROC(udev->bus);
1143 parm->size[0] += sizeof(info[0]);
1145 for (setup = setup_start, n = 0;
1146 setup != setup_end; setup++, n++) {
1148 /* skip USB transfers without callbacks: */
1149 if (setup->callback == NULL) {
1152 /* see if there is a matching endpoint */
1153 ep = usbd_get_endpoint(udev,
1154 ifaces[setup->if_index], setup);
1157 * Check that the USB PIPE is valid and that
1158 * the endpoint mode is proper.
1160 * Make sure we don't allocate a streams
1161 * transfer when such a combination is not
1164 if ((ep == NULL) || (ep->methods == NULL) ||
1165 ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1166 (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1167 (setup->stream_id != 0 &&
1168 (setup->stream_id >= USB_MAX_EP_STREAMS ||
1169 (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1170 if (setup->flags.no_pipe_ok)
1172 if ((setup->usb_mode != USB_MODE_DUAL) &&
1173 (setup->usb_mode != udev->flags.usb_mode))
1175 parm->err = USB_ERR_NO_PIPE;
1179 /* align data properly */
1180 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1182 /* store current setup pointer */
1183 parm->curr_setup = setup;
1187 * Common initialization of the
1188 * "usb_xfer" structure.
1190 xfer = USB_ADD_BYTES(buf, parm->size[0]);
1191 xfer->address = udev->address;
1192 xfer->priv_sc = priv_sc;
1195 usb_callout_init_mtx(&xfer->timeout_handle,
1196 &udev->bus->bus_mtx, 0);
1199 * Setup a dummy xfer, hence we are
1200 * writing to the "usb_xfer"
1201 * structure pointed to by "xfer"
1202 * before we have allocated any
1205 xfer = &udev->scratch.xfer_setup[0].dummy;
1206 memset(xfer, 0, sizeof(*xfer));
1210 /* set transfer endpoint pointer */
1211 xfer->endpoint = ep;
1213 /* set transfer stream ID */
1214 xfer->stream_id = setup->stream_id;
1216 parm->size[0] += sizeof(xfer[0]);
1217 parm->methods = xfer->endpoint->methods;
1218 parm->curr_xfer = xfer;
1221 * Call the Host or Device controller transfer
1224 (udev->bus->methods->xfer_setup) (parm);
1226 /* check for error */
1232 * Increment the endpoint refcount. This
1233 * basically prevents setting a new
1234 * configuration and alternate setting
1235 * when USB transfers are in use on
1236 * the given interface. Search the USB
1237 * code for "endpoint->refcount_alloc" if you
1238 * want more information.
1240 USB_BUS_LOCK(info->bus);
1241 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1242 parm->err = USB_ERR_INVAL;
1244 xfer->endpoint->refcount_alloc++;
1246 if (xfer->endpoint->refcount_alloc == 0)
1247 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1248 USB_BUS_UNLOCK(info->bus);
1251 * Whenever we set ppxfer[] then we
1252 * also need to increment the
1255 info->setup_refcount++;
1258 * Transfer is successfully setup and
1264 /* check for error */
1269 if (buf != NULL || parm->err != 0)
1272 /* if no transfers, nothing to do */
1276 /* align data properly */
1277 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1279 /* store offset temporarily */
1280 parm->size[1] = parm->size[0];
1283 * The number of DMA tags required depends on
1284 * the number of endpoints. The current estimate
1285 * for maximum number of DMA tags per endpoint
1287 * 1) for loading memory
1288 * 2) for allocating memory
1289 * 3) for fixing memory [UHCI]
1291 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1294 * DMA tags for QH, TD, Data and more.
1296 parm->dma_tag_max += 8;
1298 parm->dma_tag_p += parm->dma_tag_max;
1300 parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1303 /* align data properly */
1304 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1306 /* store offset temporarily */
1307 parm->size[3] = parm->size[0];
1309 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1312 /* align data properly */
1313 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1315 /* store offset temporarily */
1316 parm->size[4] = parm->size[0];
1318 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1321 /* store end offset temporarily */
1322 parm->size[5] = parm->size[0];
1324 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1327 /* store end offset temporarily */
1329 parm->size[2] = parm->size[0];
1331 /* align data properly */
1332 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1334 parm->size[6] = parm->size[0];
1336 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1339 /* align data properly */
1340 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1342 /* allocate zeroed memory */
1343 buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1346 parm->err = USB_ERR_NOMEM;
1347 DPRINTFN(0, "cannot allocate memory block for "
1348 "configuration (%d bytes)\n",
1352 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1353 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1354 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1355 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1356 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1361 if (info->setup_refcount == 0) {
1363 * "usbd_transfer_unsetup_sub" will unlock
1364 * the bus mutex before returning !
1366 USB_BUS_LOCK(info->bus);
1368 /* something went wrong */
1369 usbd_transfer_unsetup_sub(info, 0);
1373 /* check if any errors happened */
1375 usbd_transfer_unsetup(ppxfer, n_setup);
1380 usbd_ctrl_unlock(udev);
1385 /*------------------------------------------------------------------------*
1386 * usbd_transfer_unsetup_sub - factored out code
1387 *------------------------------------------------------------------------*/
1389 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1392 struct usb_page_cache *pc;
1395 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1397 /* wait for any outstanding DMA operations */
1401 temp = usbd_get_dma_delay(info->udev);
1403 usb_pause_mtx(&info->bus->bus_mtx,
1404 USB_MS_TO_TICKS(temp));
1408 /* make sure that our done messages are not queued anywhere */
1409 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1411 USB_BUS_UNLOCK(info->bus);
1414 /* free DMA'able memory, if any */
1415 pc = info->dma_page_cache_start;
1416 while (pc != info->dma_page_cache_end) {
1417 usb_pc_free_mem(pc);
1421 /* free DMA maps in all "xfer->frbuffers" */
1422 pc = info->xfer_page_cache_start;
1423 while (pc != info->xfer_page_cache_end) {
1424 usb_pc_dmamap_destroy(pc);
1428 /* free all DMA tags */
1429 usb_dma_tag_unsetup(&info->dma_parent_tag);
1432 cv_destroy(&info->cv_drain);
1435 * free the "memory_base" last, hence the "info" structure is
1436 * contained within the "memory_base"!
1438 free(info->memory_base, M_USB);
1441 /*------------------------------------------------------------------------*
1442 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1444 * NOTE: All USB transfers in progress will get called back passing
1445 * the error code "USB_ERR_CANCELLED" before this function
1447 *------------------------------------------------------------------------*/
1449 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1451 struct usb_xfer *xfer;
1452 struct usb_xfer_root *info;
1453 uint8_t needs_delay = 0;
1455 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1456 "usbd_transfer_unsetup can sleep!");
1459 xfer = pxfer[n_setup];
1466 USB_XFER_LOCK(xfer);
1467 USB_BUS_LOCK(info->bus);
1470 * HINT: when you start/stop a transfer, it might be a
1471 * good idea to directly use the "pxfer[]" structure:
1473 * usbd_transfer_start(sc->pxfer[0]);
1474 * usbd_transfer_stop(sc->pxfer[0]);
1476 * That way, if your code has many parts that will not
1477 * stop running under the same lock, in other words
1478 * "xfer_mtx", the usbd_transfer_start and
1479 * usbd_transfer_stop functions will simply return
1480 * when they detect a NULL pointer argument.
1482 * To avoid any races we clear the "pxfer[]" pointer
1483 * while holding the private mutex of the driver:
1485 pxfer[n_setup] = NULL;
1487 USB_BUS_UNLOCK(info->bus);
1488 USB_XFER_UNLOCK(xfer);
1490 usbd_transfer_drain(xfer);
1493 if (xfer->flags_int.bdma_enable)
1497 * NOTE: default endpoint does not have an
1498 * interface, even if endpoint->iface_index == 0
1500 USB_BUS_LOCK(info->bus);
1501 xfer->endpoint->refcount_alloc--;
1502 USB_BUS_UNLOCK(info->bus);
1504 usb_callout_drain(&xfer->timeout_handle);
1506 USB_BUS_LOCK(info->bus);
1508 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1509 "reference count\n"));
1511 info->setup_refcount--;
1513 if (info->setup_refcount == 0) {
1514 usbd_transfer_unsetup_sub(info,
1517 USB_BUS_UNLOCK(info->bus);
1522 /*------------------------------------------------------------------------*
1523 * usbd_control_transfer_init - factored out code
1525 * In USB Device Mode we have to wait for the SETUP packet which
1526 * containst the "struct usb_device_request" structure, before we can
1527 * transfer any data. In USB Host Mode we already have the SETUP
1528 * packet at the moment the USB transfer is started. This leads us to
1529 * having to setup the USB transfer at two different places in
1530 * time. This function just contains factored out control transfer
1531 * initialisation code, so that we don't duplicate the code.
1532 *------------------------------------------------------------------------*/
1534 usbd_control_transfer_init(struct usb_xfer *xfer)
1536 struct usb_device_request req;
1538 /* copy out the USB request header */
1540 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1542 /* setup remainder */
1544 xfer->flags_int.control_rem = UGETW(req.wLength);
1546 /* copy direction to endpoint variable */
1548 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1550 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1553 /*------------------------------------------------------------------------*
1554 * usbd_control_transfer_did_data
1556 * This function returns non-zero if a control endpoint has
1557 * transferred the first DATA packet after the SETUP packet.
1558 * Else it returns zero.
1559 *------------------------------------------------------------------------*/
1561 usbd_control_transfer_did_data(struct usb_xfer *xfer)
1563 struct usb_device_request req;
1565 /* SETUP packet is not yet sent */
1566 if (xfer->flags_int.control_hdr != 0)
1569 /* copy out the USB request header */
1570 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1572 /* compare remainder to the initial value */
1573 return (xfer->flags_int.control_rem != UGETW(req.wLength));
1576 /*------------------------------------------------------------------------*
1577 * usbd_setup_ctrl_transfer
1579 * This function handles initialisation of control transfers. Control
1580 * transfers are special in that regard that they can both transmit
1586 *------------------------------------------------------------------------*/
1588 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1592 /* Check for control endpoint stall */
1593 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1594 /* the control transfer is no longer active */
1595 xfer->flags_int.control_stall = 1;
1596 xfer->flags_int.control_act = 0;
1598 /* don't stall control transfer by default */
1599 xfer->flags_int.control_stall = 0;
1602 /* Check for invalid number of frames */
1603 if (xfer->nframes > 2) {
1605 * If you need to split a control transfer, you
1606 * have to do one part at a time. Only with
1607 * non-control transfers you can do multiple
1610 DPRINTFN(0, "Too many frames: %u\n",
1611 (unsigned int)xfer->nframes);
1616 * Check if there is a control
1617 * transfer in progress:
1619 if (xfer->flags_int.control_act) {
1621 if (xfer->flags_int.control_hdr) {
1623 /* clear send header flag */
1625 xfer->flags_int.control_hdr = 0;
1627 /* setup control transfer */
1628 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1629 usbd_control_transfer_init(xfer);
1632 /* get data length */
1638 /* the size of the SETUP structure is hardcoded ! */
1640 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1641 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1642 xfer->frlengths[0], sizeof(struct
1643 usb_device_request));
1646 /* check USB mode */
1647 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1649 /* check number of frames */
1650 if (xfer->nframes != 1) {
1652 * We need to receive the setup
1653 * message first so that we know the
1656 DPRINTF("Misconfigured transfer\n");
1660 * Set a dummy "control_rem" value. This
1661 * variable will be overwritten later by a
1662 * call to "usbd_control_transfer_init()" !
1664 xfer->flags_int.control_rem = 0xFFFF;
1667 /* setup "endpoint" and "control_rem" */
1669 usbd_control_transfer_init(xfer);
1672 /* set transfer-header flag */
1674 xfer->flags_int.control_hdr = 1;
1676 /* get data length */
1678 len = (xfer->sumlen - sizeof(struct usb_device_request));
1681 /* update did data flag */
1683 xfer->flags_int.control_did_data =
1684 usbd_control_transfer_did_data(xfer);
1686 /* check if there is a length mismatch */
1688 if (len > xfer->flags_int.control_rem) {
1689 DPRINTFN(0, "Length (%d) greater than "
1690 "remaining length (%d)\n", len,
1691 xfer->flags_int.control_rem);
1694 /* check if we are doing a short transfer */
1696 if (xfer->flags.force_short_xfer) {
1697 xfer->flags_int.control_rem = 0;
1699 if ((len != xfer->max_data_length) &&
1700 (len != xfer->flags_int.control_rem) &&
1701 (xfer->nframes != 1)) {
1702 DPRINTFN(0, "Short control transfer without "
1703 "force_short_xfer set\n");
1706 xfer->flags_int.control_rem -= len;
1709 /* the status part is executed when "control_act" is 0 */
1711 if ((xfer->flags_int.control_rem > 0) ||
1712 (xfer->flags.manual_status)) {
1713 /* don't execute the STATUS stage yet */
1714 xfer->flags_int.control_act = 1;
1717 if ((!xfer->flags_int.control_hdr) &&
1718 (xfer->nframes == 1)) {
1720 * This is not a valid operation!
1722 DPRINTFN(0, "Invalid parameter "
1727 /* time to execute the STATUS stage */
1728 xfer->flags_int.control_act = 0;
1730 return (0); /* success */
1733 return (1); /* failure */
1736 /*------------------------------------------------------------------------*
1737 * usbd_transfer_submit - start USB hardware for the given transfer
1739 * This function should only be called from the USB callback.
1740 *------------------------------------------------------------------------*/
1742 usbd_transfer_submit(struct usb_xfer *xfer)
1744 struct usb_xfer_root *info;
1745 struct usb_bus *bus;
1751 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1752 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1756 if (USB_DEBUG_VAR > 0) {
1759 usb_dump_endpoint(xfer->endpoint);
1761 USB_BUS_UNLOCK(bus);
1765 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1766 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1768 /* Only open the USB transfer once! */
1769 if (!xfer->flags_int.open) {
1770 xfer->flags_int.open = 1;
1775 (xfer->endpoint->methods->open) (xfer);
1776 USB_BUS_UNLOCK(bus);
1778 /* set "transferring" flag */
1779 xfer->flags_int.transferring = 1;
1782 /* increment power reference */
1783 usbd_transfer_power_ref(xfer, 1);
1786 * Check if the transfer is waiting on a queue, most
1787 * frequently the "done_q":
1789 if (xfer->wait_queue) {
1791 usbd_transfer_dequeue(xfer);
1792 USB_BUS_UNLOCK(bus);
1794 /* clear "did_dma_delay" flag */
1795 xfer->flags_int.did_dma_delay = 0;
1797 /* clear "did_close" flag */
1798 xfer->flags_int.did_close = 0;
1801 /* clear "bdma_setup" flag */
1802 xfer->flags_int.bdma_setup = 0;
1804 /* by default we cannot cancel any USB transfer immediately */
1805 xfer->flags_int.can_cancel_immed = 0;
1807 /* clear lengths and frame counts by default */
1812 /* clear any previous errors */
1815 /* Check if the device is still alive */
1816 if (info->udev->state < USB_STATE_POWERED) {
1819 * Must return cancelled error code else
1820 * device drivers can hang.
1822 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1823 USB_BUS_UNLOCK(bus);
1828 if (xfer->nframes == 0) {
1829 if (xfer->flags.stall_pipe) {
1831 * Special case - want to stall without transferring
1834 DPRINTF("xfer=%p nframes=0: stall "
1835 "or clear stall!\n", xfer);
1837 xfer->flags_int.can_cancel_immed = 1;
1838 /* start the transfer */
1839 usb_command_wrapper(&xfer->endpoint->
1840 endpoint_q[xfer->stream_id], xfer);
1841 USB_BUS_UNLOCK(bus);
1845 usbd_transfer_done(xfer, USB_ERR_INVAL);
1846 USB_BUS_UNLOCK(bus);
1849 /* compute some variables */
1851 for (x = 0; x != xfer->nframes; x++) {
1852 /* make a copy of the frlenghts[] */
1853 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1854 /* compute total transfer length */
1855 xfer->sumlen += xfer->frlengths[x];
1856 if (xfer->sumlen < xfer->frlengths[x]) {
1857 /* length wrapped around */
1859 usbd_transfer_done(xfer, USB_ERR_INVAL);
1860 USB_BUS_UNLOCK(bus);
1865 /* clear some internal flags */
1867 xfer->flags_int.short_xfer_ok = 0;
1868 xfer->flags_int.short_frames_ok = 0;
1870 /* check if this is a control transfer */
1872 if (xfer->flags_int.control_xfr) {
1874 if (usbd_setup_ctrl_transfer(xfer)) {
1876 usbd_transfer_done(xfer, USB_ERR_STALLED);
1877 USB_BUS_UNLOCK(bus);
1882 * Setup filtered version of some transfer flags,
1883 * in case of data read direction
1885 if (USB_GET_DATA_ISREAD(xfer)) {
1887 if (xfer->flags.short_frames_ok) {
1888 xfer->flags_int.short_xfer_ok = 1;
1889 xfer->flags_int.short_frames_ok = 1;
1890 } else if (xfer->flags.short_xfer_ok) {
1891 xfer->flags_int.short_xfer_ok = 1;
1893 /* check for control transfer */
1894 if (xfer->flags_int.control_xfr) {
1896 * 1) Control transfers do not support
1897 * reception of multiple short USB
1898 * frames in host mode and device side
1899 * mode, with exception of:
1901 * 2) Due to sometimes buggy device
1902 * side firmware we need to do a
1903 * STATUS stage in case of short
1904 * control transfers in USB host mode.
1905 * The STATUS stage then becomes the
1906 * "alt_next" to the DATA stage.
1908 xfer->flags_int.short_frames_ok = 1;
1913 * Check if BUS-DMA support is enabled and try to load virtual
1914 * buffers into DMA, if any:
1917 if (xfer->flags_int.bdma_enable) {
1918 /* insert the USB transfer last in the BUS-DMA queue */
1919 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1924 * Enter the USB transfer into the Host Controller or
1925 * Device Controller schedule:
1927 usbd_pipe_enter(xfer);
1930 /*------------------------------------------------------------------------*
1931 * usbd_pipe_enter - factored out code
1932 *------------------------------------------------------------------------*/
1934 usbd_pipe_enter(struct usb_xfer *xfer)
1936 struct usb_endpoint *ep;
1938 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1940 USB_BUS_LOCK(xfer->xroot->bus);
1942 ep = xfer->endpoint;
1946 /* the transfer can now be cancelled */
1947 xfer->flags_int.can_cancel_immed = 1;
1949 /* enter the transfer */
1950 (ep->methods->enter) (xfer);
1952 /* check for transfer error */
1954 /* some error has happened */
1955 usbd_transfer_done(xfer, 0);
1956 USB_BUS_UNLOCK(xfer->xroot->bus);
1960 /* start the transfer */
1961 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1962 USB_BUS_UNLOCK(xfer->xroot->bus);
1965 /*------------------------------------------------------------------------*
1966 * usbd_transfer_start - start an USB transfer
1968 * NOTE: Calling this function more than one time will only
1969 * result in a single transfer start, until the USB transfer
1971 *------------------------------------------------------------------------*/
1973 usbd_transfer_start(struct usb_xfer *xfer)
1976 /* transfer is gone */
1979 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1981 /* mark the USB transfer started */
1983 if (!xfer->flags_int.started) {
1984 /* lock the BUS lock to avoid races updating flags_int */
1985 USB_BUS_LOCK(xfer->xroot->bus);
1986 xfer->flags_int.started = 1;
1987 USB_BUS_UNLOCK(xfer->xroot->bus);
1989 /* check if the USB transfer callback is already transferring */
1991 if (xfer->flags_int.transferring) {
1994 USB_BUS_LOCK(xfer->xroot->bus);
1995 /* call the USB transfer callback */
1996 usbd_callback_ss_done_defer(xfer);
1997 USB_BUS_UNLOCK(xfer->xroot->bus);
2000 /*------------------------------------------------------------------------*
2001 * usbd_transfer_stop - stop an USB transfer
2003 * NOTE: Calling this function more than one time will only
2004 * result in a single transfer stop.
2005 * NOTE: When this function returns it is not safe to free nor
2006 * reuse any DMA buffers. See "usbd_transfer_drain()".
2007 *------------------------------------------------------------------------*/
2009 usbd_transfer_stop(struct usb_xfer *xfer)
2011 struct usb_endpoint *ep;
2014 /* transfer is gone */
2017 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2019 /* check if the USB transfer was ever opened */
2021 if (!xfer->flags_int.open) {
2022 if (xfer->flags_int.started) {
2023 /* nothing to do except clearing the "started" flag */
2024 /* lock the BUS lock to avoid races updating flags_int */
2025 USB_BUS_LOCK(xfer->xroot->bus);
2026 xfer->flags_int.started = 0;
2027 USB_BUS_UNLOCK(xfer->xroot->bus);
2031 /* try to stop the current USB transfer */
2033 USB_BUS_LOCK(xfer->xroot->bus);
2034 /* override any previous error */
2035 xfer->error = USB_ERR_CANCELLED;
2038 * Clear "open" and "started" when both private and USB lock
2039 * is locked so that we don't get a race updating "flags_int"
2041 xfer->flags_int.open = 0;
2042 xfer->flags_int.started = 0;
2045 * Check if we can cancel the USB transfer immediately.
2047 if (xfer->flags_int.transferring) {
2048 if (xfer->flags_int.can_cancel_immed &&
2049 (!xfer->flags_int.did_close)) {
2052 * The following will lead to an USB_ERR_CANCELLED
2053 * error code being passed to the USB callback.
2055 (xfer->endpoint->methods->close) (xfer);
2056 /* only close once */
2057 xfer->flags_int.did_close = 1;
2059 /* need to wait for the next done callback */
2064 /* close here and now */
2065 (xfer->endpoint->methods->close) (xfer);
2068 * Any additional DMA delay is done by
2069 * "usbd_transfer_unsetup()".
2073 * Special case. Check if we need to restart a blocked
2076 ep = xfer->endpoint;
2079 * If the current USB transfer is completing we need
2080 * to start the next one:
2082 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2083 usb_command_wrapper(
2084 &ep->endpoint_q[xfer->stream_id], NULL);
2088 USB_BUS_UNLOCK(xfer->xroot->bus);
2091 /*------------------------------------------------------------------------*
2092 * usbd_transfer_pending
2094 * This function will check if an USB transfer is pending which is a
2095 * little bit complicated!
2098 * 1: Pending: The USB transfer will receive a callback in the future.
2099 *------------------------------------------------------------------------*/
2101 usbd_transfer_pending(struct usb_xfer *xfer)
2103 struct usb_xfer_root *info;
2104 struct usb_xfer_queue *pq;
2107 /* transfer is gone */
2110 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2112 if (xfer->flags_int.transferring) {
2116 USB_BUS_LOCK(xfer->xroot->bus);
2117 if (xfer->wait_queue) {
2118 /* we are waiting on a queue somewhere */
2119 USB_BUS_UNLOCK(xfer->xroot->bus);
2125 if (pq->curr == xfer) {
2126 /* we are currently scheduled for callback */
2127 USB_BUS_UNLOCK(xfer->xroot->bus);
2130 /* we are not pending */
2131 USB_BUS_UNLOCK(xfer->xroot->bus);
2135 /*------------------------------------------------------------------------*
2136 * usbd_transfer_drain
2138 * This function will stop the USB transfer and wait for any
2139 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2140 * are loaded into DMA can safely be freed or reused after that this
2141 * function has returned.
2142 *------------------------------------------------------------------------*/
2144 usbd_transfer_drain(struct usb_xfer *xfer)
2146 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2147 "usbd_transfer_drain can sleep!");
2150 /* transfer is gone */
2153 if (xfer->xroot->xfer_mtx != &Giant) {
2154 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
2156 USB_XFER_LOCK(xfer);
2158 usbd_transfer_stop(xfer);
2160 while (usbd_transfer_pending(xfer) ||
2161 xfer->flags_int.doing_callback) {
2164 * It is allowed that the callback can drop its
2165 * transfer mutex. In that case checking only
2166 * "usbd_transfer_pending()" is not enough to tell if
2167 * the USB transfer is fully drained. We also need to
2168 * check the internal "doing_callback" flag.
2170 xfer->flags_int.draining = 1;
2173 * Wait until the current outstanding USB
2174 * transfer is complete !
2176 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
2178 USB_XFER_UNLOCK(xfer);
2181 struct usb_page_cache *
2182 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2184 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2186 return (&xfer->frbuffers[frindex]);
2190 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2192 struct usb_page_search page_info;
2194 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2196 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2197 return (page_info.buffer);
2200 /*------------------------------------------------------------------------*
2201 * usbd_xfer_get_fps_shift
2203 * The following function is only useful for isochronous transfers. It
2204 * returns how many times the frame execution rate has been shifted
2210 *------------------------------------------------------------------------*/
2212 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2214 return (xfer->fps_shift);
2218 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2220 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2222 return (xfer->frlengths[frindex]);
2225 /*------------------------------------------------------------------------*
2226 * usbd_xfer_set_frame_data
2228 * This function sets the pointer of the buffer that should
2229 * loaded directly into DMA for the given USB frame. Passing "ptr"
2230 * equal to NULL while the corresponding "frlength" is greater
2231 * than zero gives undefined results!
2232 *------------------------------------------------------------------------*/
2234 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2235 void *ptr, usb_frlength_t len)
2237 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2239 /* set virtual address to load and length */
2240 xfer->frbuffers[frindex].buffer = ptr;
2241 usbd_xfer_set_frame_len(xfer, frindex, len);
2245 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2246 void **ptr, int *len)
2248 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2251 *ptr = xfer->frbuffers[frindex].buffer;
2253 *len = xfer->frlengths[frindex];
2256 /*------------------------------------------------------------------------*
2257 * usbd_xfer_old_frame_length
2259 * This function returns the framelength of the given frame at the
2260 * time the transfer was submitted. This function can be used to
2261 * compute the starting data pointer of the next isochronous frame
2262 * when an isochronous transfer has completed.
2263 *------------------------------------------------------------------------*/
2265 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2267 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2269 return (xfer->frlengths[frindex + xfer->max_frame_count]);
2273 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2277 *actlen = xfer->actlen;
2279 *sumlen = xfer->sumlen;
2280 if (aframes != NULL)
2281 *aframes = xfer->aframes;
2282 if (nframes != NULL)
2283 *nframes = xfer->nframes;
2286 /*------------------------------------------------------------------------*
2287 * usbd_xfer_set_frame_offset
2289 * This function sets the frame data buffer offset relative to the beginning
2290 * of the USB DMA buffer allocated for this USB transfer.
2291 *------------------------------------------------------------------------*/
2293 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2294 usb_frcount_t frindex)
2296 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2297 "when the USB buffer is external\n"));
2298 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2300 /* set virtual address to load */
2301 xfer->frbuffers[frindex].buffer =
2302 USB_ADD_BYTES(xfer->local_buffer, offset);
2306 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2312 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2318 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2324 usbd_xfer_max_frames(struct usb_xfer *xfer)
2326 return (xfer->max_frame_count);
2330 usbd_xfer_max_len(struct usb_xfer *xfer)
2332 return (xfer->max_data_length);
2336 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2338 return (xfer->max_frame_size);
2342 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2345 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2347 xfer->frlengths[frindex] = len;
2350 /*------------------------------------------------------------------------*
2351 * usb_callback_proc - factored out code
2353 * This function performs USB callbacks.
2354 *------------------------------------------------------------------------*/
2356 usb_callback_proc(struct usb_proc_msg *_pm)
2358 struct usb_done_msg *pm = (void *)_pm;
2359 struct usb_xfer_root *info = pm->xroot;
2361 /* Change locking order */
2362 USB_BUS_UNLOCK(info->bus);
2365 * We exploit the fact that the mutex is the same for all
2366 * callbacks that will be called from this thread:
2368 mtx_lock(info->xfer_mtx);
2369 USB_BUS_LOCK(info->bus);
2371 /* Continue where we lost track */
2372 usb_command_wrapper(&info->done_q,
2375 mtx_unlock(info->xfer_mtx);
2378 /*------------------------------------------------------------------------*
2379 * usbd_callback_ss_done_defer
2381 * This function will defer the start, stop and done callback to the
2383 *------------------------------------------------------------------------*/
2385 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2387 struct usb_xfer_root *info = xfer->xroot;
2388 struct usb_xfer_queue *pq = &info->done_q;
2390 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2392 if (pq->curr != xfer) {
2393 usbd_transfer_enqueue(pq, xfer);
2395 if (!pq->recurse_1) {
2398 * We have to postpone the callback due to the fact we
2399 * will have a Lock Order Reversal, LOR, if we try to
2402 (void) usb_proc_msignal(info->done_p,
2403 &info->done_m[0], &info->done_m[1]);
2405 /* clear second recurse flag */
2412 /*------------------------------------------------------------------------*
2413 * usbd_callback_wrapper
2415 * This is a wrapper for USB callbacks. This wrapper does some
2416 * auto-magic things like figuring out if we can call the callback
2417 * directly from the current context or if we need to wakeup the
2418 * interrupt process.
2419 *------------------------------------------------------------------------*/
2421 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2423 struct usb_xfer *xfer = pq->curr;
2424 struct usb_xfer_root *info = xfer->xroot;
2426 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2427 if ((pq->recurse_3 != 0 || mtx_owned(info->xfer_mtx) == 0) &&
2428 SCHEDULER_STOPPED() == 0) {
2430 * Cases that end up here:
2432 * 5) HW interrupt done callback or other source.
2433 * 6) HW completed transfer during callback
2435 DPRINTFN(3, "case 5 and 6\n");
2438 * We have to postpone the callback due to the fact we
2439 * will have a Lock Order Reversal, LOR, if we try to
2442 * Postponing the callback also ensures that other USB
2443 * transfer queues get a chance.
2445 (void) usb_proc_msignal(info->done_p,
2446 &info->done_m[0], &info->done_m[1]);
2450 * Cases that end up here:
2452 * 1) We are starting a transfer
2453 * 2) We are prematurely calling back a transfer
2454 * 3) We are stopping a transfer
2455 * 4) We are doing an ordinary callback
2457 DPRINTFN(3, "case 1-4\n");
2458 /* get next USB transfer in the queue */
2459 info->done_q.curr = NULL;
2461 /* set flag in case of drain */
2462 xfer->flags_int.doing_callback = 1;
2464 USB_BUS_UNLOCK(info->bus);
2465 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2467 /* set correct USB state for callback */
2468 if (!xfer->flags_int.transferring) {
2469 xfer->usb_state = USB_ST_SETUP;
2470 if (!xfer->flags_int.started) {
2471 /* we got stopped before we even got started */
2472 USB_BUS_LOCK(info->bus);
2477 if (usbd_callback_wrapper_sub(xfer)) {
2478 /* the callback has been deferred */
2479 USB_BUS_LOCK(info->bus);
2483 /* decrement power reference */
2484 usbd_transfer_power_ref(xfer, -1);
2486 xfer->flags_int.transferring = 0;
2489 xfer->usb_state = USB_ST_ERROR;
2491 /* set transferred state */
2492 xfer->usb_state = USB_ST_TRANSFERRED;
2494 /* sync DMA memory, if any */
2495 if (xfer->flags_int.bdma_enable &&
2496 (!xfer->flags_int.bdma_no_post_sync)) {
2497 usb_bdma_post_sync(xfer);
2504 if (xfer->usb_state != USB_ST_SETUP) {
2505 USB_BUS_LOCK(info->bus);
2506 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2507 USB_BUS_UNLOCK(info->bus);
2510 /* call processing routine */
2511 (xfer->callback) (xfer, xfer->error);
2513 /* pickup the USB mutex again */
2514 USB_BUS_LOCK(info->bus);
2517 * Check if we got started after that we got cancelled, but
2518 * before we managed to do the callback.
2520 if ((!xfer->flags_int.open) &&
2521 (xfer->flags_int.started) &&
2522 (xfer->usb_state == USB_ST_ERROR)) {
2523 /* clear flag in case of drain */
2524 xfer->flags_int.doing_callback = 0;
2525 /* try to loop, but not recursivly */
2526 usb_command_wrapper(&info->done_q, xfer);
2531 /* clear flag in case of drain */
2532 xfer->flags_int.doing_callback = 0;
2535 * Check if we are draining.
2537 if (xfer->flags_int.draining &&
2538 (!xfer->flags_int.transferring)) {
2539 /* "usbd_transfer_drain()" is waiting for end of transfer */
2540 xfer->flags_int.draining = 0;
2541 cv_broadcast(&info->cv_drain);
2544 /* do the next callback, if any */
2545 usb_command_wrapper(&info->done_q,
2549 /*------------------------------------------------------------------------*
2550 * usb_dma_delay_done_cb
2552 * This function is called when the DMA delay has been exectuded, and
2553 * will make sure that the callback is called to complete the USB
2554 * transfer. This code path is ususally only used when there is an USB
2555 * error like USB_ERR_CANCELLED.
2556 *------------------------------------------------------------------------*/
2558 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2560 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2562 DPRINTFN(3, "Completed %p\n", xfer);
2564 /* queue callback for execution, again */
2565 usbd_transfer_done(xfer, 0);
2568 /*------------------------------------------------------------------------*
2569 * usbd_transfer_dequeue
2571 * - This function is used to remove an USB transfer from a USB
2574 * - This function can be called multiple times in a row.
2575 *------------------------------------------------------------------------*/
2577 usbd_transfer_dequeue(struct usb_xfer *xfer)
2579 struct usb_xfer_queue *pq;
2581 pq = xfer->wait_queue;
2583 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2584 xfer->wait_queue = NULL;
2588 /*------------------------------------------------------------------------*
2589 * usbd_transfer_enqueue
2591 * - This function is used to insert an USB transfer into a USB *
2594 * - This function can be called multiple times in a row.
2595 *------------------------------------------------------------------------*/
2597 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2600 * Insert the USB transfer into the queue, if it is not
2601 * already on a USB transfer queue:
2603 if (xfer->wait_queue == NULL) {
2604 xfer->wait_queue = pq;
2605 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2609 /*------------------------------------------------------------------------*
2610 * usbd_transfer_done
2612 * - This function is used to remove an USB transfer from the busdma,
2613 * pipe or interrupt queue.
2615 * - This function is used to queue the USB transfer on the done
2618 * - This function is used to stop any USB transfer timeouts.
2619 *------------------------------------------------------------------------*/
2621 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2623 struct usb_xfer_root *info = xfer->xroot;
2625 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2627 DPRINTF("err=%s\n", usbd_errstr(error));
2630 * If we are not transferring then just return.
2631 * This can happen during transfer cancel.
2633 if (!xfer->flags_int.transferring) {
2634 DPRINTF("not transferring\n");
2635 /* end of control transfer, if any */
2636 xfer->flags_int.control_act = 0;
2639 /* only set transfer error, if not already set */
2640 if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2641 xfer->error = error;
2643 /* stop any callouts */
2644 usb_callout_stop(&xfer->timeout_handle);
2647 * If we are waiting on a queue, just remove the USB transfer
2648 * from the queue, if any. We should have the required locks
2649 * locked to do the remove when this function is called.
2651 usbd_transfer_dequeue(xfer);
2654 if (mtx_owned(info->xfer_mtx)) {
2655 struct usb_xfer_queue *pq;
2658 * If the private USB lock is not locked, then we assume
2659 * that the BUS-DMA load stage has been passed:
2663 if (pq->curr == xfer) {
2664 /* start the next BUS-DMA load, if any */
2665 usb_command_wrapper(pq, NULL);
2669 /* keep some statistics */
2670 if (xfer->error == USB_ERR_CANCELLED) {
2671 info->udev->stats_cancelled.uds_requests
2672 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2673 } else if (xfer->error != USB_ERR_NORMAL_COMPLETION) {
2674 info->udev->stats_err.uds_requests
2675 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2677 info->udev->stats_ok.uds_requests
2678 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2681 /* call the USB transfer callback */
2682 usbd_callback_ss_done_defer(xfer);
2685 /*------------------------------------------------------------------------*
2686 * usbd_transfer_start_cb
2688 * This function is called to start the USB transfer when
2689 * "xfer->interval" is greater than zero, and and the endpoint type is
2691 *------------------------------------------------------------------------*/
2693 usbd_transfer_start_cb(void *arg)
2695 struct usb_xfer *xfer = arg;
2696 struct usb_endpoint *ep = xfer->endpoint;
2698 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2703 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2706 /* the transfer can now be cancelled */
2707 xfer->flags_int.can_cancel_immed = 1;
2709 /* start USB transfer, if no error */
2710 if (xfer->error == 0)
2711 (ep->methods->start) (xfer);
2713 /* check for transfer error */
2715 /* some error has happened */
2716 usbd_transfer_done(xfer, 0);
2720 /*------------------------------------------------------------------------*
2721 * usbd_xfer_set_stall
2723 * This function is used to set the stall flag outside the
2724 * callback. This function is NULL safe.
2725 *------------------------------------------------------------------------*/
2727 usbd_xfer_set_stall(struct usb_xfer *xfer)
2733 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2735 /* avoid any races by locking the USB mutex */
2736 USB_BUS_LOCK(xfer->xroot->bus);
2737 xfer->flags.stall_pipe = 1;
2738 USB_BUS_UNLOCK(xfer->xroot->bus);
2742 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2744 return (xfer->endpoint->is_stalled);
2747 /*------------------------------------------------------------------------*
2748 * usbd_transfer_clear_stall
2750 * This function is used to clear the stall flag outside the
2751 * callback. This function is NULL safe.
2752 *------------------------------------------------------------------------*/
2754 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2760 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2762 /* avoid any races by locking the USB mutex */
2763 USB_BUS_LOCK(xfer->xroot->bus);
2765 xfer->flags.stall_pipe = 0;
2767 USB_BUS_UNLOCK(xfer->xroot->bus);
2770 /*------------------------------------------------------------------------*
2773 * This function is used to add an USB transfer to the pipe transfer list.
2774 *------------------------------------------------------------------------*/
2776 usbd_pipe_start(struct usb_xfer_queue *pq)
2778 struct usb_endpoint *ep;
2779 struct usb_xfer *xfer;
2783 ep = xfer->endpoint;
2785 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2788 * If the endpoint is already stalled we do nothing !
2790 if (ep->is_stalled) {
2794 * Check if we are supposed to stall the endpoint:
2796 if (xfer->flags.stall_pipe) {
2797 struct usb_device *udev;
2798 struct usb_xfer_root *info;
2800 /* clear stall command */
2801 xfer->flags.stall_pipe = 0;
2803 /* get pointer to USB device */
2808 * Only stall BULK and INTERRUPT endpoints.
2810 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2811 if ((type == UE_BULK) ||
2812 (type == UE_INTERRUPT)) {
2817 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2818 (udev->bus->methods->set_stall) (
2819 udev, ep, &did_stall);
2820 } else if (udev->ctrl_xfer[1]) {
2821 info = udev->ctrl_xfer[1]->xroot;
2823 USB_BUS_CS_PROC(info->bus),
2824 &udev->cs_msg[0], &udev->cs_msg[1]);
2826 /* should not happen */
2827 DPRINTFN(0, "No stall handler\n");
2830 * Check if we should stall. Some USB hardware
2831 * handles set- and clear-stall in hardware.
2835 * The transfer will be continued when
2836 * the clear-stall control endpoint
2837 * message is received.
2842 } else if (type == UE_ISOCHRONOUS) {
2845 * Make sure any FIFO overflow or other FIFO
2846 * error conditions go away by resetting the
2847 * endpoint FIFO through the clear stall
2850 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2851 (udev->bus->methods->clear_stall) (udev, ep);
2855 /* Set or clear stall complete - special case */
2856 if (xfer->nframes == 0) {
2857 /* we are complete */
2859 usbd_transfer_done(xfer, 0);
2865 * 1) Start the first transfer queued.
2867 * 2) Re-start the current USB transfer.
2870 * Check if there should be any
2871 * pre transfer start delay:
2873 if (xfer->interval > 0) {
2874 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2875 if ((type == UE_BULK) ||
2876 (type == UE_CONTROL)) {
2877 usbd_transfer_timeout_ms(xfer,
2878 &usbd_transfer_start_cb,
2886 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2888 /* the transfer can now be cancelled */
2889 xfer->flags_int.can_cancel_immed = 1;
2891 /* start USB transfer, if no error */
2892 if (xfer->error == 0)
2893 (ep->methods->start) (xfer);
2895 /* check for transfer error */
2897 /* some error has happened */
2898 usbd_transfer_done(xfer, 0);
2902 /*------------------------------------------------------------------------*
2903 * usbd_transfer_timeout_ms
2905 * This function is used to setup a timeout on the given USB
2906 * transfer. If the timeout has been deferred the callback given by
2907 * "cb" will get called after "ms" milliseconds.
2908 *------------------------------------------------------------------------*/
2910 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2911 void (*cb) (void *arg), usb_timeout_t ms)
2913 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2916 usb_callout_reset(&xfer->timeout_handle,
2917 USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2920 /*------------------------------------------------------------------------*
2921 * usbd_callback_wrapper_sub
2923 * - This function will update variables in an USB transfer after
2924 * that the USB transfer is complete.
2926 * - This function is used to start the next USB transfer on the
2927 * ep transfer queue, if any.
2929 * NOTE: In some special cases the USB transfer will not be removed from
2930 * the pipe queue, but remain first. To enforce USB transfer removal call
2931 * this function passing the error code "USB_ERR_CANCELLED".
2935 * Else: The callback has been deferred.
2936 *------------------------------------------------------------------------*/
2938 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2940 struct usb_endpoint *ep;
2941 struct usb_bus *bus;
2944 bus = xfer->xroot->bus;
2946 if ((!xfer->flags_int.open) &&
2947 (!xfer->flags_int.did_close)) {
2950 (xfer->endpoint->methods->close) (xfer);
2951 USB_BUS_UNLOCK(bus);
2952 /* only close once */
2953 xfer->flags_int.did_close = 1;
2954 return (1); /* wait for new callback */
2957 * If we have a non-hardware induced error we
2958 * need to do the DMA delay!
2960 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2961 (xfer->error == USB_ERR_CANCELLED ||
2962 xfer->error == USB_ERR_TIMEOUT ||
2963 bus->methods->start_dma_delay != NULL)) {
2967 /* only delay once */
2968 xfer->flags_int.did_dma_delay = 1;
2970 /* we can not cancel this delay */
2971 xfer->flags_int.can_cancel_immed = 0;
2973 temp = usbd_get_dma_delay(xfer->xroot->udev);
2975 DPRINTFN(3, "DMA delay, %u ms, "
2976 "on %p\n", temp, xfer);
2981 * Some hardware solutions have dedicated
2982 * events when it is safe to free DMA'ed
2983 * memory. For the other hardware platforms we
2984 * use a static delay.
2986 if (bus->methods->start_dma_delay != NULL) {
2987 (bus->methods->start_dma_delay) (xfer);
2989 usbd_transfer_timeout_ms(xfer,
2990 (void (*)(void *))&usb_dma_delay_done_cb,
2993 USB_BUS_UNLOCK(bus);
2994 return (1); /* wait for new callback */
2997 /* check actual number of frames */
2998 if (xfer->aframes > xfer->nframes) {
2999 if (xfer->error == 0) {
3000 panic("%s: actual number of frames, %d, is "
3001 "greater than initial number of frames, %d\n",
3002 __FUNCTION__, xfer->aframes, xfer->nframes);
3004 /* just set some valid value */
3005 xfer->aframes = xfer->nframes;
3008 /* compute actual length */
3011 for (x = 0; x != xfer->aframes; x++) {
3012 xfer->actlen += xfer->frlengths[x];
3016 * Frames that were not transferred get zero actual length in
3017 * case the USB device driver does not check the actual number
3018 * of frames transferred, "xfer->aframes":
3020 for (; x < xfer->nframes; x++) {
3021 usbd_xfer_set_frame_len(xfer, x, 0);
3024 /* check actual length */
3025 if (xfer->actlen > xfer->sumlen) {
3026 if (xfer->error == 0) {
3027 panic("%s: actual length, %d, is greater than "
3028 "initial length, %d\n",
3029 __FUNCTION__, xfer->actlen, xfer->sumlen);
3031 /* just set some valid value */
3032 xfer->actlen = xfer->sumlen;
3035 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
3036 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
3037 xfer->aframes, xfer->nframes);
3040 /* end of control transfer, if any */
3041 xfer->flags_int.control_act = 0;
3043 #if USB_HAVE_TT_SUPPORT
3044 switch (xfer->error) {
3045 case USB_ERR_NORMAL_COMPLETION:
3046 case USB_ERR_SHORT_XFER:
3047 case USB_ERR_STALLED:
3048 case USB_ERR_CANCELLED:
3052 /* try to reset the TT, if any */
3054 uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint);
3055 USB_BUS_UNLOCK(bus);
3059 /* check if we should block the execution queue */
3060 if ((xfer->error != USB_ERR_CANCELLED) &&
3061 (xfer->flags.pipe_bof)) {
3062 DPRINTFN(2, "xfer=%p: Block On Failure "
3063 "on endpoint=%p\n", xfer, xfer->endpoint);
3067 /* check for short transfers */
3068 if (xfer->actlen < xfer->sumlen) {
3070 /* end of control transfer, if any */
3071 xfer->flags_int.control_act = 0;
3073 if (!xfer->flags_int.short_xfer_ok) {
3074 xfer->error = USB_ERR_SHORT_XFER;
3075 if (xfer->flags.pipe_bof) {
3076 DPRINTFN(2, "xfer=%p: Block On Failure on "
3077 "Short Transfer on endpoint %p.\n",
3078 xfer, xfer->endpoint);
3084 * Check if we are in the middle of a
3087 if (xfer->flags_int.control_act) {
3088 DPRINTFN(5, "xfer=%p: Control transfer "
3089 "active on endpoint=%p\n", xfer, xfer->endpoint);
3095 ep = xfer->endpoint;
3098 * If the current USB transfer is completing we need to start the
3102 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
3103 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
3105 if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
3106 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
3107 /* there is another USB transfer waiting */
3109 /* this is the last USB transfer */
3110 /* clear isochronous sync flag */
3111 xfer->endpoint->is_synced = 0;
3114 USB_BUS_UNLOCK(bus);
3119 /*------------------------------------------------------------------------*
3120 * usb_command_wrapper
3122 * This function is used to execute commands non-recursivly on an USB
3124 *------------------------------------------------------------------------*/
3126 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3130 * If the transfer is not already processing,
3133 if (pq->curr != xfer) {
3134 usbd_transfer_enqueue(pq, xfer);
3135 if (pq->curr != NULL) {
3136 /* something is already processing */
3137 DPRINTFN(6, "busy %p\n", pq->curr);
3142 /* Get next element in queue */
3146 if (!pq->recurse_1) {
3148 /* clear third recurse flag */
3152 /* set two first recurse flags */
3156 if (pq->curr == NULL) {
3157 xfer = TAILQ_FIRST(&pq->head);
3159 TAILQ_REMOVE(&pq->head, xfer,
3161 xfer->wait_queue = NULL;
3167 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3169 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3172 * Set third recurse flag to indicate
3173 * recursion happened:
3177 } while (!pq->recurse_2);
3179 /* clear first recurse flag */
3183 /* clear second recurse flag */
3188 /*------------------------------------------------------------------------*
3189 * usbd_ctrl_transfer_setup
3191 * This function is used to setup the default USB control endpoint
3193 *------------------------------------------------------------------------*/
3195 usbd_ctrl_transfer_setup(struct usb_device *udev)
3197 struct usb_xfer *xfer;
3199 uint8_t iface_index;
3201 /* check for root HUB */
3202 if (udev->parent_hub == NULL)
3206 xfer = udev->ctrl_xfer[0];
3208 USB_XFER_LOCK(xfer);
3210 ((xfer->address == udev->address) &&
3211 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3212 udev->ddesc.bMaxPacketSize));
3213 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3216 * NOTE: checking "xfer->address" and
3217 * starting the USB transfer must be
3220 usbd_transfer_start(xfer);
3223 USB_XFER_UNLOCK(xfer);
3230 * All parameters are exactly the same like before.
3236 * Update wMaxPacketSize for the default control endpoint:
3238 udev->ctrl_ep_desc.wMaxPacketSize[0] =
3239 udev->ddesc.bMaxPacketSize;
3242 * Unsetup any existing USB transfer:
3244 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3247 * Reset clear stall error counter.
3249 udev->clear_stall_errors = 0;
3252 * Try to setup a new USB transfer for the
3253 * default control endpoint:
3256 if (usbd_transfer_setup(udev, &iface_index,
3257 udev->ctrl_xfer, udev->bus->control_ep_quirk ?
3258 usb_control_ep_quirk_cfg : usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3259 &udev->device_mtx)) {
3260 DPRINTFN(0, "could not setup default "
3267 /*------------------------------------------------------------------------*
3268 * usbd_clear_data_toggle - factored out code
3270 * NOTE: the intention of this function is not to reset the hardware
3272 *------------------------------------------------------------------------*/
3274 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3276 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3278 /* check that we have a valid case */
3279 if (udev->flags.usb_mode == USB_MODE_HOST &&
3280 udev->parent_hub != NULL &&
3281 udev->bus->methods->clear_stall != NULL &&
3282 ep->methods != NULL) {
3283 (udev->bus->methods->clear_stall) (udev, ep);
3287 /*------------------------------------------------------------------------*
3288 * usbd_clear_data_toggle - factored out code
3290 * NOTE: the intention of this function is not to reset the hardware
3291 * data toggle on the USB device side.
3292 *------------------------------------------------------------------------*/
3294 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3296 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3298 USB_BUS_LOCK(udev->bus);
3299 ep->toggle_next = 0;
3300 /* some hardware needs a callback to clear the data toggle */
3301 usbd_clear_stall_locked(udev, ep);
3302 USB_BUS_UNLOCK(udev->bus);
3305 /*------------------------------------------------------------------------*
3306 * usbd_clear_stall_callback - factored out clear stall callback
3309 * xfer1: Clear Stall Control Transfer
3310 * xfer2: Stalled USB Transfer
3312 * This function is NULL safe.
3318 * Clear stall config example:
3320 * static const struct usb_config my_clearstall = {
3321 * .type = UE_CONTROL,
3323 * .direction = UE_DIR_ANY,
3324 * .interval = 50, //50 milliseconds
3325 * .bufsize = sizeof(struct usb_device_request),
3326 * .timeout = 1000, //1.000 seconds
3327 * .callback = &my_clear_stall_callback, // **
3328 * .usb_mode = USB_MODE_HOST,
3331 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3332 * passing the correct parameters.
3333 *------------------------------------------------------------------------*/
3335 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3336 struct usb_xfer *xfer2)
3338 struct usb_device_request req;
3340 if (xfer2 == NULL) {
3341 /* looks like we are tearing down */
3342 DPRINTF("NULL input parameter\n");
3345 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3346 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3348 switch (USB_GET_STATE(xfer1)) {
3352 * pre-clear the data toggle to DATA0 ("umass.c" and
3353 * "ata-usb.c" depends on this)
3356 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3358 /* setup a clear-stall packet */
3360 req.bmRequestType = UT_WRITE_ENDPOINT;
3361 req.bRequest = UR_CLEAR_FEATURE;
3362 USETW(req.wValue, UF_ENDPOINT_HALT);
3363 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3365 USETW(req.wLength, 0);
3368 * "usbd_transfer_setup_sub()" will ensure that
3369 * we have sufficient room in the buffer for
3370 * the request structure!
3373 /* copy in the transfer */
3375 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3378 xfer1->frlengths[0] = sizeof(req);
3381 usbd_transfer_submit(xfer1);
3384 case USB_ST_TRANSFERRED:
3387 default: /* Error */
3388 if (xfer1->error == USB_ERR_CANCELLED) {
3393 return (1); /* Clear Stall Finished */
3396 /*------------------------------------------------------------------------*
3397 * usbd_transfer_poll
3399 * The following function gets called from the USB keyboard driver and
3400 * UMASS when the system has paniced.
3402 * NOTE: It is currently not possible to resume normal operation on
3403 * the USB controller which has been polled, due to clearing of the
3404 * "up_dsleep" and "up_msleep" flags.
3405 *------------------------------------------------------------------------*/
3407 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3409 struct usb_xfer *xfer;
3410 struct usb_xfer_root *xroot;
3411 struct usb_device *udev;
3412 struct usb_proc_msg *pm;
3417 for (n = 0; n != max; n++) {
3418 /* Extra checks to avoid panic */
3421 continue; /* no USB transfer */
3422 xroot = xfer->xroot;
3424 continue; /* no USB root */
3427 continue; /* no USB device */
3428 if (udev->bus == NULL)
3429 continue; /* no BUS structure */
3430 if (udev->bus->methods == NULL)
3431 continue; /* no BUS methods */
3432 if (udev->bus->methods->xfer_poll == NULL)
3433 continue; /* no poll method */
3435 /* make sure that the BUS mutex is not locked */
3437 while (mtx_owned(&xroot->udev->bus->bus_mtx) && !SCHEDULER_STOPPED()) {
3438 mtx_unlock(&xroot->udev->bus->bus_mtx);
3442 /* make sure that the transfer mutex is not locked */
3444 while (mtx_owned(xroot->xfer_mtx) && !SCHEDULER_STOPPED()) {
3445 mtx_unlock(xroot->xfer_mtx);
3449 /* Make sure cv_signal() and cv_broadcast() is not called */
3450 USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3451 USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3452 USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3453 USB_BUS_NON_GIANT_ISOC_PROC(udev->bus)->up_msleep = 0;
3454 USB_BUS_NON_GIANT_BULK_PROC(udev->bus)->up_msleep = 0;
3456 /* poll USB hardware */
3457 (udev->bus->methods->xfer_poll) (udev->bus);
3459 USB_BUS_LOCK(xroot->bus);
3461 /* check for clear stall */
3462 if (udev->ctrl_xfer[1] != NULL) {
3464 /* poll clear stall start */
3465 pm = &udev->cs_msg[0].hdr;
3466 (pm->pm_callback) (pm);
3467 /* poll clear stall done thread */
3468 pm = &udev->ctrl_xfer[1]->
3469 xroot->done_m[0].hdr;
3470 (pm->pm_callback) (pm);
3473 /* poll done thread */
3474 pm = &xroot->done_m[0].hdr;
3475 (pm->pm_callback) (pm);
3477 USB_BUS_UNLOCK(xroot->bus);
3479 /* restore transfer mutex */
3481 mtx_lock(xroot->xfer_mtx);
3483 /* restore BUS mutex */
3485 mtx_lock(&xroot->udev->bus->bus_mtx);
3490 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3491 uint8_t type, enum usb_dev_speed speed)
3493 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3494 [USB_SPEED_LOW] = 8,
3495 [USB_SPEED_FULL] = 64,
3496 [USB_SPEED_HIGH] = 1024,
3497 [USB_SPEED_VARIABLE] = 1024,
3498 [USB_SPEED_SUPER] = 1024,
3501 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3502 [USB_SPEED_LOW] = 0, /* invalid */
3503 [USB_SPEED_FULL] = 1023,
3504 [USB_SPEED_HIGH] = 1024,
3505 [USB_SPEED_VARIABLE] = 3584,
3506 [USB_SPEED_SUPER] = 1024,
3509 static const uint16_t control_min[USB_SPEED_MAX] = {
3510 [USB_SPEED_LOW] = 8,
3511 [USB_SPEED_FULL] = 8,
3512 [USB_SPEED_HIGH] = 64,
3513 [USB_SPEED_VARIABLE] = 512,
3514 [USB_SPEED_SUPER] = 512,
3517 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3518 [USB_SPEED_LOW] = 8,
3519 [USB_SPEED_FULL] = 8,
3520 [USB_SPEED_HIGH] = 512,
3521 [USB_SPEED_VARIABLE] = 512,
3522 [USB_SPEED_SUPER] = 1024,
3527 memset(ptr, 0, sizeof(*ptr));
3531 ptr->range.max = intr_range_max[speed];
3533 case UE_ISOCHRONOUS:
3534 ptr->range.max = isoc_range_max[speed];
3537 if (type == UE_BULK)
3538 temp = bulk_min[speed];
3539 else /* UE_CONTROL */
3540 temp = control_min[speed];
3542 /* default is fixed */
3543 ptr->fixed[0] = temp;
3544 ptr->fixed[1] = temp;
3545 ptr->fixed[2] = temp;
3546 ptr->fixed[3] = temp;
3548 if (speed == USB_SPEED_FULL) {
3549 /* multiple sizes */
3554 if ((speed == USB_SPEED_VARIABLE) &&
3555 (type == UE_BULK)) {
3556 /* multiple sizes */
3557 ptr->fixed[2] = 1024;
3558 ptr->fixed[3] = 1536;
3565 usbd_xfer_softc(struct usb_xfer *xfer)
3567 return (xfer->priv_sc);
3571 usbd_xfer_get_priv(struct usb_xfer *xfer)
3573 return (xfer->priv_fifo);
3577 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3579 xfer->priv_fifo = ptr;
3583 usbd_xfer_state(struct usb_xfer *xfer)
3585 return (xfer->usb_state);
3589 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3592 case USB_FORCE_SHORT_XFER:
3593 xfer->flags.force_short_xfer = 1;
3595 case USB_SHORT_XFER_OK:
3596 xfer->flags.short_xfer_ok = 1;
3598 case USB_MULTI_SHORT_OK:
3599 xfer->flags.short_frames_ok = 1;
3601 case USB_MANUAL_STATUS:
3602 xfer->flags.manual_status = 1;
3608 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3611 case USB_FORCE_SHORT_XFER:
3612 xfer->flags.force_short_xfer = 0;
3614 case USB_SHORT_XFER_OK:
3615 xfer->flags.short_xfer_ok = 0;
3617 case USB_MULTI_SHORT_OK:
3618 xfer->flags.short_frames_ok = 0;
3620 case USB_MANUAL_STATUS:
3621 xfer->flags.manual_status = 0;
3627 * The following function returns in milliseconds when the isochronous
3628 * transfer was completed by the hardware. The returned value wraps
3629 * around 65536 milliseconds.
3632 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3634 return (xfer->isoc_time_complete);
3638 * The following function returns non-zero if the max packet size
3639 * field was clamped to a valid value. Else it returns zero.
3642 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3644 return (xfer->flags_int.maxp_was_clamped);