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 /* function prototypes */
110 static void usbd_update_max_frame_size(struct usb_xfer *);
111 static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
112 static void usbd_control_transfer_init(struct usb_xfer *);
113 static int usbd_setup_ctrl_transfer(struct usb_xfer *);
114 static void usb_callback_proc(struct usb_proc_msg *);
115 static void usbd_callback_ss_done_defer(struct usb_xfer *);
116 static void usbd_callback_wrapper(struct usb_xfer_queue *);
117 static void usbd_transfer_start_cb(void *);
118 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
119 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
120 uint8_t type, enum usb_dev_speed speed);
122 /*------------------------------------------------------------------------*
123 * usb_request_callback
124 *------------------------------------------------------------------------*/
126 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
128 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
129 usb_handle_request_callback(xfer, error);
131 usbd_do_request_callback(xfer, error);
134 /*------------------------------------------------------------------------*
135 * usbd_update_max_frame_size
137 * This function updates the maximum frame size, hence high speed USB
138 * can transfer multiple consecutive packets.
139 *------------------------------------------------------------------------*/
141 usbd_update_max_frame_size(struct usb_xfer *xfer)
143 /* compute maximum frame size */
144 /* this computation should not overflow 16-bit */
145 /* max = 15 * 1024 */
147 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
150 /*------------------------------------------------------------------------*
153 * The following function is called when we need to
154 * synchronize with DMA hardware.
157 * 0: no DMA delay required
158 * Else: milliseconds of DMA delay
159 *------------------------------------------------------------------------*/
161 usbd_get_dma_delay(struct usb_device *udev)
163 const struct usb_bus_methods *mtod;
166 mtod = udev->bus->methods;
169 if (mtod->get_dma_delay) {
170 (mtod->get_dma_delay) (udev, &temp);
172 * Round up and convert to milliseconds. Note that we use
173 * 1024 milliseconds per second. to save a division.
181 /*------------------------------------------------------------------------*
182 * usbd_transfer_setup_sub_malloc
184 * This function will allocate one or more DMA'able memory chunks
185 * according to "size", "align" and "count" arguments. "ppc" is
186 * pointed to a linear array of USB page caches afterwards.
188 * If the "align" argument is equal to "1" a non-contiguous allocation
189 * can happen. Else if the "align" argument is greater than "1", the
190 * allocation will always be contiguous in memory.
195 *------------------------------------------------------------------------*/
198 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
199 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
202 struct usb_page_cache *pc;
213 USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
215 USB_ASSERT(size > 0, ("Invalid size = 0\n"));
218 return (0); /* nothing to allocate */
221 * Make sure that the size is aligned properly.
223 size = -((-size) & (-align));
226 * Try multi-allocation chunks to reduce the number of DMA
227 * allocations, hence DMA allocations are slow.
230 /* special case - non-cached multi page DMA memory */
232 n_dma_pg = (2 + (size / USB_PAGE_SIZE));
234 } else if (size >= USB_PAGE_SIZE) {
239 /* compute number of objects per page */
240 #ifdef USB_DMA_SINGLE_ALLOC
243 n_obj = (USB_PAGE_SIZE / size);
246 * Compute number of DMA chunks, rounded up
249 n_dma_pc = ((count + n_obj - 1) / n_obj);
254 * DMA memory is allocated once, but mapped twice. That's why
255 * there is one list for auto-free and another list for
256 * non-auto-free which only holds the mapping and not the
259 if (parm->buf == NULL) {
260 /* reserve memory (auto-free) */
261 parm->dma_page_ptr += n_dma_pc * n_dma_pg;
262 parm->dma_page_cache_ptr += n_dma_pc;
264 /* reserve memory (no-auto-free) */
265 parm->dma_page_ptr += count * n_dma_pg;
266 parm->xfer_page_cache_ptr += count;
269 for (x = 0; x != n_dma_pc; x++) {
270 /* need to initialize the page cache */
271 parm->dma_page_cache_ptr[x].tag_parent =
272 &parm->curr_xfer->xroot->dma_parent_tag;
274 for (x = 0; x != count; x++) {
275 /* need to initialize the page cache */
276 parm->xfer_page_cache_ptr[x].tag_parent =
277 &parm->curr_xfer->xroot->dma_parent_tag;
282 *ppc = parm->xfer_page_cache_ptr;
284 *ppc = parm->dma_page_cache_ptr;
286 r = count; /* set remainder count */
287 z = n_obj * size; /* set allocation size */
288 pc = parm->xfer_page_cache_ptr;
289 pg = parm->dma_page_ptr;
293 * Avoid mapping memory twice if only a single object
294 * should be allocated per page cache:
296 for (x = 0; x != n_dma_pc; x++) {
297 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
299 return (1); /* failure */
301 /* Make room for one DMA page cache and "n_dma_pg" pages */
302 parm->dma_page_cache_ptr++;
306 for (x = 0; x != n_dma_pc; x++) {
309 /* compute last remainder */
313 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
315 return (1); /* failure */
317 /* Set beginning of current buffer */
318 buf = parm->dma_page_cache_ptr->buffer;
319 /* Make room for one DMA page cache and "n_dma_pg" pages */
320 parm->dma_page_cache_ptr++;
323 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
325 /* Load sub-chunk into DMA */
326 if (usb_pc_dmamap_create(pc, size)) {
327 return (1); /* failure */
329 pc->buffer = USB_ADD_BYTES(buf, y * size);
332 mtx_lock(pc->tag_parent->mtx);
333 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
334 mtx_unlock(pc->tag_parent->mtx);
335 return (1); /* failure */
337 mtx_unlock(pc->tag_parent->mtx);
342 parm->xfer_page_cache_ptr = pc;
343 parm->dma_page_ptr = pg;
348 /*------------------------------------------------------------------------*
349 * usbd_transfer_setup_sub - transfer setup subroutine
351 * This function must be called from the "xfer_setup" callback of the
352 * USB Host or Device controller driver when setting up an USB
353 * transfer. This function will setup correct packet sizes, buffer
354 * sizes, flags and more, that are stored in the "usb_xfer"
356 *------------------------------------------------------------------------*/
358 usbd_transfer_setup_sub(struct usb_setup_params *parm)
364 struct usb_xfer *xfer = parm->curr_xfer;
365 const struct usb_config *setup = parm->curr_setup;
366 struct usb_endpoint_ss_comp_descriptor *ecomp;
367 struct usb_endpoint_descriptor *edesc;
368 struct usb_std_packet_size std_size;
369 usb_frcount_t n_frlengths;
370 usb_frcount_t n_frbuffers;
377 * Sanity check. The following parameters must be initialized before
378 * calling this function.
380 if ((parm->hc_max_packet_size == 0) ||
381 (parm->hc_max_packet_count == 0) ||
382 (parm->hc_max_frame_size == 0)) {
383 parm->err = USB_ERR_INVAL;
386 edesc = xfer->endpoint->edesc;
387 ecomp = xfer->endpoint->ecomp;
389 type = (edesc->bmAttributes & UE_XFERTYPE);
391 xfer->flags = setup->flags;
392 xfer->nframes = setup->frames;
393 xfer->timeout = setup->timeout;
394 xfer->callback = setup->callback;
395 xfer->interval = setup->interval;
396 xfer->endpointno = edesc->bEndpointAddress;
397 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
398 xfer->max_packet_count = 1;
399 /* make a shadow copy: */
400 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
402 parm->bufsize = setup->bufsize;
404 switch (parm->speed) {
409 xfer->max_packet_count +=
410 (xfer->max_packet_size >> 11) & 3;
412 /* check for invalid max packet count */
413 if (xfer->max_packet_count > 3)
414 xfer->max_packet_count = 3;
419 xfer->max_packet_size &= 0x7FF;
421 case USB_SPEED_SUPER:
422 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
425 xfer->max_packet_count += ecomp->bMaxBurst;
427 if ((xfer->max_packet_count == 0) ||
428 (xfer->max_packet_count > 16))
429 xfer->max_packet_count = 16;
433 xfer->max_packet_count = 1;
439 mult = UE_GET_SS_ISO_MULT(
440 ecomp->bmAttributes) + 1;
444 xfer->max_packet_count *= mult;
450 xfer->max_packet_size &= 0x7FF;
455 /* range check "max_packet_count" */
457 if (xfer->max_packet_count > parm->hc_max_packet_count) {
458 xfer->max_packet_count = parm->hc_max_packet_count;
461 /* store max packet size value before filtering */
463 maxp_old = xfer->max_packet_size;
465 /* filter "wMaxPacketSize" according to HC capabilities */
467 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
468 (xfer->max_packet_size == 0)) {
469 xfer->max_packet_size = parm->hc_max_packet_size;
471 /* filter "wMaxPacketSize" according to standard sizes */
473 usbd_get_std_packet_size(&std_size, type, parm->speed);
475 if (std_size.range.min || std_size.range.max) {
477 if (xfer->max_packet_size < std_size.range.min) {
478 xfer->max_packet_size = std_size.range.min;
480 if (xfer->max_packet_size > std_size.range.max) {
481 xfer->max_packet_size = std_size.range.max;
485 if (xfer->max_packet_size >= std_size.fixed[3]) {
486 xfer->max_packet_size = std_size.fixed[3];
487 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
488 xfer->max_packet_size = std_size.fixed[2];
489 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
490 xfer->max_packet_size = std_size.fixed[1];
492 /* only one possibility left */
493 xfer->max_packet_size = std_size.fixed[0];
498 * Check if the max packet size was outside its allowed range
499 * and clamped to a valid value:
501 if (maxp_old != xfer->max_packet_size)
502 xfer->flags_int.maxp_was_clamped = 1;
504 /* compute "max_frame_size" */
506 usbd_update_max_frame_size(xfer);
508 /* check interrupt interval and transfer pre-delay */
510 if (type == UE_ISOCHRONOUS) {
512 uint16_t frame_limit;
514 xfer->interval = 0; /* not used, must be zero */
515 xfer->flags_int.isochronous_xfr = 1; /* set flag */
517 if (xfer->timeout == 0) {
519 * set a default timeout in
520 * case something goes wrong!
522 xfer->timeout = 1000 / 4;
524 switch (parm->speed) {
527 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
531 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
532 xfer->fps_shift = edesc->bInterval;
533 if (xfer->fps_shift > 0)
535 if (xfer->fps_shift > 3)
537 if (xfer->flags.pre_scale_frames != 0)
538 xfer->nframes <<= (3 - xfer->fps_shift);
542 if (xfer->nframes > frame_limit) {
544 * this is not going to work
547 parm->err = USB_ERR_INVAL;
550 if (xfer->nframes == 0) {
552 * this is not a valid value
554 parm->err = USB_ERR_ZERO_NFRAMES;
560 * If a value is specified use that else check the
561 * endpoint descriptor!
563 if (type == UE_INTERRUPT) {
567 if (xfer->interval == 0) {
569 xfer->interval = edesc->bInterval;
571 switch (parm->speed) {
577 if (xfer->interval < 4)
579 else if (xfer->interval > 16)
580 xfer->interval = (1 << (16 - 4));
583 (1 << (xfer->interval - 4));
588 if (xfer->interval == 0) {
590 * One millisecond is the smallest
591 * interval we support:
599 while ((temp != 0) && (temp < xfer->interval)) {
604 switch (parm->speed) {
609 xfer->fps_shift += 3;
616 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
617 * to be equal to zero when setting up USB transfers, hence
618 * this leads to alot of extra code in the USB kernel.
621 if ((xfer->max_frame_size == 0) ||
622 (xfer->max_packet_size == 0)) {
626 if ((parm->bufsize <= MIN_PKT) &&
627 (type != UE_CONTROL) &&
631 xfer->max_packet_size = MIN_PKT;
632 xfer->max_packet_count = 1;
633 parm->bufsize = 0; /* automatic setup length */
634 usbd_update_max_frame_size(xfer);
637 parm->err = USB_ERR_ZERO_MAXP;
646 * check if we should setup a default
650 if (parm->bufsize == 0) {
652 parm->bufsize = xfer->max_frame_size;
654 if (type == UE_ISOCHRONOUS) {
655 parm->bufsize *= xfer->nframes;
659 * check if we are about to setup a proxy
663 if (xfer->flags.proxy_buffer) {
665 /* round bufsize up */
667 parm->bufsize += (xfer->max_frame_size - 1);
669 if (parm->bufsize < xfer->max_frame_size) {
670 /* length wrapped around */
671 parm->err = USB_ERR_INVAL;
674 /* subtract remainder */
676 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
678 /* add length of USB device request structure, if any */
680 if (type == UE_CONTROL) {
681 parm->bufsize += REQ_SIZE; /* SETUP message */
684 xfer->max_data_length = parm->bufsize;
686 /* Setup "n_frlengths" and "n_frbuffers" */
688 if (type == UE_ISOCHRONOUS) {
689 n_frlengths = xfer->nframes;
693 if (type == UE_CONTROL) {
694 xfer->flags_int.control_xfr = 1;
695 if (xfer->nframes == 0) {
696 if (parm->bufsize <= REQ_SIZE) {
698 * there will never be any data
707 if (xfer->nframes == 0) {
712 n_frlengths = xfer->nframes;
713 n_frbuffers = xfer->nframes;
717 * check if we have room for the
718 * USB device request structure:
721 if (type == UE_CONTROL) {
723 if (xfer->max_data_length < REQ_SIZE) {
724 /* length wrapped around or too small bufsize */
725 parm->err = USB_ERR_INVAL;
728 xfer->max_data_length -= REQ_SIZE;
731 * Setup "frlengths" and shadow "frlengths" for keeping the
732 * initial frame lengths when a USB transfer is complete. This
733 * information is useful when computing isochronous offsets.
735 xfer->frlengths = parm->xfer_length_ptr;
736 parm->xfer_length_ptr += 2 * n_frlengths;
738 /* setup "frbuffers" */
739 xfer->frbuffers = parm->xfer_page_cache_ptr;
740 parm->xfer_page_cache_ptr += n_frbuffers;
742 /* initialize max frame count */
743 xfer->max_frame_count = xfer->nframes;
746 * check if we need to setup
750 if (!xfer->flags.ext_buffer) {
752 struct usb_page_search page_info;
753 struct usb_page_cache *pc;
755 if (usbd_transfer_setup_sub_malloc(parm,
756 &pc, parm->bufsize, 1, 1)) {
757 parm->err = USB_ERR_NOMEM;
758 } else if (parm->buf != NULL) {
760 usbd_get_page(pc, 0, &page_info);
762 xfer->local_buffer = page_info.buffer;
764 usbd_xfer_set_frame_offset(xfer, 0, 0);
766 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
767 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
772 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
774 if (parm->buf != NULL) {
776 USB_ADD_BYTES(parm->buf, parm->size[0]);
778 usbd_xfer_set_frame_offset(xfer, 0, 0);
780 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
781 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
784 parm->size[0] += parm->bufsize;
786 /* align data again */
787 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
791 * Compute maximum buffer size
794 if (parm->bufsize_max < parm->bufsize) {
795 parm->bufsize_max = parm->bufsize;
798 if (xfer->flags_int.bdma_enable) {
800 * Setup "dma_page_ptr".
802 * Proof for formula below:
804 * Assume there are three USB frames having length "a", "b" and
805 * "c". These USB frames will at maximum need "z"
806 * "usb_page" structures. "z" is given by:
808 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
809 * ((c / USB_PAGE_SIZE) + 2);
811 * Constraining "a", "b" and "c" like this:
813 * (a + b + c) <= parm->bufsize
817 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
819 * Here is the general formula:
821 xfer->dma_page_ptr = parm->dma_page_ptr;
822 parm->dma_page_ptr += (2 * n_frbuffers);
823 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
827 /* correct maximum data length */
828 xfer->max_data_length = 0;
830 /* subtract USB frame remainder from "hc_max_frame_size" */
832 xfer->max_hc_frame_size =
833 (parm->hc_max_frame_size -
834 (parm->hc_max_frame_size % xfer->max_frame_size));
836 if (xfer->max_hc_frame_size == 0) {
837 parm->err = USB_ERR_INVAL;
841 /* initialize frame buffers */
844 for (x = 0; x != n_frbuffers; x++) {
845 xfer->frbuffers[x].tag_parent =
846 &xfer->xroot->dma_parent_tag;
848 if (xfer->flags_int.bdma_enable &&
849 (parm->bufsize_max > 0)) {
851 if (usb_pc_dmamap_create(
853 parm->bufsize_max)) {
854 parm->err = USB_ERR_NOMEM;
864 * Set some dummy values so that we avoid division by zero:
866 xfer->max_hc_frame_size = 1;
867 xfer->max_frame_size = 1;
868 xfer->max_packet_size = 1;
869 xfer->max_data_length = 0;
871 xfer->max_frame_count = 0;
875 /*------------------------------------------------------------------------*
876 * usbd_transfer_setup - setup an array of USB transfers
878 * NOTE: You must always call "usbd_transfer_unsetup" after calling
879 * "usbd_transfer_setup" if success was returned.
881 * The idea is that the USB device driver should pre-allocate all its
882 * transfers by one call to this function.
887 *------------------------------------------------------------------------*/
889 usbd_transfer_setup(struct usb_device *udev,
890 const uint8_t *ifaces, struct usb_xfer **ppxfer,
891 const struct usb_config *setup_start, uint16_t n_setup,
892 void *priv_sc, struct mtx *xfer_mtx)
894 const struct usb_config *setup_end = setup_start + n_setup;
895 const struct usb_config *setup;
896 struct usb_setup_params *parm;
897 struct usb_endpoint *ep;
898 struct usb_xfer_root *info;
899 struct usb_xfer *xfer;
901 usb_error_t error = 0;
906 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
907 "usbd_transfer_setup can sleep!");
909 /* do some checking first */
912 DPRINTFN(6, "setup array has zero length!\n");
913 return (USB_ERR_INVAL);
916 DPRINTFN(6, "ifaces array is NULL!\n");
917 return (USB_ERR_INVAL);
919 if (xfer_mtx == NULL) {
920 DPRINTFN(6, "using global lock\n");
924 /* more sanity checks */
926 for (setup = setup_start, n = 0;
927 setup != setup_end; setup++, n++) {
928 if (setup->bufsize == (usb_frlength_t)-1) {
929 error = USB_ERR_BAD_BUFSIZE;
930 DPRINTF("invalid bufsize\n");
932 if (setup->callback == NULL) {
933 error = USB_ERR_NO_CALLBACK;
934 DPRINTF("no callback\n");
942 /* Protect scratch area */
943 do_unlock = usbd_enum_lock(udev);
948 parm = &udev->scratch.xfer_setup[0].parm;
949 memset(parm, 0, sizeof(*parm));
952 parm->speed = usbd_get_speed(udev);
953 parm->hc_max_packet_count = 1;
955 if (parm->speed >= USB_SPEED_MAX) {
956 parm->err = USB_ERR_INVAL;
959 /* setup all transfers */
965 * Initialize the "usb_xfer_root" structure,
966 * which is common for all our USB transfers.
968 info = USB_ADD_BYTES(buf, 0);
970 info->memory_base = buf;
971 info->memory_size = parm->size[0];
974 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
975 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
977 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
978 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
980 cv_init(&info->cv_drain, "WDRAIN");
982 info->xfer_mtx = xfer_mtx;
984 usb_dma_tag_setup(&info->dma_parent_tag,
985 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
986 xfer_mtx, &usb_bdma_done_event, udev->bus->dma_bits,
990 info->bus = udev->bus;
993 TAILQ_INIT(&info->done_q.head);
994 info->done_q.command = &usbd_callback_wrapper;
996 TAILQ_INIT(&info->dma_q.head);
997 info->dma_q.command = &usb_bdma_work_loop;
999 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
1000 info->done_m[0].xroot = info;
1001 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1002 info->done_m[1].xroot = info;
1005 * In device side mode control endpoint
1006 * requests need to run from a separate
1007 * context, else there is a chance of
1010 if (setup_start == usb_control_ep_cfg)
1012 USB_BUS_CONTROL_XFER_PROC(udev->bus);
1013 else if (xfer_mtx == &Giant)
1015 USB_BUS_GIANT_PROC(udev->bus);
1018 USB_BUS_NON_GIANT_PROC(udev->bus);
1024 parm->size[0] += sizeof(info[0]);
1026 for (setup = setup_start, n = 0;
1027 setup != setup_end; setup++, n++) {
1029 /* skip USB transfers without callbacks: */
1030 if (setup->callback == NULL) {
1033 /* see if there is a matching endpoint */
1034 ep = usbd_get_endpoint(udev,
1035 ifaces[setup->if_index], setup);
1038 * Check that the USB PIPE is valid and that
1039 * the endpoint mode is proper.
1041 * Make sure we don't allocate a streams
1042 * transfer when such a combination is not
1045 if ((ep == NULL) || (ep->methods == NULL) ||
1046 ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1047 (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1048 (setup->stream_id != 0 &&
1049 (setup->stream_id >= USB_MAX_EP_STREAMS ||
1050 (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1051 if (setup->flags.no_pipe_ok)
1053 if ((setup->usb_mode != USB_MODE_DUAL) &&
1054 (setup->usb_mode != udev->flags.usb_mode))
1056 parm->err = USB_ERR_NO_PIPE;
1060 /* align data properly */
1061 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1063 /* store current setup pointer */
1064 parm->curr_setup = setup;
1068 * Common initialization of the
1069 * "usb_xfer" structure.
1071 xfer = USB_ADD_BYTES(buf, parm->size[0]);
1072 xfer->address = udev->address;
1073 xfer->priv_sc = priv_sc;
1076 usb_callout_init_mtx(&xfer->timeout_handle,
1077 &udev->bus->bus_mtx, 0);
1080 * Setup a dummy xfer, hence we are
1081 * writing to the "usb_xfer"
1082 * structure pointed to by "xfer"
1083 * before we have allocated any
1086 xfer = &udev->scratch.xfer_setup[0].dummy;
1087 memset(xfer, 0, sizeof(*xfer));
1091 /* set transfer endpoint pointer */
1092 xfer->endpoint = ep;
1094 /* set transfer stream ID */
1095 xfer->stream_id = setup->stream_id;
1097 parm->size[0] += sizeof(xfer[0]);
1098 parm->methods = xfer->endpoint->methods;
1099 parm->curr_xfer = xfer;
1102 * Call the Host or Device controller transfer
1105 (udev->bus->methods->xfer_setup) (parm);
1107 /* check for error */
1113 * Increment the endpoint refcount. This
1114 * basically prevents setting a new
1115 * configuration and alternate setting
1116 * when USB transfers are in use on
1117 * the given interface. Search the USB
1118 * code for "endpoint->refcount_alloc" if you
1119 * want more information.
1121 USB_BUS_LOCK(info->bus);
1122 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1123 parm->err = USB_ERR_INVAL;
1125 xfer->endpoint->refcount_alloc++;
1127 if (xfer->endpoint->refcount_alloc == 0)
1128 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1129 USB_BUS_UNLOCK(info->bus);
1132 * Whenever we set ppxfer[] then we
1133 * also need to increment the
1136 info->setup_refcount++;
1139 * Transfer is successfully setup and
1145 /* check for error */
1150 if (buf != NULL || parm->err != 0)
1153 /* if no transfers, nothing to do */
1157 /* align data properly */
1158 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1160 /* store offset temporarily */
1161 parm->size[1] = parm->size[0];
1164 * The number of DMA tags required depends on
1165 * the number of endpoints. The current estimate
1166 * for maximum number of DMA tags per endpoint
1168 * 1) for loading memory
1169 * 2) for allocating memory
1170 * 3) for fixing memory [UHCI]
1172 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1175 * DMA tags for QH, TD, Data and more.
1177 parm->dma_tag_max += 8;
1179 parm->dma_tag_p += parm->dma_tag_max;
1181 parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1184 /* align data properly */
1185 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1187 /* store offset temporarily */
1188 parm->size[3] = parm->size[0];
1190 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1193 /* align data properly */
1194 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1196 /* store offset temporarily */
1197 parm->size[4] = parm->size[0];
1199 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1202 /* store end offset temporarily */
1203 parm->size[5] = parm->size[0];
1205 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1208 /* store end offset temporarily */
1210 parm->size[2] = parm->size[0];
1212 /* align data properly */
1213 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1215 parm->size[6] = parm->size[0];
1217 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1220 /* align data properly */
1221 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1223 /* allocate zeroed memory */
1224 buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1227 parm->err = USB_ERR_NOMEM;
1228 DPRINTFN(0, "cannot allocate memory block for "
1229 "configuration (%d bytes)\n",
1233 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1234 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1235 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1236 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1237 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1242 if (info->setup_refcount == 0) {
1244 * "usbd_transfer_unsetup_sub" will unlock
1245 * the bus mutex before returning !
1247 USB_BUS_LOCK(info->bus);
1249 /* something went wrong */
1250 usbd_transfer_unsetup_sub(info, 0);
1254 /* check if any errors happened */
1256 usbd_transfer_unsetup(ppxfer, n_setup);
1261 usbd_enum_unlock(udev);
1266 /*------------------------------------------------------------------------*
1267 * usbd_transfer_unsetup_sub - factored out code
1268 *------------------------------------------------------------------------*/
1270 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1273 struct usb_page_cache *pc;
1276 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1278 /* wait for any outstanding DMA operations */
1282 temp = usbd_get_dma_delay(info->udev);
1284 usb_pause_mtx(&info->bus->bus_mtx,
1285 USB_MS_TO_TICKS(temp));
1289 /* make sure that our done messages are not queued anywhere */
1290 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1292 USB_BUS_UNLOCK(info->bus);
1295 /* free DMA'able memory, if any */
1296 pc = info->dma_page_cache_start;
1297 while (pc != info->dma_page_cache_end) {
1298 usb_pc_free_mem(pc);
1302 /* free DMA maps in all "xfer->frbuffers" */
1303 pc = info->xfer_page_cache_start;
1304 while (pc != info->xfer_page_cache_end) {
1305 usb_pc_dmamap_destroy(pc);
1309 /* free all DMA tags */
1310 usb_dma_tag_unsetup(&info->dma_parent_tag);
1313 cv_destroy(&info->cv_drain);
1316 * free the "memory_base" last, hence the "info" structure is
1317 * contained within the "memory_base"!
1319 free(info->memory_base, M_USB);
1322 /*------------------------------------------------------------------------*
1323 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1325 * NOTE: All USB transfers in progress will get called back passing
1326 * the error code "USB_ERR_CANCELLED" before this function
1328 *------------------------------------------------------------------------*/
1330 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1332 struct usb_xfer *xfer;
1333 struct usb_xfer_root *info;
1334 uint8_t needs_delay = 0;
1336 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1337 "usbd_transfer_unsetup can sleep!");
1340 xfer = pxfer[n_setup];
1347 USB_XFER_LOCK(xfer);
1348 USB_BUS_LOCK(info->bus);
1351 * HINT: when you start/stop a transfer, it might be a
1352 * good idea to directly use the "pxfer[]" structure:
1354 * usbd_transfer_start(sc->pxfer[0]);
1355 * usbd_transfer_stop(sc->pxfer[0]);
1357 * That way, if your code has many parts that will not
1358 * stop running under the same lock, in other words
1359 * "xfer_mtx", the usbd_transfer_start and
1360 * usbd_transfer_stop functions will simply return
1361 * when they detect a NULL pointer argument.
1363 * To avoid any races we clear the "pxfer[]" pointer
1364 * while holding the private mutex of the driver:
1366 pxfer[n_setup] = NULL;
1368 USB_BUS_UNLOCK(info->bus);
1369 USB_XFER_UNLOCK(xfer);
1371 usbd_transfer_drain(xfer);
1374 if (xfer->flags_int.bdma_enable)
1378 * NOTE: default endpoint does not have an
1379 * interface, even if endpoint->iface_index == 0
1381 USB_BUS_LOCK(info->bus);
1382 xfer->endpoint->refcount_alloc--;
1383 USB_BUS_UNLOCK(info->bus);
1385 usb_callout_drain(&xfer->timeout_handle);
1387 USB_BUS_LOCK(info->bus);
1389 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1390 "reference count\n"));
1392 info->setup_refcount--;
1394 if (info->setup_refcount == 0) {
1395 usbd_transfer_unsetup_sub(info,
1398 USB_BUS_UNLOCK(info->bus);
1403 /*------------------------------------------------------------------------*
1404 * usbd_control_transfer_init - factored out code
1406 * In USB Device Mode we have to wait for the SETUP packet which
1407 * containst the "struct usb_device_request" structure, before we can
1408 * transfer any data. In USB Host Mode we already have the SETUP
1409 * packet at the moment the USB transfer is started. This leads us to
1410 * having to setup the USB transfer at two different places in
1411 * time. This function just contains factored out control transfer
1412 * initialisation code, so that we don't duplicate the code.
1413 *------------------------------------------------------------------------*/
1415 usbd_control_transfer_init(struct usb_xfer *xfer)
1417 struct usb_device_request req;
1419 /* copy out the USB request header */
1421 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1423 /* setup remainder */
1425 xfer->flags_int.control_rem = UGETW(req.wLength);
1427 /* copy direction to endpoint variable */
1429 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1431 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1434 /*------------------------------------------------------------------------*
1435 * usbd_control_transfer_did_data
1437 * This function returns non-zero if a control endpoint has
1438 * transferred the first DATA packet after the SETUP packet.
1439 * Else it returns zero.
1440 *------------------------------------------------------------------------*/
1442 usbd_control_transfer_did_data(struct usb_xfer *xfer)
1444 struct usb_device_request req;
1446 /* SETUP packet is not yet sent */
1447 if (xfer->flags_int.control_hdr != 0)
1450 /* copy out the USB request header */
1451 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1453 /* compare remainder to the initial value */
1454 return (xfer->flags_int.control_rem != UGETW(req.wLength));
1457 /*------------------------------------------------------------------------*
1458 * usbd_setup_ctrl_transfer
1460 * This function handles initialisation of control transfers. Control
1461 * transfers are special in that regard that they can both transmit
1467 *------------------------------------------------------------------------*/
1469 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1473 /* Check for control endpoint stall */
1474 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1475 /* the control transfer is no longer active */
1476 xfer->flags_int.control_stall = 1;
1477 xfer->flags_int.control_act = 0;
1479 /* don't stall control transfer by default */
1480 xfer->flags_int.control_stall = 0;
1483 /* Check for invalid number of frames */
1484 if (xfer->nframes > 2) {
1486 * If you need to split a control transfer, you
1487 * have to do one part at a time. Only with
1488 * non-control transfers you can do multiple
1491 DPRINTFN(0, "Too many frames: %u\n",
1492 (unsigned int)xfer->nframes);
1497 * Check if there is a control
1498 * transfer in progress:
1500 if (xfer->flags_int.control_act) {
1502 if (xfer->flags_int.control_hdr) {
1504 /* clear send header flag */
1506 xfer->flags_int.control_hdr = 0;
1508 /* setup control transfer */
1509 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1510 usbd_control_transfer_init(xfer);
1513 /* get data length */
1519 /* the size of the SETUP structure is hardcoded ! */
1521 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1522 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1523 xfer->frlengths[0], sizeof(struct
1524 usb_device_request));
1527 /* check USB mode */
1528 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1530 /* check number of frames */
1531 if (xfer->nframes != 1) {
1533 * We need to receive the setup
1534 * message first so that we know the
1537 DPRINTF("Misconfigured transfer\n");
1541 * Set a dummy "control_rem" value. This
1542 * variable will be overwritten later by a
1543 * call to "usbd_control_transfer_init()" !
1545 xfer->flags_int.control_rem = 0xFFFF;
1548 /* setup "endpoint" and "control_rem" */
1550 usbd_control_transfer_init(xfer);
1553 /* set transfer-header flag */
1555 xfer->flags_int.control_hdr = 1;
1557 /* get data length */
1559 len = (xfer->sumlen - sizeof(struct usb_device_request));
1562 /* update did data flag */
1564 xfer->flags_int.control_did_data =
1565 usbd_control_transfer_did_data(xfer);
1567 /* check if there is a length mismatch */
1569 if (len > xfer->flags_int.control_rem) {
1570 DPRINTFN(0, "Length (%d) greater than "
1571 "remaining length (%d)\n", len,
1572 xfer->flags_int.control_rem);
1575 /* check if we are doing a short transfer */
1577 if (xfer->flags.force_short_xfer) {
1578 xfer->flags_int.control_rem = 0;
1580 if ((len != xfer->max_data_length) &&
1581 (len != xfer->flags_int.control_rem) &&
1582 (xfer->nframes != 1)) {
1583 DPRINTFN(0, "Short control transfer without "
1584 "force_short_xfer set\n");
1587 xfer->flags_int.control_rem -= len;
1590 /* the status part is executed when "control_act" is 0 */
1592 if ((xfer->flags_int.control_rem > 0) ||
1593 (xfer->flags.manual_status)) {
1594 /* don't execute the STATUS stage yet */
1595 xfer->flags_int.control_act = 1;
1598 if ((!xfer->flags_int.control_hdr) &&
1599 (xfer->nframes == 1)) {
1601 * This is not a valid operation!
1603 DPRINTFN(0, "Invalid parameter "
1608 /* time to execute the STATUS stage */
1609 xfer->flags_int.control_act = 0;
1611 return (0); /* success */
1614 return (1); /* failure */
1617 /*------------------------------------------------------------------------*
1618 * usbd_transfer_submit - start USB hardware for the given transfer
1620 * This function should only be called from the USB callback.
1621 *------------------------------------------------------------------------*/
1623 usbd_transfer_submit(struct usb_xfer *xfer)
1625 struct usb_xfer_root *info;
1626 struct usb_bus *bus;
1632 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1633 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1637 if (USB_DEBUG_VAR > 0) {
1640 usb_dump_endpoint(xfer->endpoint);
1642 USB_BUS_UNLOCK(bus);
1646 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1647 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1649 /* Only open the USB transfer once! */
1650 if (!xfer->flags_int.open) {
1651 xfer->flags_int.open = 1;
1656 (xfer->endpoint->methods->open) (xfer);
1657 USB_BUS_UNLOCK(bus);
1659 /* set "transferring" flag */
1660 xfer->flags_int.transferring = 1;
1663 /* increment power reference */
1664 usbd_transfer_power_ref(xfer, 1);
1667 * Check if the transfer is waiting on a queue, most
1668 * frequently the "done_q":
1670 if (xfer->wait_queue) {
1672 usbd_transfer_dequeue(xfer);
1673 USB_BUS_UNLOCK(bus);
1675 /* clear "did_dma_delay" flag */
1676 xfer->flags_int.did_dma_delay = 0;
1678 /* clear "did_close" flag */
1679 xfer->flags_int.did_close = 0;
1682 /* clear "bdma_setup" flag */
1683 xfer->flags_int.bdma_setup = 0;
1685 /* by default we cannot cancel any USB transfer immediately */
1686 xfer->flags_int.can_cancel_immed = 0;
1688 /* clear lengths and frame counts by default */
1693 /* clear any previous errors */
1696 /* Check if the device is still alive */
1697 if (info->udev->state < USB_STATE_POWERED) {
1700 * Must return cancelled error code else
1701 * device drivers can hang.
1703 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1704 USB_BUS_UNLOCK(bus);
1709 if (xfer->nframes == 0) {
1710 if (xfer->flags.stall_pipe) {
1712 * Special case - want to stall without transferring
1715 DPRINTF("xfer=%p nframes=0: stall "
1716 "or clear stall!\n", xfer);
1718 xfer->flags_int.can_cancel_immed = 1;
1719 /* start the transfer */
1720 usb_command_wrapper(&xfer->endpoint->
1721 endpoint_q[xfer->stream_id], xfer);
1722 USB_BUS_UNLOCK(bus);
1726 usbd_transfer_done(xfer, USB_ERR_INVAL);
1727 USB_BUS_UNLOCK(bus);
1730 /* compute some variables */
1732 for (x = 0; x != xfer->nframes; x++) {
1733 /* make a copy of the frlenghts[] */
1734 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1735 /* compute total transfer length */
1736 xfer->sumlen += xfer->frlengths[x];
1737 if (xfer->sumlen < xfer->frlengths[x]) {
1738 /* length wrapped around */
1740 usbd_transfer_done(xfer, USB_ERR_INVAL);
1741 USB_BUS_UNLOCK(bus);
1746 /* clear some internal flags */
1748 xfer->flags_int.short_xfer_ok = 0;
1749 xfer->flags_int.short_frames_ok = 0;
1751 /* check if this is a control transfer */
1753 if (xfer->flags_int.control_xfr) {
1755 if (usbd_setup_ctrl_transfer(xfer)) {
1757 usbd_transfer_done(xfer, USB_ERR_STALLED);
1758 USB_BUS_UNLOCK(bus);
1763 * Setup filtered version of some transfer flags,
1764 * in case of data read direction
1766 if (USB_GET_DATA_ISREAD(xfer)) {
1768 if (xfer->flags.short_frames_ok) {
1769 xfer->flags_int.short_xfer_ok = 1;
1770 xfer->flags_int.short_frames_ok = 1;
1771 } else if (xfer->flags.short_xfer_ok) {
1772 xfer->flags_int.short_xfer_ok = 1;
1774 /* check for control transfer */
1775 if (xfer->flags_int.control_xfr) {
1777 * 1) Control transfers do not support
1778 * reception of multiple short USB
1779 * frames in host mode and device side
1780 * mode, with exception of:
1782 * 2) Due to sometimes buggy device
1783 * side firmware we need to do a
1784 * STATUS stage in case of short
1785 * control transfers in USB host mode.
1786 * The STATUS stage then becomes the
1787 * "alt_next" to the DATA stage.
1789 xfer->flags_int.short_frames_ok = 1;
1794 * Check if BUS-DMA support is enabled and try to load virtual
1795 * buffers into DMA, if any:
1798 if (xfer->flags_int.bdma_enable) {
1799 /* insert the USB transfer last in the BUS-DMA queue */
1800 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1805 * Enter the USB transfer into the Host Controller or
1806 * Device Controller schedule:
1808 usbd_pipe_enter(xfer);
1811 /*------------------------------------------------------------------------*
1812 * usbd_pipe_enter - factored out code
1813 *------------------------------------------------------------------------*/
1815 usbd_pipe_enter(struct usb_xfer *xfer)
1817 struct usb_endpoint *ep;
1819 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1821 USB_BUS_LOCK(xfer->xroot->bus);
1823 ep = xfer->endpoint;
1827 /* the transfer can now be cancelled */
1828 xfer->flags_int.can_cancel_immed = 1;
1830 /* enter the transfer */
1831 (ep->methods->enter) (xfer);
1833 /* check for transfer error */
1835 /* some error has happened */
1836 usbd_transfer_done(xfer, 0);
1837 USB_BUS_UNLOCK(xfer->xroot->bus);
1841 /* start the transfer */
1842 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1843 USB_BUS_UNLOCK(xfer->xroot->bus);
1846 /*------------------------------------------------------------------------*
1847 * usbd_transfer_start - start an USB transfer
1849 * NOTE: Calling this function more than one time will only
1850 * result in a single transfer start, until the USB transfer
1852 *------------------------------------------------------------------------*/
1854 usbd_transfer_start(struct usb_xfer *xfer)
1857 /* transfer is gone */
1860 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1862 /* mark the USB transfer started */
1864 if (!xfer->flags_int.started) {
1865 /* lock the BUS lock to avoid races updating flags_int */
1866 USB_BUS_LOCK(xfer->xroot->bus);
1867 xfer->flags_int.started = 1;
1868 USB_BUS_UNLOCK(xfer->xroot->bus);
1870 /* check if the USB transfer callback is already transferring */
1872 if (xfer->flags_int.transferring) {
1875 USB_BUS_LOCK(xfer->xroot->bus);
1876 /* call the USB transfer callback */
1877 usbd_callback_ss_done_defer(xfer);
1878 USB_BUS_UNLOCK(xfer->xroot->bus);
1881 /*------------------------------------------------------------------------*
1882 * usbd_transfer_stop - stop an USB transfer
1884 * NOTE: Calling this function more than one time will only
1885 * result in a single transfer stop.
1886 * NOTE: When this function returns it is not safe to free nor
1887 * reuse any DMA buffers. See "usbd_transfer_drain()".
1888 *------------------------------------------------------------------------*/
1890 usbd_transfer_stop(struct usb_xfer *xfer)
1892 struct usb_endpoint *ep;
1895 /* transfer is gone */
1898 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1900 /* check if the USB transfer was ever opened */
1902 if (!xfer->flags_int.open) {
1903 if (xfer->flags_int.started) {
1904 /* nothing to do except clearing the "started" flag */
1905 /* lock the BUS lock to avoid races updating flags_int */
1906 USB_BUS_LOCK(xfer->xroot->bus);
1907 xfer->flags_int.started = 0;
1908 USB_BUS_UNLOCK(xfer->xroot->bus);
1912 /* try to stop the current USB transfer */
1914 USB_BUS_LOCK(xfer->xroot->bus);
1915 /* override any previous error */
1916 xfer->error = USB_ERR_CANCELLED;
1919 * Clear "open" and "started" when both private and USB lock
1920 * is locked so that we don't get a race updating "flags_int"
1922 xfer->flags_int.open = 0;
1923 xfer->flags_int.started = 0;
1926 * Check if we can cancel the USB transfer immediately.
1928 if (xfer->flags_int.transferring) {
1929 if (xfer->flags_int.can_cancel_immed &&
1930 (!xfer->flags_int.did_close)) {
1933 * The following will lead to an USB_ERR_CANCELLED
1934 * error code being passed to the USB callback.
1936 (xfer->endpoint->methods->close) (xfer);
1937 /* only close once */
1938 xfer->flags_int.did_close = 1;
1940 /* need to wait for the next done callback */
1945 /* close here and now */
1946 (xfer->endpoint->methods->close) (xfer);
1949 * Any additional DMA delay is done by
1950 * "usbd_transfer_unsetup()".
1954 * Special case. Check if we need to restart a blocked
1957 ep = xfer->endpoint;
1960 * If the current USB transfer is completing we need
1961 * to start the next one:
1963 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
1964 usb_command_wrapper(
1965 &ep->endpoint_q[xfer->stream_id], NULL);
1969 USB_BUS_UNLOCK(xfer->xroot->bus);
1972 /*------------------------------------------------------------------------*
1973 * usbd_transfer_pending
1975 * This function will check if an USB transfer is pending which is a
1976 * little bit complicated!
1979 * 1: Pending: The USB transfer will receive a callback in the future.
1980 *------------------------------------------------------------------------*/
1982 usbd_transfer_pending(struct usb_xfer *xfer)
1984 struct usb_xfer_root *info;
1985 struct usb_xfer_queue *pq;
1988 /* transfer is gone */
1991 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1993 if (xfer->flags_int.transferring) {
1997 USB_BUS_LOCK(xfer->xroot->bus);
1998 if (xfer->wait_queue) {
1999 /* we are waiting on a queue somewhere */
2000 USB_BUS_UNLOCK(xfer->xroot->bus);
2006 if (pq->curr == xfer) {
2007 /* we are currently scheduled for callback */
2008 USB_BUS_UNLOCK(xfer->xroot->bus);
2011 /* we are not pending */
2012 USB_BUS_UNLOCK(xfer->xroot->bus);
2016 /*------------------------------------------------------------------------*
2017 * usbd_transfer_drain
2019 * This function will stop the USB transfer and wait for any
2020 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2021 * are loaded into DMA can safely be freed or reused after that this
2022 * function has returned.
2023 *------------------------------------------------------------------------*/
2025 usbd_transfer_drain(struct usb_xfer *xfer)
2027 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2028 "usbd_transfer_drain can sleep!");
2031 /* transfer is gone */
2034 if (xfer->xroot->xfer_mtx != &Giant) {
2035 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
2037 USB_XFER_LOCK(xfer);
2039 usbd_transfer_stop(xfer);
2041 while (usbd_transfer_pending(xfer) ||
2042 xfer->flags_int.doing_callback) {
2045 * It is allowed that the callback can drop its
2046 * transfer mutex. In that case checking only
2047 * "usbd_transfer_pending()" is not enough to tell if
2048 * the USB transfer is fully drained. We also need to
2049 * check the internal "doing_callback" flag.
2051 xfer->flags_int.draining = 1;
2054 * Wait until the current outstanding USB
2055 * transfer is complete !
2057 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
2059 USB_XFER_UNLOCK(xfer);
2062 struct usb_page_cache *
2063 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2065 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2067 return (&xfer->frbuffers[frindex]);
2071 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2073 struct usb_page_search page_info;
2075 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2077 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2078 return (page_info.buffer);
2081 /*------------------------------------------------------------------------*
2082 * usbd_xfer_get_fps_shift
2084 * The following function is only useful for isochronous transfers. It
2085 * returns how many times the frame execution rate has been shifted
2091 *------------------------------------------------------------------------*/
2093 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2095 return (xfer->fps_shift);
2099 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2101 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2103 return (xfer->frlengths[frindex]);
2106 /*------------------------------------------------------------------------*
2107 * usbd_xfer_set_frame_data
2109 * This function sets the pointer of the buffer that should
2110 * loaded directly into DMA for the given USB frame. Passing "ptr"
2111 * equal to NULL while the corresponding "frlength" is greater
2112 * than zero gives undefined results!
2113 *------------------------------------------------------------------------*/
2115 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2116 void *ptr, usb_frlength_t len)
2118 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2120 /* set virtual address to load and length */
2121 xfer->frbuffers[frindex].buffer = ptr;
2122 usbd_xfer_set_frame_len(xfer, frindex, len);
2126 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2127 void **ptr, int *len)
2129 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2132 *ptr = xfer->frbuffers[frindex].buffer;
2134 *len = xfer->frlengths[frindex];
2137 /*------------------------------------------------------------------------*
2138 * usbd_xfer_old_frame_length
2140 * This function returns the framelength of the given frame at the
2141 * time the transfer was submitted. This function can be used to
2142 * compute the starting data pointer of the next isochronous frame
2143 * when an isochronous transfer has completed.
2144 *------------------------------------------------------------------------*/
2146 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2148 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2150 return (xfer->frlengths[frindex + xfer->max_frame_count]);
2154 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2158 *actlen = xfer->actlen;
2160 *sumlen = xfer->sumlen;
2161 if (aframes != NULL)
2162 *aframes = xfer->aframes;
2163 if (nframes != NULL)
2164 *nframes = xfer->nframes;
2167 /*------------------------------------------------------------------------*
2168 * usbd_xfer_set_frame_offset
2170 * This function sets the frame data buffer offset relative to the beginning
2171 * of the USB DMA buffer allocated for this USB transfer.
2172 *------------------------------------------------------------------------*/
2174 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2175 usb_frcount_t frindex)
2177 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2178 "when the USB buffer is external\n"));
2179 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2181 /* set virtual address to load */
2182 xfer->frbuffers[frindex].buffer =
2183 USB_ADD_BYTES(xfer->local_buffer, offset);
2187 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2193 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2199 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2205 usbd_xfer_max_frames(struct usb_xfer *xfer)
2207 return (xfer->max_frame_count);
2211 usbd_xfer_max_len(struct usb_xfer *xfer)
2213 return (xfer->max_data_length);
2217 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2219 return (xfer->max_frame_size);
2223 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2226 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2228 xfer->frlengths[frindex] = len;
2231 /*------------------------------------------------------------------------*
2232 * usb_callback_proc - factored out code
2234 * This function performs USB callbacks.
2235 *------------------------------------------------------------------------*/
2237 usb_callback_proc(struct usb_proc_msg *_pm)
2239 struct usb_done_msg *pm = (void *)_pm;
2240 struct usb_xfer_root *info = pm->xroot;
2242 /* Change locking order */
2243 USB_BUS_UNLOCK(info->bus);
2246 * We exploit the fact that the mutex is the same for all
2247 * callbacks that will be called from this thread:
2249 mtx_lock(info->xfer_mtx);
2250 USB_BUS_LOCK(info->bus);
2252 /* Continue where we lost track */
2253 usb_command_wrapper(&info->done_q,
2256 mtx_unlock(info->xfer_mtx);
2259 /*------------------------------------------------------------------------*
2260 * usbd_callback_ss_done_defer
2262 * This function will defer the start, stop and done callback to the
2264 *------------------------------------------------------------------------*/
2266 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2268 struct usb_xfer_root *info = xfer->xroot;
2269 struct usb_xfer_queue *pq = &info->done_q;
2271 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2273 if (pq->curr != xfer) {
2274 usbd_transfer_enqueue(pq, xfer);
2276 if (!pq->recurse_1) {
2279 * We have to postpone the callback due to the fact we
2280 * will have a Lock Order Reversal, LOR, if we try to
2283 if (usb_proc_msignal(info->done_p,
2284 &info->done_m[0], &info->done_m[1])) {
2288 /* clear second recurse flag */
2295 /*------------------------------------------------------------------------*
2296 * usbd_callback_wrapper
2298 * This is a wrapper for USB callbacks. This wrapper does some
2299 * auto-magic things like figuring out if we can call the callback
2300 * directly from the current context or if we need to wakeup the
2301 * interrupt process.
2302 *------------------------------------------------------------------------*/
2304 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2306 struct usb_xfer *xfer = pq->curr;
2307 struct usb_xfer_root *info = xfer->xroot;
2309 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2310 if (!mtx_owned(info->xfer_mtx) && !SCHEDULER_STOPPED()) {
2312 * Cases that end up here:
2314 * 5) HW interrupt done callback or other source.
2316 DPRINTFN(3, "case 5\n");
2319 * We have to postpone the callback due to the fact we
2320 * will have a Lock Order Reversal, LOR, if we try to
2323 if (usb_proc_msignal(info->done_p,
2324 &info->done_m[0], &info->done_m[1])) {
2330 * Cases that end up here:
2332 * 1) We are starting a transfer
2333 * 2) We are prematurely calling back a transfer
2334 * 3) We are stopping a transfer
2335 * 4) We are doing an ordinary callback
2337 DPRINTFN(3, "case 1-4\n");
2338 /* get next USB transfer in the queue */
2339 info->done_q.curr = NULL;
2341 /* set flag in case of drain */
2342 xfer->flags_int.doing_callback = 1;
2344 USB_BUS_UNLOCK(info->bus);
2345 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2347 /* set correct USB state for callback */
2348 if (!xfer->flags_int.transferring) {
2349 xfer->usb_state = USB_ST_SETUP;
2350 if (!xfer->flags_int.started) {
2351 /* we got stopped before we even got started */
2352 USB_BUS_LOCK(info->bus);
2357 if (usbd_callback_wrapper_sub(xfer)) {
2358 /* the callback has been deferred */
2359 USB_BUS_LOCK(info->bus);
2363 /* decrement power reference */
2364 usbd_transfer_power_ref(xfer, -1);
2366 xfer->flags_int.transferring = 0;
2369 xfer->usb_state = USB_ST_ERROR;
2371 /* set transferred state */
2372 xfer->usb_state = USB_ST_TRANSFERRED;
2374 /* sync DMA memory, if any */
2375 if (xfer->flags_int.bdma_enable &&
2376 (!xfer->flags_int.bdma_no_post_sync)) {
2377 usb_bdma_post_sync(xfer);
2384 if (xfer->usb_state != USB_ST_SETUP)
2385 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2387 /* call processing routine */
2388 (xfer->callback) (xfer, xfer->error);
2390 /* pickup the USB mutex again */
2391 USB_BUS_LOCK(info->bus);
2394 * Check if we got started after that we got cancelled, but
2395 * before we managed to do the callback.
2397 if ((!xfer->flags_int.open) &&
2398 (xfer->flags_int.started) &&
2399 (xfer->usb_state == USB_ST_ERROR)) {
2400 /* clear flag in case of drain */
2401 xfer->flags_int.doing_callback = 0;
2402 /* try to loop, but not recursivly */
2403 usb_command_wrapper(&info->done_q, xfer);
2408 /* clear flag in case of drain */
2409 xfer->flags_int.doing_callback = 0;
2412 * Check if we are draining.
2414 if (xfer->flags_int.draining &&
2415 (!xfer->flags_int.transferring)) {
2416 /* "usbd_transfer_drain()" is waiting for end of transfer */
2417 xfer->flags_int.draining = 0;
2418 cv_broadcast(&info->cv_drain);
2421 /* do the next callback, if any */
2422 usb_command_wrapper(&info->done_q,
2426 /*------------------------------------------------------------------------*
2427 * usb_dma_delay_done_cb
2429 * This function is called when the DMA delay has been exectuded, and
2430 * will make sure that the callback is called to complete the USB
2431 * transfer. This code path is ususally only used when there is an USB
2432 * error like USB_ERR_CANCELLED.
2433 *------------------------------------------------------------------------*/
2435 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2437 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2439 DPRINTFN(3, "Completed %p\n", xfer);
2441 /* queue callback for execution, again */
2442 usbd_transfer_done(xfer, 0);
2445 /*------------------------------------------------------------------------*
2446 * usbd_transfer_dequeue
2448 * - This function is used to remove an USB transfer from a USB
2451 * - This function can be called multiple times in a row.
2452 *------------------------------------------------------------------------*/
2454 usbd_transfer_dequeue(struct usb_xfer *xfer)
2456 struct usb_xfer_queue *pq;
2458 pq = xfer->wait_queue;
2460 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2461 xfer->wait_queue = NULL;
2465 /*------------------------------------------------------------------------*
2466 * usbd_transfer_enqueue
2468 * - This function is used to insert an USB transfer into a USB *
2471 * - This function can be called multiple times in a row.
2472 *------------------------------------------------------------------------*/
2474 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2477 * Insert the USB transfer into the queue, if it is not
2478 * already on a USB transfer queue:
2480 if (xfer->wait_queue == NULL) {
2481 xfer->wait_queue = pq;
2482 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2486 /*------------------------------------------------------------------------*
2487 * usbd_transfer_done
2489 * - This function is used to remove an USB transfer from the busdma,
2490 * pipe or interrupt queue.
2492 * - This function is used to queue the USB transfer on the done
2495 * - This function is used to stop any USB transfer timeouts.
2496 *------------------------------------------------------------------------*/
2498 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2500 struct usb_xfer_root *info = xfer->xroot;
2502 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2504 DPRINTF("err=%s\n", usbd_errstr(error));
2507 * If we are not transferring then just return.
2508 * This can happen during transfer cancel.
2510 if (!xfer->flags_int.transferring) {
2511 DPRINTF("not transferring\n");
2512 /* end of control transfer, if any */
2513 xfer->flags_int.control_act = 0;
2516 /* only set transfer error, if not already set */
2517 if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2518 xfer->error = error;
2520 /* stop any callouts */
2521 usb_callout_stop(&xfer->timeout_handle);
2524 * If we are waiting on a queue, just remove the USB transfer
2525 * from the queue, if any. We should have the required locks
2526 * locked to do the remove when this function is called.
2528 usbd_transfer_dequeue(xfer);
2531 if (mtx_owned(info->xfer_mtx)) {
2532 struct usb_xfer_queue *pq;
2535 * If the private USB lock is not locked, then we assume
2536 * that the BUS-DMA load stage has been passed:
2540 if (pq->curr == xfer) {
2541 /* start the next BUS-DMA load, if any */
2542 usb_command_wrapper(pq, NULL);
2546 /* keep some statistics */
2548 info->bus->stats_err.uds_requests
2549 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2551 info->bus->stats_ok.uds_requests
2552 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2555 /* call the USB transfer callback */
2556 usbd_callback_ss_done_defer(xfer);
2559 /*------------------------------------------------------------------------*
2560 * usbd_transfer_start_cb
2562 * This function is called to start the USB transfer when
2563 * "xfer->interval" is greater than zero, and and the endpoint type is
2565 *------------------------------------------------------------------------*/
2567 usbd_transfer_start_cb(void *arg)
2569 struct usb_xfer *xfer = arg;
2570 struct usb_endpoint *ep = xfer->endpoint;
2572 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2577 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2580 /* the transfer can now be cancelled */
2581 xfer->flags_int.can_cancel_immed = 1;
2583 /* start USB transfer, if no error */
2584 if (xfer->error == 0)
2585 (ep->methods->start) (xfer);
2587 /* check for transfer error */
2589 /* some error has happened */
2590 usbd_transfer_done(xfer, 0);
2594 /*------------------------------------------------------------------------*
2595 * usbd_xfer_set_stall
2597 * This function is used to set the stall flag outside the
2598 * callback. This function is NULL safe.
2599 *------------------------------------------------------------------------*/
2601 usbd_xfer_set_stall(struct usb_xfer *xfer)
2607 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2609 /* avoid any races by locking the USB mutex */
2610 USB_BUS_LOCK(xfer->xroot->bus);
2611 xfer->flags.stall_pipe = 1;
2612 USB_BUS_UNLOCK(xfer->xroot->bus);
2616 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2618 return (xfer->endpoint->is_stalled);
2621 /*------------------------------------------------------------------------*
2622 * usbd_transfer_clear_stall
2624 * This function is used to clear the stall flag outside the
2625 * callback. This function is NULL safe.
2626 *------------------------------------------------------------------------*/
2628 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2634 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2636 /* avoid any races by locking the USB mutex */
2637 USB_BUS_LOCK(xfer->xroot->bus);
2639 xfer->flags.stall_pipe = 0;
2641 USB_BUS_UNLOCK(xfer->xroot->bus);
2644 /*------------------------------------------------------------------------*
2647 * This function is used to add an USB transfer to the pipe transfer list.
2648 *------------------------------------------------------------------------*/
2650 usbd_pipe_start(struct usb_xfer_queue *pq)
2652 struct usb_endpoint *ep;
2653 struct usb_xfer *xfer;
2657 ep = xfer->endpoint;
2659 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2662 * If the endpoint is already stalled we do nothing !
2664 if (ep->is_stalled) {
2668 * Check if we are supposed to stall the endpoint:
2670 if (xfer->flags.stall_pipe) {
2671 struct usb_device *udev;
2672 struct usb_xfer_root *info;
2674 /* clear stall command */
2675 xfer->flags.stall_pipe = 0;
2677 /* get pointer to USB device */
2682 * Only stall BULK and INTERRUPT endpoints.
2684 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2685 if ((type == UE_BULK) ||
2686 (type == UE_INTERRUPT)) {
2691 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2692 (udev->bus->methods->set_stall) (
2693 udev, ep, &did_stall);
2694 } else if (udev->ctrl_xfer[1]) {
2695 info = udev->ctrl_xfer[1]->xroot;
2697 USB_BUS_NON_GIANT_PROC(info->bus),
2698 &udev->cs_msg[0], &udev->cs_msg[1]);
2700 /* should not happen */
2701 DPRINTFN(0, "No stall handler\n");
2704 * Check if we should stall. Some USB hardware
2705 * handles set- and clear-stall in hardware.
2709 * The transfer will be continued when
2710 * the clear-stall control endpoint
2711 * message is received.
2716 } else if (type == UE_ISOCHRONOUS) {
2719 * Make sure any FIFO overflow or other FIFO
2720 * error conditions go away by resetting the
2721 * endpoint FIFO through the clear stall
2724 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2725 (udev->bus->methods->clear_stall) (udev, ep);
2729 /* Set or clear stall complete - special case */
2730 if (xfer->nframes == 0) {
2731 /* we are complete */
2733 usbd_transfer_done(xfer, 0);
2739 * 1) Start the first transfer queued.
2741 * 2) Re-start the current USB transfer.
2744 * Check if there should be any
2745 * pre transfer start delay:
2747 if (xfer->interval > 0) {
2748 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2749 if ((type == UE_BULK) ||
2750 (type == UE_CONTROL)) {
2751 usbd_transfer_timeout_ms(xfer,
2752 &usbd_transfer_start_cb,
2760 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2762 /* the transfer can now be cancelled */
2763 xfer->flags_int.can_cancel_immed = 1;
2765 /* start USB transfer, if no error */
2766 if (xfer->error == 0)
2767 (ep->methods->start) (xfer);
2769 /* check for transfer error */
2771 /* some error has happened */
2772 usbd_transfer_done(xfer, 0);
2776 /*------------------------------------------------------------------------*
2777 * usbd_transfer_timeout_ms
2779 * This function is used to setup a timeout on the given USB
2780 * transfer. If the timeout has been deferred the callback given by
2781 * "cb" will get called after "ms" milliseconds.
2782 *------------------------------------------------------------------------*/
2784 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2785 void (*cb) (void *arg), usb_timeout_t ms)
2787 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2790 usb_callout_reset(&xfer->timeout_handle,
2791 USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2794 /*------------------------------------------------------------------------*
2795 * usbd_callback_wrapper_sub
2797 * - This function will update variables in an USB transfer after
2798 * that the USB transfer is complete.
2800 * - This function is used to start the next USB transfer on the
2801 * ep transfer queue, if any.
2803 * NOTE: In some special cases the USB transfer will not be removed from
2804 * the pipe queue, but remain first. To enforce USB transfer removal call
2805 * this function passing the error code "USB_ERR_CANCELLED".
2809 * Else: The callback has been deferred.
2810 *------------------------------------------------------------------------*/
2812 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2814 struct usb_endpoint *ep;
2815 struct usb_bus *bus;
2818 bus = xfer->xroot->bus;
2820 if ((!xfer->flags_int.open) &&
2821 (!xfer->flags_int.did_close)) {
2824 (xfer->endpoint->methods->close) (xfer);
2825 USB_BUS_UNLOCK(bus);
2826 /* only close once */
2827 xfer->flags_int.did_close = 1;
2828 return (1); /* wait for new callback */
2831 * If we have a non-hardware induced error we
2832 * need to do the DMA delay!
2834 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2835 (xfer->error == USB_ERR_CANCELLED ||
2836 xfer->error == USB_ERR_TIMEOUT ||
2837 bus->methods->start_dma_delay != NULL)) {
2841 /* only delay once */
2842 xfer->flags_int.did_dma_delay = 1;
2844 /* we can not cancel this delay */
2845 xfer->flags_int.can_cancel_immed = 0;
2847 temp = usbd_get_dma_delay(xfer->xroot->udev);
2849 DPRINTFN(3, "DMA delay, %u ms, "
2850 "on %p\n", temp, xfer);
2855 * Some hardware solutions have dedicated
2856 * events when it is safe to free DMA'ed
2857 * memory. For the other hardware platforms we
2858 * use a static delay.
2860 if (bus->methods->start_dma_delay != NULL) {
2861 (bus->methods->start_dma_delay) (xfer);
2863 usbd_transfer_timeout_ms(xfer,
2864 (void (*)(void *))&usb_dma_delay_done_cb,
2867 USB_BUS_UNLOCK(bus);
2868 return (1); /* wait for new callback */
2871 /* check actual number of frames */
2872 if (xfer->aframes > xfer->nframes) {
2873 if (xfer->error == 0) {
2874 panic("%s: actual number of frames, %d, is "
2875 "greater than initial number of frames, %d\n",
2876 __FUNCTION__, xfer->aframes, xfer->nframes);
2878 /* just set some valid value */
2879 xfer->aframes = xfer->nframes;
2882 /* compute actual length */
2885 for (x = 0; x != xfer->aframes; x++) {
2886 xfer->actlen += xfer->frlengths[x];
2890 * Frames that were not transferred get zero actual length in
2891 * case the USB device driver does not check the actual number
2892 * of frames transferred, "xfer->aframes":
2894 for (; x < xfer->nframes; x++) {
2895 usbd_xfer_set_frame_len(xfer, x, 0);
2898 /* check actual length */
2899 if (xfer->actlen > xfer->sumlen) {
2900 if (xfer->error == 0) {
2901 panic("%s: actual length, %d, is greater than "
2902 "initial length, %d\n",
2903 __FUNCTION__, xfer->actlen, xfer->sumlen);
2905 /* just set some valid value */
2906 xfer->actlen = xfer->sumlen;
2909 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2910 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2911 xfer->aframes, xfer->nframes);
2914 /* end of control transfer, if any */
2915 xfer->flags_int.control_act = 0;
2917 #if USB_HAVE_TT_SUPPORT
2918 switch (xfer->error) {
2919 case USB_ERR_NORMAL_COMPLETION:
2920 case USB_ERR_SHORT_XFER:
2921 case USB_ERR_STALLED:
2922 case USB_ERR_CANCELLED:
2926 /* try to reset the TT, if any */
2928 uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint);
2929 USB_BUS_UNLOCK(bus);
2933 /* check if we should block the execution queue */
2934 if ((xfer->error != USB_ERR_CANCELLED) &&
2935 (xfer->flags.pipe_bof)) {
2936 DPRINTFN(2, "xfer=%p: Block On Failure "
2937 "on endpoint=%p\n", xfer, xfer->endpoint);
2941 /* check for short transfers */
2942 if (xfer->actlen < xfer->sumlen) {
2944 /* end of control transfer, if any */
2945 xfer->flags_int.control_act = 0;
2947 if (!xfer->flags_int.short_xfer_ok) {
2948 xfer->error = USB_ERR_SHORT_XFER;
2949 if (xfer->flags.pipe_bof) {
2950 DPRINTFN(2, "xfer=%p: Block On Failure on "
2951 "Short Transfer on endpoint %p.\n",
2952 xfer, xfer->endpoint);
2958 * Check if we are in the middle of a
2961 if (xfer->flags_int.control_act) {
2962 DPRINTFN(5, "xfer=%p: Control transfer "
2963 "active on endpoint=%p\n", xfer, xfer->endpoint);
2969 ep = xfer->endpoint;
2972 * If the current USB transfer is completing we need to start the
2976 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2977 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
2979 if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
2980 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
2981 /* there is another USB transfer waiting */
2983 /* this is the last USB transfer */
2984 /* clear isochronous sync flag */
2985 xfer->endpoint->is_synced = 0;
2988 USB_BUS_UNLOCK(bus);
2993 /*------------------------------------------------------------------------*
2994 * usb_command_wrapper
2996 * This function is used to execute commands non-recursivly on an USB
2998 *------------------------------------------------------------------------*/
3000 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3004 * If the transfer is not already processing,
3007 if (pq->curr != xfer) {
3008 usbd_transfer_enqueue(pq, xfer);
3009 if (pq->curr != NULL) {
3010 /* something is already processing */
3011 DPRINTFN(6, "busy %p\n", pq->curr);
3016 /* Get next element in queue */
3020 if (!pq->recurse_1) {
3024 /* set both recurse flags */
3028 if (pq->curr == NULL) {
3029 xfer = TAILQ_FIRST(&pq->head);
3031 TAILQ_REMOVE(&pq->head, xfer,
3033 xfer->wait_queue = NULL;
3039 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3041 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3043 } while (!pq->recurse_2);
3045 /* clear first recurse flag */
3049 /* clear second recurse flag */
3054 /*------------------------------------------------------------------------*
3055 * usbd_ctrl_transfer_setup
3057 * This function is used to setup the default USB control endpoint
3059 *------------------------------------------------------------------------*/
3061 usbd_ctrl_transfer_setup(struct usb_device *udev)
3063 struct usb_xfer *xfer;
3065 uint8_t iface_index;
3067 /* check for root HUB */
3068 if (udev->parent_hub == NULL)
3072 xfer = udev->ctrl_xfer[0];
3074 USB_XFER_LOCK(xfer);
3076 ((xfer->address == udev->address) &&
3077 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3078 udev->ddesc.bMaxPacketSize));
3079 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3082 * NOTE: checking "xfer->address" and
3083 * starting the USB transfer must be
3086 usbd_transfer_start(xfer);
3089 USB_XFER_UNLOCK(xfer);
3096 * All parameters are exactly the same like before.
3102 * Update wMaxPacketSize for the default control endpoint:
3104 udev->ctrl_ep_desc.wMaxPacketSize[0] =
3105 udev->ddesc.bMaxPacketSize;
3108 * Unsetup any existing USB transfer:
3110 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3113 * Reset clear stall error counter.
3115 udev->clear_stall_errors = 0;
3118 * Try to setup a new USB transfer for the
3119 * default control endpoint:
3122 if (usbd_transfer_setup(udev, &iface_index,
3123 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3124 &udev->device_mtx)) {
3125 DPRINTFN(0, "could not setup default "
3132 /*------------------------------------------------------------------------*
3133 * usbd_clear_data_toggle - factored out code
3135 * NOTE: the intention of this function is not to reset the hardware
3137 *------------------------------------------------------------------------*/
3139 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3141 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3143 /* check that we have a valid case */
3144 if (udev->flags.usb_mode == USB_MODE_HOST &&
3145 udev->parent_hub != NULL &&
3146 udev->bus->methods->clear_stall != NULL &&
3147 ep->methods != NULL) {
3148 (udev->bus->methods->clear_stall) (udev, ep);
3152 /*------------------------------------------------------------------------*
3153 * usbd_clear_data_toggle - factored out code
3155 * NOTE: the intention of this function is not to reset the hardware
3156 * data toggle on the USB device side.
3157 *------------------------------------------------------------------------*/
3159 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3161 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3163 USB_BUS_LOCK(udev->bus);
3164 ep->toggle_next = 0;
3165 /* some hardware needs a callback to clear the data toggle */
3166 usbd_clear_stall_locked(udev, ep);
3167 USB_BUS_UNLOCK(udev->bus);
3170 /*------------------------------------------------------------------------*
3171 * usbd_clear_stall_callback - factored out clear stall callback
3174 * xfer1: Clear Stall Control Transfer
3175 * xfer2: Stalled USB Transfer
3177 * This function is NULL safe.
3183 * Clear stall config example:
3185 * static const struct usb_config my_clearstall = {
3186 * .type = UE_CONTROL,
3188 * .direction = UE_DIR_ANY,
3189 * .interval = 50, //50 milliseconds
3190 * .bufsize = sizeof(struct usb_device_request),
3191 * .timeout = 1000, //1.000 seconds
3192 * .callback = &my_clear_stall_callback, // **
3193 * .usb_mode = USB_MODE_HOST,
3196 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3197 * passing the correct parameters.
3198 *------------------------------------------------------------------------*/
3200 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3201 struct usb_xfer *xfer2)
3203 struct usb_device_request req;
3205 if (xfer2 == NULL) {
3206 /* looks like we are tearing down */
3207 DPRINTF("NULL input parameter\n");
3210 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3211 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3213 switch (USB_GET_STATE(xfer1)) {
3217 * pre-clear the data toggle to DATA0 ("umass.c" and
3218 * "ata-usb.c" depends on this)
3221 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3223 /* setup a clear-stall packet */
3225 req.bmRequestType = UT_WRITE_ENDPOINT;
3226 req.bRequest = UR_CLEAR_FEATURE;
3227 USETW(req.wValue, UF_ENDPOINT_HALT);
3228 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3230 USETW(req.wLength, 0);
3233 * "usbd_transfer_setup_sub()" will ensure that
3234 * we have sufficient room in the buffer for
3235 * the request structure!
3238 /* copy in the transfer */
3240 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3243 xfer1->frlengths[0] = sizeof(req);
3246 usbd_transfer_submit(xfer1);
3249 case USB_ST_TRANSFERRED:
3252 default: /* Error */
3253 if (xfer1->error == USB_ERR_CANCELLED) {
3258 return (1); /* Clear Stall Finished */
3261 /*------------------------------------------------------------------------*
3262 * usbd_transfer_poll
3264 * The following function gets called from the USB keyboard driver and
3265 * UMASS when the system has paniced.
3267 * NOTE: It is currently not possible to resume normal operation on
3268 * the USB controller which has been polled, due to clearing of the
3269 * "up_dsleep" and "up_msleep" flags.
3270 *------------------------------------------------------------------------*/
3272 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3274 struct usb_xfer *xfer;
3275 struct usb_xfer_root *xroot;
3276 struct usb_device *udev;
3277 struct usb_proc_msg *pm;
3282 for (n = 0; n != max; n++) {
3283 /* Extra checks to avoid panic */
3286 continue; /* no USB transfer */
3287 xroot = xfer->xroot;
3289 continue; /* no USB root */
3292 continue; /* no USB device */
3293 if (udev->bus == NULL)
3294 continue; /* no BUS structure */
3295 if (udev->bus->methods == NULL)
3296 continue; /* no BUS methods */
3297 if (udev->bus->methods->xfer_poll == NULL)
3298 continue; /* no poll method */
3300 /* make sure that the BUS mutex is not locked */
3302 while (mtx_owned(&xroot->udev->bus->bus_mtx) && !SCHEDULER_STOPPED()) {
3303 mtx_unlock(&xroot->udev->bus->bus_mtx);
3307 /* make sure that the transfer mutex is not locked */
3309 while (mtx_owned(xroot->xfer_mtx) && !SCHEDULER_STOPPED()) {
3310 mtx_unlock(xroot->xfer_mtx);
3314 /* Make sure cv_signal() and cv_broadcast() is not called */
3315 USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3316 USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3317 USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3318 USB_BUS_NON_GIANT_PROC(udev->bus)->up_msleep = 0;
3320 /* poll USB hardware */
3321 (udev->bus->methods->xfer_poll) (udev->bus);
3323 USB_BUS_LOCK(xroot->bus);
3325 /* check for clear stall */
3326 if (udev->ctrl_xfer[1] != NULL) {
3328 /* poll clear stall start */
3329 pm = &udev->cs_msg[0].hdr;
3330 (pm->pm_callback) (pm);
3331 /* poll clear stall done thread */
3332 pm = &udev->ctrl_xfer[1]->
3333 xroot->done_m[0].hdr;
3334 (pm->pm_callback) (pm);
3337 /* poll done thread */
3338 pm = &xroot->done_m[0].hdr;
3339 (pm->pm_callback) (pm);
3341 USB_BUS_UNLOCK(xroot->bus);
3343 /* restore transfer mutex */
3345 mtx_lock(xroot->xfer_mtx);
3347 /* restore BUS mutex */
3349 mtx_lock(&xroot->udev->bus->bus_mtx);
3354 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3355 uint8_t type, enum usb_dev_speed speed)
3357 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3358 [USB_SPEED_LOW] = 8,
3359 [USB_SPEED_FULL] = 64,
3360 [USB_SPEED_HIGH] = 1024,
3361 [USB_SPEED_VARIABLE] = 1024,
3362 [USB_SPEED_SUPER] = 1024,
3365 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3366 [USB_SPEED_LOW] = 0, /* invalid */
3367 [USB_SPEED_FULL] = 1023,
3368 [USB_SPEED_HIGH] = 1024,
3369 [USB_SPEED_VARIABLE] = 3584,
3370 [USB_SPEED_SUPER] = 1024,
3373 static const uint16_t control_min[USB_SPEED_MAX] = {
3374 [USB_SPEED_LOW] = 8,
3375 [USB_SPEED_FULL] = 8,
3376 [USB_SPEED_HIGH] = 64,
3377 [USB_SPEED_VARIABLE] = 512,
3378 [USB_SPEED_SUPER] = 512,
3381 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3382 [USB_SPEED_LOW] = 8,
3383 [USB_SPEED_FULL] = 8,
3384 [USB_SPEED_HIGH] = 512,
3385 [USB_SPEED_VARIABLE] = 512,
3386 [USB_SPEED_SUPER] = 1024,
3391 memset(ptr, 0, sizeof(*ptr));
3395 ptr->range.max = intr_range_max[speed];
3397 case UE_ISOCHRONOUS:
3398 ptr->range.max = isoc_range_max[speed];
3401 if (type == UE_BULK)
3402 temp = bulk_min[speed];
3403 else /* UE_CONTROL */
3404 temp = control_min[speed];
3406 /* default is fixed */
3407 ptr->fixed[0] = temp;
3408 ptr->fixed[1] = temp;
3409 ptr->fixed[2] = temp;
3410 ptr->fixed[3] = temp;
3412 if (speed == USB_SPEED_FULL) {
3413 /* multiple sizes */
3418 if ((speed == USB_SPEED_VARIABLE) &&
3419 (type == UE_BULK)) {
3420 /* multiple sizes */
3421 ptr->fixed[2] = 1024;
3422 ptr->fixed[3] = 1536;
3429 usbd_xfer_softc(struct usb_xfer *xfer)
3431 return (xfer->priv_sc);
3435 usbd_xfer_get_priv(struct usb_xfer *xfer)
3437 return (xfer->priv_fifo);
3441 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3443 xfer->priv_fifo = ptr;
3447 usbd_xfer_state(struct usb_xfer *xfer)
3449 return (xfer->usb_state);
3453 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3456 case USB_FORCE_SHORT_XFER:
3457 xfer->flags.force_short_xfer = 1;
3459 case USB_SHORT_XFER_OK:
3460 xfer->flags.short_xfer_ok = 1;
3462 case USB_MULTI_SHORT_OK:
3463 xfer->flags.short_frames_ok = 1;
3465 case USB_MANUAL_STATUS:
3466 xfer->flags.manual_status = 1;
3472 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3475 case USB_FORCE_SHORT_XFER:
3476 xfer->flags.force_short_xfer = 0;
3478 case USB_SHORT_XFER_OK:
3479 xfer->flags.short_xfer_ok = 0;
3481 case USB_MULTI_SHORT_OK:
3482 xfer->flags.short_frames_ok = 0;
3484 case USB_MANUAL_STATUS:
3485 xfer->flags.manual_status = 0;
3491 * The following function returns in milliseconds when the isochronous
3492 * transfer was completed by the hardware. The returned value wraps
3493 * around 65536 milliseconds.
3496 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3498 return (xfer->isoc_time_complete);
3502 * The following function returns non-zero if the max packet size
3503 * field was clamped to a valid value. Else it returns zero.
3506 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3508 return (xfer->flags_int.maxp_was_clamped);