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>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
53 #define USB_DEBUG_VAR usb_debug
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_transfer.h>
59 #include <dev/usb/usb_device.h>
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_util.h>
63 #include <dev/usb/usb_controller.h>
64 #include <dev/usb/usb_bus.h>
65 #include <dev/usb/usb_pf.h>
66 #endif /* USB_GLOBAL_INCLUDE_FILE */
68 struct usb_std_packet_size {
70 uint16_t min; /* inclusive */
71 uint16_t max; /* inclusive */
77 static usb_callback_t usb_request_callback;
79 static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
81 /* This transfer is used for generic control endpoint transfers */
85 .endpoint = 0x00, /* Control endpoint */
86 .direction = UE_DIR_ANY,
87 .bufsize = USB_EP0_BUFSIZE, /* bytes */
88 .flags = {.proxy_buffer = 1,},
89 .callback = &usb_request_callback,
90 .usb_mode = USB_MODE_DUAL, /* both modes */
93 /* This transfer is used for generic clear stall only */
97 .endpoint = 0x00, /* Control pipe */
98 .direction = UE_DIR_ANY,
99 .bufsize = sizeof(struct usb_device_request),
100 .callback = &usb_do_clear_stall_callback,
101 .timeout = 1000, /* 1 second */
102 .interval = 50, /* 50ms */
103 .usb_mode = USB_MODE_HOST,
107 /* function prototypes */
109 static void usbd_update_max_frame_size(struct usb_xfer *);
110 static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
111 static void usbd_control_transfer_init(struct usb_xfer *);
112 static int usbd_setup_ctrl_transfer(struct usb_xfer *);
113 static void usb_callback_proc(struct usb_proc_msg *);
114 static void usbd_callback_ss_done_defer(struct usb_xfer *);
115 static void usbd_callback_wrapper(struct usb_xfer_queue *);
116 static void usbd_transfer_start_cb(void *);
117 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
118 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
119 uint8_t type, enum usb_dev_speed speed);
121 /*------------------------------------------------------------------------*
122 * usb_request_callback
123 *------------------------------------------------------------------------*/
125 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
127 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
128 usb_handle_request_callback(xfer, error);
130 usbd_do_request_callback(xfer, error);
133 /*------------------------------------------------------------------------*
134 * usbd_update_max_frame_size
136 * This function updates the maximum frame size, hence high speed USB
137 * can transfer multiple consecutive packets.
138 *------------------------------------------------------------------------*/
140 usbd_update_max_frame_size(struct usb_xfer *xfer)
142 /* compute maximum frame size */
143 /* this computation should not overflow 16-bit */
144 /* max = 15 * 1024 */
146 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
149 /*------------------------------------------------------------------------*
152 * The following function is called when we need to
153 * synchronize with DMA hardware.
156 * 0: no DMA delay required
157 * Else: milliseconds of DMA delay
158 *------------------------------------------------------------------------*/
160 usbd_get_dma_delay(struct usb_device *udev)
162 const struct usb_bus_methods *mtod;
165 mtod = udev->bus->methods;
168 if (mtod->get_dma_delay) {
169 (mtod->get_dma_delay) (udev, &temp);
171 * Round up and convert to milliseconds. Note that we use
172 * 1024 milliseconds per second. to save a division.
180 /*------------------------------------------------------------------------*
181 * usbd_transfer_setup_sub_malloc
183 * This function will allocate one or more DMA'able memory chunks
184 * according to "size", "align" and "count" arguments. "ppc" is
185 * pointed to a linear array of USB page caches afterwards.
187 * If the "align" argument is equal to "1" a non-contiguous allocation
188 * can happen. Else if the "align" argument is greater than "1", the
189 * allocation will always be contiguous in memory.
194 *------------------------------------------------------------------------*/
197 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
198 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
201 struct usb_page_cache *pc;
212 USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
214 USB_ASSERT(size > 0, ("Invalid size = 0\n"));
217 return (0); /* nothing to allocate */
220 * Make sure that the size is aligned properly.
222 size = -((-size) & (-align));
225 * Try multi-allocation chunks to reduce the number of DMA
226 * allocations, hence DMA allocations are slow.
229 /* special case - non-cached multi page DMA memory */
231 n_dma_pg = (2 + (size / USB_PAGE_SIZE));
233 } else if (size >= USB_PAGE_SIZE) {
238 /* compute number of objects per page */
239 #ifdef USB_DMA_SINGLE_ALLOC
242 n_obj = (USB_PAGE_SIZE / size);
245 * Compute number of DMA chunks, rounded up
248 n_dma_pc = howmany(count, n_obj);
253 * DMA memory is allocated once, but mapped twice. That's why
254 * there is one list for auto-free and another list for
255 * non-auto-free which only holds the mapping and not the
258 if (parm->buf == NULL) {
259 /* reserve memory (auto-free) */
260 parm->dma_page_ptr += n_dma_pc * n_dma_pg;
261 parm->dma_page_cache_ptr += n_dma_pc;
263 /* reserve memory (no-auto-free) */
264 parm->dma_page_ptr += count * n_dma_pg;
265 parm->xfer_page_cache_ptr += count;
268 for (x = 0; x != n_dma_pc; x++) {
269 /* need to initialize the page cache */
270 parm->dma_page_cache_ptr[x].tag_parent =
271 &parm->curr_xfer->xroot->dma_parent_tag;
273 for (x = 0; x != count; x++) {
274 /* need to initialize the page cache */
275 parm->xfer_page_cache_ptr[x].tag_parent =
276 &parm->curr_xfer->xroot->dma_parent_tag;
281 *ppc = parm->xfer_page_cache_ptr;
283 *ppc = parm->dma_page_cache_ptr;
285 r = count; /* set remainder count */
286 z = n_obj * size; /* set allocation size */
287 pc = parm->xfer_page_cache_ptr;
288 pg = parm->dma_page_ptr;
292 * Avoid mapping memory twice if only a single object
293 * should be allocated per page cache:
295 for (x = 0; x != n_dma_pc; x++) {
296 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
298 return (1); /* failure */
300 /* Make room for one DMA page cache and "n_dma_pg" pages */
301 parm->dma_page_cache_ptr++;
305 for (x = 0; x != n_dma_pc; x++) {
308 /* compute last remainder */
312 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
314 return (1); /* failure */
316 /* Set beginning of current buffer */
317 buf = parm->dma_page_cache_ptr->buffer;
318 /* Make room for one DMA page cache and "n_dma_pg" pages */
319 parm->dma_page_cache_ptr++;
322 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
324 /* Load sub-chunk into DMA */
325 if (usb_pc_dmamap_create(pc, size)) {
326 return (1); /* failure */
328 pc->buffer = USB_ADD_BYTES(buf, y * size);
331 USB_MTX_LOCK(pc->tag_parent->mtx);
332 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
333 USB_MTX_UNLOCK(pc->tag_parent->mtx);
334 return (1); /* failure */
336 USB_MTX_UNLOCK(pc->tag_parent->mtx);
341 parm->xfer_page_cache_ptr = pc;
342 parm->dma_page_ptr = pg;
347 /*------------------------------------------------------------------------*
348 * usbd_transfer_setup_sub - transfer setup subroutine
350 * This function must be called from the "xfer_setup" callback of the
351 * USB Host or Device controller driver when setting up an USB
352 * transfer. This function will setup correct packet sizes, buffer
353 * sizes, flags and more, that are stored in the "usb_xfer"
355 *------------------------------------------------------------------------*/
357 usbd_transfer_setup_sub(struct usb_setup_params *parm)
363 struct usb_xfer *xfer = parm->curr_xfer;
364 const struct usb_config *setup = parm->curr_setup;
365 struct usb_endpoint_ss_comp_descriptor *ecomp;
366 struct usb_endpoint_descriptor *edesc;
367 struct usb_std_packet_size std_size;
368 usb_frcount_t n_frlengths;
369 usb_frcount_t n_frbuffers;
376 * Sanity check. The following parameters must be initialized before
377 * calling this function.
379 if ((parm->hc_max_packet_size == 0) ||
380 (parm->hc_max_packet_count == 0) ||
381 (parm->hc_max_frame_size == 0)) {
382 parm->err = USB_ERR_INVAL;
385 edesc = xfer->endpoint->edesc;
386 ecomp = xfer->endpoint->ecomp;
388 type = (edesc->bmAttributes & UE_XFERTYPE);
390 xfer->flags = setup->flags;
391 xfer->nframes = setup->frames;
392 xfer->timeout = setup->timeout;
393 xfer->callback = setup->callback;
394 xfer->interval = setup->interval;
395 xfer->endpointno = edesc->bEndpointAddress;
396 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
397 xfer->max_packet_count = 1;
398 /* make a shadow copy: */
399 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
401 parm->bufsize = setup->bufsize;
403 switch (parm->speed) {
408 xfer->max_packet_count +=
409 (xfer->max_packet_size >> 11) & 3;
411 /* check for invalid max packet count */
412 if (xfer->max_packet_count > 3)
413 xfer->max_packet_count = 3;
418 xfer->max_packet_size &= 0x7FF;
420 case USB_SPEED_SUPER:
421 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
424 xfer->max_packet_count += ecomp->bMaxBurst;
426 if ((xfer->max_packet_count == 0) ||
427 (xfer->max_packet_count > 16))
428 xfer->max_packet_count = 16;
432 xfer->max_packet_count = 1;
438 mult = UE_GET_SS_ISO_MULT(
439 ecomp->bmAttributes) + 1;
443 xfer->max_packet_count *= mult;
449 xfer->max_packet_size &= 0x7FF;
454 /* range check "max_packet_count" */
456 if (xfer->max_packet_count > parm->hc_max_packet_count) {
457 xfer->max_packet_count = parm->hc_max_packet_count;
460 /* store max packet size value before filtering */
462 maxp_old = xfer->max_packet_size;
464 /* filter "wMaxPacketSize" according to HC capabilities */
466 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
467 (xfer->max_packet_size == 0)) {
468 xfer->max_packet_size = parm->hc_max_packet_size;
470 /* filter "wMaxPacketSize" according to standard sizes */
472 usbd_get_std_packet_size(&std_size, type, parm->speed);
474 if (std_size.range.min || std_size.range.max) {
476 if (xfer->max_packet_size < std_size.range.min) {
477 xfer->max_packet_size = std_size.range.min;
479 if (xfer->max_packet_size > std_size.range.max) {
480 xfer->max_packet_size = std_size.range.max;
484 if (xfer->max_packet_size >= std_size.fixed[3]) {
485 xfer->max_packet_size = std_size.fixed[3];
486 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
487 xfer->max_packet_size = std_size.fixed[2];
488 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
489 xfer->max_packet_size = std_size.fixed[1];
491 /* only one possibility left */
492 xfer->max_packet_size = std_size.fixed[0];
497 * Check if the max packet size was outside its allowed range
498 * and clamped to a valid value:
500 if (maxp_old != xfer->max_packet_size)
501 xfer->flags_int.maxp_was_clamped = 1;
503 /* compute "max_frame_size" */
505 usbd_update_max_frame_size(xfer);
507 /* check interrupt interval and transfer pre-delay */
509 if (type == UE_ISOCHRONOUS) {
511 uint16_t frame_limit;
513 xfer->interval = 0; /* not used, must be zero */
514 xfer->flags_int.isochronous_xfr = 1; /* set flag */
516 if (xfer->timeout == 0) {
518 * set a default timeout in
519 * case something goes wrong!
521 xfer->timeout = 1000 / 4;
523 switch (parm->speed) {
526 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
530 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
531 xfer->fps_shift = edesc->bInterval;
532 if (xfer->fps_shift > 0)
534 if (xfer->fps_shift > 3)
536 if (xfer->flags.pre_scale_frames != 0)
537 xfer->nframes <<= (3 - xfer->fps_shift);
541 if (xfer->nframes > frame_limit) {
543 * this is not going to work
546 parm->err = USB_ERR_INVAL;
549 if (xfer->nframes == 0) {
551 * this is not a valid value
553 parm->err = USB_ERR_ZERO_NFRAMES;
559 * If a value is specified use that else check the
560 * endpoint descriptor!
562 if (type == UE_INTERRUPT) {
566 if (xfer->interval == 0) {
568 xfer->interval = edesc->bInterval;
570 switch (parm->speed) {
576 if (xfer->interval < 4)
578 else if (xfer->interval > 16)
579 xfer->interval = (1 << (16 - 4));
582 (1 << (xfer->interval - 4));
587 if (xfer->interval == 0) {
589 * One millisecond is the smallest
590 * interval we support:
598 while ((temp != 0) && (temp < xfer->interval)) {
603 switch (parm->speed) {
608 xfer->fps_shift += 3;
615 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
616 * to be equal to zero when setting up USB transfers, hence
617 * this leads to a lot of extra code in the USB kernel.
620 if ((xfer->max_frame_size == 0) ||
621 (xfer->max_packet_size == 0)) {
625 if ((parm->bufsize <= MIN_PKT) &&
626 (type != UE_CONTROL) &&
630 xfer->max_packet_size = MIN_PKT;
631 xfer->max_packet_count = 1;
632 parm->bufsize = 0; /* automatic setup length */
633 usbd_update_max_frame_size(xfer);
636 parm->err = USB_ERR_ZERO_MAXP;
645 * check if we should setup a default
649 if (parm->bufsize == 0) {
651 parm->bufsize = xfer->max_frame_size;
653 if (type == UE_ISOCHRONOUS) {
654 parm->bufsize *= xfer->nframes;
658 * check if we are about to setup a proxy
662 if (xfer->flags.proxy_buffer) {
664 /* round bufsize up */
666 parm->bufsize += (xfer->max_frame_size - 1);
668 if (parm->bufsize < xfer->max_frame_size) {
669 /* length wrapped around */
670 parm->err = USB_ERR_INVAL;
673 /* subtract remainder */
675 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
677 /* add length of USB device request structure, if any */
679 if (type == UE_CONTROL) {
680 parm->bufsize += REQ_SIZE; /* SETUP message */
683 xfer->max_data_length = parm->bufsize;
685 /* Setup "n_frlengths" and "n_frbuffers" */
687 if (type == UE_ISOCHRONOUS) {
688 n_frlengths = xfer->nframes;
692 if (type == UE_CONTROL) {
693 xfer->flags_int.control_xfr = 1;
694 if (xfer->nframes == 0) {
695 if (parm->bufsize <= REQ_SIZE) {
697 * there will never be any data
706 if (xfer->nframes == 0) {
711 n_frlengths = xfer->nframes;
712 n_frbuffers = xfer->nframes;
716 * check if we have room for the
717 * USB device request structure:
720 if (type == UE_CONTROL) {
722 if (xfer->max_data_length < REQ_SIZE) {
723 /* length wrapped around or too small bufsize */
724 parm->err = USB_ERR_INVAL;
727 xfer->max_data_length -= REQ_SIZE;
730 * Setup "frlengths" and shadow "frlengths" for keeping the
731 * initial frame lengths when a USB transfer is complete. This
732 * information is useful when computing isochronous offsets.
734 xfer->frlengths = parm->xfer_length_ptr;
735 parm->xfer_length_ptr += 2 * n_frlengths;
737 /* setup "frbuffers" */
738 xfer->frbuffers = parm->xfer_page_cache_ptr;
739 parm->xfer_page_cache_ptr += n_frbuffers;
741 /* initialize max frame count */
742 xfer->max_frame_count = xfer->nframes;
745 * check if we need to setup
749 if (!xfer->flags.ext_buffer) {
751 struct usb_page_search page_info;
752 struct usb_page_cache *pc;
754 if (usbd_transfer_setup_sub_malloc(parm,
755 &pc, parm->bufsize, 1, 1)) {
756 parm->err = USB_ERR_NOMEM;
757 } else if (parm->buf != NULL) {
759 usbd_get_page(pc, 0, &page_info);
761 xfer->local_buffer = page_info.buffer;
763 usbd_xfer_set_frame_offset(xfer, 0, 0);
765 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
766 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
771 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
773 if (parm->buf != NULL) {
775 USB_ADD_BYTES(parm->buf, parm->size[0]);
777 usbd_xfer_set_frame_offset(xfer, 0, 0);
779 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
780 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
783 parm->size[0] += parm->bufsize;
785 /* align data again */
786 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
790 * Compute maximum buffer size
793 if (parm->bufsize_max < parm->bufsize) {
794 parm->bufsize_max = parm->bufsize;
797 if (xfer->flags_int.bdma_enable) {
799 * Setup "dma_page_ptr".
801 * Proof for formula below:
803 * Assume there are three USB frames having length "a", "b" and
804 * "c". These USB frames will at maximum need "z"
805 * "usb_page" structures. "z" is given by:
807 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
808 * ((c / USB_PAGE_SIZE) + 2);
810 * Constraining "a", "b" and "c" like this:
812 * (a + b + c) <= parm->bufsize
816 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
818 * Here is the general formula:
820 xfer->dma_page_ptr = parm->dma_page_ptr;
821 parm->dma_page_ptr += (2 * n_frbuffers);
822 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
826 /* correct maximum data length */
827 xfer->max_data_length = 0;
829 /* subtract USB frame remainder from "hc_max_frame_size" */
831 xfer->max_hc_frame_size =
832 (parm->hc_max_frame_size -
833 (parm->hc_max_frame_size % xfer->max_frame_size));
835 if (xfer->max_hc_frame_size == 0) {
836 parm->err = USB_ERR_INVAL;
840 /* initialize frame buffers */
843 for (x = 0; x != n_frbuffers; x++) {
844 xfer->frbuffers[x].tag_parent =
845 &xfer->xroot->dma_parent_tag;
847 if (xfer->flags_int.bdma_enable &&
848 (parm->bufsize_max > 0)) {
850 if (usb_pc_dmamap_create(
852 parm->bufsize_max)) {
853 parm->err = USB_ERR_NOMEM;
863 * Set some dummy values so that we avoid division by zero:
865 xfer->max_hc_frame_size = 1;
866 xfer->max_frame_size = 1;
867 xfer->max_packet_size = 1;
868 xfer->max_data_length = 0;
870 xfer->max_frame_count = 0;
875 usbd_transfer_setup_has_bulk(const struct usb_config *setup_start,
879 uint8_t type = setup_start[n_setup].type;
880 if (type == UE_BULK || type == UE_BULK_INTR ||
887 /*------------------------------------------------------------------------*
888 * usbd_transfer_setup - setup an array of USB transfers
890 * NOTE: You must always call "usbd_transfer_unsetup" after calling
891 * "usbd_transfer_setup" if success was returned.
893 * The idea is that the USB device driver should pre-allocate all its
894 * transfers by one call to this function.
899 *------------------------------------------------------------------------*/
901 usbd_transfer_setup(struct usb_device *udev,
902 const uint8_t *ifaces, struct usb_xfer **ppxfer,
903 const struct usb_config *setup_start, uint16_t n_setup,
904 void *priv_sc, struct mtx *xfer_mtx)
906 const struct usb_config *setup_end = setup_start + n_setup;
907 const struct usb_config *setup;
908 struct usb_setup_params *parm;
909 struct usb_endpoint *ep;
910 struct usb_xfer_root *info;
911 struct usb_xfer *xfer;
913 usb_error_t error = 0;
918 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
919 "usbd_transfer_setup can sleep!");
921 /* do some checking first */
924 DPRINTFN(6, "setup array has zero length!\n");
925 return (USB_ERR_INVAL);
927 if (ifaces == NULL) {
928 DPRINTFN(6, "ifaces array is NULL!\n");
929 return (USB_ERR_INVAL);
931 if (xfer_mtx == NULL) {
932 DPRINTFN(6, "using global lock\n");
936 /* more sanity checks */
938 for (setup = setup_start, n = 0;
939 setup != setup_end; setup++, n++) {
940 if (setup->bufsize == (usb_frlength_t)-1) {
941 error = USB_ERR_BAD_BUFSIZE;
942 DPRINTF("invalid bufsize\n");
944 if (setup->callback == NULL) {
945 error = USB_ERR_NO_CALLBACK;
946 DPRINTF("no callback\n");
954 /* Protect scratch area */
955 do_unlock = usbd_ctrl_lock(udev);
960 parm = &udev->scratch.xfer_setup[0].parm;
961 memset(parm, 0, sizeof(*parm));
964 parm->speed = usbd_get_speed(udev);
965 parm->hc_max_packet_count = 1;
967 if (parm->speed >= USB_SPEED_MAX) {
968 parm->err = USB_ERR_INVAL;
971 /* setup all transfers */
977 * Initialize the "usb_xfer_root" structure,
978 * which is common for all our USB transfers.
980 info = USB_ADD_BYTES(buf, 0);
982 info->memory_base = buf;
983 info->memory_size = parm->size[0];
986 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
987 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
989 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
990 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
992 cv_init(&info->cv_drain, "WDRAIN");
994 info->xfer_mtx = xfer_mtx;
996 usb_dma_tag_setup(&info->dma_parent_tag,
997 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
998 xfer_mtx, &usb_bdma_done_event, udev->bus->dma_bits,
1002 info->bus = udev->bus;
1005 TAILQ_INIT(&info->done_q.head);
1006 info->done_q.command = &usbd_callback_wrapper;
1008 TAILQ_INIT(&info->dma_q.head);
1009 info->dma_q.command = &usb_bdma_work_loop;
1011 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
1012 info->done_m[0].xroot = info;
1013 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1014 info->done_m[1].xroot = info;
1017 * In device side mode control endpoint
1018 * requests need to run from a separate
1019 * context, else there is a chance of
1022 if (setup_start == usb_control_ep_cfg)
1024 USB_BUS_CONTROL_XFER_PROC(udev->bus);
1025 else if (xfer_mtx == &Giant)
1027 USB_BUS_GIANT_PROC(udev->bus);
1028 else if (usbd_transfer_setup_has_bulk(setup_start, n_setup))
1030 USB_BUS_NON_GIANT_BULK_PROC(udev->bus);
1033 USB_BUS_NON_GIANT_ISOC_PROC(udev->bus);
1039 parm->size[0] += sizeof(info[0]);
1041 for (setup = setup_start, n = 0;
1042 setup != setup_end; setup++, n++) {
1044 /* skip USB transfers without callbacks: */
1045 if (setup->callback == NULL) {
1048 /* see if there is a matching endpoint */
1049 ep = usbd_get_endpoint(udev,
1050 ifaces[setup->if_index], setup);
1053 * Check that the USB PIPE is valid and that
1054 * the endpoint mode is proper.
1056 * Make sure we don't allocate a streams
1057 * transfer when such a combination is not
1060 if ((ep == NULL) || (ep->methods == NULL) ||
1061 ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1062 (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1063 (setup->stream_id != 0 &&
1064 (setup->stream_id >= USB_MAX_EP_STREAMS ||
1065 (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1066 if (setup->flags.no_pipe_ok)
1068 if ((setup->usb_mode != USB_MODE_DUAL) &&
1069 (setup->usb_mode != udev->flags.usb_mode))
1071 parm->err = USB_ERR_NO_PIPE;
1075 /* align data properly */
1076 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1078 /* store current setup pointer */
1079 parm->curr_setup = setup;
1083 * Common initialization of the
1084 * "usb_xfer" structure.
1086 xfer = USB_ADD_BYTES(buf, parm->size[0]);
1087 xfer->address = udev->address;
1088 xfer->priv_sc = priv_sc;
1091 usb_callout_init_mtx(&xfer->timeout_handle,
1092 &udev->bus->bus_mtx, 0);
1095 * Setup a dummy xfer, hence we are
1096 * writing to the "usb_xfer"
1097 * structure pointed to by "xfer"
1098 * before we have allocated any
1101 xfer = &udev->scratch.xfer_setup[0].dummy;
1102 memset(xfer, 0, sizeof(*xfer));
1106 /* set transfer endpoint pointer */
1107 xfer->endpoint = ep;
1109 /* set transfer stream ID */
1110 xfer->stream_id = setup->stream_id;
1112 parm->size[0] += sizeof(xfer[0]);
1113 parm->methods = xfer->endpoint->methods;
1114 parm->curr_xfer = xfer;
1117 * Call the Host or Device controller transfer
1120 (udev->bus->methods->xfer_setup) (parm);
1122 /* check for error */
1128 * Increment the endpoint refcount. This
1129 * basically prevents setting a new
1130 * configuration and alternate setting
1131 * when USB transfers are in use on
1132 * the given interface. Search the USB
1133 * code for "endpoint->refcount_alloc" if you
1134 * want more information.
1136 USB_BUS_LOCK(info->bus);
1137 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1138 parm->err = USB_ERR_INVAL;
1140 xfer->endpoint->refcount_alloc++;
1142 if (xfer->endpoint->refcount_alloc == 0)
1143 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1144 USB_BUS_UNLOCK(info->bus);
1147 * Whenever we set ppxfer[] then we
1148 * also need to increment the
1151 info->setup_refcount++;
1154 * Transfer is successfully setup and
1160 /* check for error */
1165 if (buf != NULL || parm->err != 0)
1168 /* if no transfers, nothing to do */
1172 /* align data properly */
1173 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1175 /* store offset temporarily */
1176 parm->size[1] = parm->size[0];
1179 * The number of DMA tags required depends on
1180 * the number of endpoints. The current estimate
1181 * for maximum number of DMA tags per endpoint
1183 * 1) for loading memory
1184 * 2) for allocating memory
1185 * 3) for fixing memory [UHCI]
1187 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1190 * DMA tags for QH, TD, Data and more.
1192 parm->dma_tag_max += 8;
1194 parm->dma_tag_p += parm->dma_tag_max;
1196 parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1199 /* align data properly */
1200 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1202 /* store offset temporarily */
1203 parm->size[3] = parm->size[0];
1205 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1208 /* align data properly */
1209 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1211 /* store offset temporarily */
1212 parm->size[4] = parm->size[0];
1214 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1217 /* store end offset temporarily */
1218 parm->size[5] = parm->size[0];
1220 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1223 /* store end offset temporarily */
1225 parm->size[2] = parm->size[0];
1227 /* align data properly */
1228 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1230 parm->size[6] = parm->size[0];
1232 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1235 /* align data properly */
1236 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1238 /* allocate zeroed memory */
1239 buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1242 parm->err = USB_ERR_NOMEM;
1243 DPRINTFN(0, "cannot allocate memory block for "
1244 "configuration (%d bytes)\n",
1248 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1249 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1250 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1251 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1252 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1257 if (info->setup_refcount == 0) {
1259 * "usbd_transfer_unsetup_sub" will unlock
1260 * the bus mutex before returning !
1262 USB_BUS_LOCK(info->bus);
1264 /* something went wrong */
1265 usbd_transfer_unsetup_sub(info, 0);
1269 /* check if any errors happened */
1271 usbd_transfer_unsetup(ppxfer, n_setup);
1276 usbd_ctrl_unlock(udev);
1281 /*------------------------------------------------------------------------*
1282 * usbd_transfer_unsetup_sub - factored out code
1283 *------------------------------------------------------------------------*/
1285 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1288 struct usb_page_cache *pc;
1291 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1293 /* wait for any outstanding DMA operations */
1297 temp = usbd_get_dma_delay(info->udev);
1299 usb_pause_mtx(&info->bus->bus_mtx,
1300 USB_MS_TO_TICKS(temp));
1304 /* make sure that our done messages are not queued anywhere */
1305 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1307 USB_BUS_UNLOCK(info->bus);
1310 /* free DMA'able memory, if any */
1311 pc = info->dma_page_cache_start;
1312 while (pc != info->dma_page_cache_end) {
1313 usb_pc_free_mem(pc);
1317 /* free DMA maps in all "xfer->frbuffers" */
1318 pc = info->xfer_page_cache_start;
1319 while (pc != info->xfer_page_cache_end) {
1320 usb_pc_dmamap_destroy(pc);
1324 /* free all DMA tags */
1325 usb_dma_tag_unsetup(&info->dma_parent_tag);
1328 cv_destroy(&info->cv_drain);
1331 * free the "memory_base" last, hence the "info" structure is
1332 * contained within the "memory_base"!
1334 free(info->memory_base, M_USB);
1337 /*------------------------------------------------------------------------*
1338 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1340 * NOTE: All USB transfers in progress will get called back passing
1341 * the error code "USB_ERR_CANCELLED" before this function
1343 *------------------------------------------------------------------------*/
1345 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1347 struct usb_xfer *xfer;
1348 struct usb_xfer_root *info;
1349 uint8_t needs_delay = 0;
1351 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1352 "usbd_transfer_unsetup can sleep!");
1355 xfer = pxfer[n_setup];
1362 USB_XFER_LOCK(xfer);
1363 USB_BUS_LOCK(info->bus);
1366 * HINT: when you start/stop a transfer, it might be a
1367 * good idea to directly use the "pxfer[]" structure:
1369 * usbd_transfer_start(sc->pxfer[0]);
1370 * usbd_transfer_stop(sc->pxfer[0]);
1372 * That way, if your code has many parts that will not
1373 * stop running under the same lock, in other words
1374 * "xfer_mtx", the usbd_transfer_start and
1375 * usbd_transfer_stop functions will simply return
1376 * when they detect a NULL pointer argument.
1378 * To avoid any races we clear the "pxfer[]" pointer
1379 * while holding the private mutex of the driver:
1381 pxfer[n_setup] = NULL;
1383 USB_BUS_UNLOCK(info->bus);
1384 USB_XFER_UNLOCK(xfer);
1386 usbd_transfer_drain(xfer);
1389 if (xfer->flags_int.bdma_enable)
1393 * NOTE: default endpoint does not have an
1394 * interface, even if endpoint->iface_index == 0
1396 USB_BUS_LOCK(info->bus);
1397 xfer->endpoint->refcount_alloc--;
1398 USB_BUS_UNLOCK(info->bus);
1400 usb_callout_drain(&xfer->timeout_handle);
1402 USB_BUS_LOCK(info->bus);
1404 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1405 "reference count\n"));
1407 info->setup_refcount--;
1409 if (info->setup_refcount == 0) {
1410 usbd_transfer_unsetup_sub(info,
1413 USB_BUS_UNLOCK(info->bus);
1418 /*------------------------------------------------------------------------*
1419 * usbd_control_transfer_init - factored out code
1421 * In USB Device Mode we have to wait for the SETUP packet which
1422 * containst the "struct usb_device_request" structure, before we can
1423 * transfer any data. In USB Host Mode we already have the SETUP
1424 * packet at the moment the USB transfer is started. This leads us to
1425 * having to setup the USB transfer at two different places in
1426 * time. This function just contains factored out control transfer
1427 * initialisation code, so that we don't duplicate the code.
1428 *------------------------------------------------------------------------*/
1430 usbd_control_transfer_init(struct usb_xfer *xfer)
1432 struct usb_device_request req;
1434 /* copy out the USB request header */
1436 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1438 /* setup remainder */
1440 xfer->flags_int.control_rem = UGETW(req.wLength);
1442 /* copy direction to endpoint variable */
1444 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1446 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1449 /*------------------------------------------------------------------------*
1450 * usbd_control_transfer_did_data
1452 * This function returns non-zero if a control endpoint has
1453 * transferred the first DATA packet after the SETUP packet.
1454 * Else it returns zero.
1455 *------------------------------------------------------------------------*/
1457 usbd_control_transfer_did_data(struct usb_xfer *xfer)
1459 struct usb_device_request req;
1461 /* SETUP packet is not yet sent */
1462 if (xfer->flags_int.control_hdr != 0)
1465 /* copy out the USB request header */
1466 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1468 /* compare remainder to the initial value */
1469 return (xfer->flags_int.control_rem != UGETW(req.wLength));
1472 /*------------------------------------------------------------------------*
1473 * usbd_setup_ctrl_transfer
1475 * This function handles initialisation of control transfers. Control
1476 * transfers are special in that regard that they can both transmit
1482 *------------------------------------------------------------------------*/
1484 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1488 /* Check for control endpoint stall */
1489 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1490 /* the control transfer is no longer active */
1491 xfer->flags_int.control_stall = 1;
1492 xfer->flags_int.control_act = 0;
1494 /* don't stall control transfer by default */
1495 xfer->flags_int.control_stall = 0;
1498 /* Check for invalid number of frames */
1499 if (xfer->nframes > 2) {
1501 * If you need to split a control transfer, you
1502 * have to do one part at a time. Only with
1503 * non-control transfers you can do multiple
1506 DPRINTFN(0, "Too many frames: %u\n",
1507 (unsigned int)xfer->nframes);
1512 * Check if there is a control
1513 * transfer in progress:
1515 if (xfer->flags_int.control_act) {
1517 if (xfer->flags_int.control_hdr) {
1519 /* clear send header flag */
1521 xfer->flags_int.control_hdr = 0;
1523 /* setup control transfer */
1524 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1525 usbd_control_transfer_init(xfer);
1528 /* get data length */
1534 /* the size of the SETUP structure is hardcoded ! */
1536 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1537 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1538 xfer->frlengths[0], sizeof(struct
1539 usb_device_request));
1542 /* check USB mode */
1543 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1545 /* check number of frames */
1546 if (xfer->nframes != 1) {
1548 * We need to receive the setup
1549 * message first so that we know the
1552 DPRINTF("Misconfigured transfer\n");
1556 * Set a dummy "control_rem" value. This
1557 * variable will be overwritten later by a
1558 * call to "usbd_control_transfer_init()" !
1560 xfer->flags_int.control_rem = 0xFFFF;
1563 /* setup "endpoint" and "control_rem" */
1565 usbd_control_transfer_init(xfer);
1568 /* set transfer-header flag */
1570 xfer->flags_int.control_hdr = 1;
1572 /* get data length */
1574 len = (xfer->sumlen - sizeof(struct usb_device_request));
1577 /* update did data flag */
1579 xfer->flags_int.control_did_data =
1580 usbd_control_transfer_did_data(xfer);
1582 /* check if there is a length mismatch */
1584 if (len > xfer->flags_int.control_rem) {
1585 DPRINTFN(0, "Length (%d) greater than "
1586 "remaining length (%d)\n", len,
1587 xfer->flags_int.control_rem);
1590 /* check if we are doing a short transfer */
1592 if (xfer->flags.force_short_xfer) {
1593 xfer->flags_int.control_rem = 0;
1595 if ((len != xfer->max_data_length) &&
1596 (len != xfer->flags_int.control_rem) &&
1597 (xfer->nframes != 1)) {
1598 DPRINTFN(0, "Short control transfer without "
1599 "force_short_xfer set\n");
1602 xfer->flags_int.control_rem -= len;
1605 /* the status part is executed when "control_act" is 0 */
1607 if ((xfer->flags_int.control_rem > 0) ||
1608 (xfer->flags.manual_status)) {
1609 /* don't execute the STATUS stage yet */
1610 xfer->flags_int.control_act = 1;
1613 if ((!xfer->flags_int.control_hdr) &&
1614 (xfer->nframes == 1)) {
1616 * This is not a valid operation!
1618 DPRINTFN(0, "Invalid parameter "
1623 /* time to execute the STATUS stage */
1624 xfer->flags_int.control_act = 0;
1626 return (0); /* success */
1629 return (1); /* failure */
1632 /*------------------------------------------------------------------------*
1633 * usbd_transfer_submit - start USB hardware for the given transfer
1635 * This function should only be called from the USB callback.
1636 *------------------------------------------------------------------------*/
1638 usbd_transfer_submit(struct usb_xfer *xfer)
1640 struct usb_xfer_root *info;
1641 struct usb_bus *bus;
1647 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1648 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1652 if (USB_DEBUG_VAR > 0) {
1655 usb_dump_endpoint(xfer->endpoint);
1657 USB_BUS_UNLOCK(bus);
1661 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1662 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1664 /* Only open the USB transfer once! */
1665 if (!xfer->flags_int.open) {
1666 xfer->flags_int.open = 1;
1671 (xfer->endpoint->methods->open) (xfer);
1672 USB_BUS_UNLOCK(bus);
1674 /* set "transferring" flag */
1675 xfer->flags_int.transferring = 1;
1678 /* increment power reference */
1679 usbd_transfer_power_ref(xfer, 1);
1682 * Check if the transfer is waiting on a queue, most
1683 * frequently the "done_q":
1685 if (xfer->wait_queue) {
1687 usbd_transfer_dequeue(xfer);
1688 USB_BUS_UNLOCK(bus);
1690 /* clear "did_dma_delay" flag */
1691 xfer->flags_int.did_dma_delay = 0;
1693 /* clear "did_close" flag */
1694 xfer->flags_int.did_close = 0;
1697 /* clear "bdma_setup" flag */
1698 xfer->flags_int.bdma_setup = 0;
1700 /* by default we cannot cancel any USB transfer immediately */
1701 xfer->flags_int.can_cancel_immed = 0;
1703 /* clear lengths and frame counts by default */
1708 /* clear any previous errors */
1711 /* Check if the device is still alive */
1712 if (info->udev->state < USB_STATE_POWERED) {
1715 * Must return cancelled error code else
1716 * device drivers can hang.
1718 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1719 USB_BUS_UNLOCK(bus);
1724 if (xfer->nframes == 0) {
1725 if (xfer->flags.stall_pipe) {
1727 * Special case - want to stall without transferring
1730 DPRINTF("xfer=%p nframes=0: stall "
1731 "or clear stall!\n", xfer);
1733 xfer->flags_int.can_cancel_immed = 1;
1734 /* start the transfer */
1735 usb_command_wrapper(&xfer->endpoint->
1736 endpoint_q[xfer->stream_id], xfer);
1737 USB_BUS_UNLOCK(bus);
1741 usbd_transfer_done(xfer, USB_ERR_INVAL);
1742 USB_BUS_UNLOCK(bus);
1745 /* compute some variables */
1747 for (x = 0; x != xfer->nframes; x++) {
1748 /* make a copy of the frlenghts[] */
1749 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1750 /* compute total transfer length */
1751 xfer->sumlen += xfer->frlengths[x];
1752 if (xfer->sumlen < xfer->frlengths[x]) {
1753 /* length wrapped around */
1755 usbd_transfer_done(xfer, USB_ERR_INVAL);
1756 USB_BUS_UNLOCK(bus);
1761 /* clear some internal flags */
1763 xfer->flags_int.short_xfer_ok = 0;
1764 xfer->flags_int.short_frames_ok = 0;
1766 /* check if this is a control transfer */
1768 if (xfer->flags_int.control_xfr) {
1770 if (usbd_setup_ctrl_transfer(xfer)) {
1772 usbd_transfer_done(xfer, USB_ERR_STALLED);
1773 USB_BUS_UNLOCK(bus);
1778 * Setup filtered version of some transfer flags,
1779 * in case of data read direction
1781 if (USB_GET_DATA_ISREAD(xfer)) {
1783 if (xfer->flags.short_frames_ok) {
1784 xfer->flags_int.short_xfer_ok = 1;
1785 xfer->flags_int.short_frames_ok = 1;
1786 } else if (xfer->flags.short_xfer_ok) {
1787 xfer->flags_int.short_xfer_ok = 1;
1789 /* check for control transfer */
1790 if (xfer->flags_int.control_xfr) {
1792 * 1) Control transfers do not support
1793 * reception of multiple short USB
1794 * frames in host mode and device side
1795 * mode, with exception of:
1797 * 2) Due to sometimes buggy device
1798 * side firmware we need to do a
1799 * STATUS stage in case of short
1800 * control transfers in USB host mode.
1801 * The STATUS stage then becomes the
1802 * "alt_next" to the DATA stage.
1804 xfer->flags_int.short_frames_ok = 1;
1809 * Check if BUS-DMA support is enabled and try to load virtual
1810 * buffers into DMA, if any:
1813 if (xfer->flags_int.bdma_enable) {
1814 /* insert the USB transfer last in the BUS-DMA queue */
1815 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1820 * Enter the USB transfer into the Host Controller or
1821 * Device Controller schedule:
1823 usbd_pipe_enter(xfer);
1826 /*------------------------------------------------------------------------*
1827 * usbd_pipe_enter - factored out code
1828 *------------------------------------------------------------------------*/
1830 usbd_pipe_enter(struct usb_xfer *xfer)
1832 struct usb_endpoint *ep;
1834 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1836 USB_BUS_LOCK(xfer->xroot->bus);
1838 ep = xfer->endpoint;
1842 /* the transfer can now be cancelled */
1843 xfer->flags_int.can_cancel_immed = 1;
1845 /* enter the transfer */
1846 (ep->methods->enter) (xfer);
1848 /* check for transfer error */
1850 /* some error has happened */
1851 usbd_transfer_done(xfer, 0);
1852 USB_BUS_UNLOCK(xfer->xroot->bus);
1856 /* start the transfer */
1857 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1858 USB_BUS_UNLOCK(xfer->xroot->bus);
1861 /*------------------------------------------------------------------------*
1862 * usbd_transfer_start - start an USB transfer
1864 * NOTE: Calling this function more than one time will only
1865 * result in a single transfer start, until the USB transfer
1867 *------------------------------------------------------------------------*/
1869 usbd_transfer_start(struct usb_xfer *xfer)
1872 /* transfer is gone */
1875 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1877 /* mark the USB transfer started */
1879 if (!xfer->flags_int.started) {
1880 /* lock the BUS lock to avoid races updating flags_int */
1881 USB_BUS_LOCK(xfer->xroot->bus);
1882 xfer->flags_int.started = 1;
1883 USB_BUS_UNLOCK(xfer->xroot->bus);
1885 /* check if the USB transfer callback is already transferring */
1887 if (xfer->flags_int.transferring) {
1890 USB_BUS_LOCK(xfer->xroot->bus);
1891 /* call the USB transfer callback */
1892 usbd_callback_ss_done_defer(xfer);
1893 USB_BUS_UNLOCK(xfer->xroot->bus);
1896 /*------------------------------------------------------------------------*
1897 * usbd_transfer_stop - stop an USB transfer
1899 * NOTE: Calling this function more than one time will only
1900 * result in a single transfer stop.
1901 * NOTE: When this function returns it is not safe to free nor
1902 * reuse any DMA buffers. See "usbd_transfer_drain()".
1903 *------------------------------------------------------------------------*/
1905 usbd_transfer_stop(struct usb_xfer *xfer)
1907 struct usb_endpoint *ep;
1910 /* transfer is gone */
1913 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1915 /* check if the USB transfer was ever opened */
1917 if (!xfer->flags_int.open) {
1918 if (xfer->flags_int.started) {
1919 /* nothing to do except clearing the "started" flag */
1920 /* lock the BUS lock to avoid races updating flags_int */
1921 USB_BUS_LOCK(xfer->xroot->bus);
1922 xfer->flags_int.started = 0;
1923 USB_BUS_UNLOCK(xfer->xroot->bus);
1927 /* try to stop the current USB transfer */
1929 USB_BUS_LOCK(xfer->xroot->bus);
1930 /* override any previous error */
1931 xfer->error = USB_ERR_CANCELLED;
1934 * Clear "open" and "started" when both private and USB lock
1935 * is locked so that we don't get a race updating "flags_int"
1937 xfer->flags_int.open = 0;
1938 xfer->flags_int.started = 0;
1941 * Check if we can cancel the USB transfer immediately.
1943 if (xfer->flags_int.transferring) {
1944 if (xfer->flags_int.can_cancel_immed &&
1945 (!xfer->flags_int.did_close)) {
1948 * The following will lead to an USB_ERR_CANCELLED
1949 * error code being passed to the USB callback.
1951 (xfer->endpoint->methods->close) (xfer);
1952 /* only close once */
1953 xfer->flags_int.did_close = 1;
1955 /* need to wait for the next done callback */
1960 /* close here and now */
1961 (xfer->endpoint->methods->close) (xfer);
1964 * Any additional DMA delay is done by
1965 * "usbd_transfer_unsetup()".
1969 * Special case. Check if we need to restart a blocked
1972 ep = xfer->endpoint;
1975 * If the current USB transfer is completing we need
1976 * to start the next one:
1978 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
1979 usb_command_wrapper(
1980 &ep->endpoint_q[xfer->stream_id], NULL);
1984 USB_BUS_UNLOCK(xfer->xroot->bus);
1987 /*------------------------------------------------------------------------*
1988 * usbd_transfer_pending
1990 * This function will check if an USB transfer is pending which is a
1991 * little bit complicated!
1994 * 1: Pending: The USB transfer will receive a callback in the future.
1995 *------------------------------------------------------------------------*/
1997 usbd_transfer_pending(struct usb_xfer *xfer)
1999 struct usb_xfer_root *info;
2000 struct usb_xfer_queue *pq;
2003 /* transfer is gone */
2006 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2008 if (xfer->flags_int.transferring) {
2012 USB_BUS_LOCK(xfer->xroot->bus);
2013 if (xfer->wait_queue) {
2014 /* we are waiting on a queue somewhere */
2015 USB_BUS_UNLOCK(xfer->xroot->bus);
2021 if (pq->curr == xfer) {
2022 /* we are currently scheduled for callback */
2023 USB_BUS_UNLOCK(xfer->xroot->bus);
2026 /* we are not pending */
2027 USB_BUS_UNLOCK(xfer->xroot->bus);
2031 /*------------------------------------------------------------------------*
2032 * usbd_transfer_drain
2034 * This function will stop the USB transfer and wait for any
2035 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2036 * are loaded into DMA can safely be freed or reused after that this
2037 * function has returned.
2038 *------------------------------------------------------------------------*/
2040 usbd_transfer_drain(struct usb_xfer *xfer)
2042 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2043 "usbd_transfer_drain can sleep!");
2046 /* transfer is gone */
2049 if (xfer->xroot->xfer_mtx != &Giant) {
2050 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
2052 USB_XFER_LOCK(xfer);
2054 usbd_transfer_stop(xfer);
2056 while (usbd_transfer_pending(xfer) ||
2057 xfer->flags_int.doing_callback) {
2060 * It is allowed that the callback can drop its
2061 * transfer mutex. In that case checking only
2062 * "usbd_transfer_pending()" is not enough to tell if
2063 * the USB transfer is fully drained. We also need to
2064 * check the internal "doing_callback" flag.
2066 xfer->flags_int.draining = 1;
2069 * Wait until the current outstanding USB
2070 * transfer is complete !
2072 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
2074 USB_XFER_UNLOCK(xfer);
2077 struct usb_page_cache *
2078 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2080 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2082 return (&xfer->frbuffers[frindex]);
2086 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2088 struct usb_page_search page_info;
2090 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2092 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2093 return (page_info.buffer);
2096 /*------------------------------------------------------------------------*
2097 * usbd_xfer_get_fps_shift
2099 * The following function is only useful for isochronous transfers. It
2100 * returns how many times the frame execution rate has been shifted
2106 *------------------------------------------------------------------------*/
2108 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2110 return (xfer->fps_shift);
2114 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2116 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2118 return (xfer->frlengths[frindex]);
2121 /*------------------------------------------------------------------------*
2122 * usbd_xfer_set_frame_data
2124 * This function sets the pointer of the buffer that should
2125 * loaded directly into DMA for the given USB frame. Passing "ptr"
2126 * equal to NULL while the corresponding "frlength" is greater
2127 * than zero gives undefined results!
2128 *------------------------------------------------------------------------*/
2130 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2131 void *ptr, usb_frlength_t len)
2133 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2135 /* set virtual address to load and length */
2136 xfer->frbuffers[frindex].buffer = ptr;
2137 usbd_xfer_set_frame_len(xfer, frindex, len);
2141 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2142 void **ptr, int *len)
2144 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2147 *ptr = xfer->frbuffers[frindex].buffer;
2149 *len = xfer->frlengths[frindex];
2152 /*------------------------------------------------------------------------*
2153 * usbd_xfer_old_frame_length
2155 * This function returns the framelength of the given frame at the
2156 * time the transfer was submitted. This function can be used to
2157 * compute the starting data pointer of the next isochronous frame
2158 * when an isochronous transfer has completed.
2159 *------------------------------------------------------------------------*/
2161 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2163 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2165 return (xfer->frlengths[frindex + xfer->max_frame_count]);
2169 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2173 *actlen = xfer->actlen;
2175 *sumlen = xfer->sumlen;
2176 if (aframes != NULL)
2177 *aframes = xfer->aframes;
2178 if (nframes != NULL)
2179 *nframes = xfer->nframes;
2182 /*------------------------------------------------------------------------*
2183 * usbd_xfer_set_frame_offset
2185 * This function sets the frame data buffer offset relative to the beginning
2186 * of the USB DMA buffer allocated for this USB transfer.
2187 *------------------------------------------------------------------------*/
2189 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2190 usb_frcount_t frindex)
2192 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2193 "when the USB buffer is external\n"));
2194 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2196 /* set virtual address to load */
2197 xfer->frbuffers[frindex].buffer =
2198 USB_ADD_BYTES(xfer->local_buffer, offset);
2202 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2208 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2214 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2220 usbd_xfer_max_frames(struct usb_xfer *xfer)
2222 return (xfer->max_frame_count);
2226 usbd_xfer_max_len(struct usb_xfer *xfer)
2228 return (xfer->max_data_length);
2232 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2234 return (xfer->max_frame_size);
2238 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2241 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2243 xfer->frlengths[frindex] = len;
2246 /*------------------------------------------------------------------------*
2247 * usb_callback_proc - factored out code
2249 * This function performs USB callbacks.
2250 *------------------------------------------------------------------------*/
2252 usb_callback_proc(struct usb_proc_msg *_pm)
2254 struct usb_done_msg *pm = (void *)_pm;
2255 struct usb_xfer_root *info = pm->xroot;
2257 /* Change locking order */
2258 USB_BUS_UNLOCK(info->bus);
2261 * We exploit the fact that the mutex is the same for all
2262 * callbacks that will be called from this thread:
2264 USB_MTX_LOCK(info->xfer_mtx);
2265 USB_BUS_LOCK(info->bus);
2267 /* Continue where we lost track */
2268 usb_command_wrapper(&info->done_q,
2271 USB_MTX_UNLOCK(info->xfer_mtx);
2274 /*------------------------------------------------------------------------*
2275 * usbd_callback_ss_done_defer
2277 * This function will defer the start, stop and done callback to the
2279 *------------------------------------------------------------------------*/
2281 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2283 struct usb_xfer_root *info = xfer->xroot;
2284 struct usb_xfer_queue *pq = &info->done_q;
2286 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2288 if (pq->curr != xfer) {
2289 usbd_transfer_enqueue(pq, xfer);
2291 if (!pq->recurse_1) {
2294 * We have to postpone the callback due to the fact we
2295 * will have a Lock Order Reversal, LOR, if we try to
2298 (void) usb_proc_msignal(info->done_p,
2299 &info->done_m[0], &info->done_m[1]);
2301 /* clear second recurse flag */
2308 /*------------------------------------------------------------------------*
2309 * usbd_callback_wrapper
2311 * This is a wrapper for USB callbacks. This wrapper does some
2312 * auto-magic things like figuring out if we can call the callback
2313 * directly from the current context or if we need to wakeup the
2314 * interrupt process.
2315 *------------------------------------------------------------------------*/
2317 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2319 struct usb_xfer *xfer = pq->curr;
2320 struct usb_xfer_root *info = xfer->xroot;
2322 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2323 if ((pq->recurse_3 != 0 || mtx_owned(info->xfer_mtx) == 0) &&
2324 USB_IN_POLLING_MODE_FUNC() == 0) {
2326 * Cases that end up here:
2328 * 5) HW interrupt done callback or other source.
2329 * 6) HW completed transfer during callback
2331 DPRINTFN(3, "case 5 and 6\n");
2334 * We have to postpone the callback due to the fact we
2335 * will have a Lock Order Reversal, LOR, if we try to
2338 * Postponing the callback also ensures that other USB
2339 * transfer queues get a chance.
2341 (void) usb_proc_msignal(info->done_p,
2342 &info->done_m[0], &info->done_m[1]);
2346 * Cases that end up here:
2348 * 1) We are starting a transfer
2349 * 2) We are prematurely calling back a transfer
2350 * 3) We are stopping a transfer
2351 * 4) We are doing an ordinary callback
2353 DPRINTFN(3, "case 1-4\n");
2354 /* get next USB transfer in the queue */
2355 info->done_q.curr = NULL;
2357 /* set flag in case of drain */
2358 xfer->flags_int.doing_callback = 1;
2360 USB_BUS_UNLOCK(info->bus);
2361 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2363 /* set correct USB state for callback */
2364 if (!xfer->flags_int.transferring) {
2365 xfer->usb_state = USB_ST_SETUP;
2366 if (!xfer->flags_int.started) {
2367 /* we got stopped before we even got started */
2368 USB_BUS_LOCK(info->bus);
2373 if (usbd_callback_wrapper_sub(xfer)) {
2374 /* the callback has been deferred */
2375 USB_BUS_LOCK(info->bus);
2379 /* decrement power reference */
2380 usbd_transfer_power_ref(xfer, -1);
2382 xfer->flags_int.transferring = 0;
2385 xfer->usb_state = USB_ST_ERROR;
2387 /* set transferred state */
2388 xfer->usb_state = USB_ST_TRANSFERRED;
2390 /* sync DMA memory, if any */
2391 if (xfer->flags_int.bdma_enable &&
2392 (!xfer->flags_int.bdma_no_post_sync)) {
2393 usb_bdma_post_sync(xfer);
2400 if (xfer->usb_state != USB_ST_SETUP) {
2401 USB_BUS_LOCK(info->bus);
2402 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2403 USB_BUS_UNLOCK(info->bus);
2406 /* call processing routine */
2407 (xfer->callback) (xfer, xfer->error);
2409 /* pickup the USB mutex again */
2410 USB_BUS_LOCK(info->bus);
2413 * Check if we got started after that we got cancelled, but
2414 * before we managed to do the callback.
2416 if ((!xfer->flags_int.open) &&
2417 (xfer->flags_int.started) &&
2418 (xfer->usb_state == USB_ST_ERROR)) {
2419 /* clear flag in case of drain */
2420 xfer->flags_int.doing_callback = 0;
2421 /* try to loop, but not recursivly */
2422 usb_command_wrapper(&info->done_q, xfer);
2427 /* clear flag in case of drain */
2428 xfer->flags_int.doing_callback = 0;
2431 * Check if we are draining.
2433 if (xfer->flags_int.draining &&
2434 (!xfer->flags_int.transferring)) {
2435 /* "usbd_transfer_drain()" is waiting for end of transfer */
2436 xfer->flags_int.draining = 0;
2437 cv_broadcast(&info->cv_drain);
2440 /* do the next callback, if any */
2441 usb_command_wrapper(&info->done_q,
2445 /*------------------------------------------------------------------------*
2446 * usb_dma_delay_done_cb
2448 * This function is called when the DMA delay has been exectuded, and
2449 * will make sure that the callback is called to complete the USB
2450 * transfer. This code path is usually only used when there is an USB
2451 * error like USB_ERR_CANCELLED.
2452 *------------------------------------------------------------------------*/
2454 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2456 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2458 DPRINTFN(3, "Completed %p\n", xfer);
2460 /* queue callback for execution, again */
2461 usbd_transfer_done(xfer, 0);
2464 /*------------------------------------------------------------------------*
2465 * usbd_transfer_dequeue
2467 * - This function is used to remove an USB transfer from a USB
2470 * - This function can be called multiple times in a row.
2471 *------------------------------------------------------------------------*/
2473 usbd_transfer_dequeue(struct usb_xfer *xfer)
2475 struct usb_xfer_queue *pq;
2477 pq = xfer->wait_queue;
2479 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2480 xfer->wait_queue = NULL;
2484 /*------------------------------------------------------------------------*
2485 * usbd_transfer_enqueue
2487 * - This function is used to insert an USB transfer into a USB *
2490 * - This function can be called multiple times in a row.
2491 *------------------------------------------------------------------------*/
2493 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2496 * Insert the USB transfer into the queue, if it is not
2497 * already on a USB transfer queue:
2499 if (xfer->wait_queue == NULL) {
2500 xfer->wait_queue = pq;
2501 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2505 /*------------------------------------------------------------------------*
2506 * usbd_transfer_done
2508 * - This function is used to remove an USB transfer from the busdma,
2509 * pipe or interrupt queue.
2511 * - This function is used to queue the USB transfer on the done
2514 * - This function is used to stop any USB transfer timeouts.
2515 *------------------------------------------------------------------------*/
2517 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2519 struct usb_xfer_root *info = xfer->xroot;
2521 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2523 DPRINTF("err=%s\n", usbd_errstr(error));
2526 * If we are not transferring then just return.
2527 * This can happen during transfer cancel.
2529 if (!xfer->flags_int.transferring) {
2530 DPRINTF("not transferring\n");
2531 /* end of control transfer, if any */
2532 xfer->flags_int.control_act = 0;
2535 /* only set transfer error, if not already set */
2536 if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2537 xfer->error = error;
2539 /* stop any callouts */
2540 usb_callout_stop(&xfer->timeout_handle);
2543 * If we are waiting on a queue, just remove the USB transfer
2544 * from the queue, if any. We should have the required locks
2545 * locked to do the remove when this function is called.
2547 usbd_transfer_dequeue(xfer);
2550 if (mtx_owned(info->xfer_mtx)) {
2551 struct usb_xfer_queue *pq;
2554 * If the private USB lock is not locked, then we assume
2555 * that the BUS-DMA load stage has been passed:
2559 if (pq->curr == xfer) {
2560 /* start the next BUS-DMA load, if any */
2561 usb_command_wrapper(pq, NULL);
2565 /* keep some statistics */
2567 info->bus->stats_err.uds_requests
2568 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2570 info->bus->stats_ok.uds_requests
2571 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2574 /* call the USB transfer callback */
2575 usbd_callback_ss_done_defer(xfer);
2578 /*------------------------------------------------------------------------*
2579 * usbd_transfer_start_cb
2581 * This function is called to start the USB transfer when
2582 * "xfer->interval" is greater than zero, and and the endpoint type is
2584 *------------------------------------------------------------------------*/
2586 usbd_transfer_start_cb(void *arg)
2588 struct usb_xfer *xfer = arg;
2589 struct usb_endpoint *ep = xfer->endpoint;
2591 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2596 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2599 /* the transfer can now be cancelled */
2600 xfer->flags_int.can_cancel_immed = 1;
2602 /* start USB transfer, if no error */
2603 if (xfer->error == 0)
2604 (ep->methods->start) (xfer);
2606 /* check for transfer error */
2608 /* some error has happened */
2609 usbd_transfer_done(xfer, 0);
2613 /*------------------------------------------------------------------------*
2614 * usbd_xfer_set_stall
2616 * This function is used to set the stall flag outside the
2617 * callback. This function is NULL safe.
2618 *------------------------------------------------------------------------*/
2620 usbd_xfer_set_stall(struct usb_xfer *xfer)
2626 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2628 /* avoid any races by locking the USB mutex */
2629 USB_BUS_LOCK(xfer->xroot->bus);
2630 xfer->flags.stall_pipe = 1;
2631 USB_BUS_UNLOCK(xfer->xroot->bus);
2635 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2637 return (xfer->endpoint->is_stalled);
2640 /*------------------------------------------------------------------------*
2641 * usbd_transfer_clear_stall
2643 * This function is used to clear the stall flag outside the
2644 * callback. This function is NULL safe.
2645 *------------------------------------------------------------------------*/
2647 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2653 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2655 /* avoid any races by locking the USB mutex */
2656 USB_BUS_LOCK(xfer->xroot->bus);
2658 xfer->flags.stall_pipe = 0;
2660 USB_BUS_UNLOCK(xfer->xroot->bus);
2663 /*------------------------------------------------------------------------*
2666 * This function is used to add an USB transfer to the pipe transfer list.
2667 *------------------------------------------------------------------------*/
2669 usbd_pipe_start(struct usb_xfer_queue *pq)
2671 struct usb_endpoint *ep;
2672 struct usb_xfer *xfer;
2676 ep = xfer->endpoint;
2678 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2681 * If the endpoint is already stalled we do nothing !
2683 if (ep->is_stalled) {
2687 * Check if we are supposed to stall the endpoint:
2689 if (xfer->flags.stall_pipe) {
2690 struct usb_device *udev;
2691 struct usb_xfer_root *info;
2693 /* clear stall command */
2694 xfer->flags.stall_pipe = 0;
2696 /* get pointer to USB device */
2701 * Only stall BULK and INTERRUPT endpoints.
2703 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2704 if ((type == UE_BULK) ||
2705 (type == UE_INTERRUPT)) {
2710 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2711 (udev->bus->methods->set_stall) (
2712 udev, ep, &did_stall);
2713 } else if (udev->ctrl_xfer[1]) {
2714 info = udev->ctrl_xfer[1]->xroot;
2716 USB_BUS_CS_PROC(info->bus),
2717 &udev->cs_msg[0], &udev->cs_msg[1]);
2719 /* should not happen */
2720 DPRINTFN(0, "No stall handler\n");
2723 * Check if we should stall. Some USB hardware
2724 * handles set- and clear-stall in hardware.
2728 * The transfer will be continued when
2729 * the clear-stall control endpoint
2730 * message is received.
2735 } else if (type == UE_ISOCHRONOUS) {
2738 * Make sure any FIFO overflow or other FIFO
2739 * error conditions go away by resetting the
2740 * endpoint FIFO through the clear stall
2743 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2744 (udev->bus->methods->clear_stall) (udev, ep);
2748 /* Set or clear stall complete - special case */
2749 if (xfer->nframes == 0) {
2750 /* we are complete */
2752 usbd_transfer_done(xfer, 0);
2758 * 1) Start the first transfer queued.
2760 * 2) Re-start the current USB transfer.
2763 * Check if there should be any
2764 * pre transfer start delay:
2766 if (xfer->interval > 0) {
2767 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2768 if ((type == UE_BULK) ||
2769 (type == UE_CONTROL)) {
2770 usbd_transfer_timeout_ms(xfer,
2771 &usbd_transfer_start_cb,
2779 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2781 /* the transfer can now be cancelled */
2782 xfer->flags_int.can_cancel_immed = 1;
2784 /* start USB transfer, if no error */
2785 if (xfer->error == 0)
2786 (ep->methods->start) (xfer);
2788 /* check for transfer error */
2790 /* some error has happened */
2791 usbd_transfer_done(xfer, 0);
2795 /*------------------------------------------------------------------------*
2796 * usbd_transfer_timeout_ms
2798 * This function is used to setup a timeout on the given USB
2799 * transfer. If the timeout has been deferred the callback given by
2800 * "cb" will get called after "ms" milliseconds.
2801 *------------------------------------------------------------------------*/
2803 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2804 void (*cb) (void *arg), usb_timeout_t ms)
2806 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2809 usb_callout_reset(&xfer->timeout_handle,
2810 USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2813 /*------------------------------------------------------------------------*
2814 * usbd_callback_wrapper_sub
2816 * - This function will update variables in an USB transfer after
2817 * that the USB transfer is complete.
2819 * - This function is used to start the next USB transfer on the
2820 * ep transfer queue, if any.
2822 * NOTE: In some special cases the USB transfer will not be removed from
2823 * the pipe queue, but remain first. To enforce USB transfer removal call
2824 * this function passing the error code "USB_ERR_CANCELLED".
2828 * Else: The callback has been deferred.
2829 *------------------------------------------------------------------------*/
2831 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2833 struct usb_endpoint *ep;
2834 struct usb_bus *bus;
2837 bus = xfer->xroot->bus;
2839 if ((!xfer->flags_int.open) &&
2840 (!xfer->flags_int.did_close)) {
2843 (xfer->endpoint->methods->close) (xfer);
2844 USB_BUS_UNLOCK(bus);
2845 /* only close once */
2846 xfer->flags_int.did_close = 1;
2847 return (1); /* wait for new callback */
2850 * If we have a non-hardware induced error we
2851 * need to do the DMA delay!
2853 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2854 (xfer->error == USB_ERR_CANCELLED ||
2855 xfer->error == USB_ERR_TIMEOUT ||
2856 bus->methods->start_dma_delay != NULL)) {
2860 /* only delay once */
2861 xfer->flags_int.did_dma_delay = 1;
2863 /* we can not cancel this delay */
2864 xfer->flags_int.can_cancel_immed = 0;
2866 temp = usbd_get_dma_delay(xfer->xroot->udev);
2868 DPRINTFN(3, "DMA delay, %u ms, "
2869 "on %p\n", temp, xfer);
2874 * Some hardware solutions have dedicated
2875 * events when it is safe to free DMA'ed
2876 * memory. For the other hardware platforms we
2877 * use a static delay.
2879 if (bus->methods->start_dma_delay != NULL) {
2880 (bus->methods->start_dma_delay) (xfer);
2882 usbd_transfer_timeout_ms(xfer,
2883 (void (*)(void *))&usb_dma_delay_done_cb,
2886 USB_BUS_UNLOCK(bus);
2887 return (1); /* wait for new callback */
2890 /* check actual number of frames */
2891 if (xfer->aframes > xfer->nframes) {
2892 if (xfer->error == 0) {
2893 panic("%s: actual number of frames, %d, is "
2894 "greater than initial number of frames, %d\n",
2895 __FUNCTION__, xfer->aframes, xfer->nframes);
2897 /* just set some valid value */
2898 xfer->aframes = xfer->nframes;
2901 /* compute actual length */
2904 for (x = 0; x != xfer->aframes; x++) {
2905 xfer->actlen += xfer->frlengths[x];
2909 * Frames that were not transferred get zero actual length in
2910 * case the USB device driver does not check the actual number
2911 * of frames transferred, "xfer->aframes":
2913 for (; x < xfer->nframes; x++) {
2914 usbd_xfer_set_frame_len(xfer, x, 0);
2917 /* check actual length */
2918 if (xfer->actlen > xfer->sumlen) {
2919 if (xfer->error == 0) {
2920 panic("%s: actual length, %d, is greater than "
2921 "initial length, %d\n",
2922 __FUNCTION__, xfer->actlen, xfer->sumlen);
2924 /* just set some valid value */
2925 xfer->actlen = xfer->sumlen;
2928 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2929 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2930 xfer->aframes, xfer->nframes);
2933 /* end of control transfer, if any */
2934 xfer->flags_int.control_act = 0;
2936 #if USB_HAVE_TT_SUPPORT
2937 switch (xfer->error) {
2938 case USB_ERR_NORMAL_COMPLETION:
2939 case USB_ERR_SHORT_XFER:
2940 case USB_ERR_STALLED:
2941 case USB_ERR_CANCELLED:
2945 /* try to reset the TT, if any */
2947 uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint);
2948 USB_BUS_UNLOCK(bus);
2952 /* check if we should block the execution queue */
2953 if ((xfer->error != USB_ERR_CANCELLED) &&
2954 (xfer->flags.pipe_bof)) {
2955 DPRINTFN(2, "xfer=%p: Block On Failure "
2956 "on endpoint=%p\n", xfer, xfer->endpoint);
2960 /* check for short transfers */
2961 if (xfer->actlen < xfer->sumlen) {
2963 /* end of control transfer, if any */
2964 xfer->flags_int.control_act = 0;
2966 if (!xfer->flags_int.short_xfer_ok) {
2967 xfer->error = USB_ERR_SHORT_XFER;
2968 if (xfer->flags.pipe_bof) {
2969 DPRINTFN(2, "xfer=%p: Block On Failure on "
2970 "Short Transfer on endpoint %p.\n",
2971 xfer, xfer->endpoint);
2977 * Check if we are in the middle of a
2980 if (xfer->flags_int.control_act) {
2981 DPRINTFN(5, "xfer=%p: Control transfer "
2982 "active on endpoint=%p\n", xfer, xfer->endpoint);
2988 ep = xfer->endpoint;
2991 * If the current USB transfer is completing we need to start the
2995 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2996 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
2998 if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
2999 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
3000 /* there is another USB transfer waiting */
3002 /* this is the last USB transfer */
3003 /* clear isochronous sync flag */
3004 xfer->endpoint->is_synced = 0;
3007 USB_BUS_UNLOCK(bus);
3012 /*------------------------------------------------------------------------*
3013 * usb_command_wrapper
3015 * This function is used to execute commands non-recursivly on an USB
3017 *------------------------------------------------------------------------*/
3019 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3023 * If the transfer is not already processing,
3026 if (pq->curr != xfer) {
3027 usbd_transfer_enqueue(pq, xfer);
3028 if (pq->curr != NULL) {
3029 /* something is already processing */
3030 DPRINTFN(6, "busy %p\n", pq->curr);
3035 /* Get next element in queue */
3039 if (!pq->recurse_1) {
3041 /* clear third recurse flag */
3045 /* set two first recurse flags */
3049 if (pq->curr == NULL) {
3050 xfer = TAILQ_FIRST(&pq->head);
3052 TAILQ_REMOVE(&pq->head, xfer,
3054 xfer->wait_queue = NULL;
3060 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3062 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3065 * Set third recurse flag to indicate
3066 * recursion happened:
3070 } while (!pq->recurse_2);
3072 /* clear first recurse flag */
3076 /* clear second recurse flag */
3081 /*------------------------------------------------------------------------*
3082 * usbd_ctrl_transfer_setup
3084 * This function is used to setup the default USB control endpoint
3086 *------------------------------------------------------------------------*/
3088 usbd_ctrl_transfer_setup(struct usb_device *udev)
3090 struct usb_xfer *xfer;
3092 uint8_t iface_index;
3094 /* check for root HUB */
3095 if (udev->parent_hub == NULL)
3099 xfer = udev->ctrl_xfer[0];
3101 USB_XFER_LOCK(xfer);
3103 ((xfer->address == udev->address) &&
3104 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3105 udev->ddesc.bMaxPacketSize));
3106 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3109 * NOTE: checking "xfer->address" and
3110 * starting the USB transfer must be
3113 usbd_transfer_start(xfer);
3116 USB_XFER_UNLOCK(xfer);
3123 * All parameters are exactly the same like before.
3129 * Update wMaxPacketSize for the default control endpoint:
3131 udev->ctrl_ep_desc.wMaxPacketSize[0] =
3132 udev->ddesc.bMaxPacketSize;
3135 * Unsetup any existing USB transfer:
3137 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3140 * Reset clear stall error counter.
3142 udev->clear_stall_errors = 0;
3145 * Try to setup a new USB transfer for the
3146 * default control endpoint:
3149 if (usbd_transfer_setup(udev, &iface_index,
3150 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3151 &udev->device_mtx)) {
3152 DPRINTFN(0, "could not setup default "
3159 /*------------------------------------------------------------------------*
3160 * usbd_clear_data_toggle - factored out code
3162 * NOTE: the intention of this function is not to reset the hardware
3164 *------------------------------------------------------------------------*/
3166 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3168 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3170 /* check that we have a valid case */
3171 if (udev->flags.usb_mode == USB_MODE_HOST &&
3172 udev->parent_hub != NULL &&
3173 udev->bus->methods->clear_stall != NULL &&
3174 ep->methods != NULL) {
3175 (udev->bus->methods->clear_stall) (udev, ep);
3179 /*------------------------------------------------------------------------*
3180 * usbd_clear_data_toggle - factored out code
3182 * NOTE: the intention of this function is not to reset the hardware
3183 * data toggle on the USB device side.
3184 *------------------------------------------------------------------------*/
3186 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3188 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3190 USB_BUS_LOCK(udev->bus);
3191 ep->toggle_next = 0;
3192 /* some hardware needs a callback to clear the data toggle */
3193 usbd_clear_stall_locked(udev, ep);
3194 USB_BUS_UNLOCK(udev->bus);
3197 /*------------------------------------------------------------------------*
3198 * usbd_clear_stall_callback - factored out clear stall callback
3201 * xfer1: Clear Stall Control Transfer
3202 * xfer2: Stalled USB Transfer
3204 * This function is NULL safe.
3210 * Clear stall config example:
3212 * static const struct usb_config my_clearstall = {
3213 * .type = UE_CONTROL,
3215 * .direction = UE_DIR_ANY,
3216 * .interval = 50, //50 milliseconds
3217 * .bufsize = sizeof(struct usb_device_request),
3218 * .timeout = 1000, //1.000 seconds
3219 * .callback = &my_clear_stall_callback, // **
3220 * .usb_mode = USB_MODE_HOST,
3223 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3224 * passing the correct parameters.
3225 *------------------------------------------------------------------------*/
3227 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3228 struct usb_xfer *xfer2)
3230 struct usb_device_request req;
3232 if (xfer2 == NULL) {
3233 /* looks like we are tearing down */
3234 DPRINTF("NULL input parameter\n");
3237 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3238 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3240 switch (USB_GET_STATE(xfer1)) {
3244 * pre-clear the data toggle to DATA0 ("umass.c" and
3245 * "ata-usb.c" depends on this)
3248 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3250 /* setup a clear-stall packet */
3252 req.bmRequestType = UT_WRITE_ENDPOINT;
3253 req.bRequest = UR_CLEAR_FEATURE;
3254 USETW(req.wValue, UF_ENDPOINT_HALT);
3255 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3257 USETW(req.wLength, 0);
3260 * "usbd_transfer_setup_sub()" will ensure that
3261 * we have sufficient room in the buffer for
3262 * the request structure!
3265 /* copy in the transfer */
3267 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3270 xfer1->frlengths[0] = sizeof(req);
3273 usbd_transfer_submit(xfer1);
3276 case USB_ST_TRANSFERRED:
3279 default: /* Error */
3280 if (xfer1->error == USB_ERR_CANCELLED) {
3285 return (1); /* Clear Stall Finished */
3288 /*------------------------------------------------------------------------*
3289 * usbd_transfer_poll
3291 * The following function gets called from the USB keyboard driver and
3292 * UMASS when the system has paniced.
3294 * NOTE: It is currently not possible to resume normal operation on
3295 * the USB controller which has been polled, due to clearing of the
3296 * "up_dsleep" and "up_msleep" flags.
3297 *------------------------------------------------------------------------*/
3299 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3301 struct usb_xfer *xfer;
3302 struct usb_xfer_root *xroot;
3303 struct usb_device *udev;
3304 struct usb_proc_msg *pm;
3305 struct usb_bus *bus;
3307 uint16_t drop_bus_spin;
3311 for (n = 0; n != max; n++) {
3312 /* Extra checks to avoid panic */
3315 continue; /* no USB transfer */
3316 xroot = xfer->xroot;
3318 continue; /* no USB root */
3321 continue; /* no USB device */
3324 continue; /* no BUS structure */
3325 if (bus->methods == NULL)
3326 continue; /* no BUS methods */
3327 if (bus->methods->xfer_poll == NULL)
3328 continue; /* no poll method */
3334 if (USB_IN_POLLING_MODE_FUNC() == 0) {
3335 /* make sure that the BUS spin mutex is not locked */
3336 while (mtx_owned(&bus->bus_spin_lock)) {
3337 mtx_unlock_spin(&bus->bus_spin_lock);
3341 /* make sure that the BUS mutex is not locked */
3342 while (mtx_owned(&bus->bus_mtx)) {
3343 mtx_unlock(&bus->bus_mtx);
3347 /* make sure that the transfer mutex is not locked */
3348 while (mtx_owned(xroot->xfer_mtx)) {
3349 mtx_unlock(xroot->xfer_mtx);
3354 /* Make sure cv_signal() and cv_broadcast() is not called */
3355 USB_BUS_CONTROL_XFER_PROC(bus)->up_msleep = 0;
3356 USB_BUS_EXPLORE_PROC(bus)->up_msleep = 0;
3357 USB_BUS_GIANT_PROC(bus)->up_msleep = 0;
3358 USB_BUS_NON_GIANT_ISOC_PROC(bus)->up_msleep = 0;
3359 USB_BUS_NON_GIANT_BULK_PROC(bus)->up_msleep = 0;
3361 /* poll USB hardware */
3362 (bus->methods->xfer_poll) (bus);
3364 USB_BUS_LOCK(xroot->bus);
3366 /* check for clear stall */
3367 if (udev->ctrl_xfer[1] != NULL) {
3369 /* poll clear stall start */
3370 pm = &udev->cs_msg[0].hdr;
3371 (pm->pm_callback) (pm);
3372 /* poll clear stall done thread */
3373 pm = &udev->ctrl_xfer[1]->
3374 xroot->done_m[0].hdr;
3375 (pm->pm_callback) (pm);
3378 /* poll done thread */
3379 pm = &xroot->done_m[0].hdr;
3380 (pm->pm_callback) (pm);
3382 USB_BUS_UNLOCK(xroot->bus);
3384 /* restore transfer mutex */
3386 mtx_lock(xroot->xfer_mtx);
3388 /* restore BUS mutex */
3390 mtx_lock(&bus->bus_mtx);
3392 /* restore BUS spin mutex */
3393 while (drop_bus_spin--)
3394 mtx_lock_spin(&bus->bus_spin_lock);
3399 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3400 uint8_t type, enum usb_dev_speed speed)
3402 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3403 [USB_SPEED_LOW] = 8,
3404 [USB_SPEED_FULL] = 64,
3405 [USB_SPEED_HIGH] = 1024,
3406 [USB_SPEED_VARIABLE] = 1024,
3407 [USB_SPEED_SUPER] = 1024,
3410 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3411 [USB_SPEED_LOW] = 0, /* invalid */
3412 [USB_SPEED_FULL] = 1023,
3413 [USB_SPEED_HIGH] = 1024,
3414 [USB_SPEED_VARIABLE] = 3584,
3415 [USB_SPEED_SUPER] = 1024,
3418 static const uint16_t control_min[USB_SPEED_MAX] = {
3419 [USB_SPEED_LOW] = 8,
3420 [USB_SPEED_FULL] = 8,
3421 [USB_SPEED_HIGH] = 64,
3422 [USB_SPEED_VARIABLE] = 512,
3423 [USB_SPEED_SUPER] = 512,
3426 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3427 [USB_SPEED_LOW] = 8,
3428 [USB_SPEED_FULL] = 8,
3429 [USB_SPEED_HIGH] = 512,
3430 [USB_SPEED_VARIABLE] = 512,
3431 [USB_SPEED_SUPER] = 1024,
3436 memset(ptr, 0, sizeof(*ptr));
3440 ptr->range.max = intr_range_max[speed];
3442 case UE_ISOCHRONOUS:
3443 ptr->range.max = isoc_range_max[speed];
3446 if (type == UE_BULK)
3447 temp = bulk_min[speed];
3448 else /* UE_CONTROL */
3449 temp = control_min[speed];
3451 /* default is fixed */
3452 ptr->fixed[0] = temp;
3453 ptr->fixed[1] = temp;
3454 ptr->fixed[2] = temp;
3455 ptr->fixed[3] = temp;
3457 if (speed == USB_SPEED_FULL) {
3458 /* multiple sizes */
3463 if ((speed == USB_SPEED_VARIABLE) &&
3464 (type == UE_BULK)) {
3465 /* multiple sizes */
3466 ptr->fixed[2] = 1024;
3467 ptr->fixed[3] = 1536;
3474 usbd_xfer_softc(struct usb_xfer *xfer)
3476 return (xfer->priv_sc);
3480 usbd_xfer_get_priv(struct usb_xfer *xfer)
3482 return (xfer->priv_fifo);
3486 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3488 xfer->priv_fifo = ptr;
3492 usbd_xfer_state(struct usb_xfer *xfer)
3494 return (xfer->usb_state);
3498 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3501 case USB_FORCE_SHORT_XFER:
3502 xfer->flags.force_short_xfer = 1;
3504 case USB_SHORT_XFER_OK:
3505 xfer->flags.short_xfer_ok = 1;
3507 case USB_MULTI_SHORT_OK:
3508 xfer->flags.short_frames_ok = 1;
3510 case USB_MANUAL_STATUS:
3511 xfer->flags.manual_status = 1;
3517 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3520 case USB_FORCE_SHORT_XFER:
3521 xfer->flags.force_short_xfer = 0;
3523 case USB_SHORT_XFER_OK:
3524 xfer->flags.short_xfer_ok = 0;
3526 case USB_MULTI_SHORT_OK:
3527 xfer->flags.short_frames_ok = 0;
3529 case USB_MANUAL_STATUS:
3530 xfer->flags.manual_status = 0;
3536 * The following function returns in milliseconds when the isochronous
3537 * transfer was completed by the hardware. The returned value wraps
3538 * around 65536 milliseconds.
3541 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3543 return (xfer->isoc_time_complete);
3547 * The following function returns non-zero if the max packet size
3548 * field was clamped to a valid value. Else it returns zero.
3551 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3553 return (xfer->flags_int.maxp_was_clamped);