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 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
50 #define USB_DEBUG_VAR usb_debug
52 #include <dev/usb/usb_core.h>
53 #include <dev/usb/usb_busdma.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_transfer.h>
56 #include <dev/usb/usb_device.h>
57 #include <dev/usb/usb_debug.h>
58 #include <dev/usb/usb_util.h>
60 #include <dev/usb/usb_controller.h>
61 #include <dev/usb/usb_bus.h>
62 #include <dev/usb/usb_pf.h>
64 struct usb_std_packet_size {
66 uint16_t min; /* inclusive */
67 uint16_t max; /* inclusive */
73 static usb_callback_t usb_request_callback;
75 static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
77 /* This transfer is used for generic control endpoint transfers */
81 .endpoint = 0x00, /* Control endpoint */
82 .direction = UE_DIR_ANY,
83 .bufsize = USB_EP0_BUFSIZE, /* bytes */
84 .flags = {.proxy_buffer = 1,},
85 .callback = &usb_request_callback,
86 .usb_mode = USB_MODE_DUAL, /* both modes */
89 /* This transfer is used for generic clear stall only */
93 .endpoint = 0x00, /* Control pipe */
94 .direction = UE_DIR_ANY,
95 .bufsize = sizeof(struct usb_device_request),
96 .callback = &usb_do_clear_stall_callback,
97 .timeout = 1000, /* 1 second */
98 .interval = 50, /* 50ms */
99 .usb_mode = USB_MODE_HOST,
103 /* function prototypes */
105 static void usbd_update_max_frame_size(struct usb_xfer *);
106 static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
107 static void usbd_control_transfer_init(struct usb_xfer *);
108 static int usbd_setup_ctrl_transfer(struct usb_xfer *);
109 static void usb_callback_proc(struct usb_proc_msg *);
110 static void usbd_callback_ss_done_defer(struct usb_xfer *);
111 static void usbd_callback_wrapper(struct usb_xfer_queue *);
112 static void usbd_transfer_start_cb(void *);
113 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
114 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
115 uint8_t type, enum usb_dev_speed speed);
117 /*------------------------------------------------------------------------*
118 * usb_request_callback
119 *------------------------------------------------------------------------*/
121 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
123 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
124 usb_handle_request_callback(xfer, error);
126 usbd_do_request_callback(xfer, error);
129 /*------------------------------------------------------------------------*
130 * usbd_update_max_frame_size
132 * This function updates the maximum frame size, hence high speed USB
133 * can transfer multiple consecutive packets.
134 *------------------------------------------------------------------------*/
136 usbd_update_max_frame_size(struct usb_xfer *xfer)
138 /* compute maximum frame size */
139 /* this computation should not overflow 16-bit */
140 /* max = 15 * 1024 */
142 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
145 /*------------------------------------------------------------------------*
148 * The following function is called when we need to
149 * synchronize with DMA hardware.
152 * 0: no DMA delay required
153 * Else: milliseconds of DMA delay
154 *------------------------------------------------------------------------*/
156 usbd_get_dma_delay(struct usb_device *udev)
158 struct usb_bus_methods *mtod;
161 mtod = udev->bus->methods;
164 if (mtod->get_dma_delay) {
165 (mtod->get_dma_delay) (udev, &temp);
167 * Round up and convert to milliseconds. Note that we use
168 * 1024 milliseconds per second. to save a division.
176 /*------------------------------------------------------------------------*
177 * usbd_transfer_setup_sub_malloc
179 * This function will allocate one or more DMA'able memory chunks
180 * according to "size", "align" and "count" arguments. "ppc" is
181 * pointed to a linear array of USB page caches afterwards.
186 *------------------------------------------------------------------------*/
189 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
190 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
193 struct usb_page_cache *pc;
203 USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x\n",
205 USB_ASSERT(size > 0, ("Invalid size = 0\n"));
208 return (0); /* nothing to allocate */
211 * Make sure that the size is aligned properly.
213 size = -((-size) & (-align));
216 * Try multi-allocation chunks to reduce the number of DMA
217 * allocations, hence DMA allocations are slow.
219 if (size >= PAGE_SIZE) {
223 /* compute number of objects per page */
224 n_obj = (PAGE_SIZE / size);
226 * Compute number of DMA chunks, rounded up
229 n_dma_pc = ((count + n_obj - 1) / n_obj);
232 if (parm->buf == NULL) {
234 parm->dma_page_ptr += n_dma_pc;
235 parm->dma_page_cache_ptr += n_dma_pc;
236 parm->dma_page_ptr += count;
237 parm->xfer_page_cache_ptr += count;
240 for (x = 0; x != n_dma_pc; x++) {
241 /* need to initialize the page cache */
242 parm->dma_page_cache_ptr[x].tag_parent =
243 &parm->curr_xfer->xroot->dma_parent_tag;
245 for (x = 0; x != count; x++) {
246 /* need to initialize the page cache */
247 parm->xfer_page_cache_ptr[x].tag_parent =
248 &parm->curr_xfer->xroot->dma_parent_tag;
252 *ppc = parm->xfer_page_cache_ptr;
254 r = count; /* set remainder count */
255 z = n_obj * size; /* set allocation size */
256 pc = parm->xfer_page_cache_ptr;
257 pg = parm->dma_page_ptr;
259 for (x = 0; x != n_dma_pc; x++) {
262 /* compute last remainder */
266 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
268 return (1); /* failure */
270 /* Set beginning of current buffer */
271 buf = parm->dma_page_cache_ptr->buffer;
272 /* Make room for one DMA page cache and one page */
273 parm->dma_page_cache_ptr++;
276 for (y = 0; (y != n_obj); y++, r--, pc++, pg++) {
278 /* Load sub-chunk into DMA */
279 if (usb_pc_dmamap_create(pc, size)) {
280 return (1); /* failure */
282 pc->buffer = USB_ADD_BYTES(buf, y * size);
285 mtx_lock(pc->tag_parent->mtx);
286 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
287 mtx_unlock(pc->tag_parent->mtx);
288 return (1); /* failure */
290 mtx_unlock(pc->tag_parent->mtx);
294 parm->xfer_page_cache_ptr = pc;
295 parm->dma_page_ptr = pg;
300 /*------------------------------------------------------------------------*
301 * usbd_transfer_setup_sub - transfer setup subroutine
303 * This function must be called from the "xfer_setup" callback of the
304 * USB Host or Device controller driver when setting up an USB
305 * transfer. This function will setup correct packet sizes, buffer
306 * sizes, flags and more, that are stored in the "usb_xfer"
308 *------------------------------------------------------------------------*/
310 usbd_transfer_setup_sub(struct usb_setup_params *parm)
316 struct usb_xfer *xfer = parm->curr_xfer;
317 const struct usb_config *setup = parm->curr_setup;
318 struct usb_endpoint_ss_comp_descriptor *ecomp;
319 struct usb_endpoint_descriptor *edesc;
320 struct usb_std_packet_size std_size;
321 usb_frcount_t n_frlengths;
322 usb_frcount_t n_frbuffers;
328 * Sanity check. The following parameters must be initialized before
329 * calling this function.
331 if ((parm->hc_max_packet_size == 0) ||
332 (parm->hc_max_packet_count == 0) ||
333 (parm->hc_max_frame_size == 0)) {
334 parm->err = USB_ERR_INVAL;
337 edesc = xfer->endpoint->edesc;
338 ecomp = xfer->endpoint->ecomp;
340 type = (edesc->bmAttributes & UE_XFERTYPE);
342 xfer->flags = setup->flags;
343 xfer->nframes = setup->frames;
344 xfer->timeout = setup->timeout;
345 xfer->callback = setup->callback;
346 xfer->interval = setup->interval;
347 xfer->endpointno = edesc->bEndpointAddress;
348 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
349 xfer->max_packet_count = 1;
350 /* make a shadow copy: */
351 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
353 parm->bufsize = setup->bufsize;
355 switch (parm->speed) {
360 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
362 /* check for invalid max packet count */
363 if (xfer->max_packet_count > 3)
364 xfer->max_packet_count = 3;
369 xfer->max_packet_size &= 0x7FF;
371 case USB_SPEED_SUPER:
372 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
375 xfer->max_packet_count += ecomp->bMaxBurst;
377 if ((xfer->max_packet_count == 0) ||
378 (xfer->max_packet_count > 16))
379 xfer->max_packet_count = 16;
383 xfer->max_packet_count = 1;
389 mult = (ecomp->bmAttributes & 3) + 1;
393 xfer->max_packet_count *= mult;
399 xfer->max_packet_size &= 0x7FF;
404 /* range check "max_packet_count" */
406 if (xfer->max_packet_count > parm->hc_max_packet_count) {
407 xfer->max_packet_count = parm->hc_max_packet_count;
409 /* filter "wMaxPacketSize" according to HC capabilities */
411 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
412 (xfer->max_packet_size == 0)) {
413 xfer->max_packet_size = parm->hc_max_packet_size;
415 /* filter "wMaxPacketSize" according to standard sizes */
417 usbd_get_std_packet_size(&std_size, type, parm->speed);
419 if (std_size.range.min || std_size.range.max) {
421 if (xfer->max_packet_size < std_size.range.min) {
422 xfer->max_packet_size = std_size.range.min;
424 if (xfer->max_packet_size > std_size.range.max) {
425 xfer->max_packet_size = std_size.range.max;
429 if (xfer->max_packet_size >= std_size.fixed[3]) {
430 xfer->max_packet_size = std_size.fixed[3];
431 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
432 xfer->max_packet_size = std_size.fixed[2];
433 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
434 xfer->max_packet_size = std_size.fixed[1];
436 /* only one possibility left */
437 xfer->max_packet_size = std_size.fixed[0];
441 /* compute "max_frame_size" */
443 usbd_update_max_frame_size(xfer);
445 /* check interrupt interval and transfer pre-delay */
447 if (type == UE_ISOCHRONOUS) {
449 uint16_t frame_limit;
451 xfer->interval = 0; /* not used, must be zero */
452 xfer->flags_int.isochronous_xfr = 1; /* set flag */
454 if (xfer->timeout == 0) {
456 * set a default timeout in
457 * case something goes wrong!
459 xfer->timeout = 1000 / 4;
461 switch (parm->speed) {
464 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
468 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
469 xfer->fps_shift = edesc->bInterval;
470 if (xfer->fps_shift > 0)
472 if (xfer->fps_shift > 3)
474 if (xfer->flags.pre_scale_frames != 0)
475 xfer->nframes <<= (3 - xfer->fps_shift);
479 if (xfer->nframes > frame_limit) {
481 * this is not going to work
484 parm->err = USB_ERR_INVAL;
487 if (xfer->nframes == 0) {
489 * this is not a valid value
491 parm->err = USB_ERR_ZERO_NFRAMES;
497 * If a value is specified use that else check the
498 * endpoint descriptor!
500 if (type == UE_INTERRUPT) {
504 if (xfer->interval == 0) {
506 xfer->interval = edesc->bInterval;
508 switch (parm->speed) {
514 if (xfer->interval < 4)
516 else if (xfer->interval > 16)
517 xfer->interval = (1 << (16 - 4));
520 (1 << (xfer->interval - 4));
525 if (xfer->interval == 0) {
527 * One millisecond is the smallest
528 * interval we support:
536 while ((temp != 0) && (temp < xfer->interval)) {
541 switch (parm->speed) {
546 xfer->fps_shift += 3;
553 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
554 * to be equal to zero when setting up USB transfers, hence
555 * this leads to alot of extra code in the USB kernel.
558 if ((xfer->max_frame_size == 0) ||
559 (xfer->max_packet_size == 0)) {
563 if ((parm->bufsize <= MIN_PKT) &&
564 (type != UE_CONTROL) &&
568 xfer->max_packet_size = MIN_PKT;
569 xfer->max_packet_count = 1;
570 parm->bufsize = 0; /* automatic setup length */
571 usbd_update_max_frame_size(xfer);
574 parm->err = USB_ERR_ZERO_MAXP;
583 * check if we should setup a default
587 if (parm->bufsize == 0) {
589 parm->bufsize = xfer->max_frame_size;
591 if (type == UE_ISOCHRONOUS) {
592 parm->bufsize *= xfer->nframes;
596 * check if we are about to setup a proxy
600 if (xfer->flags.proxy_buffer) {
602 /* round bufsize up */
604 parm->bufsize += (xfer->max_frame_size - 1);
606 if (parm->bufsize < xfer->max_frame_size) {
607 /* length wrapped around */
608 parm->err = USB_ERR_INVAL;
611 /* subtract remainder */
613 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
615 /* add length of USB device request structure, if any */
617 if (type == UE_CONTROL) {
618 parm->bufsize += REQ_SIZE; /* SETUP message */
621 xfer->max_data_length = parm->bufsize;
623 /* Setup "n_frlengths" and "n_frbuffers" */
625 if (type == UE_ISOCHRONOUS) {
626 n_frlengths = xfer->nframes;
630 if (type == UE_CONTROL) {
631 xfer->flags_int.control_xfr = 1;
632 if (xfer->nframes == 0) {
633 if (parm->bufsize <= REQ_SIZE) {
635 * there will never be any data
644 if (xfer->nframes == 0) {
649 n_frlengths = xfer->nframes;
650 n_frbuffers = xfer->nframes;
654 * check if we have room for the
655 * USB device request structure:
658 if (type == UE_CONTROL) {
660 if (xfer->max_data_length < REQ_SIZE) {
661 /* length wrapped around or too small bufsize */
662 parm->err = USB_ERR_INVAL;
665 xfer->max_data_length -= REQ_SIZE;
668 * Setup "frlengths" and shadow "frlengths" for keeping the
669 * initial frame lengths when a USB transfer is complete. This
670 * information is useful when computing isochronous offsets.
672 xfer->frlengths = parm->xfer_length_ptr;
673 parm->xfer_length_ptr += 2 * n_frlengths;
675 /* setup "frbuffers" */
676 xfer->frbuffers = parm->xfer_page_cache_ptr;
677 parm->xfer_page_cache_ptr += n_frbuffers;
679 /* initialize max frame count */
680 xfer->max_frame_count = xfer->nframes;
683 * check if we need to setup
687 if (!xfer->flags.ext_buffer) {
690 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
695 USB_ADD_BYTES(parm->buf, parm->size[0]);
697 usbd_xfer_set_frame_offset(xfer, 0, 0);
699 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
700 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
703 parm->size[0] += parm->bufsize;
705 /* align data again */
706 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
709 * Compute maximum buffer size
712 if (parm->bufsize_max < parm->bufsize) {
713 parm->bufsize_max = parm->bufsize;
716 if (xfer->flags_int.bdma_enable) {
718 * Setup "dma_page_ptr".
720 * Proof for formula below:
722 * Assume there are three USB frames having length "a", "b" and
723 * "c". These USB frames will at maximum need "z"
724 * "usb_page" structures. "z" is given by:
726 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
727 * ((c / USB_PAGE_SIZE) + 2);
729 * Constraining "a", "b" and "c" like this:
731 * (a + b + c) <= parm->bufsize
735 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
737 * Here is the general formula:
739 xfer->dma_page_ptr = parm->dma_page_ptr;
740 parm->dma_page_ptr += (2 * n_frbuffers);
741 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
745 /* correct maximum data length */
746 xfer->max_data_length = 0;
748 /* subtract USB frame remainder from "hc_max_frame_size" */
750 xfer->max_hc_frame_size =
751 (parm->hc_max_frame_size -
752 (parm->hc_max_frame_size % xfer->max_frame_size));
754 if (xfer->max_hc_frame_size == 0) {
755 parm->err = USB_ERR_INVAL;
759 /* initialize frame buffers */
762 for (x = 0; x != n_frbuffers; x++) {
763 xfer->frbuffers[x].tag_parent =
764 &xfer->xroot->dma_parent_tag;
766 if (xfer->flags_int.bdma_enable &&
767 (parm->bufsize_max > 0)) {
769 if (usb_pc_dmamap_create(
771 parm->bufsize_max)) {
772 parm->err = USB_ERR_NOMEM;
782 * Set some dummy values so that we avoid division by zero:
784 xfer->max_hc_frame_size = 1;
785 xfer->max_frame_size = 1;
786 xfer->max_packet_size = 1;
787 xfer->max_data_length = 0;
789 xfer->max_frame_count = 0;
793 /*------------------------------------------------------------------------*
794 * usbd_transfer_setup - setup an array of USB transfers
796 * NOTE: You must always call "usbd_transfer_unsetup" after calling
797 * "usbd_transfer_setup" if success was returned.
799 * The idea is that the USB device driver should pre-allocate all its
800 * transfers by one call to this function.
805 *------------------------------------------------------------------------*/
807 usbd_transfer_setup(struct usb_device *udev,
808 const uint8_t *ifaces, struct usb_xfer **ppxfer,
809 const struct usb_config *setup_start, uint16_t n_setup,
810 void *priv_sc, struct mtx *xfer_mtx)
812 struct usb_xfer dummy;
813 struct usb_setup_params parm;
814 const struct usb_config *setup_end = setup_start + n_setup;
815 const struct usb_config *setup;
816 struct usb_endpoint *ep;
817 struct usb_xfer_root *info;
818 struct usb_xfer *xfer;
827 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
828 "usbd_transfer_setup can sleep!");
830 /* do some checking first */
833 DPRINTFN(6, "setup array has zero length!\n");
834 return (USB_ERR_INVAL);
837 DPRINTFN(6, "ifaces array is NULL!\n");
838 return (USB_ERR_INVAL);
840 if (xfer_mtx == NULL) {
841 DPRINTFN(6, "using global lock\n");
845 for (setup = setup_start, n = 0;
846 setup != setup_end; setup++, n++) {
847 if (setup->bufsize == (usb_frlength_t)-1) {
848 parm.err = USB_ERR_BAD_BUFSIZE;
849 DPRINTF("invalid bufsize\n");
851 if (setup->callback == NULL) {
852 parm.err = USB_ERR_NO_CALLBACK;
853 DPRINTF("no callback\n");
861 bzero(&parm, sizeof(parm));
864 parm.speed = usbd_get_speed(udev);
865 parm.hc_max_packet_count = 1;
867 if (parm.speed >= USB_SPEED_MAX) {
868 parm.err = USB_ERR_INVAL;
871 /* setup all transfers */
877 * Initialize the "usb_xfer_root" structure,
878 * which is common for all our USB transfers.
880 info = USB_ADD_BYTES(buf, 0);
882 info->memory_base = buf;
883 info->memory_size = parm.size[0];
886 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
887 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
889 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
890 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
892 cv_init(&info->cv_drain, "WDRAIN");
894 info->xfer_mtx = xfer_mtx;
896 usb_dma_tag_setup(&info->dma_parent_tag,
897 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
898 xfer_mtx, &usb_bdma_done_event, 32, parm.dma_tag_max);
901 info->bus = udev->bus;
904 TAILQ_INIT(&info->done_q.head);
905 info->done_q.command = &usbd_callback_wrapper;
907 TAILQ_INIT(&info->dma_q.head);
908 info->dma_q.command = &usb_bdma_work_loop;
910 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
911 info->done_m[0].xroot = info;
912 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
913 info->done_m[1].xroot = info;
916 * In device side mode control endpoint
917 * requests need to run from a separate
918 * context, else there is a chance of
921 if (setup_start == usb_control_ep_cfg)
923 &udev->bus->control_xfer_proc;
924 else if (xfer_mtx == &Giant)
926 &udev->bus->giant_callback_proc;
929 &udev->bus->non_giant_callback_proc;
935 parm.size[0] += sizeof(info[0]);
937 for (setup = setup_start, n = 0;
938 setup != setup_end; setup++, n++) {
940 /* skip USB transfers without callbacks: */
941 if (setup->callback == NULL) {
944 /* see if there is a matching endpoint */
945 ep = usbd_get_endpoint(udev,
946 ifaces[setup->if_index], setup);
948 if ((ep == NULL) || (ep->methods == NULL)) {
949 if (setup->flags.no_pipe_ok)
951 if ((setup->usb_mode != USB_MODE_DUAL) &&
952 (setup->usb_mode != udev->flags.usb_mode))
954 parm.err = USB_ERR_NO_PIPE;
958 /* align data properly */
959 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
961 /* store current setup pointer */
962 parm.curr_setup = setup;
966 * Common initialization of the
967 * "usb_xfer" structure.
969 xfer = USB_ADD_BYTES(buf, parm.size[0]);
970 xfer->address = udev->address;
971 xfer->priv_sc = priv_sc;
974 usb_callout_init_mtx(&xfer->timeout_handle,
975 &udev->bus->bus_mtx, 0);
978 * Setup a dummy xfer, hence we are
979 * writing to the "usb_xfer"
980 * structure pointed to by "xfer"
981 * before we have allocated any
985 bzero(&dummy, sizeof(dummy));
989 /* set transfer endpoint pointer */
992 parm.size[0] += sizeof(xfer[0]);
993 parm.methods = xfer->endpoint->methods;
994 parm.curr_xfer = xfer;
997 * Call the Host or Device controller transfer
1000 (udev->bus->methods->xfer_setup) (&parm);
1002 /* check for error */
1008 * Increment the endpoint refcount. This
1009 * basically prevents setting a new
1010 * configuration and alternate setting
1011 * when USB transfers are in use on
1012 * the given interface. Search the USB
1013 * code for "endpoint->refcount_alloc" if you
1014 * want more information.
1016 USB_BUS_LOCK(info->bus);
1017 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1018 parm.err = USB_ERR_INVAL;
1020 xfer->endpoint->refcount_alloc++;
1022 if (xfer->endpoint->refcount_alloc == 0)
1023 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1024 USB_BUS_UNLOCK(info->bus);
1027 * Whenever we set ppxfer[] then we
1028 * also need to increment the
1031 info->setup_refcount++;
1034 * Transfer is successfully setup and
1040 /* check for error */
1045 if (buf || parm.err) {
1048 if (refcount == 0) {
1049 /* no transfers - nothing to do ! */
1052 /* align data properly */
1053 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1055 /* store offset temporarily */
1056 parm.size[1] = parm.size[0];
1059 * The number of DMA tags required depends on
1060 * the number of endpoints. The current estimate
1061 * for maximum number of DMA tags per endpoint
1064 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX);
1067 * DMA tags for QH, TD, Data and more.
1069 parm.dma_tag_max += 8;
1071 parm.dma_tag_p += parm.dma_tag_max;
1073 parm.size[0] += ((uint8_t *)parm.dma_tag_p) -
1076 /* align data properly */
1077 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1079 /* store offset temporarily */
1080 parm.size[3] = parm.size[0];
1082 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) -
1085 /* align data properly */
1086 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1088 /* store offset temporarily */
1089 parm.size[4] = parm.size[0];
1091 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) -
1094 /* store end offset temporarily */
1095 parm.size[5] = parm.size[0];
1097 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) -
1100 /* store end offset temporarily */
1102 parm.size[2] = parm.size[0];
1104 /* align data properly */
1105 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1107 parm.size[6] = parm.size[0];
1109 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) -
1112 /* align data properly */
1113 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1115 /* allocate zeroed memory */
1116 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO);
1119 parm.err = USB_ERR_NOMEM;
1120 DPRINTFN(0, "cannot allocate memory block for "
1121 "configuration (%d bytes)\n",
1125 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]);
1126 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]);
1127 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]);
1128 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]);
1129 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1134 if (info->setup_refcount == 0) {
1136 * "usbd_transfer_unsetup_sub" will unlock
1137 * the bus mutex before returning !
1139 USB_BUS_LOCK(info->bus);
1141 /* something went wrong */
1142 usbd_transfer_unsetup_sub(info, 0);
1146 usbd_transfer_unsetup(ppxfer, n_setup);
1151 /*------------------------------------------------------------------------*
1152 * usbd_transfer_unsetup_sub - factored out code
1153 *------------------------------------------------------------------------*/
1155 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1158 struct usb_page_cache *pc;
1161 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1163 /* wait for any outstanding DMA operations */
1167 temp = usbd_get_dma_delay(info->udev);
1169 usb_pause_mtx(&info->bus->bus_mtx,
1170 USB_MS_TO_TICKS(temp));
1174 /* make sure that our done messages are not queued anywhere */
1175 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1177 USB_BUS_UNLOCK(info->bus);
1180 /* free DMA'able memory, if any */
1181 pc = info->dma_page_cache_start;
1182 while (pc != info->dma_page_cache_end) {
1183 usb_pc_free_mem(pc);
1187 /* free DMA maps in all "xfer->frbuffers" */
1188 pc = info->xfer_page_cache_start;
1189 while (pc != info->xfer_page_cache_end) {
1190 usb_pc_dmamap_destroy(pc);
1194 /* free all DMA tags */
1195 usb_dma_tag_unsetup(&info->dma_parent_tag);
1198 cv_destroy(&info->cv_drain);
1201 * free the "memory_base" last, hence the "info" structure is
1202 * contained within the "memory_base"!
1204 free(info->memory_base, M_USB);
1207 /*------------------------------------------------------------------------*
1208 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1210 * NOTE: All USB transfers in progress will get called back passing
1211 * the error code "USB_ERR_CANCELLED" before this function
1213 *------------------------------------------------------------------------*/
1215 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1217 struct usb_xfer *xfer;
1218 struct usb_xfer_root *info;
1219 uint8_t needs_delay = 0;
1221 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1222 "usbd_transfer_unsetup can sleep!");
1225 xfer = pxfer[n_setup];
1232 USB_XFER_LOCK(xfer);
1233 USB_BUS_LOCK(info->bus);
1236 * HINT: when you start/stop a transfer, it might be a
1237 * good idea to directly use the "pxfer[]" structure:
1239 * usbd_transfer_start(sc->pxfer[0]);
1240 * usbd_transfer_stop(sc->pxfer[0]);
1242 * That way, if your code has many parts that will not
1243 * stop running under the same lock, in other words
1244 * "xfer_mtx", the usbd_transfer_start and
1245 * usbd_transfer_stop functions will simply return
1246 * when they detect a NULL pointer argument.
1248 * To avoid any races we clear the "pxfer[]" pointer
1249 * while holding the private mutex of the driver:
1251 pxfer[n_setup] = NULL;
1253 USB_BUS_UNLOCK(info->bus);
1254 USB_XFER_UNLOCK(xfer);
1256 usbd_transfer_drain(xfer);
1259 if (xfer->flags_int.bdma_enable)
1263 * NOTE: default endpoint does not have an
1264 * interface, even if endpoint->iface_index == 0
1266 USB_BUS_LOCK(info->bus);
1267 xfer->endpoint->refcount_alloc--;
1268 USB_BUS_UNLOCK(info->bus);
1270 usb_callout_drain(&xfer->timeout_handle);
1272 USB_BUS_LOCK(info->bus);
1274 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1275 "reference count\n"));
1277 info->setup_refcount--;
1279 if (info->setup_refcount == 0) {
1280 usbd_transfer_unsetup_sub(info,
1283 USB_BUS_UNLOCK(info->bus);
1288 /*------------------------------------------------------------------------*
1289 * usbd_control_transfer_init - factored out code
1291 * In USB Device Mode we have to wait for the SETUP packet which
1292 * containst the "struct usb_device_request" structure, before we can
1293 * transfer any data. In USB Host Mode we already have the SETUP
1294 * packet at the moment the USB transfer is started. This leads us to
1295 * having to setup the USB transfer at two different places in
1296 * time. This function just contains factored out control transfer
1297 * initialisation code, so that we don't duplicate the code.
1298 *------------------------------------------------------------------------*/
1300 usbd_control_transfer_init(struct usb_xfer *xfer)
1302 struct usb_device_request req;
1304 /* copy out the USB request header */
1306 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1308 /* setup remainder */
1310 xfer->flags_int.control_rem = UGETW(req.wLength);
1312 /* copy direction to endpoint variable */
1314 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1316 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1319 /*------------------------------------------------------------------------*
1320 * usbd_setup_ctrl_transfer
1322 * This function handles initialisation of control transfers. Control
1323 * transfers are special in that regard that they can both transmit
1329 *------------------------------------------------------------------------*/
1331 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1335 /* Check for control endpoint stall */
1336 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1337 /* the control transfer is no longer active */
1338 xfer->flags_int.control_stall = 1;
1339 xfer->flags_int.control_act = 0;
1341 /* don't stall control transfer by default */
1342 xfer->flags_int.control_stall = 0;
1345 /* Check for invalid number of frames */
1346 if (xfer->nframes > 2) {
1348 * If you need to split a control transfer, you
1349 * have to do one part at a time. Only with
1350 * non-control transfers you can do multiple
1353 DPRINTFN(0, "Too many frames: %u\n",
1354 (unsigned int)xfer->nframes);
1359 * Check if there is a control
1360 * transfer in progress:
1362 if (xfer->flags_int.control_act) {
1364 if (xfer->flags_int.control_hdr) {
1366 /* clear send header flag */
1368 xfer->flags_int.control_hdr = 0;
1370 /* setup control transfer */
1371 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1372 usbd_control_transfer_init(xfer);
1375 /* get data length */
1381 /* the size of the SETUP structure is hardcoded ! */
1383 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1384 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1385 xfer->frlengths[0], sizeof(struct
1386 usb_device_request));
1389 /* check USB mode */
1390 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1392 /* check number of frames */
1393 if (xfer->nframes != 1) {
1395 * We need to receive the setup
1396 * message first so that we know the
1399 DPRINTF("Misconfigured transfer\n");
1403 * Set a dummy "control_rem" value. This
1404 * variable will be overwritten later by a
1405 * call to "usbd_control_transfer_init()" !
1407 xfer->flags_int.control_rem = 0xFFFF;
1410 /* setup "endpoint" and "control_rem" */
1412 usbd_control_transfer_init(xfer);
1415 /* set transfer-header flag */
1417 xfer->flags_int.control_hdr = 1;
1419 /* get data length */
1421 len = (xfer->sumlen - sizeof(struct usb_device_request));
1424 /* check if there is a length mismatch */
1426 if (len > xfer->flags_int.control_rem) {
1427 DPRINTFN(0, "Length (%d) greater than "
1428 "remaining length (%d)\n", len,
1429 xfer->flags_int.control_rem);
1432 /* check if we are doing a short transfer */
1434 if (xfer->flags.force_short_xfer) {
1435 xfer->flags_int.control_rem = 0;
1437 if ((len != xfer->max_data_length) &&
1438 (len != xfer->flags_int.control_rem) &&
1439 (xfer->nframes != 1)) {
1440 DPRINTFN(0, "Short control transfer without "
1441 "force_short_xfer set\n");
1444 xfer->flags_int.control_rem -= len;
1447 /* the status part is executed when "control_act" is 0 */
1449 if ((xfer->flags_int.control_rem > 0) ||
1450 (xfer->flags.manual_status)) {
1451 /* don't execute the STATUS stage yet */
1452 xfer->flags_int.control_act = 1;
1455 if ((!xfer->flags_int.control_hdr) &&
1456 (xfer->nframes == 1)) {
1458 * This is not a valid operation!
1460 DPRINTFN(0, "Invalid parameter "
1465 /* time to execute the STATUS stage */
1466 xfer->flags_int.control_act = 0;
1468 return (0); /* success */
1471 return (1); /* failure */
1474 /*------------------------------------------------------------------------*
1475 * usbd_transfer_submit - start USB hardware for the given transfer
1477 * This function should only be called from the USB callback.
1478 *------------------------------------------------------------------------*/
1480 usbd_transfer_submit(struct usb_xfer *xfer)
1482 struct usb_xfer_root *info;
1483 struct usb_bus *bus;
1489 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1490 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1494 if (USB_DEBUG_VAR > 0) {
1497 usb_dump_endpoint(xfer->endpoint);
1499 USB_BUS_UNLOCK(bus);
1503 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1504 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1506 /* Only open the USB transfer once! */
1507 if (!xfer->flags_int.open) {
1508 xfer->flags_int.open = 1;
1513 (xfer->endpoint->methods->open) (xfer);
1514 USB_BUS_UNLOCK(bus);
1516 /* set "transferring" flag */
1517 xfer->flags_int.transferring = 1;
1520 /* increment power reference */
1521 usbd_transfer_power_ref(xfer, 1);
1524 * Check if the transfer is waiting on a queue, most
1525 * frequently the "done_q":
1527 if (xfer->wait_queue) {
1529 usbd_transfer_dequeue(xfer);
1530 USB_BUS_UNLOCK(bus);
1532 /* clear "did_dma_delay" flag */
1533 xfer->flags_int.did_dma_delay = 0;
1535 /* clear "did_close" flag */
1536 xfer->flags_int.did_close = 0;
1539 /* clear "bdma_setup" flag */
1540 xfer->flags_int.bdma_setup = 0;
1542 /* by default we cannot cancel any USB transfer immediately */
1543 xfer->flags_int.can_cancel_immed = 0;
1545 /* clear lengths and frame counts by default */
1550 /* clear any previous errors */
1553 /* Check if the device is still alive */
1554 if (info->udev->state < USB_STATE_POWERED) {
1557 * Must return cancelled error code else
1558 * device drivers can hang.
1560 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1561 USB_BUS_UNLOCK(bus);
1566 if (xfer->nframes == 0) {
1567 if (xfer->flags.stall_pipe) {
1569 * Special case - want to stall without transferring
1572 DPRINTF("xfer=%p nframes=0: stall "
1573 "or clear stall!\n", xfer);
1575 xfer->flags_int.can_cancel_immed = 1;
1576 /* start the transfer */
1577 usb_command_wrapper(&xfer->endpoint->endpoint_q, xfer);
1578 USB_BUS_UNLOCK(bus);
1582 usbd_transfer_done(xfer, USB_ERR_INVAL);
1583 USB_BUS_UNLOCK(bus);
1586 /* compute some variables */
1588 for (x = 0; x != xfer->nframes; x++) {
1589 /* make a copy of the frlenghts[] */
1590 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1591 /* compute total transfer length */
1592 xfer->sumlen += xfer->frlengths[x];
1593 if (xfer->sumlen < xfer->frlengths[x]) {
1594 /* length wrapped around */
1596 usbd_transfer_done(xfer, USB_ERR_INVAL);
1597 USB_BUS_UNLOCK(bus);
1602 /* clear some internal flags */
1604 xfer->flags_int.short_xfer_ok = 0;
1605 xfer->flags_int.short_frames_ok = 0;
1607 /* check if this is a control transfer */
1609 if (xfer->flags_int.control_xfr) {
1611 if (usbd_setup_ctrl_transfer(xfer)) {
1613 usbd_transfer_done(xfer, USB_ERR_STALLED);
1614 USB_BUS_UNLOCK(bus);
1619 * Setup filtered version of some transfer flags,
1620 * in case of data read direction
1622 if (USB_GET_DATA_ISREAD(xfer)) {
1624 if (xfer->flags.short_frames_ok) {
1625 xfer->flags_int.short_xfer_ok = 1;
1626 xfer->flags_int.short_frames_ok = 1;
1627 } else if (xfer->flags.short_xfer_ok) {
1628 xfer->flags_int.short_xfer_ok = 1;
1630 /* check for control transfer */
1631 if (xfer->flags_int.control_xfr) {
1633 * 1) Control transfers do not support
1634 * reception of multiple short USB
1635 * frames in host mode and device side
1636 * mode, with exception of:
1638 * 2) Due to sometimes buggy device
1639 * side firmware we need to do a
1640 * STATUS stage in case of short
1641 * control transfers in USB host mode.
1642 * The STATUS stage then becomes the
1643 * "alt_next" to the DATA stage.
1645 xfer->flags_int.short_frames_ok = 1;
1650 * Check if BUS-DMA support is enabled and try to load virtual
1651 * buffers into DMA, if any:
1654 if (xfer->flags_int.bdma_enable) {
1655 /* insert the USB transfer last in the BUS-DMA queue */
1656 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1661 * Enter the USB transfer into the Host Controller or
1662 * Device Controller schedule:
1664 usbd_pipe_enter(xfer);
1667 /*------------------------------------------------------------------------*
1668 * usbd_pipe_enter - factored out code
1669 *------------------------------------------------------------------------*/
1671 usbd_pipe_enter(struct usb_xfer *xfer)
1673 struct usb_endpoint *ep;
1675 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1677 USB_BUS_LOCK(xfer->xroot->bus);
1679 ep = xfer->endpoint;
1683 /* enter the transfer */
1684 (ep->methods->enter) (xfer);
1686 xfer->flags_int.can_cancel_immed = 1;
1688 /* check for transfer error */
1690 /* some error has happened */
1691 usbd_transfer_done(xfer, 0);
1692 USB_BUS_UNLOCK(xfer->xroot->bus);
1696 /* start the transfer */
1697 usb_command_wrapper(&ep->endpoint_q, xfer);
1698 USB_BUS_UNLOCK(xfer->xroot->bus);
1701 /*------------------------------------------------------------------------*
1702 * usbd_transfer_start - start an USB transfer
1704 * NOTE: Calling this function more than one time will only
1705 * result in a single transfer start, until the USB transfer
1707 *------------------------------------------------------------------------*/
1709 usbd_transfer_start(struct usb_xfer *xfer)
1712 /* transfer is gone */
1715 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1717 /* mark the USB transfer started */
1719 if (!xfer->flags_int.started) {
1720 /* lock the BUS lock to avoid races updating flags_int */
1721 USB_BUS_LOCK(xfer->xroot->bus);
1722 xfer->flags_int.started = 1;
1723 USB_BUS_UNLOCK(xfer->xroot->bus);
1725 /* check if the USB transfer callback is already transferring */
1727 if (xfer->flags_int.transferring) {
1730 USB_BUS_LOCK(xfer->xroot->bus);
1731 /* call the USB transfer callback */
1732 usbd_callback_ss_done_defer(xfer);
1733 USB_BUS_UNLOCK(xfer->xroot->bus);
1736 /*------------------------------------------------------------------------*
1737 * usbd_transfer_stop - stop an USB transfer
1739 * NOTE: Calling this function more than one time will only
1740 * result in a single transfer stop.
1741 * NOTE: When this function returns it is not safe to free nor
1742 * reuse any DMA buffers. See "usbd_transfer_drain()".
1743 *------------------------------------------------------------------------*/
1745 usbd_transfer_stop(struct usb_xfer *xfer)
1747 struct usb_endpoint *ep;
1750 /* transfer is gone */
1753 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1755 /* check if the USB transfer was ever opened */
1757 if (!xfer->flags_int.open) {
1758 if (xfer->flags_int.started) {
1759 /* nothing to do except clearing the "started" flag */
1760 /* lock the BUS lock to avoid races updating flags_int */
1761 USB_BUS_LOCK(xfer->xroot->bus);
1762 xfer->flags_int.started = 0;
1763 USB_BUS_UNLOCK(xfer->xroot->bus);
1767 /* try to stop the current USB transfer */
1769 USB_BUS_LOCK(xfer->xroot->bus);
1770 /* override any previous error */
1771 xfer->error = USB_ERR_CANCELLED;
1774 * Clear "open" and "started" when both private and USB lock
1775 * is locked so that we don't get a race updating "flags_int"
1777 xfer->flags_int.open = 0;
1778 xfer->flags_int.started = 0;
1781 * Check if we can cancel the USB transfer immediately.
1783 if (xfer->flags_int.transferring) {
1784 if (xfer->flags_int.can_cancel_immed &&
1785 (!xfer->flags_int.did_close)) {
1788 * The following will lead to an USB_ERR_CANCELLED
1789 * error code being passed to the USB callback.
1791 (xfer->endpoint->methods->close) (xfer);
1792 /* only close once */
1793 xfer->flags_int.did_close = 1;
1795 /* need to wait for the next done callback */
1800 /* close here and now */
1801 (xfer->endpoint->methods->close) (xfer);
1804 * Any additional DMA delay is done by
1805 * "usbd_transfer_unsetup()".
1809 * Special case. Check if we need to restart a blocked
1812 ep = xfer->endpoint;
1815 * If the current USB transfer is completing we need
1816 * to start the next one:
1818 if (ep->endpoint_q.curr == xfer) {
1819 usb_command_wrapper(&ep->endpoint_q, NULL);
1823 USB_BUS_UNLOCK(xfer->xroot->bus);
1826 /*------------------------------------------------------------------------*
1827 * usbd_transfer_pending
1829 * This function will check if an USB transfer is pending which is a
1830 * little bit complicated!
1833 * 1: Pending: The USB transfer will receive a callback in the future.
1834 *------------------------------------------------------------------------*/
1836 usbd_transfer_pending(struct usb_xfer *xfer)
1838 struct usb_xfer_root *info;
1839 struct usb_xfer_queue *pq;
1842 /* transfer is gone */
1845 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1847 if (xfer->flags_int.transferring) {
1851 USB_BUS_LOCK(xfer->xroot->bus);
1852 if (xfer->wait_queue) {
1853 /* we are waiting on a queue somewhere */
1854 USB_BUS_UNLOCK(xfer->xroot->bus);
1860 if (pq->curr == xfer) {
1861 /* we are currently scheduled for callback */
1862 USB_BUS_UNLOCK(xfer->xroot->bus);
1865 /* we are not pending */
1866 USB_BUS_UNLOCK(xfer->xroot->bus);
1870 /*------------------------------------------------------------------------*
1871 * usbd_transfer_drain
1873 * This function will stop the USB transfer and wait for any
1874 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
1875 * are loaded into DMA can safely be freed or reused after that this
1876 * function has returned.
1877 *------------------------------------------------------------------------*/
1879 usbd_transfer_drain(struct usb_xfer *xfer)
1881 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1882 "usbd_transfer_drain can sleep!");
1885 /* transfer is gone */
1888 if (xfer->xroot->xfer_mtx != &Giant) {
1889 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1891 USB_XFER_LOCK(xfer);
1893 usbd_transfer_stop(xfer);
1895 while (usbd_transfer_pending(xfer) ||
1896 xfer->flags_int.doing_callback) {
1899 * It is allowed that the callback can drop its
1900 * transfer mutex. In that case checking only
1901 * "usbd_transfer_pending()" is not enough to tell if
1902 * the USB transfer is fully drained. We also need to
1903 * check the internal "doing_callback" flag.
1905 xfer->flags_int.draining = 1;
1908 * Wait until the current outstanding USB
1909 * transfer is complete !
1911 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
1913 USB_XFER_UNLOCK(xfer);
1916 struct usb_page_cache *
1917 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
1919 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1921 return (&xfer->frbuffers[frindex]);
1924 /*------------------------------------------------------------------------*
1925 * usbd_xfer_get_fps_shift
1927 * The following function is only useful for isochronous transfers. It
1928 * returns how many times the frame execution rate has been shifted
1934 *------------------------------------------------------------------------*/
1936 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
1938 return (xfer->fps_shift);
1942 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
1944 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1946 return (xfer->frlengths[frindex]);
1949 /*------------------------------------------------------------------------*
1950 * usbd_xfer_set_frame_data
1952 * This function sets the pointer of the buffer that should
1953 * loaded directly into DMA for the given USB frame. Passing "ptr"
1954 * equal to NULL while the corresponding "frlength" is greater
1955 * than zero gives undefined results!
1956 *------------------------------------------------------------------------*/
1958 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
1959 void *ptr, usb_frlength_t len)
1961 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1963 /* set virtual address to load and length */
1964 xfer->frbuffers[frindex].buffer = ptr;
1965 usbd_xfer_set_frame_len(xfer, frindex, len);
1969 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
1970 void **ptr, int *len)
1972 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1975 *ptr = xfer->frbuffers[frindex].buffer;
1977 *len = xfer->frlengths[frindex];
1980 /*------------------------------------------------------------------------*
1981 * usbd_xfer_old_frame_length
1983 * This function returns the framelength of the given frame at the
1984 * time the transfer was submitted. This function can be used to
1985 * compute the starting data pointer of the next isochronous frame
1986 * when an isochronous transfer has completed.
1987 *------------------------------------------------------------------------*/
1989 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
1991 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1993 return (xfer->frlengths[frindex + xfer->max_frame_count]);
1997 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2001 *actlen = xfer->actlen;
2003 *sumlen = xfer->sumlen;
2004 if (aframes != NULL)
2005 *aframes = xfer->aframes;
2006 if (nframes != NULL)
2007 *nframes = xfer->nframes;
2010 /*------------------------------------------------------------------------*
2011 * usbd_xfer_set_frame_offset
2013 * This function sets the frame data buffer offset relative to the beginning
2014 * of the USB DMA buffer allocated for this USB transfer.
2015 *------------------------------------------------------------------------*/
2017 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2018 usb_frcount_t frindex)
2020 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2021 "when the USB buffer is external\n"));
2022 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2024 /* set virtual address to load */
2025 xfer->frbuffers[frindex].buffer =
2026 USB_ADD_BYTES(xfer->local_buffer, offset);
2030 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2036 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2042 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2048 usbd_xfer_max_frames(struct usb_xfer *xfer)
2050 return (xfer->max_frame_count);
2054 usbd_xfer_max_len(struct usb_xfer *xfer)
2056 return (xfer->max_data_length);
2060 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2062 return (xfer->max_frame_size);
2066 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2069 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2071 xfer->frlengths[frindex] = len;
2074 /*------------------------------------------------------------------------*
2075 * usb_callback_proc - factored out code
2077 * This function performs USB callbacks.
2078 *------------------------------------------------------------------------*/
2080 usb_callback_proc(struct usb_proc_msg *_pm)
2082 struct usb_done_msg *pm = (void *)_pm;
2083 struct usb_xfer_root *info = pm->xroot;
2085 /* Change locking order */
2086 USB_BUS_UNLOCK(info->bus);
2089 * We exploit the fact that the mutex is the same for all
2090 * callbacks that will be called from this thread:
2092 mtx_lock(info->xfer_mtx);
2093 USB_BUS_LOCK(info->bus);
2095 /* Continue where we lost track */
2096 usb_command_wrapper(&info->done_q,
2099 mtx_unlock(info->xfer_mtx);
2102 /*------------------------------------------------------------------------*
2103 * usbd_callback_ss_done_defer
2105 * This function will defer the start, stop and done callback to the
2107 *------------------------------------------------------------------------*/
2109 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2111 struct usb_xfer_root *info = xfer->xroot;
2112 struct usb_xfer_queue *pq = &info->done_q;
2114 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2116 if (pq->curr != xfer) {
2117 usbd_transfer_enqueue(pq, xfer);
2119 if (!pq->recurse_1) {
2122 * We have to postpone the callback due to the fact we
2123 * will have a Lock Order Reversal, LOR, if we try to
2126 if (usb_proc_msignal(info->done_p,
2127 &info->done_m[0], &info->done_m[1])) {
2131 /* clear second recurse flag */
2138 /*------------------------------------------------------------------------*
2139 * usbd_callback_wrapper
2141 * This is a wrapper for USB callbacks. This wrapper does some
2142 * auto-magic things like figuring out if we can call the callback
2143 * directly from the current context or if we need to wakeup the
2144 * interrupt process.
2145 *------------------------------------------------------------------------*/
2147 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2149 struct usb_xfer *xfer = pq->curr;
2150 struct usb_xfer_root *info = xfer->xroot;
2152 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2153 if (!mtx_owned(info->xfer_mtx)) {
2155 * Cases that end up here:
2157 * 5) HW interrupt done callback or other source.
2159 DPRINTFN(3, "case 5\n");
2162 * We have to postpone the callback due to the fact we
2163 * will have a Lock Order Reversal, LOR, if we try to
2166 if (usb_proc_msignal(info->done_p,
2167 &info->done_m[0], &info->done_m[1])) {
2173 * Cases that end up here:
2175 * 1) We are starting a transfer
2176 * 2) We are prematurely calling back a transfer
2177 * 3) We are stopping a transfer
2178 * 4) We are doing an ordinary callback
2180 DPRINTFN(3, "case 1-4\n");
2181 /* get next USB transfer in the queue */
2182 info->done_q.curr = NULL;
2184 /* set flag in case of drain */
2185 xfer->flags_int.doing_callback = 1;
2187 USB_BUS_UNLOCK(info->bus);
2188 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2190 /* set correct USB state for callback */
2191 if (!xfer->flags_int.transferring) {
2192 xfer->usb_state = USB_ST_SETUP;
2193 if (!xfer->flags_int.started) {
2194 /* we got stopped before we even got started */
2195 USB_BUS_LOCK(info->bus);
2200 if (usbd_callback_wrapper_sub(xfer)) {
2201 /* the callback has been deferred */
2202 USB_BUS_LOCK(info->bus);
2206 /* decrement power reference */
2207 usbd_transfer_power_ref(xfer, -1);
2209 xfer->flags_int.transferring = 0;
2212 xfer->usb_state = USB_ST_ERROR;
2214 /* set transferred state */
2215 xfer->usb_state = USB_ST_TRANSFERRED;
2217 /* sync DMA memory, if any */
2218 if (xfer->flags_int.bdma_enable &&
2219 (!xfer->flags_int.bdma_no_post_sync)) {
2220 usb_bdma_post_sync(xfer);
2227 if (xfer->usb_state != USB_ST_SETUP)
2228 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2230 /* call processing routine */
2231 (xfer->callback) (xfer, xfer->error);
2233 /* pickup the USB mutex again */
2234 USB_BUS_LOCK(info->bus);
2237 * Check if we got started after that we got cancelled, but
2238 * before we managed to do the callback.
2240 if ((!xfer->flags_int.open) &&
2241 (xfer->flags_int.started) &&
2242 (xfer->usb_state == USB_ST_ERROR)) {
2243 /* clear flag in case of drain */
2244 xfer->flags_int.doing_callback = 0;
2245 /* try to loop, but not recursivly */
2246 usb_command_wrapper(&info->done_q, xfer);
2251 /* clear flag in case of drain */
2252 xfer->flags_int.doing_callback = 0;
2255 * Check if we are draining.
2257 if (xfer->flags_int.draining &&
2258 (!xfer->flags_int.transferring)) {
2259 /* "usbd_transfer_drain()" is waiting for end of transfer */
2260 xfer->flags_int.draining = 0;
2261 cv_broadcast(&info->cv_drain);
2264 /* do the next callback, if any */
2265 usb_command_wrapper(&info->done_q,
2269 /*------------------------------------------------------------------------*
2270 * usb_dma_delay_done_cb
2272 * This function is called when the DMA delay has been exectuded, and
2273 * will make sure that the callback is called to complete the USB
2274 * transfer. This code path is ususally only used when there is an USB
2275 * error like USB_ERR_CANCELLED.
2276 *------------------------------------------------------------------------*/
2278 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2280 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2282 DPRINTFN(3, "Completed %p\n", xfer);
2284 /* queue callback for execution, again */
2285 usbd_transfer_done(xfer, 0);
2288 /*------------------------------------------------------------------------*
2289 * usbd_transfer_dequeue
2291 * - This function is used to remove an USB transfer from a USB
2294 * - This function can be called multiple times in a row.
2295 *------------------------------------------------------------------------*/
2297 usbd_transfer_dequeue(struct usb_xfer *xfer)
2299 struct usb_xfer_queue *pq;
2301 pq = xfer->wait_queue;
2303 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2304 xfer->wait_queue = NULL;
2308 /*------------------------------------------------------------------------*
2309 * usbd_transfer_enqueue
2311 * - This function is used to insert an USB transfer into a USB *
2314 * - This function can be called multiple times in a row.
2315 *------------------------------------------------------------------------*/
2317 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2320 * Insert the USB transfer into the queue, if it is not
2321 * already on a USB transfer queue:
2323 if (xfer->wait_queue == NULL) {
2324 xfer->wait_queue = pq;
2325 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2329 /*------------------------------------------------------------------------*
2330 * usbd_transfer_done
2332 * - This function is used to remove an USB transfer from the busdma,
2333 * pipe or interrupt queue.
2335 * - This function is used to queue the USB transfer on the done
2338 * - This function is used to stop any USB transfer timeouts.
2339 *------------------------------------------------------------------------*/
2341 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2343 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2345 DPRINTF("err=%s\n", usbd_errstr(error));
2348 * If we are not transferring then just return.
2349 * This can happen during transfer cancel.
2351 if (!xfer->flags_int.transferring) {
2352 DPRINTF("not transferring\n");
2353 /* end of control transfer, if any */
2354 xfer->flags_int.control_act = 0;
2357 /* only set transfer error if not already set */
2359 xfer->error = error;
2361 /* stop any callouts */
2362 usb_callout_stop(&xfer->timeout_handle);
2365 * If we are waiting on a queue, just remove the USB transfer
2366 * from the queue, if any. We should have the required locks
2367 * locked to do the remove when this function is called.
2369 usbd_transfer_dequeue(xfer);
2372 if (mtx_owned(xfer->xroot->xfer_mtx)) {
2373 struct usb_xfer_queue *pq;
2376 * If the private USB lock is not locked, then we assume
2377 * that the BUS-DMA load stage has been passed:
2379 pq = &xfer->xroot->dma_q;
2381 if (pq->curr == xfer) {
2382 /* start the next BUS-DMA load, if any */
2383 usb_command_wrapper(pq, NULL);
2387 /* keep some statistics */
2389 xfer->xroot->bus->stats_err.uds_requests
2390 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2392 xfer->xroot->bus->stats_ok.uds_requests
2393 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2396 /* call the USB transfer callback */
2397 usbd_callback_ss_done_defer(xfer);
2400 /*------------------------------------------------------------------------*
2401 * usbd_transfer_start_cb
2403 * This function is called to start the USB transfer when
2404 * "xfer->interval" is greater than zero, and and the endpoint type is
2406 *------------------------------------------------------------------------*/
2408 usbd_transfer_start_cb(void *arg)
2410 struct usb_xfer *xfer = arg;
2411 struct usb_endpoint *ep = xfer->endpoint;
2413 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2418 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2420 /* start USB transfer, if no error */
2421 if (xfer->error == 0)
2422 (ep->methods->start) (xfer);
2424 xfer->flags_int.can_cancel_immed = 1;
2426 /* check for error */
2428 /* some error has happened */
2429 usbd_transfer_done(xfer, 0);
2433 /*------------------------------------------------------------------------*
2434 * usbd_xfer_set_stall
2436 * This function is used to set the stall flag outside the
2437 * callback. This function is NULL safe.
2438 *------------------------------------------------------------------------*/
2440 usbd_xfer_set_stall(struct usb_xfer *xfer)
2446 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2448 /* avoid any races by locking the USB mutex */
2449 USB_BUS_LOCK(xfer->xroot->bus);
2450 xfer->flags.stall_pipe = 1;
2451 USB_BUS_UNLOCK(xfer->xroot->bus);
2455 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2457 return (xfer->endpoint->is_stalled);
2460 /*------------------------------------------------------------------------*
2461 * usbd_transfer_clear_stall
2463 * This function is used to clear the stall flag outside the
2464 * callback. This function is NULL safe.
2465 *------------------------------------------------------------------------*/
2467 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2473 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2475 /* avoid any races by locking the USB mutex */
2476 USB_BUS_LOCK(xfer->xroot->bus);
2478 xfer->flags.stall_pipe = 0;
2480 USB_BUS_UNLOCK(xfer->xroot->bus);
2483 /*------------------------------------------------------------------------*
2486 * This function is used to add an USB transfer to the pipe transfer list.
2487 *------------------------------------------------------------------------*/
2489 usbd_pipe_start(struct usb_xfer_queue *pq)
2491 struct usb_endpoint *ep;
2492 struct usb_xfer *xfer;
2496 ep = xfer->endpoint;
2498 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2501 * If the endpoint is already stalled we do nothing !
2503 if (ep->is_stalled) {
2507 * Check if we are supposed to stall the endpoint:
2509 if (xfer->flags.stall_pipe) {
2510 struct usb_device *udev;
2511 struct usb_xfer_root *info;
2513 /* clear stall command */
2514 xfer->flags.stall_pipe = 0;
2516 /* get pointer to USB device */
2521 * Only stall BULK and INTERRUPT endpoints.
2523 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2524 if ((type == UE_BULK) ||
2525 (type == UE_INTERRUPT)) {
2530 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2531 (udev->bus->methods->set_stall) (
2532 udev, NULL, ep, &did_stall);
2533 } else if (udev->ctrl_xfer[1]) {
2534 info = udev->ctrl_xfer[1]->xroot;
2536 &info->bus->non_giant_callback_proc,
2537 &udev->cs_msg[0], &udev->cs_msg[1]);
2539 /* should not happen */
2540 DPRINTFN(0, "No stall handler\n");
2543 * Check if we should stall. Some USB hardware
2544 * handles set- and clear-stall in hardware.
2548 * The transfer will be continued when
2549 * the clear-stall control endpoint
2550 * message is received.
2555 } else if (type == UE_ISOCHRONOUS) {
2558 * Make sure any FIFO overflow or other FIFO
2559 * error conditions go away by resetting the
2560 * endpoint FIFO through the clear stall
2563 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2564 (udev->bus->methods->clear_stall) (udev, ep);
2568 /* Set or clear stall complete - special case */
2569 if (xfer->nframes == 0) {
2570 /* we are complete */
2572 usbd_transfer_done(xfer, 0);
2578 * 1) Start the first transfer queued.
2580 * 2) Re-start the current USB transfer.
2583 * Check if there should be any
2584 * pre transfer start delay:
2586 if (xfer->interval > 0) {
2587 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2588 if ((type == UE_BULK) ||
2589 (type == UE_CONTROL)) {
2590 usbd_transfer_timeout_ms(xfer,
2591 &usbd_transfer_start_cb,
2599 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2601 /* start USB transfer, if no error */
2602 if (xfer->error == 0)
2603 (ep->methods->start) (xfer);
2605 xfer->flags_int.can_cancel_immed = 1;
2607 /* check for error */
2609 /* some error has happened */
2610 usbd_transfer_done(xfer, 0);
2614 /*------------------------------------------------------------------------*
2615 * usbd_transfer_timeout_ms
2617 * This function is used to setup a timeout on the given USB
2618 * transfer. If the timeout has been deferred the callback given by
2619 * "cb" will get called after "ms" milliseconds.
2620 *------------------------------------------------------------------------*/
2622 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2623 void (*cb) (void *arg), usb_timeout_t ms)
2625 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2628 usb_callout_reset(&xfer->timeout_handle,
2629 USB_MS_TO_TICKS(ms), cb, xfer);
2632 /*------------------------------------------------------------------------*
2633 * usbd_callback_wrapper_sub
2635 * - This function will update variables in an USB transfer after
2636 * that the USB transfer is complete.
2638 * - This function is used to start the next USB transfer on the
2639 * ep transfer queue, if any.
2641 * NOTE: In some special cases the USB transfer will not be removed from
2642 * the pipe queue, but remain first. To enforce USB transfer removal call
2643 * this function passing the error code "USB_ERR_CANCELLED".
2647 * Else: The callback has been deferred.
2648 *------------------------------------------------------------------------*/
2650 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2652 struct usb_endpoint *ep;
2653 struct usb_bus *bus;
2656 bus = xfer->xroot->bus;
2658 if ((!xfer->flags_int.open) &&
2659 (!xfer->flags_int.did_close)) {
2662 (xfer->endpoint->methods->close) (xfer);
2663 USB_BUS_UNLOCK(bus);
2664 /* only close once */
2665 xfer->flags_int.did_close = 1;
2666 return (1); /* wait for new callback */
2669 * If we have a non-hardware induced error we
2670 * need to do the DMA delay!
2672 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2673 (xfer->error == USB_ERR_CANCELLED ||
2674 xfer->error == USB_ERR_TIMEOUT ||
2675 bus->methods->start_dma_delay != NULL)) {
2679 /* only delay once */
2680 xfer->flags_int.did_dma_delay = 1;
2682 /* we can not cancel this delay */
2683 xfer->flags_int.can_cancel_immed = 0;
2685 temp = usbd_get_dma_delay(xfer->xroot->udev);
2687 DPRINTFN(3, "DMA delay, %u ms, "
2688 "on %p\n", temp, xfer);
2693 * Some hardware solutions have dedicated
2694 * events when it is safe to free DMA'ed
2695 * memory. For the other hardware platforms we
2696 * use a static delay.
2698 if (bus->methods->start_dma_delay != NULL) {
2699 (bus->methods->start_dma_delay) (xfer);
2701 usbd_transfer_timeout_ms(xfer,
2702 (void *)&usb_dma_delay_done_cb, temp);
2704 USB_BUS_UNLOCK(bus);
2705 return (1); /* wait for new callback */
2708 /* check actual number of frames */
2709 if (xfer->aframes > xfer->nframes) {
2710 if (xfer->error == 0) {
2711 panic("%s: actual number of frames, %d, is "
2712 "greater than initial number of frames, %d\n",
2713 __FUNCTION__, xfer->aframes, xfer->nframes);
2715 /* just set some valid value */
2716 xfer->aframes = xfer->nframes;
2719 /* compute actual length */
2722 for (x = 0; x != xfer->aframes; x++) {
2723 xfer->actlen += xfer->frlengths[x];
2727 * Frames that were not transferred get zero actual length in
2728 * case the USB device driver does not check the actual number
2729 * of frames transferred, "xfer->aframes":
2731 for (; x < xfer->nframes; x++) {
2732 usbd_xfer_set_frame_len(xfer, x, 0);
2735 /* check actual length */
2736 if (xfer->actlen > xfer->sumlen) {
2737 if (xfer->error == 0) {
2738 panic("%s: actual length, %d, is greater than "
2739 "initial length, %d\n",
2740 __FUNCTION__, xfer->actlen, xfer->sumlen);
2742 /* just set some valid value */
2743 xfer->actlen = xfer->sumlen;
2746 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2747 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2748 xfer->aframes, xfer->nframes);
2751 /* end of control transfer, if any */
2752 xfer->flags_int.control_act = 0;
2754 /* check if we should block the execution queue */
2755 if ((xfer->error != USB_ERR_CANCELLED) &&
2756 (xfer->flags.pipe_bof)) {
2757 DPRINTFN(2, "xfer=%p: Block On Failure "
2758 "on endpoint=%p\n", xfer, xfer->endpoint);
2762 /* check for short transfers */
2763 if (xfer->actlen < xfer->sumlen) {
2765 /* end of control transfer, if any */
2766 xfer->flags_int.control_act = 0;
2768 if (!xfer->flags_int.short_xfer_ok) {
2769 xfer->error = USB_ERR_SHORT_XFER;
2770 if (xfer->flags.pipe_bof) {
2771 DPRINTFN(2, "xfer=%p: Block On Failure on "
2772 "Short Transfer on endpoint %p.\n",
2773 xfer, xfer->endpoint);
2779 * Check if we are in the middle of a
2782 if (xfer->flags_int.control_act) {
2783 DPRINTFN(5, "xfer=%p: Control transfer "
2784 "active on endpoint=%p\n", xfer, xfer->endpoint);
2790 ep = xfer->endpoint;
2793 * If the current USB transfer is completing we need to start the
2797 if (ep->endpoint_q.curr == xfer) {
2798 usb_command_wrapper(&ep->endpoint_q, NULL);
2800 if (ep->endpoint_q.curr || TAILQ_FIRST(&ep->endpoint_q.head)) {
2801 /* there is another USB transfer waiting */
2803 /* this is the last USB transfer */
2804 /* clear isochronous sync flag */
2805 xfer->endpoint->is_synced = 0;
2808 USB_BUS_UNLOCK(bus);
2813 /*------------------------------------------------------------------------*
2814 * usb_command_wrapper
2816 * This function is used to execute commands non-recursivly on an USB
2818 *------------------------------------------------------------------------*/
2820 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2824 * If the transfer is not already processing,
2827 if (pq->curr != xfer) {
2828 usbd_transfer_enqueue(pq, xfer);
2829 if (pq->curr != NULL) {
2830 /* something is already processing */
2831 DPRINTFN(6, "busy %p\n", pq->curr);
2836 /* Get next element in queue */
2840 if (!pq->recurse_1) {
2844 /* set both recurse flags */
2848 if (pq->curr == NULL) {
2849 xfer = TAILQ_FIRST(&pq->head);
2851 TAILQ_REMOVE(&pq->head, xfer,
2853 xfer->wait_queue = NULL;
2859 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
2861 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
2863 } while (!pq->recurse_2);
2865 /* clear first recurse flag */
2869 /* clear second recurse flag */
2874 /*------------------------------------------------------------------------*
2875 * usbd_ctrl_transfer_setup
2877 * This function is used to setup the default USB control endpoint
2879 *------------------------------------------------------------------------*/
2881 usbd_ctrl_transfer_setup(struct usb_device *udev)
2883 struct usb_xfer *xfer;
2885 uint8_t iface_index;
2887 /* check for root HUB */
2888 if (udev->parent_hub == NULL)
2892 xfer = udev->ctrl_xfer[0];
2894 USB_XFER_LOCK(xfer);
2896 ((xfer->address == udev->address) &&
2897 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
2898 udev->ddesc.bMaxPacketSize));
2899 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2902 * NOTE: checking "xfer->address" and
2903 * starting the USB transfer must be
2906 usbd_transfer_start(xfer);
2909 USB_XFER_UNLOCK(xfer);
2916 * All parameters are exactly the same like before.
2922 * Update wMaxPacketSize for the default control endpoint:
2924 udev->ctrl_ep_desc.wMaxPacketSize[0] =
2925 udev->ddesc.bMaxPacketSize;
2928 * Unsetup any existing USB transfer:
2930 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2933 * Reset clear stall error counter.
2935 udev->clear_stall_errors = 0;
2938 * Try to setup a new USB transfer for the
2939 * default control endpoint:
2942 if (usbd_transfer_setup(udev, &iface_index,
2943 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
2944 &udev->device_mtx)) {
2945 DPRINTFN(0, "could not setup default "
2952 /*------------------------------------------------------------------------*
2953 * usbd_clear_data_toggle - factored out code
2955 * NOTE: the intention of this function is not to reset the hardware
2957 *------------------------------------------------------------------------*/
2959 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
2961 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2963 /* check that we have a valid case */
2964 if (udev->flags.usb_mode == USB_MODE_HOST &&
2965 udev->parent_hub != NULL &&
2966 udev->bus->methods->clear_stall != NULL &&
2967 ep->methods != NULL) {
2968 (udev->bus->methods->clear_stall) (udev, ep);
2972 /*------------------------------------------------------------------------*
2973 * usbd_clear_data_toggle - factored out code
2975 * NOTE: the intention of this function is not to reset the hardware
2976 * data toggle on the USB device side.
2977 *------------------------------------------------------------------------*/
2979 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
2981 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
2983 USB_BUS_LOCK(udev->bus);
2984 ep->toggle_next = 0;
2985 /* some hardware needs a callback to clear the data toggle */
2986 usbd_clear_stall_locked(udev, ep);
2987 USB_BUS_UNLOCK(udev->bus);
2990 /*------------------------------------------------------------------------*
2991 * usbd_clear_stall_callback - factored out clear stall callback
2994 * xfer1: Clear Stall Control Transfer
2995 * xfer2: Stalled USB Transfer
2997 * This function is NULL safe.
3003 * Clear stall config example:
3005 * static const struct usb_config my_clearstall = {
3006 * .type = UE_CONTROL,
3008 * .direction = UE_DIR_ANY,
3009 * .interval = 50, //50 milliseconds
3010 * .bufsize = sizeof(struct usb_device_request),
3011 * .timeout = 1000, //1.000 seconds
3012 * .callback = &my_clear_stall_callback, // **
3013 * .usb_mode = USB_MODE_HOST,
3016 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3017 * passing the correct parameters.
3018 *------------------------------------------------------------------------*/
3020 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3021 struct usb_xfer *xfer2)
3023 struct usb_device_request req;
3025 if (xfer2 == NULL) {
3026 /* looks like we are tearing down */
3027 DPRINTF("NULL input parameter\n");
3030 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3031 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3033 switch (USB_GET_STATE(xfer1)) {
3037 * pre-clear the data toggle to DATA0 ("umass.c" and
3038 * "ata-usb.c" depends on this)
3041 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3043 /* setup a clear-stall packet */
3045 req.bmRequestType = UT_WRITE_ENDPOINT;
3046 req.bRequest = UR_CLEAR_FEATURE;
3047 USETW(req.wValue, UF_ENDPOINT_HALT);
3048 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3050 USETW(req.wLength, 0);
3053 * "usbd_transfer_setup_sub()" will ensure that
3054 * we have sufficient room in the buffer for
3055 * the request structure!
3058 /* copy in the transfer */
3060 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3063 xfer1->frlengths[0] = sizeof(req);
3066 usbd_transfer_submit(xfer1);
3069 case USB_ST_TRANSFERRED:
3072 default: /* Error */
3073 if (xfer1->error == USB_ERR_CANCELLED) {
3078 return (1); /* Clear Stall Finished */
3081 /*------------------------------------------------------------------------*
3082 * usbd_transfer_poll
3084 * The following function gets called from the USB keyboard driver and
3085 * UMASS when the system has paniced.
3087 * NOTE: It is currently not possible to resume normal operation on
3088 * the USB controller which has been polled, due to clearing of the
3089 * "up_dsleep" and "up_msleep" flags.
3090 *------------------------------------------------------------------------*/
3092 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3094 struct usb_xfer *xfer;
3095 struct usb_xfer_root *xroot;
3096 struct usb_device *udev;
3097 struct usb_proc_msg *pm;
3102 for (n = 0; n != max; n++) {
3103 /* Extra checks to avoid panic */
3106 continue; /* no USB transfer */
3107 xroot = xfer->xroot;
3109 continue; /* no USB root */
3112 continue; /* no USB device */
3113 if (udev->bus == NULL)
3114 continue; /* no BUS structure */
3115 if (udev->bus->methods == NULL)
3116 continue; /* no BUS methods */
3117 if (udev->bus->methods->xfer_poll == NULL)
3118 continue; /* no poll method */
3120 /* make sure that the BUS mutex is not locked */
3122 while (mtx_owned(&xroot->udev->bus->bus_mtx)) {
3123 mtx_unlock(&xroot->udev->bus->bus_mtx);
3127 /* make sure that the transfer mutex is not locked */
3129 while (mtx_owned(xroot->xfer_mtx)) {
3130 mtx_unlock(xroot->xfer_mtx);
3134 /* Make sure cv_signal() and cv_broadcast() is not called */
3135 udev->bus->control_xfer_proc.up_msleep = 0;
3136 udev->bus->explore_proc.up_msleep = 0;
3137 udev->bus->giant_callback_proc.up_msleep = 0;
3138 udev->bus->non_giant_callback_proc.up_msleep = 0;
3140 /* poll USB hardware */
3141 (udev->bus->methods->xfer_poll) (udev->bus);
3143 USB_BUS_LOCK(xroot->bus);
3145 /* check for clear stall */
3146 if (udev->ctrl_xfer[1] != NULL) {
3148 /* poll clear stall start */
3149 pm = &udev->cs_msg[0].hdr;
3150 (pm->pm_callback) (pm);
3151 /* poll clear stall done thread */
3152 pm = &udev->ctrl_xfer[1]->
3153 xroot->done_m[0].hdr;
3154 (pm->pm_callback) (pm);
3157 /* poll done thread */
3158 pm = &xroot->done_m[0].hdr;
3159 (pm->pm_callback) (pm);
3161 USB_BUS_UNLOCK(xroot->bus);
3163 /* restore transfer mutex */
3165 mtx_lock(xroot->xfer_mtx);
3167 /* restore BUS mutex */
3169 mtx_lock(&xroot->udev->bus->bus_mtx);
3174 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3175 uint8_t type, enum usb_dev_speed speed)
3177 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3178 [USB_SPEED_LOW] = 8,
3179 [USB_SPEED_FULL] = 64,
3180 [USB_SPEED_HIGH] = 1024,
3181 [USB_SPEED_VARIABLE] = 1024,
3182 [USB_SPEED_SUPER] = 1024,
3185 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3186 [USB_SPEED_LOW] = 0, /* invalid */
3187 [USB_SPEED_FULL] = 1023,
3188 [USB_SPEED_HIGH] = 1024,
3189 [USB_SPEED_VARIABLE] = 3584,
3190 [USB_SPEED_SUPER] = 1024,
3193 static const uint16_t control_min[USB_SPEED_MAX] = {
3194 [USB_SPEED_LOW] = 8,
3195 [USB_SPEED_FULL] = 8,
3196 [USB_SPEED_HIGH] = 64,
3197 [USB_SPEED_VARIABLE] = 512,
3198 [USB_SPEED_SUPER] = 512,
3201 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3202 [USB_SPEED_LOW] = 8,
3203 [USB_SPEED_FULL] = 8,
3204 [USB_SPEED_HIGH] = 512,
3205 [USB_SPEED_VARIABLE] = 512,
3206 [USB_SPEED_SUPER] = 1024,
3211 memset(ptr, 0, sizeof(*ptr));
3215 ptr->range.max = intr_range_max[speed];
3217 case UE_ISOCHRONOUS:
3218 ptr->range.max = isoc_range_max[speed];
3221 if (type == UE_BULK)
3222 temp = bulk_min[speed];
3223 else /* UE_CONTROL */
3224 temp = control_min[speed];
3226 /* default is fixed */
3227 ptr->fixed[0] = temp;
3228 ptr->fixed[1] = temp;
3229 ptr->fixed[2] = temp;
3230 ptr->fixed[3] = temp;
3232 if (speed == USB_SPEED_FULL) {
3233 /* multiple sizes */
3238 if ((speed == USB_SPEED_VARIABLE) &&
3239 (type == UE_BULK)) {
3240 /* multiple sizes */
3241 ptr->fixed[2] = 1024;
3242 ptr->fixed[3] = 1536;
3249 usbd_xfer_softc(struct usb_xfer *xfer)
3251 return (xfer->priv_sc);
3255 usbd_xfer_get_priv(struct usb_xfer *xfer)
3257 return (xfer->priv_fifo);
3261 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3263 xfer->priv_fifo = ptr;
3267 usbd_xfer_state(struct usb_xfer *xfer)
3269 return (xfer->usb_state);
3273 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3276 case USB_FORCE_SHORT_XFER:
3277 xfer->flags.force_short_xfer = 1;
3279 case USB_SHORT_XFER_OK:
3280 xfer->flags.short_xfer_ok = 1;
3282 case USB_MULTI_SHORT_OK:
3283 xfer->flags.short_frames_ok = 1;
3285 case USB_MANUAL_STATUS:
3286 xfer->flags.manual_status = 1;
3292 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3295 case USB_FORCE_SHORT_XFER:
3296 xfer->flags.force_short_xfer = 0;
3298 case USB_SHORT_XFER_OK:
3299 xfer->flags.short_xfer_ok = 0;
3301 case USB_MULTI_SHORT_OK:
3302 xfer->flags.short_frames_ok = 0;
3304 case USB_MANUAL_STATUS:
3305 xfer->flags.manual_status = 0;
3311 * The following function returns in milliseconds when the isochronous
3312 * transfer was completed by the hardware. The returned value wraps
3313 * around 65536 milliseconds.
3316 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3318 return (xfer->isoc_time_complete);