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 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 n_obj = (USB_PAGE_SIZE / size);
242 * Compute number of DMA chunks, rounded up
245 n_dma_pc = ((count + n_obj - 1) / n_obj);
250 * DMA memory is allocated once, but mapped twice. That's why
251 * there is one list for auto-free and another list for
252 * non-auto-free which only holds the mapping and not the
255 if (parm->buf == NULL) {
256 /* reserve memory (auto-free) */
257 parm->dma_page_ptr += n_dma_pc * n_dma_pg;
258 parm->dma_page_cache_ptr += n_dma_pc;
260 /* reserve memory (no-auto-free) */
261 parm->dma_page_ptr += count * n_dma_pg;
262 parm->xfer_page_cache_ptr += count;
265 for (x = 0; x != n_dma_pc; x++) {
266 /* need to initialize the page cache */
267 parm->dma_page_cache_ptr[x].tag_parent =
268 &parm->curr_xfer->xroot->dma_parent_tag;
270 for (x = 0; x != count; x++) {
271 /* need to initialize the page cache */
272 parm->xfer_page_cache_ptr[x].tag_parent =
273 &parm->curr_xfer->xroot->dma_parent_tag;
277 *ppc = parm->xfer_page_cache_ptr;
279 r = count; /* set remainder count */
280 z = n_obj * size; /* set allocation size */
281 pc = parm->xfer_page_cache_ptr;
282 pg = parm->dma_page_ptr;
284 for (x = 0; x != n_dma_pc; x++) {
287 /* compute last remainder */
291 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
293 return (1); /* failure */
295 /* Set beginning of current buffer */
296 buf = parm->dma_page_cache_ptr->buffer;
297 /* Make room for one DMA page cache and one page */
298 parm->dma_page_cache_ptr++;
301 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
303 /* Load sub-chunk into DMA */
304 if (usb_pc_dmamap_create(pc, size)) {
305 return (1); /* failure */
307 pc->buffer = USB_ADD_BYTES(buf, y * size);
310 mtx_lock(pc->tag_parent->mtx);
311 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
312 mtx_unlock(pc->tag_parent->mtx);
313 return (1); /* failure */
315 mtx_unlock(pc->tag_parent->mtx);
319 parm->xfer_page_cache_ptr = pc;
320 parm->dma_page_ptr = pg;
325 /*------------------------------------------------------------------------*
326 * usbd_transfer_setup_sub - transfer setup subroutine
328 * This function must be called from the "xfer_setup" callback of the
329 * USB Host or Device controller driver when setting up an USB
330 * transfer. This function will setup correct packet sizes, buffer
331 * sizes, flags and more, that are stored in the "usb_xfer"
333 *------------------------------------------------------------------------*/
335 usbd_transfer_setup_sub(struct usb_setup_params *parm)
341 struct usb_xfer *xfer = parm->curr_xfer;
342 const struct usb_config *setup = parm->curr_setup;
343 struct usb_endpoint_ss_comp_descriptor *ecomp;
344 struct usb_endpoint_descriptor *edesc;
345 struct usb_std_packet_size std_size;
346 usb_frcount_t n_frlengths;
347 usb_frcount_t n_frbuffers;
354 * Sanity check. The following parameters must be initialized before
355 * calling this function.
357 if ((parm->hc_max_packet_size == 0) ||
358 (parm->hc_max_packet_count == 0) ||
359 (parm->hc_max_frame_size == 0)) {
360 parm->err = USB_ERR_INVAL;
363 edesc = xfer->endpoint->edesc;
364 ecomp = xfer->endpoint->ecomp;
366 type = (edesc->bmAttributes & UE_XFERTYPE);
368 xfer->flags = setup->flags;
369 xfer->nframes = setup->frames;
370 xfer->timeout = setup->timeout;
371 xfer->callback = setup->callback;
372 xfer->interval = setup->interval;
373 xfer->endpointno = edesc->bEndpointAddress;
374 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
375 xfer->max_packet_count = 1;
376 /* make a shadow copy: */
377 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
379 parm->bufsize = setup->bufsize;
381 switch (parm->speed) {
386 xfer->max_packet_count +=
387 (xfer->max_packet_size >> 11) & 3;
389 /* check for invalid max packet count */
390 if (xfer->max_packet_count > 3)
391 xfer->max_packet_count = 3;
396 xfer->max_packet_size &= 0x7FF;
398 case USB_SPEED_SUPER:
399 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
402 xfer->max_packet_count += ecomp->bMaxBurst;
404 if ((xfer->max_packet_count == 0) ||
405 (xfer->max_packet_count > 16))
406 xfer->max_packet_count = 16;
410 xfer->max_packet_count = 1;
416 mult = UE_GET_SS_ISO_MULT(
417 ecomp->bmAttributes) + 1;
421 xfer->max_packet_count *= mult;
427 xfer->max_packet_size &= 0x7FF;
432 /* range check "max_packet_count" */
434 if (xfer->max_packet_count > parm->hc_max_packet_count) {
435 xfer->max_packet_count = parm->hc_max_packet_count;
438 /* store max packet size value before filtering */
440 maxp_old = xfer->max_packet_size;
442 /* filter "wMaxPacketSize" according to HC capabilities */
444 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
445 (xfer->max_packet_size == 0)) {
446 xfer->max_packet_size = parm->hc_max_packet_size;
448 /* filter "wMaxPacketSize" according to standard sizes */
450 usbd_get_std_packet_size(&std_size, type, parm->speed);
452 if (std_size.range.min || std_size.range.max) {
454 if (xfer->max_packet_size < std_size.range.min) {
455 xfer->max_packet_size = std_size.range.min;
457 if (xfer->max_packet_size > std_size.range.max) {
458 xfer->max_packet_size = std_size.range.max;
462 if (xfer->max_packet_size >= std_size.fixed[3]) {
463 xfer->max_packet_size = std_size.fixed[3];
464 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
465 xfer->max_packet_size = std_size.fixed[2];
466 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
467 xfer->max_packet_size = std_size.fixed[1];
469 /* only one possibility left */
470 xfer->max_packet_size = std_size.fixed[0];
475 * Check if the max packet size was outside its allowed range
476 * and clamped to a valid value:
478 if (maxp_old != xfer->max_packet_size)
479 xfer->flags_int.maxp_was_clamped = 1;
481 /* compute "max_frame_size" */
483 usbd_update_max_frame_size(xfer);
485 /* check interrupt interval and transfer pre-delay */
487 if (type == UE_ISOCHRONOUS) {
489 uint16_t frame_limit;
491 xfer->interval = 0; /* not used, must be zero */
492 xfer->flags_int.isochronous_xfr = 1; /* set flag */
494 if (xfer->timeout == 0) {
496 * set a default timeout in
497 * case something goes wrong!
499 xfer->timeout = 1000 / 4;
501 switch (parm->speed) {
504 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
508 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
509 xfer->fps_shift = edesc->bInterval;
510 if (xfer->fps_shift > 0)
512 if (xfer->fps_shift > 3)
514 if (xfer->flags.pre_scale_frames != 0)
515 xfer->nframes <<= (3 - xfer->fps_shift);
519 if (xfer->nframes > frame_limit) {
521 * this is not going to work
524 parm->err = USB_ERR_INVAL;
527 if (xfer->nframes == 0) {
529 * this is not a valid value
531 parm->err = USB_ERR_ZERO_NFRAMES;
537 * If a value is specified use that else check the
538 * endpoint descriptor!
540 if (type == UE_INTERRUPT) {
544 if (xfer->interval == 0) {
546 xfer->interval = edesc->bInterval;
548 switch (parm->speed) {
554 if (xfer->interval < 4)
556 else if (xfer->interval > 16)
557 xfer->interval = (1 << (16 - 4));
560 (1 << (xfer->interval - 4));
565 if (xfer->interval == 0) {
567 * One millisecond is the smallest
568 * interval we support:
576 while ((temp != 0) && (temp < xfer->interval)) {
581 switch (parm->speed) {
586 xfer->fps_shift += 3;
593 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
594 * to be equal to zero when setting up USB transfers, hence
595 * this leads to alot of extra code in the USB kernel.
598 if ((xfer->max_frame_size == 0) ||
599 (xfer->max_packet_size == 0)) {
603 if ((parm->bufsize <= MIN_PKT) &&
604 (type != UE_CONTROL) &&
608 xfer->max_packet_size = MIN_PKT;
609 xfer->max_packet_count = 1;
610 parm->bufsize = 0; /* automatic setup length */
611 usbd_update_max_frame_size(xfer);
614 parm->err = USB_ERR_ZERO_MAXP;
623 * check if we should setup a default
627 if (parm->bufsize == 0) {
629 parm->bufsize = xfer->max_frame_size;
631 if (type == UE_ISOCHRONOUS) {
632 parm->bufsize *= xfer->nframes;
636 * check if we are about to setup a proxy
640 if (xfer->flags.proxy_buffer) {
642 /* round bufsize up */
644 parm->bufsize += (xfer->max_frame_size - 1);
646 if (parm->bufsize < xfer->max_frame_size) {
647 /* length wrapped around */
648 parm->err = USB_ERR_INVAL;
651 /* subtract remainder */
653 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
655 /* add length of USB device request structure, if any */
657 if (type == UE_CONTROL) {
658 parm->bufsize += REQ_SIZE; /* SETUP message */
661 xfer->max_data_length = parm->bufsize;
663 /* Setup "n_frlengths" and "n_frbuffers" */
665 if (type == UE_ISOCHRONOUS) {
666 n_frlengths = xfer->nframes;
670 if (type == UE_CONTROL) {
671 xfer->flags_int.control_xfr = 1;
672 if (xfer->nframes == 0) {
673 if (parm->bufsize <= REQ_SIZE) {
675 * there will never be any data
684 if (xfer->nframes == 0) {
689 n_frlengths = xfer->nframes;
690 n_frbuffers = xfer->nframes;
694 * check if we have room for the
695 * USB device request structure:
698 if (type == UE_CONTROL) {
700 if (xfer->max_data_length < REQ_SIZE) {
701 /* length wrapped around or too small bufsize */
702 parm->err = USB_ERR_INVAL;
705 xfer->max_data_length -= REQ_SIZE;
708 * Setup "frlengths" and shadow "frlengths" for keeping the
709 * initial frame lengths when a USB transfer is complete. This
710 * information is useful when computing isochronous offsets.
712 xfer->frlengths = parm->xfer_length_ptr;
713 parm->xfer_length_ptr += 2 * n_frlengths;
715 /* setup "frbuffers" */
716 xfer->frbuffers = parm->xfer_page_cache_ptr;
717 parm->xfer_page_cache_ptr += n_frbuffers;
719 /* initialize max frame count */
720 xfer->max_frame_count = xfer->nframes;
723 * check if we need to setup
727 if (!xfer->flags.ext_buffer) {
729 struct usb_page_search page_info;
730 struct usb_page_cache *pc;
732 if (usbd_transfer_setup_sub_malloc(parm,
733 &pc, parm->bufsize, 1, 1)) {
734 parm->err = USB_ERR_NOMEM;
735 } else if (parm->buf != NULL) {
737 usbd_get_page(pc, 0, &page_info);
739 xfer->local_buffer = page_info.buffer;
741 usbd_xfer_set_frame_offset(xfer, 0, 0);
743 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
744 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
749 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
751 if (parm->buf != NULL) {
753 USB_ADD_BYTES(parm->buf, parm->size[0]);
755 usbd_xfer_set_frame_offset(xfer, 0, 0);
757 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
758 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
761 parm->size[0] += parm->bufsize;
763 /* align data again */
764 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
768 * Compute maximum buffer size
771 if (parm->bufsize_max < parm->bufsize) {
772 parm->bufsize_max = parm->bufsize;
775 if (xfer->flags_int.bdma_enable) {
777 * Setup "dma_page_ptr".
779 * Proof for formula below:
781 * Assume there are three USB frames having length "a", "b" and
782 * "c". These USB frames will at maximum need "z"
783 * "usb_page" structures. "z" is given by:
785 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
786 * ((c / USB_PAGE_SIZE) + 2);
788 * Constraining "a", "b" and "c" like this:
790 * (a + b + c) <= parm->bufsize
794 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
796 * Here is the general formula:
798 xfer->dma_page_ptr = parm->dma_page_ptr;
799 parm->dma_page_ptr += (2 * n_frbuffers);
800 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
804 /* correct maximum data length */
805 xfer->max_data_length = 0;
807 /* subtract USB frame remainder from "hc_max_frame_size" */
809 xfer->max_hc_frame_size =
810 (parm->hc_max_frame_size -
811 (parm->hc_max_frame_size % xfer->max_frame_size));
813 if (xfer->max_hc_frame_size == 0) {
814 parm->err = USB_ERR_INVAL;
818 /* initialize frame buffers */
821 for (x = 0; x != n_frbuffers; x++) {
822 xfer->frbuffers[x].tag_parent =
823 &xfer->xroot->dma_parent_tag;
825 if (xfer->flags_int.bdma_enable &&
826 (parm->bufsize_max > 0)) {
828 if (usb_pc_dmamap_create(
830 parm->bufsize_max)) {
831 parm->err = USB_ERR_NOMEM;
841 * Set some dummy values so that we avoid division by zero:
843 xfer->max_hc_frame_size = 1;
844 xfer->max_frame_size = 1;
845 xfer->max_packet_size = 1;
846 xfer->max_data_length = 0;
848 xfer->max_frame_count = 0;
852 /*------------------------------------------------------------------------*
853 * usbd_transfer_setup - setup an array of USB transfers
855 * NOTE: You must always call "usbd_transfer_unsetup" after calling
856 * "usbd_transfer_setup" if success was returned.
858 * The idea is that the USB device driver should pre-allocate all its
859 * transfers by one call to this function.
864 *------------------------------------------------------------------------*/
866 usbd_transfer_setup(struct usb_device *udev,
867 const uint8_t *ifaces, struct usb_xfer **ppxfer,
868 const struct usb_config *setup_start, uint16_t n_setup,
869 void *priv_sc, struct mtx *xfer_mtx)
871 const struct usb_config *setup_end = setup_start + n_setup;
872 const struct usb_config *setup;
873 struct usb_setup_params *parm;
874 struct usb_endpoint *ep;
875 struct usb_xfer_root *info;
876 struct usb_xfer *xfer;
878 usb_error_t error = 0;
883 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
884 "usbd_transfer_setup can sleep!");
886 /* do some checking first */
889 DPRINTFN(6, "setup array has zero length!\n");
890 return (USB_ERR_INVAL);
893 DPRINTFN(6, "ifaces array is NULL!\n");
894 return (USB_ERR_INVAL);
896 if (xfer_mtx == NULL) {
897 DPRINTFN(6, "using global lock\n");
901 /* more sanity checks */
903 for (setup = setup_start, n = 0;
904 setup != setup_end; setup++, n++) {
905 if (setup->bufsize == (usb_frlength_t)-1) {
906 error = USB_ERR_BAD_BUFSIZE;
907 DPRINTF("invalid bufsize\n");
909 if (setup->callback == NULL) {
910 error = USB_ERR_NO_CALLBACK;
911 DPRINTF("no callback\n");
919 /* Protect scratch area */
920 do_unlock = usbd_enum_lock(udev);
925 parm = &udev->scratch.xfer_setup[0].parm;
926 memset(parm, 0, sizeof(*parm));
929 parm->speed = usbd_get_speed(udev);
930 parm->hc_max_packet_count = 1;
932 if (parm->speed >= USB_SPEED_MAX) {
933 parm->err = USB_ERR_INVAL;
936 /* setup all transfers */
942 * Initialize the "usb_xfer_root" structure,
943 * which is common for all our USB transfers.
945 info = USB_ADD_BYTES(buf, 0);
947 info->memory_base = buf;
948 info->memory_size = parm->size[0];
951 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
952 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
954 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
955 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
957 cv_init(&info->cv_drain, "WDRAIN");
959 info->xfer_mtx = xfer_mtx;
961 usb_dma_tag_setup(&info->dma_parent_tag,
962 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
963 xfer_mtx, &usb_bdma_done_event, 32, parm->dma_tag_max);
966 info->bus = udev->bus;
969 TAILQ_INIT(&info->done_q.head);
970 info->done_q.command = &usbd_callback_wrapper;
972 TAILQ_INIT(&info->dma_q.head);
973 info->dma_q.command = &usb_bdma_work_loop;
975 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
976 info->done_m[0].xroot = info;
977 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
978 info->done_m[1].xroot = info;
981 * In device side mode control endpoint
982 * requests need to run from a separate
983 * context, else there is a chance of
986 if (setup_start == usb_control_ep_cfg)
988 USB_BUS_CONTROL_XFER_PROC(udev->bus);
989 else if (xfer_mtx == &Giant)
991 USB_BUS_GIANT_PROC(udev->bus);
994 USB_BUS_NON_GIANT_PROC(udev->bus);
1000 parm->size[0] += sizeof(info[0]);
1002 for (setup = setup_start, n = 0;
1003 setup != setup_end; setup++, n++) {
1005 /* skip USB transfers without callbacks: */
1006 if (setup->callback == NULL) {
1009 /* see if there is a matching endpoint */
1010 ep = usbd_get_endpoint(udev,
1011 ifaces[setup->if_index], setup);
1014 * Check that the USB PIPE is valid and that
1015 * the endpoint mode is proper.
1017 * Make sure we don't allocate a streams
1018 * transfer when such a combination is not
1021 if ((ep == NULL) || (ep->methods == NULL) ||
1022 ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1023 (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1024 (setup->stream_id != 0 &&
1025 (setup->stream_id >= USB_MAX_EP_STREAMS ||
1026 (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1027 if (setup->flags.no_pipe_ok)
1029 if ((setup->usb_mode != USB_MODE_DUAL) &&
1030 (setup->usb_mode != udev->flags.usb_mode))
1032 parm->err = USB_ERR_NO_PIPE;
1036 /* align data properly */
1037 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1039 /* store current setup pointer */
1040 parm->curr_setup = setup;
1044 * Common initialization of the
1045 * "usb_xfer" structure.
1047 xfer = USB_ADD_BYTES(buf, parm->size[0]);
1048 xfer->address = udev->address;
1049 xfer->priv_sc = priv_sc;
1052 usb_callout_init_mtx(&xfer->timeout_handle,
1053 &udev->bus->bus_mtx, 0);
1056 * Setup a dummy xfer, hence we are
1057 * writing to the "usb_xfer"
1058 * structure pointed to by "xfer"
1059 * before we have allocated any
1062 xfer = &udev->scratch.xfer_setup[0].dummy;
1063 memset(xfer, 0, sizeof(*xfer));
1067 /* set transfer endpoint pointer */
1068 xfer->endpoint = ep;
1070 /* set transfer stream ID */
1071 xfer->stream_id = setup->stream_id;
1073 parm->size[0] += sizeof(xfer[0]);
1074 parm->methods = xfer->endpoint->methods;
1075 parm->curr_xfer = xfer;
1078 * Call the Host or Device controller transfer
1081 (udev->bus->methods->xfer_setup) (parm);
1083 /* check for error */
1089 * Increment the endpoint refcount. This
1090 * basically prevents setting a new
1091 * configuration and alternate setting
1092 * when USB transfers are in use on
1093 * the given interface. Search the USB
1094 * code for "endpoint->refcount_alloc" if you
1095 * want more information.
1097 USB_BUS_LOCK(info->bus);
1098 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1099 parm->err = USB_ERR_INVAL;
1101 xfer->endpoint->refcount_alloc++;
1103 if (xfer->endpoint->refcount_alloc == 0)
1104 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1105 USB_BUS_UNLOCK(info->bus);
1108 * Whenever we set ppxfer[] then we
1109 * also need to increment the
1112 info->setup_refcount++;
1115 * Transfer is successfully setup and
1121 /* check for error */
1126 if (buf != NULL || parm->err != 0)
1129 /* if no transfers, nothing to do */
1133 /* align data properly */
1134 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1136 /* store offset temporarily */
1137 parm->size[1] = parm->size[0];
1140 * The number of DMA tags required depends on
1141 * the number of endpoints. The current estimate
1142 * for maximum number of DMA tags per endpoint
1144 * 1) for loading memory
1145 * 2) for allocating memory
1146 * 3) for fixing memory [UHCI]
1148 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1151 * DMA tags for QH, TD, Data and more.
1153 parm->dma_tag_max += 8;
1155 parm->dma_tag_p += parm->dma_tag_max;
1157 parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1160 /* align data properly */
1161 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1163 /* store offset temporarily */
1164 parm->size[3] = parm->size[0];
1166 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1169 /* align data properly */
1170 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1172 /* store offset temporarily */
1173 parm->size[4] = parm->size[0];
1175 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1178 /* store end offset temporarily */
1179 parm->size[5] = parm->size[0];
1181 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1184 /* store end offset temporarily */
1186 parm->size[2] = parm->size[0];
1188 /* align data properly */
1189 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1191 parm->size[6] = parm->size[0];
1193 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1196 /* align data properly */
1197 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1199 /* allocate zeroed memory */
1200 buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1203 parm->err = USB_ERR_NOMEM;
1204 DPRINTFN(0, "cannot allocate memory block for "
1205 "configuration (%d bytes)\n",
1209 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1210 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1211 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1212 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1213 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1218 if (info->setup_refcount == 0) {
1220 * "usbd_transfer_unsetup_sub" will unlock
1221 * the bus mutex before returning !
1223 USB_BUS_LOCK(info->bus);
1225 /* something went wrong */
1226 usbd_transfer_unsetup_sub(info, 0);
1230 /* check if any errors happened */
1232 usbd_transfer_unsetup(ppxfer, n_setup);
1237 usbd_enum_unlock(udev);
1242 /*------------------------------------------------------------------------*
1243 * usbd_transfer_unsetup_sub - factored out code
1244 *------------------------------------------------------------------------*/
1246 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1249 struct usb_page_cache *pc;
1252 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1254 /* wait for any outstanding DMA operations */
1258 temp = usbd_get_dma_delay(info->udev);
1260 usb_pause_mtx(&info->bus->bus_mtx,
1261 USB_MS_TO_TICKS(temp));
1265 /* make sure that our done messages are not queued anywhere */
1266 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1268 USB_BUS_UNLOCK(info->bus);
1271 /* free DMA'able memory, if any */
1272 pc = info->dma_page_cache_start;
1273 while (pc != info->dma_page_cache_end) {
1274 usb_pc_free_mem(pc);
1278 /* free DMA maps in all "xfer->frbuffers" */
1279 pc = info->xfer_page_cache_start;
1280 while (pc != info->xfer_page_cache_end) {
1281 usb_pc_dmamap_destroy(pc);
1285 /* free all DMA tags */
1286 usb_dma_tag_unsetup(&info->dma_parent_tag);
1289 cv_destroy(&info->cv_drain);
1292 * free the "memory_base" last, hence the "info" structure is
1293 * contained within the "memory_base"!
1295 free(info->memory_base, M_USB);
1298 /*------------------------------------------------------------------------*
1299 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1301 * NOTE: All USB transfers in progress will get called back passing
1302 * the error code "USB_ERR_CANCELLED" before this function
1304 *------------------------------------------------------------------------*/
1306 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1308 struct usb_xfer *xfer;
1309 struct usb_xfer_root *info;
1310 uint8_t needs_delay = 0;
1312 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1313 "usbd_transfer_unsetup can sleep!");
1316 xfer = pxfer[n_setup];
1323 USB_XFER_LOCK(xfer);
1324 USB_BUS_LOCK(info->bus);
1327 * HINT: when you start/stop a transfer, it might be a
1328 * good idea to directly use the "pxfer[]" structure:
1330 * usbd_transfer_start(sc->pxfer[0]);
1331 * usbd_transfer_stop(sc->pxfer[0]);
1333 * That way, if your code has many parts that will not
1334 * stop running under the same lock, in other words
1335 * "xfer_mtx", the usbd_transfer_start and
1336 * usbd_transfer_stop functions will simply return
1337 * when they detect a NULL pointer argument.
1339 * To avoid any races we clear the "pxfer[]" pointer
1340 * while holding the private mutex of the driver:
1342 pxfer[n_setup] = NULL;
1344 USB_BUS_UNLOCK(info->bus);
1345 USB_XFER_UNLOCK(xfer);
1347 usbd_transfer_drain(xfer);
1350 if (xfer->flags_int.bdma_enable)
1354 * NOTE: default endpoint does not have an
1355 * interface, even if endpoint->iface_index == 0
1357 USB_BUS_LOCK(info->bus);
1358 xfer->endpoint->refcount_alloc--;
1359 USB_BUS_UNLOCK(info->bus);
1361 usb_callout_drain(&xfer->timeout_handle);
1363 USB_BUS_LOCK(info->bus);
1365 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1366 "reference count\n"));
1368 info->setup_refcount--;
1370 if (info->setup_refcount == 0) {
1371 usbd_transfer_unsetup_sub(info,
1374 USB_BUS_UNLOCK(info->bus);
1379 /*------------------------------------------------------------------------*
1380 * usbd_control_transfer_init - factored out code
1382 * In USB Device Mode we have to wait for the SETUP packet which
1383 * containst the "struct usb_device_request" structure, before we can
1384 * transfer any data. In USB Host Mode we already have the SETUP
1385 * packet at the moment the USB transfer is started. This leads us to
1386 * having to setup the USB transfer at two different places in
1387 * time. This function just contains factored out control transfer
1388 * initialisation code, so that we don't duplicate the code.
1389 *------------------------------------------------------------------------*/
1391 usbd_control_transfer_init(struct usb_xfer *xfer)
1393 struct usb_device_request req;
1395 /* copy out the USB request header */
1397 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1399 /* setup remainder */
1401 xfer->flags_int.control_rem = UGETW(req.wLength);
1403 /* copy direction to endpoint variable */
1405 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1407 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1410 /*------------------------------------------------------------------------*
1411 * usbd_setup_ctrl_transfer
1413 * This function handles initialisation of control transfers. Control
1414 * transfers are special in that regard that they can both transmit
1420 *------------------------------------------------------------------------*/
1422 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1426 /* Check for control endpoint stall */
1427 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1428 /* the control transfer is no longer active */
1429 xfer->flags_int.control_stall = 1;
1430 xfer->flags_int.control_act = 0;
1432 /* don't stall control transfer by default */
1433 xfer->flags_int.control_stall = 0;
1436 /* Check for invalid number of frames */
1437 if (xfer->nframes > 2) {
1439 * If you need to split a control transfer, you
1440 * have to do one part at a time. Only with
1441 * non-control transfers you can do multiple
1444 DPRINTFN(0, "Too many frames: %u\n",
1445 (unsigned int)xfer->nframes);
1450 * Check if there is a control
1451 * transfer in progress:
1453 if (xfer->flags_int.control_act) {
1455 if (xfer->flags_int.control_hdr) {
1457 /* clear send header flag */
1459 xfer->flags_int.control_hdr = 0;
1461 /* setup control transfer */
1462 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1463 usbd_control_transfer_init(xfer);
1466 /* get data length */
1472 /* the size of the SETUP structure is hardcoded ! */
1474 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1475 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1476 xfer->frlengths[0], sizeof(struct
1477 usb_device_request));
1480 /* check USB mode */
1481 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1483 /* check number of frames */
1484 if (xfer->nframes != 1) {
1486 * We need to receive the setup
1487 * message first so that we know the
1490 DPRINTF("Misconfigured transfer\n");
1494 * Set a dummy "control_rem" value. This
1495 * variable will be overwritten later by a
1496 * call to "usbd_control_transfer_init()" !
1498 xfer->flags_int.control_rem = 0xFFFF;
1501 /* setup "endpoint" and "control_rem" */
1503 usbd_control_transfer_init(xfer);
1506 /* set transfer-header flag */
1508 xfer->flags_int.control_hdr = 1;
1510 /* get data length */
1512 len = (xfer->sumlen - sizeof(struct usb_device_request));
1515 /* check if there is a length mismatch */
1517 if (len > xfer->flags_int.control_rem) {
1518 DPRINTFN(0, "Length (%d) greater than "
1519 "remaining length (%d)\n", len,
1520 xfer->flags_int.control_rem);
1523 /* check if we are doing a short transfer */
1525 if (xfer->flags.force_short_xfer) {
1526 xfer->flags_int.control_rem = 0;
1528 if ((len != xfer->max_data_length) &&
1529 (len != xfer->flags_int.control_rem) &&
1530 (xfer->nframes != 1)) {
1531 DPRINTFN(0, "Short control transfer without "
1532 "force_short_xfer set\n");
1535 xfer->flags_int.control_rem -= len;
1538 /* the status part is executed when "control_act" is 0 */
1540 if ((xfer->flags_int.control_rem > 0) ||
1541 (xfer->flags.manual_status)) {
1542 /* don't execute the STATUS stage yet */
1543 xfer->flags_int.control_act = 1;
1546 if ((!xfer->flags_int.control_hdr) &&
1547 (xfer->nframes == 1)) {
1549 * This is not a valid operation!
1551 DPRINTFN(0, "Invalid parameter "
1556 /* time to execute the STATUS stage */
1557 xfer->flags_int.control_act = 0;
1559 return (0); /* success */
1562 return (1); /* failure */
1565 /*------------------------------------------------------------------------*
1566 * usbd_transfer_submit - start USB hardware for the given transfer
1568 * This function should only be called from the USB callback.
1569 *------------------------------------------------------------------------*/
1571 usbd_transfer_submit(struct usb_xfer *xfer)
1573 struct usb_xfer_root *info;
1574 struct usb_bus *bus;
1580 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1581 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1585 if (USB_DEBUG_VAR > 0) {
1588 usb_dump_endpoint(xfer->endpoint);
1590 USB_BUS_UNLOCK(bus);
1594 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1595 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1597 /* Only open the USB transfer once! */
1598 if (!xfer->flags_int.open) {
1599 xfer->flags_int.open = 1;
1604 (xfer->endpoint->methods->open) (xfer);
1605 USB_BUS_UNLOCK(bus);
1607 /* set "transferring" flag */
1608 xfer->flags_int.transferring = 1;
1611 /* increment power reference */
1612 usbd_transfer_power_ref(xfer, 1);
1615 * Check if the transfer is waiting on a queue, most
1616 * frequently the "done_q":
1618 if (xfer->wait_queue) {
1620 usbd_transfer_dequeue(xfer);
1621 USB_BUS_UNLOCK(bus);
1623 /* clear "did_dma_delay" flag */
1624 xfer->flags_int.did_dma_delay = 0;
1626 /* clear "did_close" flag */
1627 xfer->flags_int.did_close = 0;
1630 /* clear "bdma_setup" flag */
1631 xfer->flags_int.bdma_setup = 0;
1633 /* by default we cannot cancel any USB transfer immediately */
1634 xfer->flags_int.can_cancel_immed = 0;
1636 /* clear lengths and frame counts by default */
1641 /* clear any previous errors */
1644 /* Check if the device is still alive */
1645 if (info->udev->state < USB_STATE_POWERED) {
1648 * Must return cancelled error code else
1649 * device drivers can hang.
1651 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1652 USB_BUS_UNLOCK(bus);
1657 if (xfer->nframes == 0) {
1658 if (xfer->flags.stall_pipe) {
1660 * Special case - want to stall without transferring
1663 DPRINTF("xfer=%p nframes=0: stall "
1664 "or clear stall!\n", xfer);
1666 xfer->flags_int.can_cancel_immed = 1;
1667 /* start the transfer */
1668 usb_command_wrapper(&xfer->endpoint->
1669 endpoint_q[xfer->stream_id], xfer);
1670 USB_BUS_UNLOCK(bus);
1674 usbd_transfer_done(xfer, USB_ERR_INVAL);
1675 USB_BUS_UNLOCK(bus);
1678 /* compute some variables */
1680 for (x = 0; x != xfer->nframes; x++) {
1681 /* make a copy of the frlenghts[] */
1682 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1683 /* compute total transfer length */
1684 xfer->sumlen += xfer->frlengths[x];
1685 if (xfer->sumlen < xfer->frlengths[x]) {
1686 /* length wrapped around */
1688 usbd_transfer_done(xfer, USB_ERR_INVAL);
1689 USB_BUS_UNLOCK(bus);
1694 /* clear some internal flags */
1696 xfer->flags_int.short_xfer_ok = 0;
1697 xfer->flags_int.short_frames_ok = 0;
1699 /* check if this is a control transfer */
1701 if (xfer->flags_int.control_xfr) {
1703 if (usbd_setup_ctrl_transfer(xfer)) {
1705 usbd_transfer_done(xfer, USB_ERR_STALLED);
1706 USB_BUS_UNLOCK(bus);
1711 * Setup filtered version of some transfer flags,
1712 * in case of data read direction
1714 if (USB_GET_DATA_ISREAD(xfer)) {
1716 if (xfer->flags.short_frames_ok) {
1717 xfer->flags_int.short_xfer_ok = 1;
1718 xfer->flags_int.short_frames_ok = 1;
1719 } else if (xfer->flags.short_xfer_ok) {
1720 xfer->flags_int.short_xfer_ok = 1;
1722 /* check for control transfer */
1723 if (xfer->flags_int.control_xfr) {
1725 * 1) Control transfers do not support
1726 * reception of multiple short USB
1727 * frames in host mode and device side
1728 * mode, with exception of:
1730 * 2) Due to sometimes buggy device
1731 * side firmware we need to do a
1732 * STATUS stage in case of short
1733 * control transfers in USB host mode.
1734 * The STATUS stage then becomes the
1735 * "alt_next" to the DATA stage.
1737 xfer->flags_int.short_frames_ok = 1;
1742 * Check if BUS-DMA support is enabled and try to load virtual
1743 * buffers into DMA, if any:
1746 if (xfer->flags_int.bdma_enable) {
1747 /* insert the USB transfer last in the BUS-DMA queue */
1748 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1753 * Enter the USB transfer into the Host Controller or
1754 * Device Controller schedule:
1756 usbd_pipe_enter(xfer);
1759 /*------------------------------------------------------------------------*
1760 * usbd_pipe_enter - factored out code
1761 *------------------------------------------------------------------------*/
1763 usbd_pipe_enter(struct usb_xfer *xfer)
1765 struct usb_endpoint *ep;
1767 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1769 USB_BUS_LOCK(xfer->xroot->bus);
1771 ep = xfer->endpoint;
1775 /* the transfer can now be cancelled */
1776 xfer->flags_int.can_cancel_immed = 1;
1778 /* enter the transfer */
1779 (ep->methods->enter) (xfer);
1781 /* check for transfer error */
1783 /* some error has happened */
1784 usbd_transfer_done(xfer, 0);
1785 USB_BUS_UNLOCK(xfer->xroot->bus);
1789 /* start the transfer */
1790 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1791 USB_BUS_UNLOCK(xfer->xroot->bus);
1794 /*------------------------------------------------------------------------*
1795 * usbd_transfer_start - start an USB transfer
1797 * NOTE: Calling this function more than one time will only
1798 * result in a single transfer start, until the USB transfer
1800 *------------------------------------------------------------------------*/
1802 usbd_transfer_start(struct usb_xfer *xfer)
1805 /* transfer is gone */
1808 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1810 /* mark the USB transfer started */
1812 if (!xfer->flags_int.started) {
1813 /* lock the BUS lock to avoid races updating flags_int */
1814 USB_BUS_LOCK(xfer->xroot->bus);
1815 xfer->flags_int.started = 1;
1816 USB_BUS_UNLOCK(xfer->xroot->bus);
1818 /* check if the USB transfer callback is already transferring */
1820 if (xfer->flags_int.transferring) {
1823 USB_BUS_LOCK(xfer->xroot->bus);
1824 /* call the USB transfer callback */
1825 usbd_callback_ss_done_defer(xfer);
1826 USB_BUS_UNLOCK(xfer->xroot->bus);
1829 /*------------------------------------------------------------------------*
1830 * usbd_transfer_stop - stop an USB transfer
1832 * NOTE: Calling this function more than one time will only
1833 * result in a single transfer stop.
1834 * NOTE: When this function returns it is not safe to free nor
1835 * reuse any DMA buffers. See "usbd_transfer_drain()".
1836 *------------------------------------------------------------------------*/
1838 usbd_transfer_stop(struct usb_xfer *xfer)
1840 struct usb_endpoint *ep;
1843 /* transfer is gone */
1846 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1848 /* check if the USB transfer was ever opened */
1850 if (!xfer->flags_int.open) {
1851 if (xfer->flags_int.started) {
1852 /* nothing to do except clearing the "started" flag */
1853 /* lock the BUS lock to avoid races updating flags_int */
1854 USB_BUS_LOCK(xfer->xroot->bus);
1855 xfer->flags_int.started = 0;
1856 USB_BUS_UNLOCK(xfer->xroot->bus);
1860 /* try to stop the current USB transfer */
1862 USB_BUS_LOCK(xfer->xroot->bus);
1863 /* override any previous error */
1864 xfer->error = USB_ERR_CANCELLED;
1867 * Clear "open" and "started" when both private and USB lock
1868 * is locked so that we don't get a race updating "flags_int"
1870 xfer->flags_int.open = 0;
1871 xfer->flags_int.started = 0;
1874 * Check if we can cancel the USB transfer immediately.
1876 if (xfer->flags_int.transferring) {
1877 if (xfer->flags_int.can_cancel_immed &&
1878 (!xfer->flags_int.did_close)) {
1881 * The following will lead to an USB_ERR_CANCELLED
1882 * error code being passed to the USB callback.
1884 (xfer->endpoint->methods->close) (xfer);
1885 /* only close once */
1886 xfer->flags_int.did_close = 1;
1888 /* need to wait for the next done callback */
1893 /* close here and now */
1894 (xfer->endpoint->methods->close) (xfer);
1897 * Any additional DMA delay is done by
1898 * "usbd_transfer_unsetup()".
1902 * Special case. Check if we need to restart a blocked
1905 ep = xfer->endpoint;
1908 * If the current USB transfer is completing we need
1909 * to start the next one:
1911 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
1912 usb_command_wrapper(
1913 &ep->endpoint_q[xfer->stream_id], NULL);
1917 USB_BUS_UNLOCK(xfer->xroot->bus);
1920 /*------------------------------------------------------------------------*
1921 * usbd_transfer_pending
1923 * This function will check if an USB transfer is pending which is a
1924 * little bit complicated!
1927 * 1: Pending: The USB transfer will receive a callback in the future.
1928 *------------------------------------------------------------------------*/
1930 usbd_transfer_pending(struct usb_xfer *xfer)
1932 struct usb_xfer_root *info;
1933 struct usb_xfer_queue *pq;
1936 /* transfer is gone */
1939 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1941 if (xfer->flags_int.transferring) {
1945 USB_BUS_LOCK(xfer->xroot->bus);
1946 if (xfer->wait_queue) {
1947 /* we are waiting on a queue somewhere */
1948 USB_BUS_UNLOCK(xfer->xroot->bus);
1954 if (pq->curr == xfer) {
1955 /* we are currently scheduled for callback */
1956 USB_BUS_UNLOCK(xfer->xroot->bus);
1959 /* we are not pending */
1960 USB_BUS_UNLOCK(xfer->xroot->bus);
1964 /*------------------------------------------------------------------------*
1965 * usbd_transfer_drain
1967 * This function will stop the USB transfer and wait for any
1968 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
1969 * are loaded into DMA can safely be freed or reused after that this
1970 * function has returned.
1971 *------------------------------------------------------------------------*/
1973 usbd_transfer_drain(struct usb_xfer *xfer)
1975 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1976 "usbd_transfer_drain can sleep!");
1979 /* transfer is gone */
1982 if (xfer->xroot->xfer_mtx != &Giant) {
1983 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1985 USB_XFER_LOCK(xfer);
1987 usbd_transfer_stop(xfer);
1989 while (usbd_transfer_pending(xfer) ||
1990 xfer->flags_int.doing_callback) {
1993 * It is allowed that the callback can drop its
1994 * transfer mutex. In that case checking only
1995 * "usbd_transfer_pending()" is not enough to tell if
1996 * the USB transfer is fully drained. We also need to
1997 * check the internal "doing_callback" flag.
1999 xfer->flags_int.draining = 1;
2002 * Wait until the current outstanding USB
2003 * transfer is complete !
2005 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
2007 USB_XFER_UNLOCK(xfer);
2010 struct usb_page_cache *
2011 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2013 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2015 return (&xfer->frbuffers[frindex]);
2019 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2021 struct usb_page_search page_info;
2023 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2025 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2026 return (page_info.buffer);
2029 /*------------------------------------------------------------------------*
2030 * usbd_xfer_get_fps_shift
2032 * The following function is only useful for isochronous transfers. It
2033 * returns how many times the frame execution rate has been shifted
2039 *------------------------------------------------------------------------*/
2041 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2043 return (xfer->fps_shift);
2047 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2049 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2051 return (xfer->frlengths[frindex]);
2054 /*------------------------------------------------------------------------*
2055 * usbd_xfer_set_frame_data
2057 * This function sets the pointer of the buffer that should
2058 * loaded directly into DMA for the given USB frame. Passing "ptr"
2059 * equal to NULL while the corresponding "frlength" is greater
2060 * than zero gives undefined results!
2061 *------------------------------------------------------------------------*/
2063 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2064 void *ptr, usb_frlength_t len)
2066 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2068 /* set virtual address to load and length */
2069 xfer->frbuffers[frindex].buffer = ptr;
2070 usbd_xfer_set_frame_len(xfer, frindex, len);
2074 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2075 void **ptr, int *len)
2077 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2080 *ptr = xfer->frbuffers[frindex].buffer;
2082 *len = xfer->frlengths[frindex];
2085 /*------------------------------------------------------------------------*
2086 * usbd_xfer_old_frame_length
2088 * This function returns the framelength of the given frame at the
2089 * time the transfer was submitted. This function can be used to
2090 * compute the starting data pointer of the next isochronous frame
2091 * when an isochronous transfer has completed.
2092 *------------------------------------------------------------------------*/
2094 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2096 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2098 return (xfer->frlengths[frindex + xfer->max_frame_count]);
2102 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2106 *actlen = xfer->actlen;
2108 *sumlen = xfer->sumlen;
2109 if (aframes != NULL)
2110 *aframes = xfer->aframes;
2111 if (nframes != NULL)
2112 *nframes = xfer->nframes;
2115 /*------------------------------------------------------------------------*
2116 * usbd_xfer_set_frame_offset
2118 * This function sets the frame data buffer offset relative to the beginning
2119 * of the USB DMA buffer allocated for this USB transfer.
2120 *------------------------------------------------------------------------*/
2122 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2123 usb_frcount_t frindex)
2125 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2126 "when the USB buffer is external\n"));
2127 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2129 /* set virtual address to load */
2130 xfer->frbuffers[frindex].buffer =
2131 USB_ADD_BYTES(xfer->local_buffer, offset);
2135 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2141 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2147 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2153 usbd_xfer_max_frames(struct usb_xfer *xfer)
2155 return (xfer->max_frame_count);
2159 usbd_xfer_max_len(struct usb_xfer *xfer)
2161 return (xfer->max_data_length);
2165 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2167 return (xfer->max_frame_size);
2171 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2174 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2176 xfer->frlengths[frindex] = len;
2179 /*------------------------------------------------------------------------*
2180 * usb_callback_proc - factored out code
2182 * This function performs USB callbacks.
2183 *------------------------------------------------------------------------*/
2185 usb_callback_proc(struct usb_proc_msg *_pm)
2187 struct usb_done_msg *pm = (void *)_pm;
2188 struct usb_xfer_root *info = pm->xroot;
2190 /* Change locking order */
2191 USB_BUS_UNLOCK(info->bus);
2194 * We exploit the fact that the mutex is the same for all
2195 * callbacks that will be called from this thread:
2197 mtx_lock(info->xfer_mtx);
2198 USB_BUS_LOCK(info->bus);
2200 /* Continue where we lost track */
2201 usb_command_wrapper(&info->done_q,
2204 mtx_unlock(info->xfer_mtx);
2207 /*------------------------------------------------------------------------*
2208 * usbd_callback_ss_done_defer
2210 * This function will defer the start, stop and done callback to the
2212 *------------------------------------------------------------------------*/
2214 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2216 struct usb_xfer_root *info = xfer->xroot;
2217 struct usb_xfer_queue *pq = &info->done_q;
2219 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2221 if (pq->curr != xfer) {
2222 usbd_transfer_enqueue(pq, xfer);
2224 if (!pq->recurse_1) {
2227 * We have to postpone the callback due to the fact we
2228 * will have a Lock Order Reversal, LOR, if we try to
2231 if (usb_proc_msignal(info->done_p,
2232 &info->done_m[0], &info->done_m[1])) {
2236 /* clear second recurse flag */
2243 /*------------------------------------------------------------------------*
2244 * usbd_callback_wrapper
2246 * This is a wrapper for USB callbacks. This wrapper does some
2247 * auto-magic things like figuring out if we can call the callback
2248 * directly from the current context or if we need to wakeup the
2249 * interrupt process.
2250 *------------------------------------------------------------------------*/
2252 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2254 struct usb_xfer *xfer = pq->curr;
2255 struct usb_xfer_root *info = xfer->xroot;
2257 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2258 if (!mtx_owned(info->xfer_mtx) && !SCHEDULER_STOPPED()) {
2260 * Cases that end up here:
2262 * 5) HW interrupt done callback or other source.
2264 DPRINTFN(3, "case 5\n");
2267 * We have to postpone the callback due to the fact we
2268 * will have a Lock Order Reversal, LOR, if we try to
2271 if (usb_proc_msignal(info->done_p,
2272 &info->done_m[0], &info->done_m[1])) {
2278 * Cases that end up here:
2280 * 1) We are starting a transfer
2281 * 2) We are prematurely calling back a transfer
2282 * 3) We are stopping a transfer
2283 * 4) We are doing an ordinary callback
2285 DPRINTFN(3, "case 1-4\n");
2286 /* get next USB transfer in the queue */
2287 info->done_q.curr = NULL;
2289 /* set flag in case of drain */
2290 xfer->flags_int.doing_callback = 1;
2292 USB_BUS_UNLOCK(info->bus);
2293 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2295 /* set correct USB state for callback */
2296 if (!xfer->flags_int.transferring) {
2297 xfer->usb_state = USB_ST_SETUP;
2298 if (!xfer->flags_int.started) {
2299 /* we got stopped before we even got started */
2300 USB_BUS_LOCK(info->bus);
2305 if (usbd_callback_wrapper_sub(xfer)) {
2306 /* the callback has been deferred */
2307 USB_BUS_LOCK(info->bus);
2311 /* decrement power reference */
2312 usbd_transfer_power_ref(xfer, -1);
2314 xfer->flags_int.transferring = 0;
2317 xfer->usb_state = USB_ST_ERROR;
2319 /* set transferred state */
2320 xfer->usb_state = USB_ST_TRANSFERRED;
2322 /* sync DMA memory, if any */
2323 if (xfer->flags_int.bdma_enable &&
2324 (!xfer->flags_int.bdma_no_post_sync)) {
2325 usb_bdma_post_sync(xfer);
2332 if (xfer->usb_state != USB_ST_SETUP)
2333 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2335 /* call processing routine */
2336 (xfer->callback) (xfer, xfer->error);
2338 /* pickup the USB mutex again */
2339 USB_BUS_LOCK(info->bus);
2342 * Check if we got started after that we got cancelled, but
2343 * before we managed to do the callback.
2345 if ((!xfer->flags_int.open) &&
2346 (xfer->flags_int.started) &&
2347 (xfer->usb_state == USB_ST_ERROR)) {
2348 /* clear flag in case of drain */
2349 xfer->flags_int.doing_callback = 0;
2350 /* try to loop, but not recursivly */
2351 usb_command_wrapper(&info->done_q, xfer);
2356 /* clear flag in case of drain */
2357 xfer->flags_int.doing_callback = 0;
2360 * Check if we are draining.
2362 if (xfer->flags_int.draining &&
2363 (!xfer->flags_int.transferring)) {
2364 /* "usbd_transfer_drain()" is waiting for end of transfer */
2365 xfer->flags_int.draining = 0;
2366 cv_broadcast(&info->cv_drain);
2369 /* do the next callback, if any */
2370 usb_command_wrapper(&info->done_q,
2374 /*------------------------------------------------------------------------*
2375 * usb_dma_delay_done_cb
2377 * This function is called when the DMA delay has been exectuded, and
2378 * will make sure that the callback is called to complete the USB
2379 * transfer. This code path is ususally only used when there is an USB
2380 * error like USB_ERR_CANCELLED.
2381 *------------------------------------------------------------------------*/
2383 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2385 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2387 DPRINTFN(3, "Completed %p\n", xfer);
2389 /* queue callback for execution, again */
2390 usbd_transfer_done(xfer, 0);
2393 /*------------------------------------------------------------------------*
2394 * usbd_transfer_dequeue
2396 * - This function is used to remove an USB transfer from a USB
2399 * - This function can be called multiple times in a row.
2400 *------------------------------------------------------------------------*/
2402 usbd_transfer_dequeue(struct usb_xfer *xfer)
2404 struct usb_xfer_queue *pq;
2406 pq = xfer->wait_queue;
2408 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2409 xfer->wait_queue = NULL;
2413 /*------------------------------------------------------------------------*
2414 * usbd_transfer_enqueue
2416 * - This function is used to insert an USB transfer into a USB *
2419 * - This function can be called multiple times in a row.
2420 *------------------------------------------------------------------------*/
2422 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2425 * Insert the USB transfer into the queue, if it is not
2426 * already on a USB transfer queue:
2428 if (xfer->wait_queue == NULL) {
2429 xfer->wait_queue = pq;
2430 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2434 /*------------------------------------------------------------------------*
2435 * usbd_transfer_done
2437 * - This function is used to remove an USB transfer from the busdma,
2438 * pipe or interrupt queue.
2440 * - This function is used to queue the USB transfer on the done
2443 * - This function is used to stop any USB transfer timeouts.
2444 *------------------------------------------------------------------------*/
2446 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2448 struct usb_xfer_root *info = xfer->xroot;
2450 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2452 DPRINTF("err=%s\n", usbd_errstr(error));
2455 * If we are not transferring then just return.
2456 * This can happen during transfer cancel.
2458 if (!xfer->flags_int.transferring) {
2459 DPRINTF("not transferring\n");
2460 /* end of control transfer, if any */
2461 xfer->flags_int.control_act = 0;
2464 /* only set transfer error, if not already set */
2465 if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2466 xfer->error = error;
2468 /* stop any callouts */
2469 usb_callout_stop(&xfer->timeout_handle);
2472 * If we are waiting on a queue, just remove the USB transfer
2473 * from the queue, if any. We should have the required locks
2474 * locked to do the remove when this function is called.
2476 usbd_transfer_dequeue(xfer);
2479 if (mtx_owned(info->xfer_mtx)) {
2480 struct usb_xfer_queue *pq;
2483 * If the private USB lock is not locked, then we assume
2484 * that the BUS-DMA load stage has been passed:
2488 if (pq->curr == xfer) {
2489 /* start the next BUS-DMA load, if any */
2490 usb_command_wrapper(pq, NULL);
2494 /* keep some statistics */
2496 info->bus->stats_err.uds_requests
2497 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2499 info->bus->stats_ok.uds_requests
2500 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2503 /* call the USB transfer callback */
2504 usbd_callback_ss_done_defer(xfer);
2507 /*------------------------------------------------------------------------*
2508 * usbd_transfer_start_cb
2510 * This function is called to start the USB transfer when
2511 * "xfer->interval" is greater than zero, and and the endpoint type is
2513 *------------------------------------------------------------------------*/
2515 usbd_transfer_start_cb(void *arg)
2517 struct usb_xfer *xfer = arg;
2518 struct usb_endpoint *ep = xfer->endpoint;
2520 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2525 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2528 /* the transfer can now be cancelled */
2529 xfer->flags_int.can_cancel_immed = 1;
2531 /* start USB transfer, if no error */
2532 if (xfer->error == 0)
2533 (ep->methods->start) (xfer);
2535 /* check for transfer error */
2537 /* some error has happened */
2538 usbd_transfer_done(xfer, 0);
2542 /*------------------------------------------------------------------------*
2543 * usbd_xfer_set_stall
2545 * This function is used to set the stall flag outside the
2546 * callback. This function is NULL safe.
2547 *------------------------------------------------------------------------*/
2549 usbd_xfer_set_stall(struct usb_xfer *xfer)
2555 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2557 /* avoid any races by locking the USB mutex */
2558 USB_BUS_LOCK(xfer->xroot->bus);
2559 xfer->flags.stall_pipe = 1;
2560 USB_BUS_UNLOCK(xfer->xroot->bus);
2564 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2566 return (xfer->endpoint->is_stalled);
2569 /*------------------------------------------------------------------------*
2570 * usbd_transfer_clear_stall
2572 * This function is used to clear the stall flag outside the
2573 * callback. This function is NULL safe.
2574 *------------------------------------------------------------------------*/
2576 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2582 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2584 /* avoid any races by locking the USB mutex */
2585 USB_BUS_LOCK(xfer->xroot->bus);
2587 xfer->flags.stall_pipe = 0;
2589 USB_BUS_UNLOCK(xfer->xroot->bus);
2592 /*------------------------------------------------------------------------*
2595 * This function is used to add an USB transfer to the pipe transfer list.
2596 *------------------------------------------------------------------------*/
2598 usbd_pipe_start(struct usb_xfer_queue *pq)
2600 struct usb_endpoint *ep;
2601 struct usb_xfer *xfer;
2605 ep = xfer->endpoint;
2607 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2610 * If the endpoint is already stalled we do nothing !
2612 if (ep->is_stalled) {
2616 * Check if we are supposed to stall the endpoint:
2618 if (xfer->flags.stall_pipe) {
2619 struct usb_device *udev;
2620 struct usb_xfer_root *info;
2622 /* clear stall command */
2623 xfer->flags.stall_pipe = 0;
2625 /* get pointer to USB device */
2630 * Only stall BULK and INTERRUPT endpoints.
2632 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2633 if ((type == UE_BULK) ||
2634 (type == UE_INTERRUPT)) {
2639 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2640 (udev->bus->methods->set_stall) (
2641 udev, ep, &did_stall);
2642 } else if (udev->ctrl_xfer[1]) {
2643 info = udev->ctrl_xfer[1]->xroot;
2645 USB_BUS_NON_GIANT_PROC(info->bus),
2646 &udev->cs_msg[0], &udev->cs_msg[1]);
2648 /* should not happen */
2649 DPRINTFN(0, "No stall handler\n");
2652 * Check if we should stall. Some USB hardware
2653 * handles set- and clear-stall in hardware.
2657 * The transfer will be continued when
2658 * the clear-stall control endpoint
2659 * message is received.
2664 } else if (type == UE_ISOCHRONOUS) {
2667 * Make sure any FIFO overflow or other FIFO
2668 * error conditions go away by resetting the
2669 * endpoint FIFO through the clear stall
2672 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2673 (udev->bus->methods->clear_stall) (udev, ep);
2677 /* Set or clear stall complete - special case */
2678 if (xfer->nframes == 0) {
2679 /* we are complete */
2681 usbd_transfer_done(xfer, 0);
2687 * 1) Start the first transfer queued.
2689 * 2) Re-start the current USB transfer.
2692 * Check if there should be any
2693 * pre transfer start delay:
2695 if (xfer->interval > 0) {
2696 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2697 if ((type == UE_BULK) ||
2698 (type == UE_CONTROL)) {
2699 usbd_transfer_timeout_ms(xfer,
2700 &usbd_transfer_start_cb,
2708 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2710 /* the transfer can now be cancelled */
2711 xfer->flags_int.can_cancel_immed = 1;
2713 /* start USB transfer, if no error */
2714 if (xfer->error == 0)
2715 (ep->methods->start) (xfer);
2717 /* check for transfer error */
2719 /* some error has happened */
2720 usbd_transfer_done(xfer, 0);
2724 /*------------------------------------------------------------------------*
2725 * usbd_transfer_timeout_ms
2727 * This function is used to setup a timeout on the given USB
2728 * transfer. If the timeout has been deferred the callback given by
2729 * "cb" will get called after "ms" milliseconds.
2730 *------------------------------------------------------------------------*/
2732 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2733 void (*cb) (void *arg), usb_timeout_t ms)
2735 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2738 usb_callout_reset(&xfer->timeout_handle,
2739 USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2742 /*------------------------------------------------------------------------*
2743 * usbd_callback_wrapper_sub
2745 * - This function will update variables in an USB transfer after
2746 * that the USB transfer is complete.
2748 * - This function is used to start the next USB transfer on the
2749 * ep transfer queue, if any.
2751 * NOTE: In some special cases the USB transfer will not be removed from
2752 * the pipe queue, but remain first. To enforce USB transfer removal call
2753 * this function passing the error code "USB_ERR_CANCELLED".
2757 * Else: The callback has been deferred.
2758 *------------------------------------------------------------------------*/
2760 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2762 struct usb_endpoint *ep;
2763 struct usb_bus *bus;
2766 bus = xfer->xroot->bus;
2768 if ((!xfer->flags_int.open) &&
2769 (!xfer->flags_int.did_close)) {
2772 (xfer->endpoint->methods->close) (xfer);
2773 USB_BUS_UNLOCK(bus);
2774 /* only close once */
2775 xfer->flags_int.did_close = 1;
2776 return (1); /* wait for new callback */
2779 * If we have a non-hardware induced error we
2780 * need to do the DMA delay!
2782 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2783 (xfer->error == USB_ERR_CANCELLED ||
2784 xfer->error == USB_ERR_TIMEOUT ||
2785 bus->methods->start_dma_delay != NULL)) {
2789 /* only delay once */
2790 xfer->flags_int.did_dma_delay = 1;
2792 /* we can not cancel this delay */
2793 xfer->flags_int.can_cancel_immed = 0;
2795 temp = usbd_get_dma_delay(xfer->xroot->udev);
2797 DPRINTFN(3, "DMA delay, %u ms, "
2798 "on %p\n", temp, xfer);
2803 * Some hardware solutions have dedicated
2804 * events when it is safe to free DMA'ed
2805 * memory. For the other hardware platforms we
2806 * use a static delay.
2808 if (bus->methods->start_dma_delay != NULL) {
2809 (bus->methods->start_dma_delay) (xfer);
2811 usbd_transfer_timeout_ms(xfer,
2812 (void (*)(void *))&usb_dma_delay_done_cb,
2815 USB_BUS_UNLOCK(bus);
2816 return (1); /* wait for new callback */
2819 /* check actual number of frames */
2820 if (xfer->aframes > xfer->nframes) {
2821 if (xfer->error == 0) {
2822 panic("%s: actual number of frames, %d, is "
2823 "greater than initial number of frames, %d\n",
2824 __FUNCTION__, xfer->aframes, xfer->nframes);
2826 /* just set some valid value */
2827 xfer->aframes = xfer->nframes;
2830 /* compute actual length */
2833 for (x = 0; x != xfer->aframes; x++) {
2834 xfer->actlen += xfer->frlengths[x];
2838 * Frames that were not transferred get zero actual length in
2839 * case the USB device driver does not check the actual number
2840 * of frames transferred, "xfer->aframes":
2842 for (; x < xfer->nframes; x++) {
2843 usbd_xfer_set_frame_len(xfer, x, 0);
2846 /* check actual length */
2847 if (xfer->actlen > xfer->sumlen) {
2848 if (xfer->error == 0) {
2849 panic("%s: actual length, %d, is greater than "
2850 "initial length, %d\n",
2851 __FUNCTION__, xfer->actlen, xfer->sumlen);
2853 /* just set some valid value */
2854 xfer->actlen = xfer->sumlen;
2857 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2858 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2859 xfer->aframes, xfer->nframes);
2862 /* end of control transfer, if any */
2863 xfer->flags_int.control_act = 0;
2865 #if USB_HAVE_TT_SUPPORT
2866 switch (xfer->error) {
2867 case USB_ERR_NORMAL_COMPLETION:
2868 case USB_ERR_SHORT_XFER:
2869 case USB_ERR_STALLED:
2870 case USB_ERR_CANCELLED:
2874 /* try to reset the TT, if any */
2876 uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint);
2877 USB_BUS_UNLOCK(bus);
2881 /* check if we should block the execution queue */
2882 if ((xfer->error != USB_ERR_CANCELLED) &&
2883 (xfer->flags.pipe_bof)) {
2884 DPRINTFN(2, "xfer=%p: Block On Failure "
2885 "on endpoint=%p\n", xfer, xfer->endpoint);
2889 /* check for short transfers */
2890 if (xfer->actlen < xfer->sumlen) {
2892 /* end of control transfer, if any */
2893 xfer->flags_int.control_act = 0;
2895 if (!xfer->flags_int.short_xfer_ok) {
2896 xfer->error = USB_ERR_SHORT_XFER;
2897 if (xfer->flags.pipe_bof) {
2898 DPRINTFN(2, "xfer=%p: Block On Failure on "
2899 "Short Transfer on endpoint %p.\n",
2900 xfer, xfer->endpoint);
2906 * Check if we are in the middle of a
2909 if (xfer->flags_int.control_act) {
2910 DPRINTFN(5, "xfer=%p: Control transfer "
2911 "active on endpoint=%p\n", xfer, xfer->endpoint);
2917 ep = xfer->endpoint;
2920 * If the current USB transfer is completing we need to start the
2924 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2925 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
2927 if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
2928 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
2929 /* there is another USB transfer waiting */
2931 /* this is the last USB transfer */
2932 /* clear isochronous sync flag */
2933 xfer->endpoint->is_synced = 0;
2936 USB_BUS_UNLOCK(bus);
2941 /*------------------------------------------------------------------------*
2942 * usb_command_wrapper
2944 * This function is used to execute commands non-recursivly on an USB
2946 *------------------------------------------------------------------------*/
2948 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2952 * If the transfer is not already processing,
2955 if (pq->curr != xfer) {
2956 usbd_transfer_enqueue(pq, xfer);
2957 if (pq->curr != NULL) {
2958 /* something is already processing */
2959 DPRINTFN(6, "busy %p\n", pq->curr);
2964 /* Get next element in queue */
2968 if (!pq->recurse_1) {
2972 /* set both recurse flags */
2976 if (pq->curr == NULL) {
2977 xfer = TAILQ_FIRST(&pq->head);
2979 TAILQ_REMOVE(&pq->head, xfer,
2981 xfer->wait_queue = NULL;
2987 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
2989 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
2991 } while (!pq->recurse_2);
2993 /* clear first recurse flag */
2997 /* clear second recurse flag */
3002 /*------------------------------------------------------------------------*
3003 * usbd_ctrl_transfer_setup
3005 * This function is used to setup the default USB control endpoint
3007 *------------------------------------------------------------------------*/
3009 usbd_ctrl_transfer_setup(struct usb_device *udev)
3011 struct usb_xfer *xfer;
3013 uint8_t iface_index;
3015 /* check for root HUB */
3016 if (udev->parent_hub == NULL)
3020 xfer = udev->ctrl_xfer[0];
3022 USB_XFER_LOCK(xfer);
3024 ((xfer->address == udev->address) &&
3025 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3026 udev->ddesc.bMaxPacketSize));
3027 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3030 * NOTE: checking "xfer->address" and
3031 * starting the USB transfer must be
3034 usbd_transfer_start(xfer);
3037 USB_XFER_UNLOCK(xfer);
3044 * All parameters are exactly the same like before.
3050 * Update wMaxPacketSize for the default control endpoint:
3052 udev->ctrl_ep_desc.wMaxPacketSize[0] =
3053 udev->ddesc.bMaxPacketSize;
3056 * Unsetup any existing USB transfer:
3058 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3061 * Reset clear stall error counter.
3063 udev->clear_stall_errors = 0;
3066 * Try to setup a new USB transfer for the
3067 * default control endpoint:
3070 if (usbd_transfer_setup(udev, &iface_index,
3071 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3072 &udev->device_mtx)) {
3073 DPRINTFN(0, "could not setup default "
3080 /*------------------------------------------------------------------------*
3081 * usbd_clear_data_toggle - factored out code
3083 * NOTE: the intention of this function is not to reset the hardware
3085 *------------------------------------------------------------------------*/
3087 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3089 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3091 /* check that we have a valid case */
3092 if (udev->flags.usb_mode == USB_MODE_HOST &&
3093 udev->parent_hub != NULL &&
3094 udev->bus->methods->clear_stall != NULL &&
3095 ep->methods != NULL) {
3096 (udev->bus->methods->clear_stall) (udev, ep);
3100 /*------------------------------------------------------------------------*
3101 * usbd_clear_data_toggle - factored out code
3103 * NOTE: the intention of this function is not to reset the hardware
3104 * data toggle on the USB device side.
3105 *------------------------------------------------------------------------*/
3107 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3109 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3111 USB_BUS_LOCK(udev->bus);
3112 ep->toggle_next = 0;
3113 /* some hardware needs a callback to clear the data toggle */
3114 usbd_clear_stall_locked(udev, ep);
3115 USB_BUS_UNLOCK(udev->bus);
3118 /*------------------------------------------------------------------------*
3119 * usbd_clear_stall_callback - factored out clear stall callback
3122 * xfer1: Clear Stall Control Transfer
3123 * xfer2: Stalled USB Transfer
3125 * This function is NULL safe.
3131 * Clear stall config example:
3133 * static const struct usb_config my_clearstall = {
3134 * .type = UE_CONTROL,
3136 * .direction = UE_DIR_ANY,
3137 * .interval = 50, //50 milliseconds
3138 * .bufsize = sizeof(struct usb_device_request),
3139 * .timeout = 1000, //1.000 seconds
3140 * .callback = &my_clear_stall_callback, // **
3141 * .usb_mode = USB_MODE_HOST,
3144 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3145 * passing the correct parameters.
3146 *------------------------------------------------------------------------*/
3148 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3149 struct usb_xfer *xfer2)
3151 struct usb_device_request req;
3153 if (xfer2 == NULL) {
3154 /* looks like we are tearing down */
3155 DPRINTF("NULL input parameter\n");
3158 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3159 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3161 switch (USB_GET_STATE(xfer1)) {
3165 * pre-clear the data toggle to DATA0 ("umass.c" and
3166 * "ata-usb.c" depends on this)
3169 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3171 /* setup a clear-stall packet */
3173 req.bmRequestType = UT_WRITE_ENDPOINT;
3174 req.bRequest = UR_CLEAR_FEATURE;
3175 USETW(req.wValue, UF_ENDPOINT_HALT);
3176 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3178 USETW(req.wLength, 0);
3181 * "usbd_transfer_setup_sub()" will ensure that
3182 * we have sufficient room in the buffer for
3183 * the request structure!
3186 /* copy in the transfer */
3188 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3191 xfer1->frlengths[0] = sizeof(req);
3194 usbd_transfer_submit(xfer1);
3197 case USB_ST_TRANSFERRED:
3200 default: /* Error */
3201 if (xfer1->error == USB_ERR_CANCELLED) {
3206 return (1); /* Clear Stall Finished */
3209 /*------------------------------------------------------------------------*
3210 * usbd_transfer_poll
3212 * The following function gets called from the USB keyboard driver and
3213 * UMASS when the system has paniced.
3215 * NOTE: It is currently not possible to resume normal operation on
3216 * the USB controller which has been polled, due to clearing of the
3217 * "up_dsleep" and "up_msleep" flags.
3218 *------------------------------------------------------------------------*/
3220 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3222 struct usb_xfer *xfer;
3223 struct usb_xfer_root *xroot;
3224 struct usb_device *udev;
3225 struct usb_proc_msg *pm;
3230 for (n = 0; n != max; n++) {
3231 /* Extra checks to avoid panic */
3234 continue; /* no USB transfer */
3235 xroot = xfer->xroot;
3237 continue; /* no USB root */
3240 continue; /* no USB device */
3241 if (udev->bus == NULL)
3242 continue; /* no BUS structure */
3243 if (udev->bus->methods == NULL)
3244 continue; /* no BUS methods */
3245 if (udev->bus->methods->xfer_poll == NULL)
3246 continue; /* no poll method */
3248 /* make sure that the BUS mutex is not locked */
3250 while (mtx_owned(&xroot->udev->bus->bus_mtx) && !SCHEDULER_STOPPED()) {
3251 mtx_unlock(&xroot->udev->bus->bus_mtx);
3255 /* make sure that the transfer mutex is not locked */
3257 while (mtx_owned(xroot->xfer_mtx) && !SCHEDULER_STOPPED()) {
3258 mtx_unlock(xroot->xfer_mtx);
3262 /* Make sure cv_signal() and cv_broadcast() is not called */
3263 USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3264 USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3265 USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3266 USB_BUS_NON_GIANT_PROC(udev->bus)->up_msleep = 0;
3268 /* poll USB hardware */
3269 (udev->bus->methods->xfer_poll) (udev->bus);
3271 USB_BUS_LOCK(xroot->bus);
3273 /* check for clear stall */
3274 if (udev->ctrl_xfer[1] != NULL) {
3276 /* poll clear stall start */
3277 pm = &udev->cs_msg[0].hdr;
3278 (pm->pm_callback) (pm);
3279 /* poll clear stall done thread */
3280 pm = &udev->ctrl_xfer[1]->
3281 xroot->done_m[0].hdr;
3282 (pm->pm_callback) (pm);
3285 /* poll done thread */
3286 pm = &xroot->done_m[0].hdr;
3287 (pm->pm_callback) (pm);
3289 USB_BUS_UNLOCK(xroot->bus);
3291 /* restore transfer mutex */
3293 mtx_lock(xroot->xfer_mtx);
3295 /* restore BUS mutex */
3297 mtx_lock(&xroot->udev->bus->bus_mtx);
3302 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3303 uint8_t type, enum usb_dev_speed speed)
3305 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3306 [USB_SPEED_LOW] = 8,
3307 [USB_SPEED_FULL] = 64,
3308 [USB_SPEED_HIGH] = 1024,
3309 [USB_SPEED_VARIABLE] = 1024,
3310 [USB_SPEED_SUPER] = 1024,
3313 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3314 [USB_SPEED_LOW] = 0, /* invalid */
3315 [USB_SPEED_FULL] = 1023,
3316 [USB_SPEED_HIGH] = 1024,
3317 [USB_SPEED_VARIABLE] = 3584,
3318 [USB_SPEED_SUPER] = 1024,
3321 static const uint16_t control_min[USB_SPEED_MAX] = {
3322 [USB_SPEED_LOW] = 8,
3323 [USB_SPEED_FULL] = 8,
3324 [USB_SPEED_HIGH] = 64,
3325 [USB_SPEED_VARIABLE] = 512,
3326 [USB_SPEED_SUPER] = 512,
3329 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3330 [USB_SPEED_LOW] = 8,
3331 [USB_SPEED_FULL] = 8,
3332 [USB_SPEED_HIGH] = 512,
3333 [USB_SPEED_VARIABLE] = 512,
3334 [USB_SPEED_SUPER] = 1024,
3339 memset(ptr, 0, sizeof(*ptr));
3343 ptr->range.max = intr_range_max[speed];
3345 case UE_ISOCHRONOUS:
3346 ptr->range.max = isoc_range_max[speed];
3349 if (type == UE_BULK)
3350 temp = bulk_min[speed];
3351 else /* UE_CONTROL */
3352 temp = control_min[speed];
3354 /* default is fixed */
3355 ptr->fixed[0] = temp;
3356 ptr->fixed[1] = temp;
3357 ptr->fixed[2] = temp;
3358 ptr->fixed[3] = temp;
3360 if (speed == USB_SPEED_FULL) {
3361 /* multiple sizes */
3366 if ((speed == USB_SPEED_VARIABLE) &&
3367 (type == UE_BULK)) {
3368 /* multiple sizes */
3369 ptr->fixed[2] = 1024;
3370 ptr->fixed[3] = 1536;
3377 usbd_xfer_softc(struct usb_xfer *xfer)
3379 return (xfer->priv_sc);
3383 usbd_xfer_get_priv(struct usb_xfer *xfer)
3385 return (xfer->priv_fifo);
3389 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3391 xfer->priv_fifo = ptr;
3395 usbd_xfer_state(struct usb_xfer *xfer)
3397 return (xfer->usb_state);
3401 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3404 case USB_FORCE_SHORT_XFER:
3405 xfer->flags.force_short_xfer = 1;
3407 case USB_SHORT_XFER_OK:
3408 xfer->flags.short_xfer_ok = 1;
3410 case USB_MULTI_SHORT_OK:
3411 xfer->flags.short_frames_ok = 1;
3413 case USB_MANUAL_STATUS:
3414 xfer->flags.manual_status = 1;
3420 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3423 case USB_FORCE_SHORT_XFER:
3424 xfer->flags.force_short_xfer = 0;
3426 case USB_SHORT_XFER_OK:
3427 xfer->flags.short_xfer_ok = 0;
3429 case USB_MULTI_SHORT_OK:
3430 xfer->flags.short_frames_ok = 0;
3432 case USB_MANUAL_STATUS:
3433 xfer->flags.manual_status = 0;
3439 * The following function returns in milliseconds when the isochronous
3440 * transfer was completed by the hardware. The returned value wraps
3441 * around 65536 milliseconds.
3444 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3446 return (xfer->isoc_time_complete);
3450 * The following function returns non-zero if the max packet size
3451 * field was clamped to a valid value. Else it returns zero.
3454 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3456 return (xfer->flags_int.maxp_was_clamped);