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/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
51 #define USB_DEBUG_VAR usb_debug
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_transfer.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_debug.h>
59 #include <dev/usb/usb_util.h>
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.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_DEFAULT_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 usb_dma_delay_done_cb(void *);
113 static void usbd_transfer_start_cb(void *);
114 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
115 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
116 uint8_t type, enum usb_dev_speed speed);
118 /*------------------------------------------------------------------------*
119 * usb_request_callback
120 *------------------------------------------------------------------------*/
122 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
124 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
125 usb_handle_request_callback(xfer, error);
127 usbd_do_request_callback(xfer, error);
130 /*------------------------------------------------------------------------*
131 * usbd_update_max_frame_size
133 * This function updates the maximum frame size, hence high speed USB
134 * can transfer multiple consecutive packets.
135 *------------------------------------------------------------------------*/
137 usbd_update_max_frame_size(struct usb_xfer *xfer)
139 /* compute maximum frame size */
141 if (xfer->max_packet_count == 2) {
142 xfer->max_frame_size = 2 * xfer->max_packet_size;
143 } else if (xfer->max_packet_count == 3) {
144 xfer->max_frame_size = 3 * xfer->max_packet_size;
146 xfer->max_frame_size = xfer->max_packet_size;
150 /*------------------------------------------------------------------------*
153 * The following function is called when we need to
154 * synchronize with DMA hardware.
157 * 0: no DMA delay required
158 * Else: milliseconds of DMA delay
159 *------------------------------------------------------------------------*/
161 usbd_get_dma_delay(struct usb_bus *bus)
165 if (bus->methods->get_dma_delay) {
166 (bus->methods->get_dma_delay) (bus, &temp);
168 * Round up and convert to milliseconds. Note that we use
169 * 1024 milliseconds per second. to save a division.
177 /*------------------------------------------------------------------------*
178 * usbd_transfer_setup_sub_malloc
180 * This function will allocate one or more DMA'able memory chunks
181 * according to "size", "align" and "count" arguments. "ppc" is
182 * pointed to a linear array of USB page caches afterwards.
187 *------------------------------------------------------------------------*/
190 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
191 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
194 struct usb_page_cache *pc;
204 USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n",
206 USB_ASSERT(size > 0, ("Invalid size = 0!\n"));
209 return (0); /* nothing to allocate */
212 * Make sure that the size is aligned properly.
214 size = -((-size) & (-align));
217 * Try multi-allocation chunks to reduce the number of DMA
218 * allocations, hence DMA allocations are slow.
220 if (size >= PAGE_SIZE) {
224 /* compute number of objects per page */
225 n_obj = (PAGE_SIZE / size);
227 * Compute number of DMA chunks, rounded up
230 n_dma_pc = ((count + n_obj - 1) / n_obj);
233 if (parm->buf == NULL) {
235 parm->dma_page_ptr += n_dma_pc;
236 parm->dma_page_cache_ptr += n_dma_pc;
237 parm->dma_page_ptr += count;
238 parm->xfer_page_cache_ptr += count;
241 for (x = 0; x != n_dma_pc; x++) {
242 /* need to initialize the page cache */
243 parm->dma_page_cache_ptr[x].tag_parent =
244 &parm->curr_xfer->xroot->dma_parent_tag;
246 for (x = 0; x != count; x++) {
247 /* need to initialize the page cache */
248 parm->xfer_page_cache_ptr[x].tag_parent =
249 &parm->curr_xfer->xroot->dma_parent_tag;
253 *ppc = parm->xfer_page_cache_ptr;
255 r = count; /* set remainder count */
256 z = n_obj * size; /* set allocation size */
257 pc = parm->xfer_page_cache_ptr;
258 pg = parm->dma_page_ptr;
260 for (x = 0; x != n_dma_pc; x++) {
263 /* compute last remainder */
267 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
269 return (1); /* failure */
271 /* Set beginning of current buffer */
272 buf = parm->dma_page_cache_ptr->buffer;
273 /* Make room for one DMA page cache and one page */
274 parm->dma_page_cache_ptr++;
277 for (y = 0; (y != n_obj); y++, r--, pc++, pg++) {
279 /* Load sub-chunk into DMA */
280 if (usb_pc_dmamap_create(pc, size)) {
281 return (1); /* failure */
283 pc->buffer = USB_ADD_BYTES(buf, y * size);
286 mtx_lock(pc->tag_parent->mtx);
287 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
288 mtx_unlock(pc->tag_parent->mtx);
289 return (1); /* failure */
291 mtx_unlock(pc->tag_parent->mtx);
295 parm->xfer_page_cache_ptr = pc;
296 parm->dma_page_ptr = pg;
301 /*------------------------------------------------------------------------*
302 * usbd_transfer_setup_sub - transfer setup subroutine
304 * This function must be called from the "xfer_setup" callback of the
305 * USB Host or Device controller driver when setting up an USB
306 * transfer. This function will setup correct packet sizes, buffer
307 * sizes, flags and more, that are stored in the "usb_xfer"
309 *------------------------------------------------------------------------*/
311 usbd_transfer_setup_sub(struct usb_setup_params *parm)
317 struct usb_xfer *xfer = parm->curr_xfer;
318 const struct usb_config *setup = parm->curr_setup;
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;
339 type = (edesc->bmAttributes & UE_XFERTYPE);
341 xfer->flags = setup->flags;
342 xfer->nframes = setup->frames;
343 xfer->timeout = setup->timeout;
344 xfer->callback = setup->callback;
345 xfer->interval = setup->interval;
346 xfer->endpointno = edesc->bEndpointAddress;
347 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
348 xfer->max_packet_count = 1;
349 /* make a shadow copy: */
350 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
352 parm->bufsize = setup->bufsize;
354 if (parm->speed == USB_SPEED_HIGH) {
355 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
356 xfer->max_packet_size &= 0x7FF;
358 /* range check "max_packet_count" */
360 if (xfer->max_packet_count > parm->hc_max_packet_count) {
361 xfer->max_packet_count = parm->hc_max_packet_count;
363 /* filter "wMaxPacketSize" according to HC capabilities */
365 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
366 (xfer->max_packet_size == 0)) {
367 xfer->max_packet_size = parm->hc_max_packet_size;
369 /* filter "wMaxPacketSize" according to standard sizes */
371 usbd_get_std_packet_size(&std_size, type, parm->speed);
373 if (std_size.range.min || std_size.range.max) {
375 if (xfer->max_packet_size < std_size.range.min) {
376 xfer->max_packet_size = std_size.range.min;
378 if (xfer->max_packet_size > std_size.range.max) {
379 xfer->max_packet_size = std_size.range.max;
383 if (xfer->max_packet_size >= std_size.fixed[3]) {
384 xfer->max_packet_size = std_size.fixed[3];
385 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
386 xfer->max_packet_size = std_size.fixed[2];
387 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
388 xfer->max_packet_size = std_size.fixed[1];
390 /* only one possibility left */
391 xfer->max_packet_size = std_size.fixed[0];
395 /* compute "max_frame_size" */
397 usbd_update_max_frame_size(xfer);
399 /* check interrupt interval and transfer pre-delay */
401 if (type == UE_ISOCHRONOUS) {
403 uint16_t frame_limit;
405 xfer->interval = 0; /* not used, must be zero */
406 xfer->flags_int.isochronous_xfr = 1; /* set flag */
408 if (xfer->timeout == 0) {
410 * set a default timeout in
411 * case something goes wrong!
413 xfer->timeout = 1000 / 4;
415 switch (parm->speed) {
418 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
421 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
425 if (xfer->nframes > frame_limit) {
427 * this is not going to work
430 parm->err = USB_ERR_INVAL;
433 if (xfer->nframes == 0) {
435 * this is not a valid value
437 parm->err = USB_ERR_ZERO_NFRAMES;
443 * if a value is specified use that else check the endpoint
446 if (xfer->interval == 0) {
448 if (type == UE_INTERRUPT) {
450 xfer->interval = edesc->bInterval;
452 switch (parm->speed) {
453 case USB_SPEED_SUPER:
454 case USB_SPEED_VARIABLE:
456 if (xfer->interval < 4)
458 else if (xfer->interval > 16)
459 xfer->interval = (1<<(16-4));
462 (1 << (xfer->interval-4));
471 if (xfer->interval == 0) {
473 * One millisecond is the smallest
474 * interval we support:
483 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
484 * to be equal to zero when setting up USB transfers, hence
485 * this leads to alot of extra code in the USB kernel.
488 if ((xfer->max_frame_size == 0) ||
489 (xfer->max_packet_size == 0)) {
493 if ((parm->bufsize <= MIN_PKT) &&
494 (type != UE_CONTROL) &&
498 xfer->max_packet_size = MIN_PKT;
499 xfer->max_packet_count = 1;
500 parm->bufsize = 0; /* automatic setup length */
501 usbd_update_max_frame_size(xfer);
504 parm->err = USB_ERR_ZERO_MAXP;
513 * check if we should setup a default
517 if (parm->bufsize == 0) {
519 parm->bufsize = xfer->max_frame_size;
521 if (type == UE_ISOCHRONOUS) {
522 parm->bufsize *= xfer->nframes;
526 * check if we are about to setup a proxy
530 if (xfer->flags.proxy_buffer) {
532 /* round bufsize up */
534 parm->bufsize += (xfer->max_frame_size - 1);
536 if (parm->bufsize < xfer->max_frame_size) {
537 /* length wrapped around */
538 parm->err = USB_ERR_INVAL;
541 /* subtract remainder */
543 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
545 /* add length of USB device request structure, if any */
547 if (type == UE_CONTROL) {
548 parm->bufsize += REQ_SIZE; /* SETUP message */
551 xfer->max_data_length = parm->bufsize;
553 /* Setup "n_frlengths" and "n_frbuffers" */
555 if (type == UE_ISOCHRONOUS) {
556 n_frlengths = xfer->nframes;
560 if (type == UE_CONTROL) {
561 xfer->flags_int.control_xfr = 1;
562 if (xfer->nframes == 0) {
563 if (parm->bufsize <= REQ_SIZE) {
565 * there will never be any data
574 if (xfer->nframes == 0) {
579 n_frlengths = xfer->nframes;
580 n_frbuffers = xfer->nframes;
584 * check if we have room for the
585 * USB device request structure:
588 if (type == UE_CONTROL) {
590 if (xfer->max_data_length < REQ_SIZE) {
591 /* length wrapped around or too small bufsize */
592 parm->err = USB_ERR_INVAL;
595 xfer->max_data_length -= REQ_SIZE;
597 /* setup "frlengths" */
598 xfer->frlengths = parm->xfer_length_ptr;
599 parm->xfer_length_ptr += n_frlengths;
601 /* setup "frbuffers" */
602 xfer->frbuffers = parm->xfer_page_cache_ptr;
603 parm->xfer_page_cache_ptr += n_frbuffers;
605 /* initialize max frame count */
606 xfer->max_frame_count = xfer->nframes;
609 * check if we need to setup
613 if (!xfer->flags.ext_buffer) {
616 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
621 USB_ADD_BYTES(parm->buf, parm->size[0]);
623 usbd_xfer_set_frame_offset(xfer, 0, 0);
625 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
626 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
629 parm->size[0] += parm->bufsize;
631 /* align data again */
632 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
635 * Compute maximum buffer size
638 if (parm->bufsize_max < parm->bufsize) {
639 parm->bufsize_max = parm->bufsize;
642 if (xfer->flags_int.bdma_enable) {
644 * Setup "dma_page_ptr".
646 * Proof for formula below:
648 * Assume there are three USB frames having length "a", "b" and
649 * "c". These USB frames will at maximum need "z"
650 * "usb_page" structures. "z" is given by:
652 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
653 * ((c / USB_PAGE_SIZE) + 2);
655 * Constraining "a", "b" and "c" like this:
657 * (a + b + c) <= parm->bufsize
661 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
663 * Here is the general formula:
665 xfer->dma_page_ptr = parm->dma_page_ptr;
666 parm->dma_page_ptr += (2 * n_frbuffers);
667 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
671 /* correct maximum data length */
672 xfer->max_data_length = 0;
674 /* subtract USB frame remainder from "hc_max_frame_size" */
676 xfer->max_hc_frame_size =
677 (parm->hc_max_frame_size -
678 (parm->hc_max_frame_size % xfer->max_frame_size));
680 if (xfer->max_hc_frame_size == 0) {
681 parm->err = USB_ERR_INVAL;
685 /* initialize frame buffers */
688 for (x = 0; x != n_frbuffers; x++) {
689 xfer->frbuffers[x].tag_parent =
690 &xfer->xroot->dma_parent_tag;
692 if (xfer->flags_int.bdma_enable &&
693 (parm->bufsize_max > 0)) {
695 if (usb_pc_dmamap_create(
697 parm->bufsize_max)) {
698 parm->err = USB_ERR_NOMEM;
708 * Set some dummy values so that we avoid division by zero:
710 xfer->max_hc_frame_size = 1;
711 xfer->max_frame_size = 1;
712 xfer->max_packet_size = 1;
713 xfer->max_data_length = 0;
715 xfer->max_frame_count = 0;
719 /*------------------------------------------------------------------------*
720 * usbd_transfer_setup - setup an array of USB transfers
722 * NOTE: You must always call "usbd_transfer_unsetup" after calling
723 * "usbd_transfer_setup" if success was returned.
725 * The idea is that the USB device driver should pre-allocate all its
726 * transfers by one call to this function.
731 *------------------------------------------------------------------------*/
733 usbd_transfer_setup(struct usb_device *udev,
734 const uint8_t *ifaces, struct usb_xfer **ppxfer,
735 const struct usb_config *setup_start, uint16_t n_setup,
736 void *priv_sc, struct mtx *xfer_mtx)
738 struct usb_xfer dummy;
739 struct usb_setup_params parm;
740 const struct usb_config *setup_end = setup_start + n_setup;
741 const struct usb_config *setup;
742 struct usb_endpoint *ep;
743 struct usb_xfer_root *info;
744 struct usb_xfer *xfer;
753 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
754 "usbd_transfer_setup can sleep!");
756 /* do some checking first */
759 DPRINTFN(6, "setup array has zero length!\n");
760 return (USB_ERR_INVAL);
763 DPRINTFN(6, "ifaces array is NULL!\n");
764 return (USB_ERR_INVAL);
766 if (xfer_mtx == NULL) {
767 DPRINTFN(6, "using global lock\n");
771 for (setup = setup_start, n = 0;
772 setup != setup_end; setup++, n++) {
773 if (setup->bufsize == (usb_frlength_t)-1) {
774 parm.err = USB_ERR_BAD_BUFSIZE;
775 DPRINTF("invalid bufsize\n");
777 if (setup->callback == NULL) {
778 parm.err = USB_ERR_NO_CALLBACK;
779 DPRINTF("no callback\n");
787 bzero(&parm, sizeof(parm));
790 parm.speed = usbd_get_speed(udev);
791 parm.hc_max_packet_count = 1;
793 if (parm.speed >= USB_SPEED_MAX) {
794 parm.err = USB_ERR_INVAL;
797 /* setup all transfers */
803 * Initialize the "usb_xfer_root" structure,
804 * which is common for all our USB transfers.
806 info = USB_ADD_BYTES(buf, 0);
808 info->memory_base = buf;
809 info->memory_size = parm.size[0];
812 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
813 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
815 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
816 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
818 cv_init(&info->cv_drain, "WDRAIN");
820 info->xfer_mtx = xfer_mtx;
822 usb_dma_tag_setup(&info->dma_parent_tag,
823 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
824 xfer_mtx, &usb_bdma_done_event, 32, parm.dma_tag_max);
827 info->bus = udev->bus;
830 TAILQ_INIT(&info->done_q.head);
831 info->done_q.command = &usbd_callback_wrapper;
833 TAILQ_INIT(&info->dma_q.head);
834 info->dma_q.command = &usb_bdma_work_loop;
836 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
837 info->done_m[0].xroot = info;
838 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
839 info->done_m[1].xroot = info;
842 * In device side mode control endpoint
843 * requests need to run from a separate
844 * context, else there is a chance of
847 if (setup_start == usb_control_ep_cfg)
849 &udev->bus->control_xfer_proc;
850 else if (xfer_mtx == &Giant)
852 &udev->bus->giant_callback_proc;
855 &udev->bus->non_giant_callback_proc;
861 parm.size[0] += sizeof(info[0]);
863 for (setup = setup_start, n = 0;
864 setup != setup_end; setup++, n++) {
866 /* skip USB transfers without callbacks: */
867 if (setup->callback == NULL) {
870 /* see if there is a matching endpoint */
871 ep = usbd_get_endpoint(udev,
872 ifaces[setup->if_index], setup);
874 if ((ep == NULL) || (ep->methods == NULL)) {
875 if (setup->flags.no_pipe_ok)
877 if ((setup->usb_mode != USB_MODE_DUAL) &&
878 (setup->usb_mode != udev->flags.usb_mode))
880 parm.err = USB_ERR_NO_PIPE;
884 /* align data properly */
885 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
887 /* store current setup pointer */
888 parm.curr_setup = setup;
892 * Common initialization of the
893 * "usb_xfer" structure.
895 xfer = USB_ADD_BYTES(buf, parm.size[0]);
896 xfer->address = udev->address;
897 xfer->priv_sc = priv_sc;
900 usb_callout_init_mtx(&xfer->timeout_handle,
901 &udev->bus->bus_mtx, 0);
904 * Setup a dummy xfer, hence we are
905 * writing to the "usb_xfer"
906 * structure pointed to by "xfer"
907 * before we have allocated any
911 bzero(&dummy, sizeof(dummy));
915 /* set transfer endpoint pointer */
918 parm.size[0] += sizeof(xfer[0]);
919 parm.methods = xfer->endpoint->methods;
920 parm.curr_xfer = xfer;
923 * Call the Host or Device controller transfer
926 (udev->bus->methods->xfer_setup) (&parm);
928 /* check for error */
934 * Increment the endpoint refcount. This
935 * basically prevents setting a new
936 * configuration and alternate setting
937 * when USB transfers are in use on
938 * the given interface. Search the USB
939 * code for "endpoint->refcount" if you
940 * want more information.
942 xfer->endpoint->refcount++;
945 * Whenever we set ppxfer[] then we
946 * also need to increment the
949 info->setup_refcount++;
952 * Transfer is successfully setup and
959 if (buf || parm.err) {
963 /* no transfers - nothing to do ! */
966 /* align data properly */
967 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
969 /* store offset temporarily */
970 parm.size[1] = parm.size[0];
973 * The number of DMA tags required depends on
974 * the number of endpoints. The current estimate
975 * for maximum number of DMA tags per endpoint
978 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX);
981 * DMA tags for QH, TD, Data and more.
983 parm.dma_tag_max += 8;
985 parm.dma_tag_p += parm.dma_tag_max;
987 parm.size[0] += ((uint8_t *)parm.dma_tag_p) -
990 /* align data properly */
991 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
993 /* store offset temporarily */
994 parm.size[3] = parm.size[0];
996 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) -
999 /* align data properly */
1000 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1002 /* store offset temporarily */
1003 parm.size[4] = parm.size[0];
1005 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) -
1008 /* store end offset temporarily */
1009 parm.size[5] = parm.size[0];
1011 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) -
1014 /* store end offset temporarily */
1016 parm.size[2] = parm.size[0];
1018 /* align data properly */
1019 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1021 parm.size[6] = parm.size[0];
1023 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) -
1026 /* align data properly */
1027 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
1029 /* allocate zeroed memory */
1030 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO);
1033 parm.err = USB_ERR_NOMEM;
1034 DPRINTFN(0, "cannot allocate memory block for "
1035 "configuration (%d bytes)\n",
1039 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]);
1040 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]);
1041 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]);
1042 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]);
1043 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1048 if (info->setup_refcount == 0) {
1050 * "usbd_transfer_unsetup_sub" will unlock
1051 * the bus mutex before returning !
1053 USB_BUS_LOCK(info->bus);
1055 /* something went wrong */
1056 usbd_transfer_unsetup_sub(info, 0);
1060 usbd_transfer_unsetup(ppxfer, n_setup);
1065 /*------------------------------------------------------------------------*
1066 * usbd_transfer_unsetup_sub - factored out code
1067 *------------------------------------------------------------------------*/
1069 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1071 struct usb_page_cache *pc;
1073 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1075 /* wait for any outstanding DMA operations */
1079 temp = usbd_get_dma_delay(info->bus);
1080 usb_pause_mtx(&info->bus->bus_mtx,
1081 USB_MS_TO_TICKS(temp));
1084 /* make sure that our done messages are not queued anywhere */
1085 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1087 USB_BUS_UNLOCK(info->bus);
1090 /* free DMA'able memory, if any */
1091 pc = info->dma_page_cache_start;
1092 while (pc != info->dma_page_cache_end) {
1093 usb_pc_free_mem(pc);
1097 /* free DMA maps in all "xfer->frbuffers" */
1098 pc = info->xfer_page_cache_start;
1099 while (pc != info->xfer_page_cache_end) {
1100 usb_pc_dmamap_destroy(pc);
1104 /* free all DMA tags */
1105 usb_dma_tag_unsetup(&info->dma_parent_tag);
1108 cv_destroy(&info->cv_drain);
1111 * free the "memory_base" last, hence the "info" structure is
1112 * contained within the "memory_base"!
1114 free(info->memory_base, M_USB);
1117 /*------------------------------------------------------------------------*
1118 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1120 * NOTE: All USB transfers in progress will get called back passing
1121 * the error code "USB_ERR_CANCELLED" before this function
1123 *------------------------------------------------------------------------*/
1125 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1127 struct usb_xfer *xfer;
1128 struct usb_xfer_root *info;
1129 uint8_t needs_delay = 0;
1131 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1132 "usbd_transfer_unsetup can sleep!");
1135 xfer = pxfer[n_setup];
1142 USB_XFER_LOCK(xfer);
1143 USB_BUS_LOCK(info->bus);
1146 * HINT: when you start/stop a transfer, it might be a
1147 * good idea to directly use the "pxfer[]" structure:
1149 * usbd_transfer_start(sc->pxfer[0]);
1150 * usbd_transfer_stop(sc->pxfer[0]);
1152 * That way, if your code has many parts that will not
1153 * stop running under the same lock, in other words
1154 * "xfer_mtx", the usbd_transfer_start and
1155 * usbd_transfer_stop functions will simply return
1156 * when they detect a NULL pointer argument.
1158 * To avoid any races we clear the "pxfer[]" pointer
1159 * while holding the private mutex of the driver:
1161 pxfer[n_setup] = NULL;
1163 USB_BUS_UNLOCK(info->bus);
1164 USB_XFER_UNLOCK(xfer);
1166 usbd_transfer_drain(xfer);
1169 if (xfer->flags_int.bdma_enable)
1173 * NOTE: default endpoint does not have an
1174 * interface, even if endpoint->iface_index == 0
1176 xfer->endpoint->refcount--;
1178 usb_callout_drain(&xfer->timeout_handle);
1180 USB_BUS_LOCK(info->bus);
1182 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1183 "reference count!\n"));
1185 info->setup_refcount--;
1187 if (info->setup_refcount == 0) {
1188 usbd_transfer_unsetup_sub(info,
1191 USB_BUS_UNLOCK(info->bus);
1196 /*------------------------------------------------------------------------*
1197 * usbd_control_transfer_init - factored out code
1199 * In USB Device Mode we have to wait for the SETUP packet which
1200 * containst the "struct usb_device_request" structure, before we can
1201 * transfer any data. In USB Host Mode we already have the SETUP
1202 * packet at the moment the USB transfer is started. This leads us to
1203 * having to setup the USB transfer at two different places in
1204 * time. This function just contains factored out control transfer
1205 * initialisation code, so that we don't duplicate the code.
1206 *------------------------------------------------------------------------*/
1208 usbd_control_transfer_init(struct usb_xfer *xfer)
1210 struct usb_device_request req;
1212 /* copy out the USB request header */
1214 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1216 /* setup remainder */
1218 xfer->flags_int.control_rem = UGETW(req.wLength);
1220 /* copy direction to endpoint variable */
1222 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1224 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1227 /*------------------------------------------------------------------------*
1228 * usbd_setup_ctrl_transfer
1230 * This function handles initialisation of control transfers. Control
1231 * transfers are special in that regard that they can both transmit
1237 *------------------------------------------------------------------------*/
1239 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1243 /* Check for control endpoint stall */
1244 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1245 /* the control transfer is no longer active */
1246 xfer->flags_int.control_stall = 1;
1247 xfer->flags_int.control_act = 0;
1249 /* don't stall control transfer by default */
1250 xfer->flags_int.control_stall = 0;
1253 /* Check for invalid number of frames */
1254 if (xfer->nframes > 2) {
1256 * If you need to split a control transfer, you
1257 * have to do one part at a time. Only with
1258 * non-control transfers you can do multiple
1261 DPRINTFN(0, "Too many frames: %u\n",
1262 (unsigned int)xfer->nframes);
1267 * Check if there is a control
1268 * transfer in progress:
1270 if (xfer->flags_int.control_act) {
1272 if (xfer->flags_int.control_hdr) {
1274 /* clear send header flag */
1276 xfer->flags_int.control_hdr = 0;
1278 /* setup control transfer */
1279 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1280 usbd_control_transfer_init(xfer);
1283 /* get data length */
1289 /* the size of the SETUP structure is hardcoded ! */
1291 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1292 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1293 xfer->frlengths[0], sizeof(struct
1294 usb_device_request));
1297 /* check USB mode */
1298 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1300 /* check number of frames */
1301 if (xfer->nframes != 1) {
1303 * We need to receive the setup
1304 * message first so that we know the
1307 DPRINTF("Misconfigured transfer\n");
1311 * Set a dummy "control_rem" value. This
1312 * variable will be overwritten later by a
1313 * call to "usbd_control_transfer_init()" !
1315 xfer->flags_int.control_rem = 0xFFFF;
1318 /* setup "endpoint" and "control_rem" */
1320 usbd_control_transfer_init(xfer);
1323 /* set transfer-header flag */
1325 xfer->flags_int.control_hdr = 1;
1327 /* get data length */
1329 len = (xfer->sumlen - sizeof(struct usb_device_request));
1332 /* check if there is a length mismatch */
1334 if (len > xfer->flags_int.control_rem) {
1335 DPRINTFN(0, "Length (%d) greater than "
1336 "remaining length (%d)!\n", len,
1337 xfer->flags_int.control_rem);
1340 /* check if we are doing a short transfer */
1342 if (xfer->flags.force_short_xfer) {
1343 xfer->flags_int.control_rem = 0;
1345 if ((len != xfer->max_data_length) &&
1346 (len != xfer->flags_int.control_rem) &&
1347 (xfer->nframes != 1)) {
1348 DPRINTFN(0, "Short control transfer without "
1349 "force_short_xfer set!\n");
1352 xfer->flags_int.control_rem -= len;
1355 /* the status part is executed when "control_act" is 0 */
1357 if ((xfer->flags_int.control_rem > 0) ||
1358 (xfer->flags.manual_status)) {
1359 /* don't execute the STATUS stage yet */
1360 xfer->flags_int.control_act = 1;
1363 if ((!xfer->flags_int.control_hdr) &&
1364 (xfer->nframes == 1)) {
1366 * This is not a valid operation!
1368 DPRINTFN(0, "Invalid parameter "
1373 /* time to execute the STATUS stage */
1374 xfer->flags_int.control_act = 0;
1376 return (0); /* success */
1379 return (1); /* failure */
1382 /*------------------------------------------------------------------------*
1383 * usbd_transfer_submit - start USB hardware for the given transfer
1385 * This function should only be called from the USB callback.
1386 *------------------------------------------------------------------------*/
1388 usbd_transfer_submit(struct usb_xfer *xfer)
1390 struct usb_xfer_root *info;
1391 struct usb_bus *bus;
1397 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1398 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1402 if (USB_DEBUG_VAR > 0) {
1405 usb_dump_endpoint(xfer->endpoint);
1407 USB_BUS_UNLOCK(bus);
1411 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1412 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1414 /* Only open the USB transfer once! */
1415 if (!xfer->flags_int.open) {
1416 xfer->flags_int.open = 1;
1421 (xfer->endpoint->methods->open) (xfer);
1422 USB_BUS_UNLOCK(bus);
1424 /* set "transferring" flag */
1425 xfer->flags_int.transferring = 1;
1428 /* increment power reference */
1429 usbd_transfer_power_ref(xfer, 1);
1432 * Check if the transfer is waiting on a queue, most
1433 * frequently the "done_q":
1435 if (xfer->wait_queue) {
1437 usbd_transfer_dequeue(xfer);
1438 USB_BUS_UNLOCK(bus);
1440 /* clear "did_dma_delay" flag */
1441 xfer->flags_int.did_dma_delay = 0;
1443 /* clear "did_close" flag */
1444 xfer->flags_int.did_close = 0;
1447 /* clear "bdma_setup" flag */
1448 xfer->flags_int.bdma_setup = 0;
1450 /* by default we cannot cancel any USB transfer immediately */
1451 xfer->flags_int.can_cancel_immed = 0;
1453 /* clear lengths and frame counts by default */
1458 /* clear any previous errors */
1461 /* Check if the device is still alive */
1462 if (info->udev->state < USB_STATE_POWERED) {
1465 * Must return cancelled error code else
1466 * device drivers can hang.
1468 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1469 USB_BUS_UNLOCK(bus);
1474 if (xfer->nframes == 0) {
1475 if (xfer->flags.stall_pipe) {
1477 * Special case - want to stall without transferring
1480 DPRINTF("xfer=%p nframes=0: stall "
1481 "or clear stall!\n", xfer);
1483 xfer->flags_int.can_cancel_immed = 1;
1484 /* start the transfer */
1485 usb_command_wrapper(&xfer->endpoint->endpoint_q, xfer);
1486 USB_BUS_UNLOCK(bus);
1490 usbd_transfer_done(xfer, USB_ERR_INVAL);
1491 USB_BUS_UNLOCK(bus);
1494 /* compute total transfer length */
1496 for (x = 0; x != xfer->nframes; x++) {
1497 xfer->sumlen += xfer->frlengths[x];
1498 if (xfer->sumlen < xfer->frlengths[x]) {
1499 /* length wrapped around */
1501 usbd_transfer_done(xfer, USB_ERR_INVAL);
1502 USB_BUS_UNLOCK(bus);
1507 /* clear some internal flags */
1509 xfer->flags_int.short_xfer_ok = 0;
1510 xfer->flags_int.short_frames_ok = 0;
1512 /* check if this is a control transfer */
1514 if (xfer->flags_int.control_xfr) {
1516 if (usbd_setup_ctrl_transfer(xfer)) {
1518 usbd_transfer_done(xfer, USB_ERR_STALLED);
1519 USB_BUS_UNLOCK(bus);
1524 * Setup filtered version of some transfer flags,
1525 * in case of data read direction
1527 if (USB_GET_DATA_ISREAD(xfer)) {
1529 if (xfer->flags.short_frames_ok) {
1530 xfer->flags_int.short_xfer_ok = 1;
1531 xfer->flags_int.short_frames_ok = 1;
1532 } else if (xfer->flags.short_xfer_ok) {
1533 xfer->flags_int.short_xfer_ok = 1;
1535 /* check for control transfer */
1536 if (xfer->flags_int.control_xfr) {
1538 * 1) Control transfers do not support
1539 * reception of multiple short USB
1540 * frames in host mode and device side
1541 * mode, with exception of:
1543 * 2) Due to sometimes buggy device
1544 * side firmware we need to do a
1545 * STATUS stage in case of short
1546 * control transfers in USB host mode.
1547 * The STATUS stage then becomes the
1548 * "alt_next" to the DATA stage.
1550 xfer->flags_int.short_frames_ok = 1;
1555 * Check if BUS-DMA support is enabled and try to load virtual
1556 * buffers into DMA, if any:
1559 if (xfer->flags_int.bdma_enable) {
1560 /* insert the USB transfer last in the BUS-DMA queue */
1561 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1566 * Enter the USB transfer into the Host Controller or
1567 * Device Controller schedule:
1569 usbd_pipe_enter(xfer);
1572 /*------------------------------------------------------------------------*
1573 * usbd_pipe_enter - factored out code
1574 *------------------------------------------------------------------------*/
1576 usbd_pipe_enter(struct usb_xfer *xfer)
1578 struct usb_endpoint *ep;
1580 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1582 USB_BUS_LOCK(xfer->xroot->bus);
1584 ep = xfer->endpoint;
1588 /* enter the transfer */
1589 (ep->methods->enter) (xfer);
1591 xfer->flags_int.can_cancel_immed = 1;
1593 /* check for transfer error */
1595 /* some error has happened */
1596 usbd_transfer_done(xfer, 0);
1597 USB_BUS_UNLOCK(xfer->xroot->bus);
1601 /* start the transfer */
1602 usb_command_wrapper(&ep->endpoint_q, xfer);
1603 USB_BUS_UNLOCK(xfer->xroot->bus);
1606 /*------------------------------------------------------------------------*
1607 * usbd_transfer_start - start an USB transfer
1609 * NOTE: Calling this function more than one time will only
1610 * result in a single transfer start, until the USB transfer
1612 *------------------------------------------------------------------------*/
1614 usbd_transfer_start(struct usb_xfer *xfer)
1617 /* transfer is gone */
1620 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1622 /* mark the USB transfer started */
1624 if (!xfer->flags_int.started) {
1625 /* lock the BUS lock to avoid races updating flags_int */
1626 USB_BUS_LOCK(xfer->xroot->bus);
1627 xfer->flags_int.started = 1;
1628 USB_BUS_UNLOCK(xfer->xroot->bus);
1630 /* check if the USB transfer callback is already transferring */
1632 if (xfer->flags_int.transferring) {
1635 USB_BUS_LOCK(xfer->xroot->bus);
1636 /* call the USB transfer callback */
1637 usbd_callback_ss_done_defer(xfer);
1638 USB_BUS_UNLOCK(xfer->xroot->bus);
1641 /*------------------------------------------------------------------------*
1642 * usbd_transfer_stop - stop an USB transfer
1644 * NOTE: Calling this function more than one time will only
1645 * result in a single transfer stop.
1646 * NOTE: When this function returns it is not safe to free nor
1647 * reuse any DMA buffers. See "usbd_transfer_drain()".
1648 *------------------------------------------------------------------------*/
1650 usbd_transfer_stop(struct usb_xfer *xfer)
1652 struct usb_endpoint *ep;
1655 /* transfer is gone */
1658 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1660 /* check if the USB transfer was ever opened */
1662 if (!xfer->flags_int.open) {
1663 if (xfer->flags_int.started) {
1664 /* nothing to do except clearing the "started" flag */
1665 /* lock the BUS lock to avoid races updating flags_int */
1666 USB_BUS_LOCK(xfer->xroot->bus);
1667 xfer->flags_int.started = 0;
1668 USB_BUS_UNLOCK(xfer->xroot->bus);
1672 /* try to stop the current USB transfer */
1674 USB_BUS_LOCK(xfer->xroot->bus);
1675 /* override any previous error */
1676 xfer->error = USB_ERR_CANCELLED;
1679 * Clear "open" and "started" when both private and USB lock
1680 * is locked so that we don't get a race updating "flags_int"
1682 xfer->flags_int.open = 0;
1683 xfer->flags_int.started = 0;
1686 * Check if we can cancel the USB transfer immediately.
1688 if (xfer->flags_int.transferring) {
1689 if (xfer->flags_int.can_cancel_immed &&
1690 (!xfer->flags_int.did_close)) {
1693 * The following will lead to an USB_ERR_CANCELLED
1694 * error code being passed to the USB callback.
1696 (xfer->endpoint->methods->close) (xfer);
1697 /* only close once */
1698 xfer->flags_int.did_close = 1;
1700 /* need to wait for the next done callback */
1705 /* close here and now */
1706 (xfer->endpoint->methods->close) (xfer);
1709 * Any additional DMA delay is done by
1710 * "usbd_transfer_unsetup()".
1714 * Special case. Check if we need to restart a blocked
1717 ep = xfer->endpoint;
1720 * If the current USB transfer is completing we need
1721 * to start the next one:
1723 if (ep->endpoint_q.curr == xfer) {
1724 usb_command_wrapper(&ep->endpoint_q, NULL);
1728 USB_BUS_UNLOCK(xfer->xroot->bus);
1731 /*------------------------------------------------------------------------*
1732 * usbd_transfer_pending
1734 * This function will check if an USB transfer is pending which is a
1735 * little bit complicated!
1738 * 1: Pending: The USB transfer will receive a callback in the future.
1739 *------------------------------------------------------------------------*/
1741 usbd_transfer_pending(struct usb_xfer *xfer)
1743 struct usb_xfer_root *info;
1744 struct usb_xfer_queue *pq;
1747 /* transfer is gone */
1750 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1752 if (xfer->flags_int.transferring) {
1756 USB_BUS_LOCK(xfer->xroot->bus);
1757 if (xfer->wait_queue) {
1758 /* we are waiting on a queue somewhere */
1759 USB_BUS_UNLOCK(xfer->xroot->bus);
1765 if (pq->curr == xfer) {
1766 /* we are currently scheduled for callback */
1767 USB_BUS_UNLOCK(xfer->xroot->bus);
1770 /* we are not pending */
1771 USB_BUS_UNLOCK(xfer->xroot->bus);
1775 /*------------------------------------------------------------------------*
1776 * usbd_transfer_drain
1778 * This function will stop the USB transfer and wait for any
1779 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
1780 * are loaded into DMA can safely be freed or reused after that this
1781 * function has returned.
1782 *------------------------------------------------------------------------*/
1784 usbd_transfer_drain(struct usb_xfer *xfer)
1786 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1787 "usbd_transfer_drain can sleep!");
1790 /* transfer is gone */
1793 if (xfer->xroot->xfer_mtx != &Giant) {
1794 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1796 USB_XFER_LOCK(xfer);
1798 usbd_transfer_stop(xfer);
1800 while (usbd_transfer_pending(xfer) ||
1801 xfer->flags_int.doing_callback) {
1804 * It is allowed that the callback can drop its
1805 * transfer mutex. In that case checking only
1806 * "usbd_transfer_pending()" is not enough to tell if
1807 * the USB transfer is fully drained. We also need to
1808 * check the internal "doing_callback" flag.
1810 xfer->flags_int.draining = 1;
1813 * Wait until the current outstanding USB
1814 * transfer is complete !
1816 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
1818 USB_XFER_UNLOCK(xfer);
1821 struct usb_page_cache *
1822 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
1824 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1826 return (&xfer->frbuffers[frindex]);
1830 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
1832 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1834 return (xfer->frlengths[frindex]);
1837 /*------------------------------------------------------------------------*
1838 * usbd_xfer_set_frame_data
1840 * This function sets the pointer of the buffer that should
1841 * loaded directly into DMA for the given USB frame. Passing "ptr"
1842 * equal to NULL while the corresponding "frlength" is greater
1843 * than zero gives undefined results!
1844 *------------------------------------------------------------------------*/
1846 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
1847 void *ptr, usb_frlength_t len)
1849 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1851 /* set virtual address to load and length */
1852 xfer->frbuffers[frindex].buffer = ptr;
1853 usbd_xfer_set_frame_len(xfer, frindex, len);
1857 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
1858 void **ptr, int *len)
1860 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1863 *ptr = xfer->frbuffers[frindex].buffer;
1865 *len = xfer->frlengths[frindex];
1869 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
1873 *actlen = xfer->actlen;
1875 *sumlen = xfer->sumlen;
1876 if (aframes != NULL)
1877 *aframes = xfer->aframes;
1878 if (nframes != NULL)
1879 *nframes = xfer->nframes;
1882 /*------------------------------------------------------------------------*
1883 * usbd_xfer_set_frame_offset
1885 * This function sets the frame data buffer offset relative to the beginning
1886 * of the USB DMA buffer allocated for this USB transfer.
1887 *------------------------------------------------------------------------*/
1889 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
1890 usb_frcount_t frindex)
1892 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
1893 "when the USB buffer is external!\n"));
1894 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1896 /* set virtual address to load */
1897 xfer->frbuffers[frindex].buffer =
1898 USB_ADD_BYTES(xfer->local_buffer, offset);
1902 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
1908 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
1914 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
1920 usbd_xfer_max_frames(struct usb_xfer *xfer)
1922 return (xfer->max_frame_count);
1926 usbd_xfer_max_len(struct usb_xfer *xfer)
1928 return (xfer->max_data_length);
1932 usbd_xfer_max_framelen(struct usb_xfer *xfer)
1934 return (xfer->max_frame_size);
1938 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
1941 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
1943 xfer->frlengths[frindex] = len;
1946 /*------------------------------------------------------------------------*
1947 * usb_callback_proc - factored out code
1949 * This function performs USB callbacks.
1950 *------------------------------------------------------------------------*/
1952 usb_callback_proc(struct usb_proc_msg *_pm)
1954 struct usb_done_msg *pm = (void *)_pm;
1955 struct usb_xfer_root *info = pm->xroot;
1957 /* Change locking order */
1958 USB_BUS_UNLOCK(info->bus);
1961 * We exploit the fact that the mutex is the same for all
1962 * callbacks that will be called from this thread:
1964 mtx_lock(info->xfer_mtx);
1965 USB_BUS_LOCK(info->bus);
1967 /* Continue where we lost track */
1968 usb_command_wrapper(&info->done_q,
1971 mtx_unlock(info->xfer_mtx);
1974 /*------------------------------------------------------------------------*
1975 * usbd_callback_ss_done_defer
1977 * This function will defer the start, stop and done callback to the
1979 *------------------------------------------------------------------------*/
1981 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
1983 struct usb_xfer_root *info = xfer->xroot;
1984 struct usb_xfer_queue *pq = &info->done_q;
1986 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1988 if (pq->curr != xfer) {
1989 usbd_transfer_enqueue(pq, xfer);
1991 if (!pq->recurse_1) {
1994 * We have to postpone the callback due to the fact we
1995 * will have a Lock Order Reversal, LOR, if we try to
1998 if (usb_proc_msignal(info->done_p,
1999 &info->done_m[0], &info->done_m[1])) {
2003 /* clear second recurse flag */
2010 /*------------------------------------------------------------------------*
2011 * usbd_callback_wrapper
2013 * This is a wrapper for USB callbacks. This wrapper does some
2014 * auto-magic things like figuring out if we can call the callback
2015 * directly from the current context or if we need to wakeup the
2016 * interrupt process.
2017 *------------------------------------------------------------------------*/
2019 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2021 struct usb_xfer *xfer = pq->curr;
2022 struct usb_xfer_root *info = xfer->xroot;
2024 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2025 if (!mtx_owned(info->xfer_mtx)) {
2027 * Cases that end up here:
2029 * 5) HW interrupt done callback or other source.
2031 DPRINTFN(3, "case 5\n");
2034 * We have to postpone the callback due to the fact we
2035 * will have a Lock Order Reversal, LOR, if we try to
2038 if (usb_proc_msignal(info->done_p,
2039 &info->done_m[0], &info->done_m[1])) {
2045 * Cases that end up here:
2047 * 1) We are starting a transfer
2048 * 2) We are prematurely calling back a transfer
2049 * 3) We are stopping a transfer
2050 * 4) We are doing an ordinary callback
2052 DPRINTFN(3, "case 1-4\n");
2053 /* get next USB transfer in the queue */
2054 info->done_q.curr = NULL;
2056 /* set flag in case of drain */
2057 xfer->flags_int.doing_callback = 1;
2059 USB_BUS_UNLOCK(info->bus);
2060 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2062 /* set correct USB state for callback */
2063 if (!xfer->flags_int.transferring) {
2064 xfer->usb_state = USB_ST_SETUP;
2065 if (!xfer->flags_int.started) {
2066 /* we got stopped before we even got started */
2067 USB_BUS_LOCK(info->bus);
2072 if (usbd_callback_wrapper_sub(xfer)) {
2073 /* the callback has been deferred */
2074 USB_BUS_LOCK(info->bus);
2078 /* decrement power reference */
2079 usbd_transfer_power_ref(xfer, -1);
2081 xfer->flags_int.transferring = 0;
2084 xfer->usb_state = USB_ST_ERROR;
2086 /* set transferred state */
2087 xfer->usb_state = USB_ST_TRANSFERRED;
2089 /* sync DMA memory, if any */
2090 if (xfer->flags_int.bdma_enable &&
2091 (!xfer->flags_int.bdma_no_post_sync)) {
2092 usb_bdma_post_sync(xfer);
2098 /* call processing routine */
2099 (xfer->callback) (xfer, xfer->error);
2101 /* pickup the USB mutex again */
2102 USB_BUS_LOCK(info->bus);
2105 * Check if we got started after that we got cancelled, but
2106 * before we managed to do the callback.
2108 if ((!xfer->flags_int.open) &&
2109 (xfer->flags_int.started) &&
2110 (xfer->usb_state == USB_ST_ERROR)) {
2111 /* clear flag in case of drain */
2112 xfer->flags_int.doing_callback = 0;
2113 /* try to loop, but not recursivly */
2114 usb_command_wrapper(&info->done_q, xfer);
2119 /* clear flag in case of drain */
2120 xfer->flags_int.doing_callback = 0;
2123 * Check if we are draining.
2125 if (xfer->flags_int.draining &&
2126 (!xfer->flags_int.transferring)) {
2127 /* "usbd_transfer_drain()" is waiting for end of transfer */
2128 xfer->flags_int.draining = 0;
2129 cv_broadcast(&info->cv_drain);
2132 /* do the next callback, if any */
2133 usb_command_wrapper(&info->done_q,
2137 /*------------------------------------------------------------------------*
2138 * usb_dma_delay_done_cb
2140 * This function is called when the DMA delay has been exectuded, and
2141 * will make sure that the callback is called to complete the USB
2142 * transfer. This code path is ususally only used when there is an USB
2143 * error like USB_ERR_CANCELLED.
2144 *------------------------------------------------------------------------*/
2146 usb_dma_delay_done_cb(void *arg)
2148 struct usb_xfer *xfer = arg;
2150 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2152 DPRINTFN(3, "Completed %p\n", xfer);
2154 /* queue callback for execution, again */
2155 usbd_transfer_done(xfer, 0);
2158 /*------------------------------------------------------------------------*
2159 * usbd_transfer_dequeue
2161 * - This function is used to remove an USB transfer from a USB
2164 * - This function can be called multiple times in a row.
2165 *------------------------------------------------------------------------*/
2167 usbd_transfer_dequeue(struct usb_xfer *xfer)
2169 struct usb_xfer_queue *pq;
2171 pq = xfer->wait_queue;
2173 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2174 xfer->wait_queue = NULL;
2178 /*------------------------------------------------------------------------*
2179 * usbd_transfer_enqueue
2181 * - This function is used to insert an USB transfer into a USB *
2184 * - This function can be called multiple times in a row.
2185 *------------------------------------------------------------------------*/
2187 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2190 * Insert the USB transfer into the queue, if it is not
2191 * already on a USB transfer queue:
2193 if (xfer->wait_queue == NULL) {
2194 xfer->wait_queue = pq;
2195 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2199 /*------------------------------------------------------------------------*
2200 * usbd_transfer_done
2202 * - This function is used to remove an USB transfer from the busdma,
2203 * pipe or interrupt queue.
2205 * - This function is used to queue the USB transfer on the done
2208 * - This function is used to stop any USB transfer timeouts.
2209 *------------------------------------------------------------------------*/
2211 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2213 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2215 DPRINTF("err=%s\n", usbd_errstr(error));
2218 * If we are not transferring then just return.
2219 * This can happen during transfer cancel.
2221 if (!xfer->flags_int.transferring) {
2222 DPRINTF("not transferring\n");
2223 /* end of control transfer, if any */
2224 xfer->flags_int.control_act = 0;
2227 /* only set transfer error if not already set */
2229 xfer->error = error;
2231 /* stop any callouts */
2232 usb_callout_stop(&xfer->timeout_handle);
2235 * If we are waiting on a queue, just remove the USB transfer
2236 * from the queue, if any. We should have the required locks
2237 * locked to do the remove when this function is called.
2239 usbd_transfer_dequeue(xfer);
2242 if (mtx_owned(xfer->xroot->xfer_mtx)) {
2243 struct usb_xfer_queue *pq;
2246 * If the private USB lock is not locked, then we assume
2247 * that the BUS-DMA load stage has been passed:
2249 pq = &xfer->xroot->dma_q;
2251 if (pq->curr == xfer) {
2252 /* start the next BUS-DMA load, if any */
2253 usb_command_wrapper(pq, NULL);
2257 /* keep some statistics */
2259 xfer->xroot->bus->stats_err.uds_requests
2260 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2262 xfer->xroot->bus->stats_ok.uds_requests
2263 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2266 /* call the USB transfer callback */
2267 usbd_callback_ss_done_defer(xfer);
2270 /*------------------------------------------------------------------------*
2271 * usbd_transfer_start_cb
2273 * This function is called to start the USB transfer when
2274 * "xfer->interval" is greater than zero, and and the endpoint type is
2276 *------------------------------------------------------------------------*/
2278 usbd_transfer_start_cb(void *arg)
2280 struct usb_xfer *xfer = arg;
2281 struct usb_endpoint *ep = xfer->endpoint;
2283 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2287 /* start the transfer */
2288 (ep->methods->start) (xfer);
2290 xfer->flags_int.can_cancel_immed = 1;
2292 /* check for error */
2294 /* some error has happened */
2295 usbd_transfer_done(xfer, 0);
2299 /*------------------------------------------------------------------------*
2300 * usbd_xfer_set_stall
2302 * This function is used to set the stall flag outside the
2303 * callback. This function is NULL safe.
2304 *------------------------------------------------------------------------*/
2306 usbd_xfer_set_stall(struct usb_xfer *xfer)
2312 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2314 /* avoid any races by locking the USB mutex */
2315 USB_BUS_LOCK(xfer->xroot->bus);
2316 xfer->flags.stall_pipe = 1;
2317 USB_BUS_UNLOCK(xfer->xroot->bus);
2321 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2323 return (xfer->endpoint->is_stalled);
2326 /*------------------------------------------------------------------------*
2327 * usbd_transfer_clear_stall
2329 * This function is used to clear the stall flag outside the
2330 * callback. This function is NULL safe.
2331 *------------------------------------------------------------------------*/
2333 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2339 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2341 /* avoid any races by locking the USB mutex */
2342 USB_BUS_LOCK(xfer->xroot->bus);
2344 xfer->flags.stall_pipe = 0;
2346 USB_BUS_UNLOCK(xfer->xroot->bus);
2349 /*------------------------------------------------------------------------*
2352 * This function is used to add an USB transfer to the pipe transfer list.
2353 *------------------------------------------------------------------------*/
2355 usbd_pipe_start(struct usb_xfer_queue *pq)
2357 struct usb_endpoint *ep;
2358 struct usb_xfer *xfer;
2362 ep = xfer->endpoint;
2364 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2367 * If the endpoint is already stalled we do nothing !
2369 if (ep->is_stalled) {
2373 * Check if we are supposed to stall the endpoint:
2375 if (xfer->flags.stall_pipe) {
2376 /* clear stall command */
2377 xfer->flags.stall_pipe = 0;
2380 * Only stall BULK and INTERRUPT endpoints.
2382 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2383 if ((type == UE_BULK) ||
2384 (type == UE_INTERRUPT)) {
2385 struct usb_device *udev;
2386 struct usb_xfer_root *info;
2393 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2394 (udev->bus->methods->set_stall) (
2395 udev, NULL, ep, &did_stall);
2396 } else if (udev->default_xfer[1]) {
2397 info = udev->default_xfer[1]->xroot;
2399 &info->bus->non_giant_callback_proc,
2400 &udev->cs_msg[0], &udev->cs_msg[1]);
2402 /* should not happen */
2403 DPRINTFN(0, "No stall handler!\n");
2406 * Check if we should stall. Some USB hardware
2407 * handles set- and clear-stall in hardware.
2411 * The transfer will be continued when
2412 * the clear-stall control endpoint
2413 * message is received.
2420 /* Set or clear stall complete - special case */
2421 if (xfer->nframes == 0) {
2422 /* we are complete */
2424 usbd_transfer_done(xfer, 0);
2430 * 1) Start the first transfer queued.
2432 * 2) Re-start the current USB transfer.
2435 * Check if there should be any
2436 * pre transfer start delay:
2438 if (xfer->interval > 0) {
2439 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2440 if ((type == UE_BULK) ||
2441 (type == UE_CONTROL)) {
2442 usbd_transfer_timeout_ms(xfer,
2443 &usbd_transfer_start_cb,
2450 /* start USB transfer */
2451 (ep->methods->start) (xfer);
2453 xfer->flags_int.can_cancel_immed = 1;
2455 /* check for error */
2457 /* some error has happened */
2458 usbd_transfer_done(xfer, 0);
2462 /*------------------------------------------------------------------------*
2463 * usbd_transfer_timeout_ms
2465 * This function is used to setup a timeout on the given USB
2466 * transfer. If the timeout has been deferred the callback given by
2467 * "cb" will get called after "ms" milliseconds.
2468 *------------------------------------------------------------------------*/
2470 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2471 void (*cb) (void *arg), usb_timeout_t ms)
2473 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2476 usb_callout_reset(&xfer->timeout_handle,
2477 USB_MS_TO_TICKS(ms), cb, xfer);
2480 /*------------------------------------------------------------------------*
2481 * usbd_callback_wrapper_sub
2483 * - This function will update variables in an USB transfer after
2484 * that the USB transfer is complete.
2486 * - This function is used to start the next USB transfer on the
2487 * ep transfer queue, if any.
2489 * NOTE: In some special cases the USB transfer will not be removed from
2490 * the pipe queue, but remain first. To enforce USB transfer removal call
2491 * this function passing the error code "USB_ERR_CANCELLED".
2495 * Else: The callback has been deferred.
2496 *------------------------------------------------------------------------*/
2498 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2500 struct usb_endpoint *ep;
2503 if ((!xfer->flags_int.open) &&
2504 (!xfer->flags_int.did_close)) {
2506 USB_BUS_LOCK(xfer->xroot->bus);
2507 (xfer->endpoint->methods->close) (xfer);
2508 USB_BUS_UNLOCK(xfer->xroot->bus);
2509 /* only close once */
2510 xfer->flags_int.did_close = 1;
2511 return (1); /* wait for new callback */
2514 * If we have a non-hardware induced error we
2515 * need to do the DMA delay!
2517 if (((xfer->error == USB_ERR_CANCELLED) ||
2518 (xfer->error == USB_ERR_TIMEOUT)) &&
2519 (!xfer->flags_int.did_dma_delay)) {
2523 /* only delay once */
2524 xfer->flags_int.did_dma_delay = 1;
2526 /* we can not cancel this delay */
2527 xfer->flags_int.can_cancel_immed = 0;
2529 temp = usbd_get_dma_delay(xfer->xroot->bus);
2531 DPRINTFN(3, "DMA delay, %u ms, "
2532 "on %p\n", temp, xfer);
2535 USB_BUS_LOCK(xfer->xroot->bus);
2536 usbd_transfer_timeout_ms(xfer,
2537 &usb_dma_delay_done_cb, temp);
2538 USB_BUS_UNLOCK(xfer->xroot->bus);
2539 return (1); /* wait for new callback */
2542 /* check actual number of frames */
2543 if (xfer->aframes > xfer->nframes) {
2544 if (xfer->error == 0) {
2545 panic("%s: actual number of frames, %d, is "
2546 "greater than initial number of frames, %d!\n",
2547 __FUNCTION__, xfer->aframes, xfer->nframes);
2549 /* just set some valid value */
2550 xfer->aframes = xfer->nframes;
2553 /* compute actual length */
2556 for (x = 0; x != xfer->aframes; x++) {
2557 xfer->actlen += xfer->frlengths[x];
2561 * Frames that were not transferred get zero actual length in
2562 * case the USB device driver does not check the actual number
2563 * of frames transferred, "xfer->aframes":
2565 for (; x < xfer->nframes; x++) {
2566 usbd_xfer_set_frame_len(xfer, x, 0);
2569 /* check actual length */
2570 if (xfer->actlen > xfer->sumlen) {
2571 if (xfer->error == 0) {
2572 panic("%s: actual length, %d, is greater than "
2573 "initial length, %d!\n",
2574 __FUNCTION__, xfer->actlen, xfer->sumlen);
2576 /* just set some valid value */
2577 xfer->actlen = xfer->sumlen;
2580 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2581 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2582 xfer->aframes, xfer->nframes);
2585 /* end of control transfer, if any */
2586 xfer->flags_int.control_act = 0;
2588 /* check if we should block the execution queue */
2589 if ((xfer->error != USB_ERR_CANCELLED) &&
2590 (xfer->flags.pipe_bof)) {
2591 DPRINTFN(2, "xfer=%p: Block On Failure "
2592 "on endpoint=%p\n", xfer, xfer->endpoint);
2596 /* check for short transfers */
2597 if (xfer->actlen < xfer->sumlen) {
2599 /* end of control transfer, if any */
2600 xfer->flags_int.control_act = 0;
2602 if (!xfer->flags_int.short_xfer_ok) {
2603 xfer->error = USB_ERR_SHORT_XFER;
2604 if (xfer->flags.pipe_bof) {
2605 DPRINTFN(2, "xfer=%p: Block On Failure on "
2606 "Short Transfer on endpoint %p.\n",
2607 xfer, xfer->endpoint);
2613 * Check if we are in the middle of a
2616 if (xfer->flags_int.control_act) {
2617 DPRINTFN(5, "xfer=%p: Control transfer "
2618 "active on endpoint=%p\n", xfer, xfer->endpoint);
2624 ep = xfer->endpoint;
2627 * If the current USB transfer is completing we need to start the
2630 USB_BUS_LOCK(xfer->xroot->bus);
2631 if (ep->endpoint_q.curr == xfer) {
2632 usb_command_wrapper(&ep->endpoint_q, NULL);
2634 if (ep->endpoint_q.curr || TAILQ_FIRST(&ep->endpoint_q.head)) {
2635 /* there is another USB transfer waiting */
2637 /* this is the last USB transfer */
2638 /* clear isochronous sync flag */
2639 xfer->endpoint->is_synced = 0;
2642 USB_BUS_UNLOCK(xfer->xroot->bus);
2647 /*------------------------------------------------------------------------*
2648 * usb_command_wrapper
2650 * This function is used to execute commands non-recursivly on an USB
2652 *------------------------------------------------------------------------*/
2654 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2658 * If the transfer is not already processing,
2661 if (pq->curr != xfer) {
2662 usbd_transfer_enqueue(pq, xfer);
2663 if (pq->curr != NULL) {
2664 /* something is already processing */
2665 DPRINTFN(6, "busy %p\n", pq->curr);
2670 /* Get next element in queue */
2674 if (!pq->recurse_1) {
2678 /* set both recurse flags */
2682 if (pq->curr == NULL) {
2683 xfer = TAILQ_FIRST(&pq->head);
2685 TAILQ_REMOVE(&pq->head, xfer,
2687 xfer->wait_queue = NULL;
2693 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
2695 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
2697 } while (!pq->recurse_2);
2699 /* clear first recurse flag */
2703 /* clear second recurse flag */
2708 /*------------------------------------------------------------------------*
2709 * usbd_default_transfer_setup
2711 * This function is used to setup the default USB control endpoint
2713 *------------------------------------------------------------------------*/
2715 usbd_default_transfer_setup(struct usb_device *udev)
2717 struct usb_xfer *xfer;
2719 uint8_t iface_index;
2721 /* check for root HUB */
2722 if (udev->parent_hub == NULL)
2726 xfer = udev->default_xfer[0];
2728 USB_XFER_LOCK(xfer);
2730 ((xfer->address == udev->address) &&
2731 (udev->default_ep_desc.wMaxPacketSize[0] ==
2732 udev->ddesc.bMaxPacketSize));
2733 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2736 * NOTE: checking "xfer->address" and
2737 * starting the USB transfer must be
2740 usbd_transfer_start(xfer);
2743 USB_XFER_UNLOCK(xfer);
2750 * All parameters are exactly the same like before.
2756 * Update wMaxPacketSize for the default control endpoint:
2758 udev->default_ep_desc.wMaxPacketSize[0] =
2759 udev->ddesc.bMaxPacketSize;
2762 * Unsetup any existing USB transfer:
2764 usbd_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
2767 * Try to setup a new USB transfer for the
2768 * default control endpoint:
2771 if (usbd_transfer_setup(udev, &iface_index,
2772 udev->default_xfer, usb_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL,
2773 udev->default_mtx)) {
2774 DPRINTFN(0, "could not setup default "
2781 /*------------------------------------------------------------------------*
2782 * usbd_clear_data_toggle - factored out code
2784 * NOTE: the intention of this function is not to reset the hardware
2786 *------------------------------------------------------------------------*/
2788 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
2790 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
2792 USB_BUS_LOCK(udev->bus);
2793 ep->toggle_next = 0;
2794 USB_BUS_UNLOCK(udev->bus);
2797 /*------------------------------------------------------------------------*
2798 * usbd_clear_stall_callback - factored out clear stall callback
2801 * xfer1: Clear Stall Control Transfer
2802 * xfer2: Stalled USB Transfer
2804 * This function is NULL safe.
2810 * Clear stall config example:
2812 * static const struct usb_config my_clearstall = {
2813 * .type = UE_CONTROL,
2815 * .direction = UE_DIR_ANY,
2816 * .interval = 50, //50 milliseconds
2817 * .bufsize = sizeof(struct usb_device_request),
2818 * .timeout = 1000, //1.000 seconds
2819 * .callback = &my_clear_stall_callback, // **
2820 * .usb_mode = USB_MODE_HOST,
2823 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
2824 * passing the correct parameters.
2825 *------------------------------------------------------------------------*/
2827 usbd_clear_stall_callback(struct usb_xfer *xfer1,
2828 struct usb_xfer *xfer2)
2830 struct usb_device_request req;
2832 if (xfer2 == NULL) {
2833 /* looks like we are tearing down */
2834 DPRINTF("NULL input parameter\n");
2837 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
2838 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
2840 switch (USB_GET_STATE(xfer1)) {
2844 * pre-clear the data toggle to DATA0 ("umass.c" and
2845 * "ata-usb.c" depends on this)
2848 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
2850 /* setup a clear-stall packet */
2852 req.bmRequestType = UT_WRITE_ENDPOINT;
2853 req.bRequest = UR_CLEAR_FEATURE;
2854 USETW(req.wValue, UF_ENDPOINT_HALT);
2855 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
2857 USETW(req.wLength, 0);
2860 * "usbd_transfer_setup_sub()" will ensure that
2861 * we have sufficient room in the buffer for
2862 * the request structure!
2865 /* copy in the transfer */
2867 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
2870 xfer1->frlengths[0] = sizeof(req);
2873 usbd_transfer_submit(xfer1);
2876 case USB_ST_TRANSFERRED:
2879 default: /* Error */
2880 if (xfer1->error == USB_ERR_CANCELLED) {
2885 return (1); /* Clear Stall Finished */
2888 /*------------------------------------------------------------------------*
2889 * usbd_transfer_poll
2891 * The following function gets called from the USB keyboard driver and
2892 * UMASS when the system has paniced.
2894 * NOTE: It is currently not possible to resume normal operation on
2895 * the USB controller which has been polled, due to clearing of the
2896 * "up_dsleep" and "up_msleep" flags.
2897 *------------------------------------------------------------------------*/
2899 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
2901 struct usb_xfer *xfer;
2902 struct usb_xfer_root *xroot;
2903 struct usb_device *udev;
2904 struct usb_proc_msg *pm;
2909 for (n = 0; n != max; n++) {
2910 /* Extra checks to avoid panic */
2913 continue; /* no USB transfer */
2914 xroot = xfer->xroot;
2916 continue; /* no USB root */
2919 continue; /* no USB device */
2920 if (udev->bus == NULL)
2921 continue; /* no BUS structure */
2922 if (udev->bus->methods == NULL)
2923 continue; /* no BUS methods */
2924 if (udev->bus->methods->xfer_poll == NULL)
2925 continue; /* no poll method */
2927 /* make sure that the BUS mutex is not locked */
2929 while (mtx_owned(&xroot->udev->bus->bus_mtx)) {
2930 mtx_unlock(&xroot->udev->bus->bus_mtx);
2934 /* make sure that the transfer mutex is not locked */
2936 while (mtx_owned(xroot->xfer_mtx)) {
2937 mtx_unlock(xroot->xfer_mtx);
2941 /* Make sure cv_signal() and cv_broadcast() is not called */
2942 udev->bus->control_xfer_proc.up_msleep = 0;
2943 udev->bus->explore_proc.up_msleep = 0;
2944 udev->bus->giant_callback_proc.up_msleep = 0;
2945 udev->bus->non_giant_callback_proc.up_msleep = 0;
2947 /* poll USB hardware */
2948 (udev->bus->methods->xfer_poll) (udev->bus);
2950 USB_BUS_LOCK(xroot->bus);
2952 /* check for clear stall */
2953 if (udev->default_xfer[1] != NULL) {
2955 /* poll clear stall start */
2956 pm = &udev->cs_msg[0].hdr;
2957 (pm->pm_callback) (pm);
2958 /* poll clear stall done thread */
2959 pm = &udev->default_xfer[1]->
2960 xroot->done_m[0].hdr;
2961 (pm->pm_callback) (pm);
2964 /* poll done thread */
2965 pm = &xroot->done_m[0].hdr;
2966 (pm->pm_callback) (pm);
2968 USB_BUS_UNLOCK(xroot->bus);
2970 /* restore transfer mutex */
2972 mtx_lock(xroot->xfer_mtx);
2974 /* restore BUS mutex */
2976 mtx_lock(&xroot->udev->bus->bus_mtx);
2981 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
2982 uint8_t type, enum usb_dev_speed speed)
2984 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
2985 [USB_SPEED_LOW] = 8,
2986 [USB_SPEED_FULL] = 64,
2987 [USB_SPEED_HIGH] = 1024,
2988 [USB_SPEED_VARIABLE] = 1024,
2989 [USB_SPEED_SUPER] = 1024,
2992 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
2993 [USB_SPEED_LOW] = 0, /* invalid */
2994 [USB_SPEED_FULL] = 1023,
2995 [USB_SPEED_HIGH] = 1024,
2996 [USB_SPEED_VARIABLE] = 3584,
2997 [USB_SPEED_SUPER] = 1024,
3000 static const uint16_t control_min[USB_SPEED_MAX] = {
3001 [USB_SPEED_LOW] = 8,
3002 [USB_SPEED_FULL] = 8,
3003 [USB_SPEED_HIGH] = 64,
3004 [USB_SPEED_VARIABLE] = 512,
3005 [USB_SPEED_SUPER] = 512,
3008 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3009 [USB_SPEED_LOW] = 0, /* not supported */
3010 [USB_SPEED_FULL] = 8,
3011 [USB_SPEED_HIGH] = 512,
3012 [USB_SPEED_VARIABLE] = 512,
3013 [USB_SPEED_SUPER] = 1024,
3018 memset(ptr, 0, sizeof(*ptr));
3022 ptr->range.max = intr_range_max[speed];
3024 case UE_ISOCHRONOUS:
3025 ptr->range.max = isoc_range_max[speed];
3028 if (type == UE_BULK)
3029 temp = bulk_min[speed];
3030 else /* UE_CONTROL */
3031 temp = control_min[speed];
3033 /* default is fixed */
3034 ptr->fixed[0] = temp;
3035 ptr->fixed[1] = temp;
3036 ptr->fixed[2] = temp;
3037 ptr->fixed[3] = temp;
3039 if (speed == USB_SPEED_FULL) {
3040 /* multiple sizes */
3045 if ((speed == USB_SPEED_VARIABLE) &&
3046 (type == UE_BULK)) {
3047 /* multiple sizes */
3048 ptr->fixed[2] = 1024;
3049 ptr->fixed[3] = 1536;
3056 usbd_xfer_softc(struct usb_xfer *xfer)
3058 return (xfer->priv_sc);
3062 usbd_xfer_get_priv(struct usb_xfer *xfer)
3064 return (xfer->priv_fifo);
3068 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3070 xfer->priv_fifo = ptr;
3074 usbd_xfer_state(struct usb_xfer *xfer)
3076 return (xfer->usb_state);
3080 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3083 case USB_FORCE_SHORT_XFER:
3084 xfer->flags.force_short_xfer = 1;
3086 case USB_SHORT_XFER_OK:
3087 xfer->flags.short_xfer_ok = 1;
3089 case USB_MULTI_SHORT_OK:
3090 xfer->flags.short_frames_ok = 1;
3092 case USB_MANUAL_STATUS:
3093 xfer->flags.manual_status = 1;
3099 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3102 case USB_FORCE_SHORT_XFER:
3103 xfer->flags.force_short_xfer = 0;
3105 case USB_SHORT_XFER_OK:
3106 xfer->flags.short_xfer_ok = 0;
3108 case USB_MULTI_SHORT_OK:
3109 xfer->flags.short_frames_ok = 0;
3111 case USB_MANUAL_STATUS:
3112 xfer->flags.manual_status = 0;
3118 * The following function returns in milliseconds when the isochronous
3119 * transfer was completed by the hardware. The returned value wraps
3120 * around 65536 milliseconds.
3123 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3125 return (xfer->isoc_time_complete);