kernel: Remove <sys/mutex.h> from all files that don't need it (2/2).
[dragonfly.git] / sys / bus / u4b / usb_transfer.c
blob5a5d4fd90d6f621e1718c5e796e052bb0788a003
1 /* $FreeBSD: head/sys/dev/usb/usb_transfer.c 276717 2015-01-05 20:22:18Z hselasky $ */
2 /*-
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
7 * are met:
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
24 * SUCH DAMAGE.
25 */
27 #include <sys/stdint.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/thread.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/unistd.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
45 #include <sys/thread2.h>
47 #include <bus/u4b/usb.h>
48 #include <bus/u4b/usbdi.h>
49 #include <bus/u4b/usbdi_util.h>
51 #define USB_DEBUG_VAR usb_debug
53 #include <bus/u4b/usb_core.h>
54 #include <bus/u4b/usb_busdma.h>
55 #include <bus/u4b/usb_process.h>
56 #include <bus/u4b/usb_transfer.h>
57 #include <bus/u4b/usb_device.h>
58 #include <bus/u4b/usb_debug.h>
59 #include <bus/u4b/usb_util.h>
61 #include <bus/u4b/usb_controller.h>
62 #include <bus/u4b/usb_bus.h>
63 #include <bus/u4b/usb_pf.h>
65 struct usb_std_packet_size {
66 struct {
67 uint16_t min; /* inclusive */
68 uint16_t max; /* inclusive */
69 } range;
71 uint16_t fixed[4];
74 static usb_callback_t usb_request_callback;
76 static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
78 /* This transfer is used for generic control endpoint transfers */
80 [0] = {
81 .type = UE_CONTROL,
82 .endpoint = 0x00, /* Control endpoint */
83 .direction = UE_DIR_ANY,
84 .bufsize = USB_EP0_BUFSIZE, /* bytes */
85 .flags = {.proxy_buffer = 1,},
86 .callback = &usb_request_callback,
87 .usb_mode = USB_MODE_DUAL, /* both modes */
90 /* This transfer is used for generic clear stall only */
92 [1] = {
93 .type = UE_CONTROL,
94 .endpoint = 0x00, /* Control pipe */
95 .direction = UE_DIR_ANY,
96 .bufsize = sizeof(struct usb_device_request),
97 .callback = &usb_do_clear_stall_callback,
98 .timeout = 1000, /* 1 second */
99 .interval = 50, /* 50ms */
100 .usb_mode = USB_MODE_HOST,
104 /* function prototypes */
106 static void usbd_update_max_frame_size(struct usb_xfer *);
107 static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
108 static void usbd_delayed_free(void *data, struct malloc_type *mtype);
109 static void usbd_control_transfer_init(struct usb_xfer *);
110 static int usbd_setup_ctrl_transfer(struct usb_xfer *);
111 static void usb_callback_proc(struct usb_proc_msg *);
112 static void usbd_callback_ss_done_defer(struct usb_xfer *);
113 static void usbd_callback_wrapper(struct usb_xfer_queue *);
114 static void usbd_transfer_start_cb(void *);
115 static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *);
116 static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
117 uint8_t type, enum usb_dev_speed speed);
119 /*------------------------------------------------------------------------*
120 * usb_request_callback
121 *------------------------------------------------------------------------*/
122 static void
123 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
125 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
126 usb_handle_request_callback(xfer, error);
127 else
128 usbd_do_request_callback(xfer, error);
131 /*------------------------------------------------------------------------*
132 * usbd_update_max_frame_size
134 * This function updates the maximum frame size, hence high speed USB
135 * can transfer multiple consecutive packets.
136 *------------------------------------------------------------------------*/
137 static void
138 usbd_update_max_frame_size(struct usb_xfer *xfer)
140 /* compute maximum frame size */
141 /* this computation should not overflow 16-bit */
142 /* max = 15 * 1024 */
144 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
147 /*------------------------------------------------------------------------*
148 * usbd_get_dma_delay
150 * The following function is called when we need to
151 * synchronize with DMA hardware.
153 * Returns:
154 * 0: no DMA delay required
155 * Else: milliseconds of DMA delay
156 *------------------------------------------------------------------------*/
157 usb_timeout_t
158 usbd_get_dma_delay(struct usb_device *udev)
160 const struct usb_bus_methods *mtod;
161 uint32_t temp;
163 mtod = udev->bus->methods;
164 temp = 0;
166 if (mtod->get_dma_delay) {
167 (mtod->get_dma_delay) (udev, &temp);
169 * Round up and convert to milliseconds. Note that we use
170 * 1024 milliseconds per second. to save a division.
172 temp += 0x3FF;
173 temp /= 0x400;
175 return (temp);
178 /*------------------------------------------------------------------------*
179 * usbd_transfer_setup_sub_malloc
181 * This function will allocate one or more DMA'able memory chunks
182 * according to "size", "align" and "count" arguments. "ppc" is
183 * pointed to a linear array of USB page caches afterwards.
185 * If the "align" argument is equal to "1" a non-contiguous allocation
186 * can happen. Else if the "align" argument is greater than "1", the
187 * allocation will always be contiguous in memory.
189 * Returns:
190 * 0: Success
191 * Else: Failure
192 *------------------------------------------------------------------------*/
193 #if USB_HAVE_BUSDMA
194 uint8_t
195 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
196 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
197 usb_size_t count)
199 struct usb_page_cache *pc;
200 struct usb_page *pg;
201 void *buf;
202 usb_size_t n_dma_pc;
203 usb_size_t n_dma_pg;
204 usb_size_t n_obj;
205 usb_size_t x;
206 usb_size_t y;
207 usb_size_t r;
208 usb_size_t z;
210 USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
211 align));
212 USB_ASSERT(size > 0, ("Invalid size = 0\n"));
214 if (count == 0) {
215 return (0); /* nothing to allocate */
218 * Make sure that the size is aligned properly.
220 size = -((-size) & (-align));
223 * Try multi-allocation chunks to reduce the number of DMA
224 * allocations, hence DMA allocations are slow.
226 if (align == 1) {
227 /* special case - non-cached multi page DMA memory */
228 n_dma_pc = count;
229 n_dma_pg = (2 + (size / USB_PAGE_SIZE));
230 n_obj = 1;
231 } else if (size >= USB_PAGE_SIZE) {
232 n_dma_pc = count;
233 n_dma_pg = 1;
234 n_obj = 1;
235 } else {
236 /* compute number of objects per page */
237 #ifdef USB_DMA_SINGLE_ALLOC
238 n_obj = 1;
239 #else
240 n_obj = (USB_PAGE_SIZE / size);
241 #endif
243 * Compute number of DMA chunks, rounded up
244 * to nearest one:
246 n_dma_pc = ((count + n_obj - 1) / n_obj);
247 n_dma_pg = 1;
251 * DMA memory is allocated once, but mapped twice. That's why
252 * there is one list for auto-free and another list for
253 * non-auto-free which only holds the mapping and not the
254 * allocation.
256 if (parm->buf == NULL) {
257 /* reserve memory (auto-free) */
258 parm->dma_page_ptr += n_dma_pc * n_dma_pg;
259 parm->dma_page_cache_ptr += n_dma_pc;
261 /* reserve memory (no-auto-free) */
262 parm->dma_page_ptr += count * n_dma_pg;
263 parm->xfer_page_cache_ptr += count;
264 return (0);
266 for (x = 0; x != n_dma_pc; x++) {
267 /* need to initialize the page cache */
268 parm->dma_page_cache_ptr[x].tag_parent =
269 &parm->curr_xfer->xroot->dma_parent_tag;
271 for (x = 0; x != count; x++) {
272 /* need to initialize the page cache */
273 parm->xfer_page_cache_ptr[x].tag_parent =
274 &parm->curr_xfer->xroot->dma_parent_tag;
277 if (ppc != NULL) {
278 if (n_obj != 1)
279 *ppc = parm->xfer_page_cache_ptr;
280 else
281 *ppc = parm->dma_page_cache_ptr;
283 r = count; /* set remainder count */
284 z = n_obj * size; /* set allocation size */
285 pc = parm->xfer_page_cache_ptr;
286 pg = parm->dma_page_ptr;
288 if (n_obj == 1) {
290 * Avoid mapping memory twice if only a single object
291 * should be allocated per page cache:
293 for (x = 0; x != n_dma_pc; x++) {
294 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
295 pg, z, align)) {
296 return (1); /* failure */
298 /* Make room for one DMA page cache and "n_dma_pg" pages */
299 parm->dma_page_cache_ptr++;
300 pg += n_dma_pg;
302 } else {
303 for (x = 0; x != n_dma_pc; x++) {
305 if (r < n_obj) {
306 /* compute last remainder */
307 z = r * size;
308 n_obj = r;
310 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
311 pg, z, align)) {
312 return (1); /* failure */
314 /* Set beginning of current buffer */
315 buf = parm->dma_page_cache_ptr->buffer;
316 /* Make room for one DMA page cache and "n_dma_pg" pages */
317 parm->dma_page_cache_ptr++;
318 pg += n_dma_pg;
320 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
322 /* Load sub-chunk into DMA */
323 if (usb_pc_dmamap_create(pc, size)) {
324 return (1); /* failure */
326 pc->buffer = USB_ADD_BYTES(buf, y * size);
327 pc->page_start = pg;
329 lockmgr(pc->tag_parent->lock, LK_EXCLUSIVE);
330 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
331 lockmgr(pc->tag_parent->lock, LK_RELEASE);
332 return (1); /* failure */
334 lockmgr(pc->tag_parent->lock, LK_RELEASE);
339 parm->xfer_page_cache_ptr = pc;
340 parm->dma_page_ptr = pg;
341 return (0);
343 #endif
345 /*------------------------------------------------------------------------*
346 * usbd_transfer_setup_sub - transfer setup subroutine
348 * This function must be called from the "xfer_setup" callback of the
349 * USB Host or Device controller driver when setting up an USB
350 * transfer. This function will setup correct packet sizes, buffer
351 * sizes, flags and more, that are stored in the "usb_xfer"
352 * structure.
353 *------------------------------------------------------------------------*/
354 void
355 usbd_transfer_setup_sub(struct usb_setup_params *parm)
357 enum {
358 REQ_SIZE = 8,
359 MIN_PKT = 8,
361 struct usb_xfer *xfer = parm->curr_xfer;
362 const struct usb_config *setup = parm->curr_setup;
363 struct usb_endpoint_ss_comp_descriptor *ecomp;
364 struct usb_endpoint_descriptor *edesc;
365 struct usb_std_packet_size std_size;
366 usb_frcount_t n_frlengths;
367 usb_frcount_t n_frbuffers;
368 usb_frcount_t x;
369 uint16_t maxp_old;
370 uint8_t type;
371 uint8_t zmps;
374 * Sanity check. The following parameters must be initialized before
375 * calling this function.
377 if ((parm->hc_max_packet_size == 0) ||
378 (parm->hc_max_packet_count == 0) ||
379 (parm->hc_max_frame_size == 0)) {
380 parm->err = USB_ERR_INVAL;
381 goto done;
383 edesc = xfer->endpoint->edesc;
384 ecomp = xfer->endpoint->ecomp;
386 type = (edesc->bmAttributes & UE_XFERTYPE);
388 xfer->flags = setup->flags;
389 xfer->nframes = setup->frames;
390 xfer->timeout = setup->timeout;
391 xfer->callback = setup->callback;
392 xfer->interval = setup->interval;
393 xfer->endpointno = edesc->bEndpointAddress;
394 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
395 xfer->max_packet_count = 1;
396 /* make a shadow copy: */
397 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
399 parm->bufsize = setup->bufsize;
401 switch (parm->speed) {
402 case USB_SPEED_HIGH:
403 switch (type) {
404 case UE_ISOCHRONOUS:
405 case UE_INTERRUPT:
406 xfer->max_packet_count +=
407 (xfer->max_packet_size >> 11) & 3;
409 /* check for invalid max packet count */
410 if (xfer->max_packet_count > 3)
411 xfer->max_packet_count = 3;
412 break;
413 default:
414 break;
416 xfer->max_packet_size &= 0x7FF;
417 break;
418 case USB_SPEED_SUPER:
419 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
421 if (ecomp != NULL)
422 xfer->max_packet_count += ecomp->bMaxBurst;
424 if ((xfer->max_packet_count == 0) ||
425 (xfer->max_packet_count > 16))
426 xfer->max_packet_count = 16;
428 switch (type) {
429 case UE_CONTROL:
430 xfer->max_packet_count = 1;
431 break;
432 case UE_ISOCHRONOUS:
433 if (ecomp != NULL) {
434 uint8_t mult;
436 mult = UE_GET_SS_ISO_MULT(
437 ecomp->bmAttributes) + 1;
438 if (mult > 3)
439 mult = 3;
441 xfer->max_packet_count *= mult;
443 break;
444 default:
445 break;
447 xfer->max_packet_size &= 0x7FF;
448 break;
449 default:
450 break;
452 /* range check "max_packet_count" */
454 if (xfer->max_packet_count > parm->hc_max_packet_count) {
455 xfer->max_packet_count = parm->hc_max_packet_count;
458 /* store max packet size value before filtering */
460 maxp_old = xfer->max_packet_size;
462 /* filter "wMaxPacketSize" according to HC capabilities */
464 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
465 (xfer->max_packet_size == 0)) {
466 xfer->max_packet_size = parm->hc_max_packet_size;
468 /* filter "wMaxPacketSize" according to standard sizes */
470 usbd_get_std_packet_size(&std_size, type, parm->speed);
472 if (std_size.range.min || std_size.range.max) {
474 if (xfer->max_packet_size < std_size.range.min) {
475 xfer->max_packet_size = std_size.range.min;
477 if (xfer->max_packet_size > std_size.range.max) {
478 xfer->max_packet_size = std_size.range.max;
480 } else {
482 if (xfer->max_packet_size >= std_size.fixed[3]) {
483 xfer->max_packet_size = std_size.fixed[3];
484 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
485 xfer->max_packet_size = std_size.fixed[2];
486 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
487 xfer->max_packet_size = std_size.fixed[1];
488 } else {
489 /* only one possibility left */
490 xfer->max_packet_size = std_size.fixed[0];
495 * Check if the max packet size was outside its allowed range
496 * and clamped to a valid value:
498 if (maxp_old != xfer->max_packet_size)
499 xfer->flags_int.maxp_was_clamped = 1;
501 /* compute "max_frame_size" */
503 usbd_update_max_frame_size(xfer);
505 /* check interrupt interval and transfer pre-delay */
507 if (type == UE_ISOCHRONOUS) {
509 uint16_t frame_limit;
511 xfer->interval = 0; /* not used, must be zero */
512 xfer->flags_int.isochronous_xfr = 1; /* set flag */
514 if (xfer->timeout == 0) {
516 * set a default timeout in
517 * case something goes wrong!
519 xfer->timeout = 1000 / 4;
521 switch (parm->speed) {
522 case USB_SPEED_LOW:
523 case USB_SPEED_FULL:
524 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
525 xfer->fps_shift = 0;
526 break;
527 default:
528 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
529 xfer->fps_shift = edesc->bInterval;
530 if (xfer->fps_shift > 0)
531 xfer->fps_shift--;
532 if (xfer->fps_shift > 3)
533 xfer->fps_shift = 3;
534 if (xfer->flags.pre_scale_frames != 0)
535 xfer->nframes <<= (3 - xfer->fps_shift);
536 break;
539 if (xfer->nframes > frame_limit) {
541 * this is not going to work
542 * cross hardware
544 parm->err = USB_ERR_INVAL;
545 goto done;
547 if (xfer->nframes == 0) {
549 * this is not a valid value
551 parm->err = USB_ERR_ZERO_NFRAMES;
552 goto done;
554 } else {
557 * If a value is specified use that else check the
558 * endpoint descriptor!
560 if (type == UE_INTERRUPT) {
562 uint32_t temp;
564 if (xfer->interval == 0) {
566 xfer->interval = edesc->bInterval;
568 switch (parm->speed) {
569 case USB_SPEED_LOW:
570 case USB_SPEED_FULL:
571 break;
572 default:
573 /* 125us -> 1ms */
574 if (xfer->interval < 4)
575 xfer->interval = 1;
576 else if (xfer->interval > 16)
577 xfer->interval = (1 << (16 - 4));
578 else
579 xfer->interval =
580 (1 << (xfer->interval - 4));
581 break;
585 if (xfer->interval == 0) {
587 * One millisecond is the smallest
588 * interval we support:
590 xfer->interval = 1;
593 xfer->fps_shift = 0;
594 temp = 1;
596 while ((temp != 0) && (temp < xfer->interval)) {
597 xfer->fps_shift++;
598 temp *= 2;
601 switch (parm->speed) {
602 case USB_SPEED_LOW:
603 case USB_SPEED_FULL:
604 break;
605 default:
606 xfer->fps_shift += 3;
607 break;
613 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
614 * to be equal to zero when setting up USB transfers, hence
615 * this leads to alot of extra code in the USB kernel.
618 if ((xfer->max_frame_size == 0) ||
619 (xfer->max_packet_size == 0)) {
621 zmps = 1;
623 if ((parm->bufsize <= MIN_PKT) &&
624 (type != UE_CONTROL) &&
625 (type != UE_BULK)) {
627 /* workaround */
628 xfer->max_packet_size = MIN_PKT;
629 xfer->max_packet_count = 1;
630 parm->bufsize = 0; /* automatic setup length */
631 usbd_update_max_frame_size(xfer);
633 } else {
634 parm->err = USB_ERR_ZERO_MAXP;
635 goto done;
638 } else {
639 zmps = 0;
643 * check if we should setup a default
644 * length:
647 if (parm->bufsize == 0) {
649 parm->bufsize = xfer->max_frame_size;
651 if (type == UE_ISOCHRONOUS) {
652 parm->bufsize *= xfer->nframes;
656 * check if we are about to setup a proxy
657 * type of buffer:
660 if (xfer->flags.proxy_buffer) {
662 /* round bufsize up */
664 parm->bufsize += (xfer->max_frame_size - 1);
666 if (parm->bufsize < xfer->max_frame_size) {
667 /* length wrapped around */
668 parm->err = USB_ERR_INVAL;
669 goto done;
671 /* subtract remainder */
673 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
675 /* add length of USB device request structure, if any */
677 if (type == UE_CONTROL) {
678 parm->bufsize += REQ_SIZE; /* SETUP message */
681 xfer->max_data_length = parm->bufsize;
683 /* Setup "n_frlengths" and "n_frbuffers" */
685 if (type == UE_ISOCHRONOUS) {
686 n_frlengths = xfer->nframes;
687 n_frbuffers = 1;
688 } else {
690 if (type == UE_CONTROL) {
691 xfer->flags_int.control_xfr = 1;
692 if (xfer->nframes == 0) {
693 if (parm->bufsize <= REQ_SIZE) {
695 * there will never be any data
696 * stage
698 xfer->nframes = 1;
699 } else {
700 xfer->nframes = 2;
703 } else {
704 if (xfer->nframes == 0) {
705 xfer->nframes = 1;
709 n_frlengths = xfer->nframes;
710 n_frbuffers = xfer->nframes;
714 * check if we have room for the
715 * USB device request structure:
718 if (type == UE_CONTROL) {
720 if (xfer->max_data_length < REQ_SIZE) {
721 /* length wrapped around or too small bufsize */
722 parm->err = USB_ERR_INVAL;
723 goto done;
725 xfer->max_data_length -= REQ_SIZE;
728 * Setup "frlengths" and shadow "frlengths" for keeping the
729 * initial frame lengths when a USB transfer is complete. This
730 * information is useful when computing isochronous offsets.
732 xfer->frlengths = parm->xfer_length_ptr;
733 parm->xfer_length_ptr += 2 * n_frlengths;
735 /* setup "frbuffers" */
736 xfer->frbuffers = parm->xfer_page_cache_ptr;
737 parm->xfer_page_cache_ptr += n_frbuffers;
739 /* initialize max frame count */
740 xfer->max_frame_count = xfer->nframes;
743 * check if we need to setup
744 * a local buffer:
747 if (!xfer->flags.ext_buffer) {
748 #if USB_HAVE_BUSDMA
749 struct usb_page_search page_info;
750 struct usb_page_cache *pc;
752 if (usbd_transfer_setup_sub_malloc(parm,
753 &pc, parm->bufsize, 1, 1)) {
754 parm->err = USB_ERR_NOMEM;
755 } else if (parm->buf != NULL) {
757 usbd_get_page(pc, 0, &page_info);
759 xfer->local_buffer = page_info.buffer;
761 usbd_xfer_set_frame_offset(xfer, 0, 0);
763 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
764 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
767 #else
768 /* align data */
769 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
771 if (parm->buf != NULL) {
772 xfer->local_buffer =
773 USB_ADD_BYTES(parm->buf, parm->size[0]);
775 usbd_xfer_set_frame_offset(xfer, 0, 0);
777 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
778 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
781 parm->size[0] += parm->bufsize;
783 /* align data again */
784 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
785 #endif
788 * Compute maximum buffer size
791 if (parm->bufsize_max < parm->bufsize) {
792 parm->bufsize_max = parm->bufsize;
794 #if USB_HAVE_BUSDMA
795 if (xfer->flags_int.bdma_enable) {
797 * Setup "dma_page_ptr".
799 * Proof for formula below:
801 * Assume there are three USB frames having length "a", "b" and
802 * "c". These USB frames will at maximum need "z"
803 * "usb_page" structures. "z" is given by:
805 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
806 * ((c / USB_PAGE_SIZE) + 2);
808 * Constraining "a", "b" and "c" like this:
810 * (a + b + c) <= parm->bufsize
812 * We know that:
814 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
816 * Here is the general formula:
818 xfer->dma_page_ptr = parm->dma_page_ptr;
819 parm->dma_page_ptr += (2 * n_frbuffers);
820 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
822 #endif
823 if (zmps) {
824 /* correct maximum data length */
825 xfer->max_data_length = 0;
827 /* subtract USB frame remainder from "hc_max_frame_size" */
829 xfer->max_hc_frame_size =
830 (parm->hc_max_frame_size -
831 (parm->hc_max_frame_size % xfer->max_frame_size));
833 if (xfer->max_hc_frame_size == 0) {
834 parm->err = USB_ERR_INVAL;
835 goto done;
838 /* initialize frame buffers */
840 if (parm->buf) {
841 for (x = 0; x != n_frbuffers; x++) {
842 xfer->frbuffers[x].tag_parent =
843 &xfer->xroot->dma_parent_tag;
844 #if USB_HAVE_BUSDMA
845 if (xfer->flags_int.bdma_enable &&
846 (parm->bufsize_max > 0)) {
848 if (usb_pc_dmamap_create(
849 xfer->frbuffers + x,
850 parm->bufsize_max)) {
851 parm->err = USB_ERR_NOMEM;
852 goto done;
855 #endif
858 done:
859 if (parm->err) {
861 * Set some dummy values so that we avoid division by zero:
863 xfer->max_hc_frame_size = 1;
864 xfer->max_frame_size = 1;
865 xfer->max_packet_size = 1;
866 xfer->max_data_length = 0;
867 xfer->nframes = 0;
868 xfer->max_frame_count = 0;
872 /*------------------------------------------------------------------------*
873 * usbd_transfer_setup - setup an array of USB transfers
875 * NOTE: You must always call "usbd_transfer_unsetup" after calling
876 * "usbd_transfer_setup" if success was returned.
878 * The idea is that the USB device driver should pre-allocate all its
879 * transfers by one call to this function.
881 * Return values:
882 * 0: Success
883 * Else: Failure
884 *------------------------------------------------------------------------*/
885 usb_error_t
886 usbd_transfer_setup(struct usb_device *udev,
887 const uint8_t *ifaces, struct usb_xfer **ppxfer,
888 const struct usb_config *setup_start, uint16_t n_setup,
889 void *priv_sc, struct lock *xfer_lock)
891 const struct usb_config *setup_end = setup_start + n_setup;
892 const struct usb_config *setup;
893 struct usb_setup_params *parm;
894 struct usb_endpoint *ep;
895 struct usb_xfer_root *info;
896 struct usb_xfer *xfer;
897 void *buf = NULL;
898 usb_error_t error = 0;
899 uint16_t n;
900 uint16_t refcount;
901 uint8_t do_unlock;
903 #if 0
904 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
905 "usbd_transfer_setup can sleep!");
906 #endif
908 /* do some checking first */
910 if (n_setup == 0) {
911 DPRINTFN(6, "setup array has zero length!\n");
912 return (USB_ERR_INVAL);
914 if (ifaces == NULL) {
915 DPRINTFN(6, "ifaces array is NULL!\n");
916 return (USB_ERR_INVAL);
918 if (xfer_lock == NULL) {
919 panic("xfer without lock!\n");
920 DPRINTFN(6, "using global lock\n");
923 /* more sanity checks */
925 for (setup = setup_start, n = 0;
926 setup != setup_end; setup++, n++) {
927 if (setup->bufsize == (usb_frlength_t)-1) {
928 error = USB_ERR_BAD_BUFSIZE;
929 DPRINTF("invalid bufsize\n");
931 if (setup->callback == NULL) {
932 error = USB_ERR_NO_CALLBACK;
933 DPRINTF("no callback\n");
935 ppxfer[n] = NULL;
938 if (error)
939 return (error);
941 /* Protect scratch area */
942 do_unlock = usbd_enum_lock(udev);
944 refcount = 0;
945 info = NULL;
947 parm = &udev->scratch.xfer_setup[0].parm;
948 memset(parm, 0, sizeof(*parm));
950 parm->udev = udev;
951 parm->speed = usbd_get_speed(udev);
952 parm->hc_max_packet_count = 1;
954 if (parm->speed >= USB_SPEED_MAX) {
955 parm->err = USB_ERR_INVAL;
956 goto done;
958 /* setup all transfers */
960 while (1) {
962 if (buf) {
964 * Initialize the "usb_xfer_root" structure,
965 * which is common for all our USB transfers.
967 info = USB_ADD_BYTES(buf, 0);
969 info->memory_base = buf;
970 info->memory_size = parm->size[0];
972 #if USB_HAVE_BUSDMA
973 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
974 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
975 #endif
976 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
977 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
979 cv_init(&info->cv_drain, "WDRAIN");
981 info->xfer_lock = xfer_lock;
982 #if USB_HAVE_BUSDMA
983 usb_dma_tag_setup(&info->dma_parent_tag,
984 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
985 xfer_lock, &usb_bdma_done_event,
986 udev->bus->dma_bits, parm->dma_tag_max);
987 #endif
989 info->bus = udev->bus;
990 info->udev = udev;
992 TAILQ_INIT(&info->done_q.head);
993 info->done_q.command = &usbd_callback_wrapper;
994 #if USB_HAVE_BUSDMA
995 TAILQ_INIT(&info->dma_q.head);
996 info->dma_q.command = &usb_bdma_work_loop;
997 #endif
998 info->done_m[0].hdr.pm_callback = &usb_callback_proc;
999 info->done_m[0].xroot = info;
1000 info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1001 info->done_m[1].xroot = info;
1004 * In device side mode control endpoint
1005 * requests need to run from a separate
1006 * context, else there is a chance of
1007 * deadlock!
1009 if (setup_start == usb_control_ep_cfg)
1010 info->done_p =
1011 USB_BUS_CONTROL_XFER_PROC(udev->bus);
1012 else
1013 info->done_p =
1014 USB_BUS_NON_GIANT_PROC(udev->bus);
1016 /* reset sizes */
1018 parm->size[0] = 0;
1019 parm->buf = buf;
1020 parm->size[0] += sizeof(info[0]);
1022 for (setup = setup_start, n = 0;
1023 setup != setup_end; setup++, n++) {
1025 /* skip USB transfers without callbacks: */
1026 if (setup->callback == NULL) {
1027 continue;
1029 /* see if there is a matching endpoint */
1030 ep = usbd_get_endpoint(udev,
1031 ifaces[setup->if_index], setup);
1034 * Check that the USB PIPE is valid and that
1035 * the endpoint mode is proper.
1037 * Make sure we don't allocate a streams
1038 * transfer when such a combination is not
1039 * valid.
1041 if ((ep == NULL) || (ep->methods == NULL) ||
1042 ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1043 (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1044 (setup->stream_id != 0 &&
1045 (setup->stream_id >= USB_MAX_EP_STREAMS ||
1046 (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1047 if (setup->flags.no_pipe_ok)
1048 continue;
1049 if ((setup->usb_mode != USB_MODE_DUAL) &&
1050 (setup->usb_mode != udev->flags.usb_mode))
1051 continue;
1052 parm->err = USB_ERR_NO_PIPE;
1053 goto done;
1056 /* align data properly */
1057 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1059 /* store current setup pointer */
1060 parm->curr_setup = setup;
1062 if (buf) {
1064 * Common initialization of the
1065 * "usb_xfer" structure.
1067 xfer = USB_ADD_BYTES(buf, parm->size[0]);
1068 xfer->address = udev->address;
1069 xfer->priv_sc = priv_sc;
1070 xfer->xroot = info;
1072 usb_callout_init_mtx(&xfer->timeout_handle,
1073 &udev->bus->bus_lock, 0);
1074 } else {
1076 * Setup a dummy xfer, hence we are
1077 * writing to the "usb_xfer"
1078 * structure pointed to by "xfer"
1079 * before we have allocated any
1080 * memory:
1082 xfer = &udev->scratch.xfer_setup[0].dummy;
1083 memset(xfer, 0, sizeof(*xfer));
1084 refcount++;
1087 /* set transfer endpoint pointer */
1088 xfer->endpoint = ep;
1090 /* set transfer stream ID */
1091 xfer->stream_id = setup->stream_id;
1093 parm->size[0] += sizeof(xfer[0]);
1094 parm->methods = xfer->endpoint->methods;
1095 parm->curr_xfer = xfer;
1098 * Call the Host or Device controller transfer
1099 * setup routine:
1101 (udev->bus->methods->xfer_setup) (parm);
1103 /* check for error */
1104 if (parm->err)
1105 goto done;
1107 if (buf) {
1109 * Increment the endpoint refcount. This
1110 * basically prevents setting a new
1111 * configuration and alternate setting
1112 * when USB transfers are in use on
1113 * the given interface. Search the USB
1114 * code for "endpoint->refcount_alloc" if you
1115 * want more information.
1117 USB_BUS_LOCK(info->bus);
1118 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1119 parm->err = USB_ERR_INVAL;
1121 xfer->endpoint->refcount_alloc++;
1123 if (xfer->endpoint->refcount_alloc == 0)
1124 panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1125 USB_BUS_UNLOCK(info->bus);
1128 * Whenever we set ppxfer[] then we
1129 * also need to increment the
1130 * "setup_refcount":
1132 info->setup_refcount++;
1135 * Transfer is successfully setup and
1136 * can be used:
1138 ppxfer[n] = xfer;
1141 /* check for error */
1142 if (parm->err)
1143 goto done;
1146 if (buf != NULL || parm->err != 0)
1147 goto done;
1149 /* if no transfers, nothing to do */
1150 if (refcount == 0)
1151 goto done;
1153 /* align data properly */
1154 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1156 /* store offset temporarily */
1157 parm->size[1] = parm->size[0];
1160 * The number of DMA tags required depends on
1161 * the number of endpoints. The current estimate
1162 * for maximum number of DMA tags per endpoint
1163 * is three:
1164 * 1) for loading memory
1165 * 2) for allocating memory
1166 * 3) for fixing memory [UHCI]
1168 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1171 * DMA tags for QH, TD, Data and more.
1173 parm->dma_tag_max += 8;
1175 parm->dma_tag_p += parm->dma_tag_max;
1177 parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1178 ((uint8_t *)0);
1180 /* align data properly */
1181 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1183 /* store offset temporarily */
1184 parm->size[3] = parm->size[0];
1186 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1187 ((uint8_t *)0);
1189 /* align data properly */
1190 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1192 /* store offset temporarily */
1193 parm->size[4] = parm->size[0];
1195 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1196 ((uint8_t *)0);
1198 /* store end offset temporarily */
1199 parm->size[5] = parm->size[0];
1201 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1202 ((uint8_t *)0);
1204 /* store end offset temporarily */
1206 parm->size[2] = parm->size[0];
1208 /* align data properly */
1209 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1211 parm->size[6] = parm->size[0];
1213 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1214 ((uint8_t *)0);
1216 /* align data properly */
1217 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1219 /* allocate zeroed memory */
1220 buf = kmalloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1222 if (buf == NULL) {
1223 parm->err = USB_ERR_NOMEM;
1224 DPRINTFN(0, "cannot allocate memory block for "
1225 "configuration (%d bytes)\n",
1226 parm->size[0]);
1227 goto done;
1229 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1230 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1231 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1232 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1233 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1236 done:
1237 if (buf) {
1238 if (info->setup_refcount == 0) {
1240 * "usbd_transfer_unsetup_sub" will unlock
1241 * the bus mutex before returning !
1243 USB_BUS_LOCK(info->bus);
1245 /* something went wrong */
1246 usbd_transfer_unsetup_sub(info, 0);
1250 /* check if any errors happened */
1251 if (parm->err)
1252 usbd_transfer_unsetup(ppxfer, n_setup);
1254 error = parm->err;
1256 if (do_unlock)
1257 usbd_enum_unlock(udev);
1259 return (error);
1262 /*------------------------------------------------------------------------*
1263 * usbd_transfer_unsetup_sub - factored out code
1264 *------------------------------------------------------------------------*/
1265 static void
1266 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1268 #if USB_HAVE_BUSDMA
1269 struct usb_page_cache *pc;
1270 #endif
1272 USB_BUS_LOCK_ASSERT(info->bus);
1274 /* wait for any outstanding DMA operations */
1275 /* This is insane */
1276 if (needs_delay) {
1277 usb_timeout_t temp;
1278 temp = usbd_get_dma_delay(info->udev);
1279 if (temp != 0) {
1280 usb_pause_mtx(&info->bus->bus_lock,
1281 USB_MS_TO_TICKS(temp));
1285 /* make sure that our done messages are not queued anywhere */
1286 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1288 USB_BUS_UNLOCK(info->bus);
1290 #if USB_HAVE_BUSDMA
1291 /* free DMA'able memory, if any */
1292 pc = info->dma_page_cache_start;
1293 while (pc != info->dma_page_cache_end) {
1294 usb_pc_free_mem(pc);
1295 pc++;
1298 /* free DMA maps in all "xfer->frbuffers" */
1299 pc = info->xfer_page_cache_start;
1300 while (pc != info->xfer_page_cache_end) {
1301 usb_pc_dmamap_destroy(pc);
1302 pc++;
1305 /* free all DMA tags */
1306 usb_dma_tag_unsetup(&info->dma_parent_tag);
1307 #endif
1309 cv_destroy(&info->cv_drain);
1312 * free the "memory_base" last, hence the "info" structure is
1313 * contained within the "memory_base"!
1315 usbd_delayed_free(info->memory_base, M_USB);
1319 * This is a horrible hack and workaround to a very bad decision by
1320 * the original U4B coder to integrate the QH/TD structures into the
1321 * xfer and then free the whole mess all at once.
1323 * The problem is that the controller may still be accessing the QHs,
1324 * because it might have gotten side-tracked onto the removed QHs
1325 * chain link. They have to remain intact long enough for the
1326 * controller to get out.
1328 * This horrible hack basically just delays freeing by 256 slots.
1329 * It's not even time-based or door-bell based (which is the way
1330 * the linux driver does it)... but to fix it properly requires rewriting
1331 * too much of this driver.
1333 #define DFREE_SLOTS 256
1334 #define DFREE_MASK (DFREE_SLOTS - 1)
1336 static struct dfree_slot {
1337 void *data;
1338 struct malloc_type *mtype;
1339 } dfree_slots[DFREE_SLOTS];
1340 static int dfree_index;
1342 static void
1343 usbd_delayed_free(void *data, struct malloc_type *mtype)
1345 struct dfree_slot slot;
1346 int index;
1348 crit_enter();
1349 index = atomic_fetchadd_int(&dfree_index, 1);
1350 index &= DFREE_MASK;
1351 slot = dfree_slots[index];
1352 dfree_slots[index].data = data;
1353 dfree_slots[index].mtype = mtype;
1354 crit_exit();
1355 if (slot.data)
1356 kfree(slot.data, slot.mtype);
1359 /*------------------------------------------------------------------------*
1360 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1362 * NOTE: All USB transfers in progress will get called back passing
1363 * the error code "USB_ERR_CANCELLED" before this function
1364 * returns.
1365 *------------------------------------------------------------------------*/
1366 void
1367 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1369 struct usb_xfer *xfer;
1370 struct usb_xfer_root *info;
1371 uint8_t needs_delay = 0;
1373 #if 0
1374 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1375 "usbd_transfer_unsetup can sleep!");
1376 #endif
1378 while (n_setup--) {
1379 xfer = pxfer[n_setup];
1381 if (xfer == NULL)
1382 continue;
1384 info = xfer->xroot;
1386 USB_XFER_LOCK(xfer);
1387 USB_BUS_LOCK(info->bus);
1390 * HINT: when you start/stop a transfer, it might be a
1391 * good idea to directly use the "pxfer[]" structure:
1393 * usbd_transfer_start(sc->pxfer[0]);
1394 * usbd_transfer_stop(sc->pxfer[0]);
1396 * That way, if your code has many parts that will not
1397 * stop running under the same lock, in other words
1398 * "xfer_mtx", the usbd_transfer_start and
1399 * usbd_transfer_stop functions will simply return
1400 * when they detect a NULL pointer argument.
1402 * To avoid any races we clear the "pxfer[]" pointer
1403 * while holding the private mutex of the driver:
1405 pxfer[n_setup] = NULL;
1407 USB_BUS_UNLOCK(info->bus);
1408 USB_XFER_UNLOCK(xfer);
1410 usbd_transfer_drain(xfer);
1412 #if USB_HAVE_BUSDMA
1413 if (xfer->flags_int.bdma_enable)
1414 needs_delay = 1;
1415 #endif
1417 * NOTE: default endpoint does not have an
1418 * interface, even if endpoint->iface_index == 0
1420 USB_BUS_LOCK(info->bus);
1421 xfer->endpoint->refcount_alloc--;
1422 USB_BUS_UNLOCK(info->bus);
1424 usb_callout_drain(&xfer->timeout_handle);
1426 USB_BUS_LOCK(info->bus);
1428 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1429 "reference count\n"));
1431 info->setup_refcount--;
1433 if (info->setup_refcount == 0) {
1434 usbd_transfer_unsetup_sub(info,
1435 needs_delay);
1436 } else {
1437 USB_BUS_UNLOCK(info->bus);
1442 /*------------------------------------------------------------------------*
1443 * usbd_control_transfer_init - factored out code
1445 * In USB Device Mode we have to wait for the SETUP packet which
1446 * containst the "struct usb_device_request" structure, before we can
1447 * transfer any data. In USB Host Mode we already have the SETUP
1448 * packet at the moment the USB transfer is started. This leads us to
1449 * having to setup the USB transfer at two different places in
1450 * time. This function just contains factored out control transfer
1451 * initialisation code, so that we don't duplicate the code.
1452 *------------------------------------------------------------------------*/
1453 static void
1454 usbd_control_transfer_init(struct usb_xfer *xfer)
1456 struct usb_device_request req;
1458 /* copy out the USB request header */
1460 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1462 /* setup remainder */
1464 xfer->flags_int.control_rem = UGETW(req.wLength);
1466 /* copy direction to endpoint variable */
1468 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1469 xfer->endpointno |=
1470 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1473 /*------------------------------------------------------------------------*
1474 * usbd_control_transfer_did_data
1476 * This function returns non-zero if a control endpoint has
1477 * transferred the first DATA packet after the SETUP packet.
1478 * Else it returns zero.
1479 *------------------------------------------------------------------------*/
1480 static uint8_t
1481 usbd_control_transfer_did_data(struct usb_xfer *xfer)
1483 struct usb_device_request req;
1485 /* SETUP packet is not yet sent */
1486 if (xfer->flags_int.control_hdr != 0)
1487 return (0);
1489 /* copy out the USB request header */
1490 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1492 /* compare remainder to the initial value */
1493 return (xfer->flags_int.control_rem != UGETW(req.wLength));
1496 /*------------------------------------------------------------------------*
1497 * usbd_setup_ctrl_transfer
1499 * This function handles initialisation of control transfers. Control
1500 * transfers are special in that regard that they can both transmit
1501 * and receive data.
1503 * Return values:
1504 * 0: Success
1505 * Else: Failure
1506 *------------------------------------------------------------------------*/
1507 static int
1508 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1510 usb_frlength_t len;
1512 /* Check for control endpoint stall */
1513 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1514 /* the control transfer is no longer active */
1515 xfer->flags_int.control_stall = 1;
1516 xfer->flags_int.control_act = 0;
1517 } else {
1518 /* don't stall control transfer by default */
1519 xfer->flags_int.control_stall = 0;
1522 /* Check for invalid number of frames */
1523 if (xfer->nframes > 2) {
1525 * If you need to split a control transfer, you
1526 * have to do one part at a time. Only with
1527 * non-control transfers you can do multiple
1528 * parts a time.
1530 DPRINTFN(0, "Too many frames: %u\n",
1531 (unsigned int)xfer->nframes);
1532 goto error;
1536 * Check if there is a control
1537 * transfer in progress:
1539 if (xfer->flags_int.control_act) {
1541 if (xfer->flags_int.control_hdr) {
1543 /* clear send header flag */
1545 xfer->flags_int.control_hdr = 0;
1547 /* setup control transfer */
1548 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1549 usbd_control_transfer_init(xfer);
1552 /* get data length */
1554 len = xfer->sumlen;
1556 } else {
1558 /* the size of the SETUP structure is hardcoded ! */
1560 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1561 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1562 xfer->frlengths[0], sizeof(struct
1563 usb_device_request));
1564 goto error;
1566 /* check USB mode */
1567 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1569 /* check number of frames */
1570 if (xfer->nframes != 1) {
1572 * We need to receive the setup
1573 * message first so that we know the
1574 * data direction!
1576 DPRINTF("Misconfigured transfer\n");
1577 goto error;
1580 * Set a dummy "control_rem" value. This
1581 * variable will be overwritten later by a
1582 * call to "usbd_control_transfer_init()" !
1584 xfer->flags_int.control_rem = 0xFFFF;
1585 } else {
1587 /* setup "endpoint" and "control_rem" */
1589 usbd_control_transfer_init(xfer);
1592 /* set transfer-header flag */
1594 xfer->flags_int.control_hdr = 1;
1596 /* get data length */
1598 len = (xfer->sumlen - sizeof(struct usb_device_request));
1601 /* update did data flag */
1603 xfer->flags_int.control_did_data =
1604 usbd_control_transfer_did_data(xfer);
1606 /* check if there is a length mismatch */
1608 if (len > xfer->flags_int.control_rem) {
1609 DPRINTFN(0, "Length (%d) greater than "
1610 "remaining length (%d)\n", len,
1611 xfer->flags_int.control_rem);
1612 goto error;
1614 /* check if we are doing a short transfer */
1616 if (xfer->flags.force_short_xfer) {
1617 xfer->flags_int.control_rem = 0;
1618 } else {
1619 if ((len != xfer->max_data_length) &&
1620 (len != xfer->flags_int.control_rem) &&
1621 (xfer->nframes != 1)) {
1622 DPRINTFN(0, "Short control transfer without "
1623 "force_short_xfer set\n");
1624 goto error;
1626 xfer->flags_int.control_rem -= len;
1629 /* the status part is executed when "control_act" is 0 */
1631 if ((xfer->flags_int.control_rem > 0) ||
1632 (xfer->flags.manual_status)) {
1633 /* don't execute the STATUS stage yet */
1634 xfer->flags_int.control_act = 1;
1636 /* sanity check */
1637 if ((!xfer->flags_int.control_hdr) &&
1638 (xfer->nframes == 1)) {
1640 * This is not a valid operation!
1642 DPRINTFN(0, "Invalid parameter "
1643 "combination\n");
1644 goto error;
1646 } else {
1647 /* time to execute the STATUS stage */
1648 xfer->flags_int.control_act = 0;
1650 return (0); /* success */
1652 error:
1653 return (1); /* failure */
1656 /*------------------------------------------------------------------------*
1657 * usbd_transfer_submit - start USB hardware for the given transfer
1659 * This function should only be called from the USB callback.
1660 *------------------------------------------------------------------------*/
1661 void
1662 usbd_transfer_submit(struct usb_xfer *xfer)
1664 struct usb_xfer_root *info;
1665 struct usb_bus *bus;
1666 usb_frcount_t x;
1668 info = xfer->xroot;
1669 bus = info->bus;
1671 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1672 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1673 "read" : "write");
1675 #ifdef USB_DEBUG
1676 if (USB_DEBUG_VAR > 0) {
1677 USB_BUS_LOCK(bus);
1679 usb_dump_endpoint(xfer->endpoint);
1681 USB_BUS_UNLOCK(bus);
1683 #endif
1685 USB_XFER_LOCK_ASSERT(xfer);
1686 USB_BUS_LOCK_ASSERT_NOTOWNED(bus);
1688 /* Only open the USB transfer once! */
1689 if (!xfer->flags_int.open) {
1690 xfer->flags_int.open = 1;
1692 DPRINTF("open\n");
1694 USB_BUS_LOCK(bus);
1695 (xfer->endpoint->methods->open) (xfer);
1696 USB_BUS_UNLOCK(bus);
1698 /* set "transferring" flag */
1699 xfer->flags_int.transferring = 1;
1701 #if USB_HAVE_POWERD
1702 /* increment power reference */
1703 usbd_transfer_power_ref(xfer, 1);
1704 #endif
1706 * Check if the transfer is waiting on a queue, most
1707 * frequently the "done_q":
1709 if (xfer->wait_queue) {
1710 USB_BUS_LOCK(bus);
1711 usbd_transfer_dequeue(xfer);
1712 USB_BUS_UNLOCK(bus);
1714 /* clear "did_dma_delay" flag */
1715 xfer->flags_int.did_dma_delay = 0;
1717 /* clear "did_close" flag */
1718 xfer->flags_int.did_close = 0;
1720 #if USB_HAVE_BUSDMA
1721 /* clear "bdma_setup" flag */
1722 xfer->flags_int.bdma_setup = 0;
1723 #endif
1724 /* by default we cannot cancel any USB transfer immediately */
1725 xfer->flags_int.can_cancel_immed = 0;
1727 /* clear lengths and frame counts by default */
1728 xfer->sumlen = 0;
1729 xfer->actlen = 0;
1730 xfer->aframes = 0;
1732 /* clear any previous errors */
1733 xfer->error = 0;
1735 /* Check if the device is still alive */
1736 if (info->udev->state < USB_STATE_POWERED) {
1737 USB_BUS_LOCK(bus);
1739 * Must return cancelled error code else
1740 * device drivers can hang.
1742 usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1743 USB_BUS_UNLOCK(bus);
1744 return;
1747 /* sanity check */
1748 if (xfer->nframes == 0) {
1749 if (xfer->flags.stall_pipe) {
1751 * Special case - want to stall without transferring
1752 * any data:
1754 DPRINTF("xfer=%p nframes=0: stall "
1755 "or clear stall!\n", xfer);
1756 USB_BUS_LOCK(bus);
1757 xfer->flags_int.can_cancel_immed = 1;
1758 /* start the transfer */
1759 usb_command_wrapper(&xfer->endpoint->
1760 endpoint_q[xfer->stream_id], xfer);
1761 USB_BUS_UNLOCK(bus);
1762 return;
1764 USB_BUS_LOCK(bus);
1765 usbd_transfer_done(xfer, USB_ERR_INVAL);
1766 USB_BUS_UNLOCK(bus);
1767 return;
1769 /* compute some variables */
1771 for (x = 0; x != xfer->nframes; x++) {
1772 /* make a copy of the frlenghts[] */
1773 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1774 /* compute total transfer length */
1775 xfer->sumlen += xfer->frlengths[x];
1776 if (xfer->sumlen < xfer->frlengths[x]) {
1777 /* length wrapped around */
1778 USB_BUS_LOCK(bus);
1779 usbd_transfer_done(xfer, USB_ERR_INVAL);
1780 USB_BUS_UNLOCK(bus);
1781 return;
1785 /* clear some internal flags */
1787 xfer->flags_int.short_xfer_ok = 0;
1788 xfer->flags_int.short_frames_ok = 0;
1790 /* check if this is a control transfer */
1792 if (xfer->flags_int.control_xfr) {
1794 if (usbd_setup_ctrl_transfer(xfer)) {
1795 USB_BUS_LOCK(bus);
1796 usbd_transfer_done(xfer, USB_ERR_STALLED);
1797 USB_BUS_UNLOCK(bus);
1798 return;
1802 * Setup filtered version of some transfer flags,
1803 * in case of data read direction
1805 if (USB_GET_DATA_ISREAD(xfer)) {
1807 if (xfer->flags.short_frames_ok) {
1808 xfer->flags_int.short_xfer_ok = 1;
1809 xfer->flags_int.short_frames_ok = 1;
1810 } else if (xfer->flags.short_xfer_ok) {
1811 xfer->flags_int.short_xfer_ok = 1;
1813 /* check for control transfer */
1814 if (xfer->flags_int.control_xfr) {
1816 * 1) Control transfers do not support
1817 * reception of multiple short USB
1818 * frames in host mode and device side
1819 * mode, with exception of:
1821 * 2) Due to sometimes buggy device
1822 * side firmware we need to do a
1823 * STATUS stage in case of short
1824 * control transfers in USB host mode.
1825 * The STATUS stage then becomes the
1826 * "alt_next" to the DATA stage.
1828 xfer->flags_int.short_frames_ok = 1;
1833 * Check if BUS-DMA support is enabled and try to load virtual
1834 * buffers into DMA, if any:
1836 #if USB_HAVE_BUSDMA
1837 if (xfer->flags_int.bdma_enable) {
1838 /* insert the USB transfer last in the BUS-DMA queue */
1839 usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1840 return;
1842 #endif
1844 * Enter the USB transfer into the Host Controller or
1845 * Device Controller schedule:
1847 usbd_pipe_enter(xfer);
1850 /*------------------------------------------------------------------------*
1851 * usbd_pipe_enter - factored out code
1852 *------------------------------------------------------------------------*/
1853 void
1854 usbd_pipe_enter(struct usb_xfer *xfer)
1856 struct usb_endpoint *ep;
1858 USB_XFER_LOCK_ASSERT(xfer);
1860 USB_BUS_LOCK(xfer->xroot->bus);
1862 ep = xfer->endpoint;
1864 DPRINTF("enter\n");
1866 /* the transfer can now be cancelled */
1867 xfer->flags_int.can_cancel_immed = 1;
1869 /* enter the transfer */
1870 (ep->methods->enter) (xfer);
1872 /* check for transfer error */
1873 if (xfer->error) {
1874 /* some error has happened */
1875 usbd_transfer_done(xfer, 0);
1876 USB_BUS_UNLOCK(xfer->xroot->bus);
1877 return;
1880 /* start the transfer */
1881 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1882 USB_BUS_UNLOCK(xfer->xroot->bus);
1885 /*------------------------------------------------------------------------*
1886 * usbd_transfer_start - start an USB transfer
1888 * NOTE: Calling this function more than one time will only
1889 * result in a single transfer start, until the USB transfer
1890 * completes.
1891 *------------------------------------------------------------------------*/
1892 void
1893 usbd_transfer_start(struct usb_xfer *xfer)
1895 if (xfer == NULL) {
1896 /* transfer is gone */
1897 return;
1899 USB_XFER_LOCK_ASSERT(xfer);
1901 /* mark the USB transfer started */
1903 if (!xfer->flags_int.started) {
1904 /* lock the BUS lock to avoid races updating flags_int */
1905 USB_BUS_LOCK(xfer->xroot->bus);
1906 xfer->flags_int.started = 1;
1907 USB_BUS_UNLOCK(xfer->xroot->bus);
1909 /* check if the USB transfer callback is already transferring */
1911 if (xfer->flags_int.transferring) {
1912 return;
1914 USB_BUS_LOCK(xfer->xroot->bus);
1915 /* call the USB transfer callback */
1916 usbd_callback_ss_done_defer(xfer);
1917 USB_BUS_UNLOCK(xfer->xroot->bus);
1920 /*------------------------------------------------------------------------*
1921 * usbd_transfer_stop - stop an USB transfer
1923 * NOTE: Calling this function more than one time will only
1924 * result in a single transfer stop.
1925 * NOTE: When this function returns it is not safe to free nor
1926 * reuse any DMA buffers. See "usbd_transfer_drain()".
1927 *------------------------------------------------------------------------*/
1928 void
1929 usbd_transfer_stop(struct usb_xfer *xfer)
1931 struct usb_endpoint *ep;
1933 if (xfer == NULL) {
1934 /* transfer is gone */
1935 return;
1937 #if 0
1938 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1939 #endif
1941 /* check if the USB transfer was ever opened */
1943 if (!xfer->flags_int.open) {
1944 if (xfer->flags_int.started) {
1945 /* nothing to do except clearing the "started" flag */
1946 /* lock the BUS lock to avoid races updating flags_int */
1947 USB_BUS_LOCK(xfer->xroot->bus);
1948 xfer->flags_int.started = 0;
1949 USB_BUS_UNLOCK(xfer->xroot->bus);
1951 return;
1953 /* try to stop the current USB transfer */
1955 USB_BUS_LOCK(xfer->xroot->bus);
1956 /* override any previous error */
1957 xfer->error = USB_ERR_CANCELLED;
1960 * Clear "open" and "started" when both private and USB lock
1961 * is locked so that we don't get a race updating "flags_int"
1963 xfer->flags_int.open = 0;
1964 xfer->flags_int.started = 0;
1967 * Check if we can cancel the USB transfer immediately.
1969 if (xfer->flags_int.transferring) {
1970 if (xfer->flags_int.can_cancel_immed &&
1971 (!xfer->flags_int.did_close)) {
1972 DPRINTF("close\n");
1974 * The following will lead to an USB_ERR_CANCELLED
1975 * error code being passed to the USB callback.
1977 (xfer->endpoint->methods->close) (xfer);
1978 /* only close once */
1979 xfer->flags_int.did_close = 1;
1980 } else {
1981 /* need to wait for the next done callback */
1983 } else {
1984 DPRINTF("close\n");
1986 /* close here and now */
1987 (xfer->endpoint->methods->close) (xfer);
1990 * Any additional DMA delay is done by
1991 * "usbd_transfer_unsetup()".
1995 * Special case. Check if we need to restart a blocked
1996 * endpoint.
1998 ep = xfer->endpoint;
2001 * If the current USB transfer is completing we need
2002 * to start the next one:
2004 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2005 usb_command_wrapper(
2006 &ep->endpoint_q[xfer->stream_id], NULL);
2010 USB_BUS_UNLOCK(xfer->xroot->bus);
2013 /*------------------------------------------------------------------------*
2014 * usbd_transfer_pending
2016 * This function will check if an USB transfer is pending which is a
2017 * little bit complicated!
2018 * Return values:
2019 * 0: Not pending
2020 * 1: Pending: The USB transfer will receive a callback in the future.
2021 *------------------------------------------------------------------------*/
2022 uint8_t
2023 usbd_transfer_pending(struct usb_xfer *xfer)
2025 struct usb_xfer_root *info;
2026 struct usb_xfer_queue *pq;
2028 if (xfer == NULL) {
2029 /* transfer is gone */
2030 return (0);
2032 #if 0
2033 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2034 #endif
2036 if (xfer->flags_int.transferring) {
2037 /* trivial case */
2038 return (1);
2040 USB_BUS_LOCK(xfer->xroot->bus);
2041 if (xfer->wait_queue) {
2042 /* we are waiting on a queue somewhere */
2043 USB_BUS_UNLOCK(xfer->xroot->bus);
2044 return (1);
2046 info = xfer->xroot;
2047 pq = &info->done_q;
2049 if (pq->curr == xfer) {
2050 /* we are currently scheduled for callback */
2051 USB_BUS_UNLOCK(xfer->xroot->bus);
2052 return (1);
2054 /* we are not pending */
2055 USB_BUS_UNLOCK(xfer->xroot->bus);
2056 return (0);
2059 /*------------------------------------------------------------------------*
2060 * usbd_transfer_drain
2062 * This function will stop the USB transfer and wait for any
2063 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2064 * are loaded into DMA can safely be freed or reused after that this
2065 * function has returned.
2066 *------------------------------------------------------------------------*/
2067 void
2068 usbd_transfer_drain(struct usb_xfer *xfer)
2070 #if 0
2071 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2072 "usbd_transfer_drain can sleep!");
2073 #endif
2075 if (xfer == NULL) {
2076 /* transfer is gone */
2077 return;
2079 USB_XFER_LOCK_ASSERT_NOTOWNED(xfer);
2080 USB_XFER_LOCK(xfer);
2082 usbd_transfer_stop(xfer);
2085 * It is allowed that the callback can drop its
2086 * transfer mutex. In that case checking only
2087 * "usbd_transfer_pending()" is not enough to tell if
2088 * the USB transfer is fully drained. We also need to
2089 * check the internal "doing_callback" flag.
2091 xfer->flags_int.draining = 1;
2094 * XXX hack, the wakeup of xfer can race conditions which
2095 * clear the pending status of the xfer.
2097 while (usbd_transfer_pending(xfer) ||
2098 xfer->flags_int.doing_callback) {
2101 * Wait until the current outstanding USB
2102 * transfer is complete !
2104 /* cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_lock); */
2105 lksleep(xfer, xfer->xroot->xfer_lock, 0, "DRAIN", hz);
2107 xfer->flags_int.draining = 0;
2108 USB_XFER_UNLOCK(xfer);
2111 struct usb_page_cache *
2112 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2114 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2116 return (&xfer->frbuffers[frindex]);
2119 void *
2120 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2122 struct usb_page_search page_info;
2124 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2126 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2127 return (page_info.buffer);
2130 /*------------------------------------------------------------------------*
2131 * usbd_xfer_get_fps_shift
2133 * The following function is only useful for isochronous transfers. It
2134 * returns how many times the frame execution rate has been shifted
2135 * down.
2137 * Return value:
2138 * Success: 0..3
2139 * Failure: 0
2140 *------------------------------------------------------------------------*/
2141 uint8_t
2142 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2144 return (xfer->fps_shift);
2147 usb_frlength_t
2148 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2150 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2152 return (xfer->frlengths[frindex]);
2155 /*------------------------------------------------------------------------*
2156 * usbd_xfer_set_frame_data
2158 * This function sets the pointer of the buffer that should
2159 * loaded directly into DMA for the given USB frame. Passing "ptr"
2160 * equal to NULL while the corresponding "frlength" is greater
2161 * than zero gives undefined results!
2162 *------------------------------------------------------------------------*/
2163 void
2164 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2165 void *ptr, usb_frlength_t len)
2167 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2169 /* set virtual address to load and length */
2170 xfer->frbuffers[frindex].buffer = ptr;
2171 usbd_xfer_set_frame_len(xfer, frindex, len);
2174 void
2175 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2176 void **ptr, int *len)
2178 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2180 if (ptr != NULL)
2181 *ptr = xfer->frbuffers[frindex].buffer;
2182 if (len != NULL)
2183 *len = xfer->frlengths[frindex];
2186 /*------------------------------------------------------------------------*
2187 * usbd_xfer_old_frame_length
2189 * This function returns the framelength of the given frame at the
2190 * time the transfer was submitted. This function can be used to
2191 * compute the starting data pointer of the next isochronous frame
2192 * when an isochronous transfer has completed.
2193 *------------------------------------------------------------------------*/
2194 usb_frlength_t
2195 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2197 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2199 return (xfer->frlengths[frindex + xfer->max_frame_count]);
2202 void
2203 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2204 int *nframes)
2206 if (actlen != NULL)
2207 *actlen = xfer->actlen;
2208 if (sumlen != NULL)
2209 *sumlen = xfer->sumlen;
2210 if (aframes != NULL)
2211 *aframes = xfer->aframes;
2212 if (nframes != NULL)
2213 *nframes = xfer->nframes;
2216 /*------------------------------------------------------------------------*
2217 * usbd_xfer_set_frame_offset
2219 * This function sets the frame data buffer offset relative to the beginning
2220 * of the USB DMA buffer allocated for this USB transfer.
2221 *------------------------------------------------------------------------*/
2222 void
2223 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2224 usb_frcount_t frindex)
2226 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2227 "when the USB buffer is external\n"));
2228 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2230 /* set virtual address to load */
2231 xfer->frbuffers[frindex].buffer =
2232 USB_ADD_BYTES(xfer->local_buffer, offset);
2235 void
2236 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2238 xfer->interval = i;
2241 void
2242 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2244 xfer->timeout = t;
2247 void
2248 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2250 xfer->nframes = n;
2253 usb_frcount_t
2254 usbd_xfer_max_frames(struct usb_xfer *xfer)
2256 return (xfer->max_frame_count);
2259 usb_frlength_t
2260 usbd_xfer_max_len(struct usb_xfer *xfer)
2262 return (xfer->max_data_length);
2265 usb_frlength_t
2266 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2268 return (xfer->max_frame_size);
2271 void
2272 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2273 usb_frlength_t len)
2275 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2277 xfer->frlengths[frindex] = len;
2280 /*------------------------------------------------------------------------*
2281 * usb_callback_proc - factored out code
2283 * This function performs USB callbacks.
2284 *------------------------------------------------------------------------*/
2285 static void
2286 usb_callback_proc(struct usb_proc_msg *_pm)
2288 struct usb_done_msg *pm = (void *)_pm;
2289 struct usb_xfer_root *info = pm->xroot;
2291 /* Change locking order */
2292 USB_BUS_UNLOCK(info->bus);
2295 * We exploit the fact that the mutex is the same for all
2296 * callbacks that will be called from this thread:
2298 lockmgr(info->xfer_lock, LK_EXCLUSIVE);
2299 USB_BUS_LOCK(info->bus);
2301 /* Continue where we lost track */
2302 usb_command_wrapper(&info->done_q,
2303 info->done_q.curr);
2305 lockmgr(info->xfer_lock, LK_RELEASE);
2308 /*------------------------------------------------------------------------*
2309 * usbd_callback_ss_done_defer
2311 * This function will defer the start, stop and done callback to the
2312 * correct thread.
2313 *------------------------------------------------------------------------*/
2314 static void
2315 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2317 struct usb_xfer_root *info = xfer->xroot;
2318 struct usb_xfer_queue *pq = &info->done_q;
2320 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2322 if (pq->curr != xfer) {
2323 usbd_transfer_enqueue(pq, xfer);
2325 if (!pq->recurse_1) {
2328 * We have to postpone the callback due to the fact we
2329 * will have a Lock Order Reversal, LOR, if we try to
2330 * proceed !
2332 if (usb_proc_msignal(info->done_p,
2333 &info->done_m[0], &info->done_m[1])) {
2334 /* ignore */
2336 } else {
2337 /* clear second recurse flag */
2338 pq->recurse_2 = 0;
2340 return;
2344 /*------------------------------------------------------------------------*
2345 * usbd_callback_wrapper
2347 * This is a wrapper for USB callbacks. This wrapper does some
2348 * auto-magic things like figuring out if we can call the callback
2349 * directly from the current context or if we need to wakeup the
2350 * interrupt process.
2351 *------------------------------------------------------------------------*/
2352 static void
2353 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2355 struct usb_xfer *xfer = pq->curr;
2356 struct usb_xfer_root *info = xfer->xroot;
2358 USB_BUS_LOCK_ASSERT(info->bus);
2359 if (!lockowned(info->xfer_lock)) {
2361 * Cases that end up here:
2363 * 5) HW interrupt done callback or other source.
2365 DPRINTFN(3, "case 5\n");
2368 * We have to postpone the callback due to the fact we
2369 * will have a Lock Order Reversal, LOR, if we try to
2370 * proceed !
2372 if (usb_proc_msignal(info->done_p,
2373 &info->done_m[0], &info->done_m[1])) {
2374 /* ignore */
2376 return;
2379 * Cases that end up here:
2381 * 1) We are starting a transfer
2382 * 2) We are prematurely calling back a transfer
2383 * 3) We are stopping a transfer
2384 * 4) We are doing an ordinary callback
2386 DPRINTFN(3, "case 1-4\n");
2387 /* get next USB transfer in the queue */
2388 info->done_q.curr = NULL;
2390 /* set flag in case of drain */
2391 xfer->flags_int.doing_callback = 1;
2393 USB_BUS_UNLOCK(info->bus);
2394 USB_BUS_LOCK_ASSERT_NOTOWNED(info->bus);
2396 /* set correct USB state for callback */
2397 if (!xfer->flags_int.transferring) {
2398 xfer->usb_state = USB_ST_SETUP;
2399 if (!xfer->flags_int.started) {
2400 /* we got stopped before we even got started */
2401 USB_BUS_LOCK(info->bus);
2402 goto done;
2404 } else {
2406 if (usbd_callback_wrapper_sub(xfer)) {
2407 /* the callback has been deferred */
2408 USB_BUS_LOCK(info->bus);
2409 goto done;
2411 #if USB_HAVE_POWERD
2412 /* decrement power reference */
2413 usbd_transfer_power_ref(xfer, -1);
2414 #endif
2415 xfer->flags_int.transferring = 0;
2417 if (xfer->error) {
2418 xfer->usb_state = USB_ST_ERROR;
2419 } else {
2420 /* set transferred state */
2421 xfer->usb_state = USB_ST_TRANSFERRED;
2422 #if USB_HAVE_BUSDMA
2423 /* sync DMA memory, if any */
2424 if (xfer->flags_int.bdma_enable &&
2425 (!xfer->flags_int.bdma_no_post_sync)) {
2426 usb_bdma_post_sync(xfer);
2428 #endif
2432 #if USB_HAVE_PF
2433 if (xfer->usb_state != USB_ST_SETUP)
2434 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2435 #endif
2436 /* call processing routine */
2437 (xfer->callback) (xfer, xfer->error);
2439 /* pickup the USB mutex again */
2440 USB_BUS_LOCK(info->bus);
2443 * Check if we got started after that we got cancelled, but
2444 * before we managed to do the callback.
2446 if ((!xfer->flags_int.open) &&
2447 (xfer->flags_int.started) &&
2448 (xfer->usb_state == USB_ST_ERROR)) {
2449 /* clear flag in case of drain */
2450 xfer->flags_int.doing_callback = 0;
2451 /* try to loop, but not recursivly */
2452 usb_command_wrapper(&info->done_q, xfer);
2453 return;
2456 done:
2457 /* clear flag in case of drain */
2458 xfer->flags_int.doing_callback = 0;
2461 * Check if we are draining.
2463 if (xfer->flags_int.draining &&
2464 (!xfer->flags_int.transferring)) {
2465 /* "usbd_transfer_drain()" is waiting for end of transfer */
2466 xfer->flags_int.draining = 0;
2467 /* cv_broadcast(&info->cv_drain); */
2468 wakeup(xfer);
2471 /* do the next callback, if any */
2472 usb_command_wrapper(&info->done_q,
2473 info->done_q.curr);
2476 /*------------------------------------------------------------------------*
2477 * usb_dma_delay_done_cb
2479 * This function is called when the DMA delay has been exectuded, and
2480 * will make sure that the callback is called to complete the USB
2481 * transfer. This code path is ususally only used when there is an USB
2482 * error like USB_ERR_CANCELLED.
2483 *------------------------------------------------------------------------*/
2484 void
2485 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2487 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2489 DPRINTFN(3, "Completed %p\n", xfer);
2491 /* queue callback for execution, again */
2492 usbd_transfer_done(xfer, 0);
2495 /*------------------------------------------------------------------------*
2496 * usbd_transfer_dequeue
2498 * - This function is used to remove an USB transfer from a USB
2499 * transfer queue.
2501 * - This function can be called multiple times in a row.
2502 *------------------------------------------------------------------------*/
2503 void
2504 usbd_transfer_dequeue(struct usb_xfer *xfer)
2506 struct usb_xfer_queue *pq;
2508 pq = xfer->wait_queue;
2509 if (pq) {
2510 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2511 xfer->wait_queue = NULL;
2515 /*------------------------------------------------------------------------*
2516 * usbd_transfer_enqueue
2518 * - This function is used to insert an USB transfer into a USB *
2519 * transfer queue.
2521 * - This function can be called multiple times in a row.
2522 *------------------------------------------------------------------------*/
2523 void
2524 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2527 * Insert the USB transfer into the queue, if it is not
2528 * already on a USB transfer queue:
2530 /* mpf ?
2531 KKASSERT(xfer->wait_queue == NULL);
2533 if (xfer->wait_queue == NULL) {
2534 xfer->wait_queue = pq;
2535 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2539 /*------------------------------------------------------------------------*
2540 * usbd_transfer_done
2542 * - This function is used to remove an USB transfer from the busdma,
2543 * pipe or interrupt queue.
2545 * - This function is used to queue the USB transfer on the done
2546 * queue.
2548 * - This function is used to stop any USB transfer timeouts.
2549 *------------------------------------------------------------------------*/
2550 void
2551 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2553 struct usb_xfer_root *info = xfer->xroot;
2555 USB_BUS_LOCK_ASSERT(info->bus);
2557 DPRINTF("err=%s\n", usbd_errstr(error));
2560 * If we are not transferring then just return.
2561 * This can happen during transfer cancel.
2563 if (!xfer->flags_int.transferring) {
2564 DPRINTF("not transferring\n");
2565 /* end of control transfer, if any */
2566 xfer->flags_int.control_act = 0;
2567 return;
2569 /* only set transfer error, if not already set */
2570 if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2571 xfer->error = error;
2573 /* stop any callouts */
2574 usb_callout_stop(&xfer->timeout_handle);
2577 * If we are waiting on a queue, just remove the USB transfer
2578 * from the queue, if any. We should have the required locks
2579 * locked to do the remove when this function is called.
2581 usbd_transfer_dequeue(xfer);
2583 #if USB_HAVE_BUSDMA
2584 if (lockowned(xfer->xroot->xfer_lock)) {
2585 struct usb_xfer_queue *pq;
2588 * If the private USB lock is not locked, then we assume
2589 * that the BUS-DMA load stage has been passed:
2591 pq = &info->dma_q;
2593 if (pq->curr == xfer) {
2594 /* start the next BUS-DMA load, if any */
2595 usb_command_wrapper(pq, NULL);
2598 #endif
2599 /* keep some statistics */
2600 if (xfer->error) {
2601 info->bus->stats_err.uds_requests
2602 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2603 } else {
2604 info->bus->stats_ok.uds_requests
2605 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2608 /* call the USB transfer callback */
2609 usbd_callback_ss_done_defer(xfer);
2612 /*------------------------------------------------------------------------*
2613 * usbd_transfer_start_cb
2615 * This function is called to start the USB transfer when
2616 * "xfer->interval" is greater than zero, and and the endpoint type is
2617 * BULK or CONTROL.
2618 *------------------------------------------------------------------------*/
2619 static void
2620 usbd_transfer_start_cb(void *arg)
2622 struct usb_xfer *xfer = arg;
2623 struct usb_endpoint *ep = xfer->endpoint;
2625 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2627 DPRINTF("start\n");
2629 #if USB_HAVE_PF
2630 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2631 #endif
2633 /* the transfer can now be cancelled */
2634 xfer->flags_int.can_cancel_immed = 1;
2636 /* start USB transfer, if no error */
2637 if (xfer->error == 0)
2638 (ep->methods->start) (xfer);
2640 /* check for transfer error */
2641 if (xfer->error) {
2642 /* some error has happened */
2643 usbd_transfer_done(xfer, 0);
2647 /*------------------------------------------------------------------------*
2648 * usbd_xfer_set_stall
2650 * This function is used to set the stall flag outside the
2651 * callback. This function is NULL safe.
2652 *------------------------------------------------------------------------*/
2653 void
2654 usbd_xfer_set_stall(struct usb_xfer *xfer)
2656 if (xfer == NULL) {
2657 /* tearing down */
2658 return;
2660 USB_XFER_LOCK_ASSERT(xfer);
2662 /* avoid any races by locking the USB mutex */
2663 USB_BUS_LOCK(xfer->xroot->bus);
2664 xfer->flags.stall_pipe = 1;
2665 USB_BUS_UNLOCK(xfer->xroot->bus);
2669 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2671 return (xfer->endpoint->is_stalled);
2674 /*------------------------------------------------------------------------*
2675 * usbd_transfer_clear_stall
2677 * This function is used to clear the stall flag outside the
2678 * callback. This function is NULL safe.
2679 *------------------------------------------------------------------------*/
2680 void
2681 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2683 if (xfer == NULL) {
2684 /* tearing down */
2685 return;
2687 USB_XFER_LOCK_ASSERT(xfer);
2689 /* avoid any races by locking the USB mutex */
2690 USB_BUS_LOCK(xfer->xroot->bus);
2692 xfer->flags.stall_pipe = 0;
2694 USB_BUS_UNLOCK(xfer->xroot->bus);
2697 /*------------------------------------------------------------------------*
2698 * usbd_pipe_start
2700 * This function is used to add an USB transfer to the pipe transfer list.
2701 *------------------------------------------------------------------------*/
2702 void
2703 usbd_pipe_start(struct usb_xfer_queue *pq)
2705 struct usb_endpoint *ep;
2706 struct usb_xfer *xfer;
2707 uint8_t type;
2709 xfer = pq->curr;
2710 ep = xfer->endpoint;
2712 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2715 * If the endpoint is already stalled we do nothing !
2717 if (ep->is_stalled) {
2718 return;
2721 * Check if we are supposed to stall the endpoint:
2723 if (xfer->flags.stall_pipe) {
2724 struct usb_device *udev;
2725 struct usb_xfer_root *info;
2727 /* clear stall command */
2728 xfer->flags.stall_pipe = 0;
2730 /* get pointer to USB device */
2731 info = xfer->xroot;
2732 udev = info->udev;
2735 * Only stall BULK and INTERRUPT endpoints.
2737 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2738 if ((type == UE_BULK) ||
2739 (type == UE_INTERRUPT)) {
2740 uint8_t did_stall;
2742 did_stall = 1;
2744 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2745 (udev->bus->methods->set_stall) (
2746 udev, ep, &did_stall);
2747 } else if (udev->ctrl_xfer[1]) {
2748 info = udev->ctrl_xfer[1]->xroot;
2749 usb_proc_msignal(
2750 USB_BUS_NON_GIANT_PROC(info->bus),
2751 &udev->cs_msg[0], &udev->cs_msg[1]);
2752 } else {
2753 /* should not happen */
2754 DPRINTFN(0, "No stall handler\n");
2757 * Check if we should stall. Some USB hardware
2758 * handles set- and clear-stall in hardware.
2760 if (did_stall) {
2762 * The transfer will be continued when
2763 * the clear-stall control endpoint
2764 * message is received.
2766 ep->is_stalled = 1;
2767 return;
2769 } else if (type == UE_ISOCHRONOUS) {
2772 * Make sure any FIFO overflow or other FIFO
2773 * error conditions go away by resetting the
2774 * endpoint FIFO through the clear stall
2775 * method.
2777 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2778 (udev->bus->methods->clear_stall) (udev, ep);
2782 /* Set or clear stall complete - special case */
2783 if (xfer->nframes == 0) {
2784 /* we are complete */
2785 xfer->aframes = 0;
2786 usbd_transfer_done(xfer, 0);
2787 return;
2790 * Handled cases:
2792 * 1) Start the first transfer queued.
2794 * 2) Re-start the current USB transfer.
2797 * Check if there should be any
2798 * pre transfer start delay:
2800 if (xfer->interval > 0) {
2801 type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2802 if ((type == UE_BULK) ||
2803 (type == UE_CONTROL)) {
2804 usbd_transfer_timeout_ms(xfer,
2805 &usbd_transfer_start_cb,
2806 xfer->interval);
2807 return;
2810 DPRINTF("start\n");
2812 #if USB_HAVE_PF
2813 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2814 #endif
2815 /* the transfer can now be cancelled */
2816 xfer->flags_int.can_cancel_immed = 1;
2818 /* start USB transfer, if no error */
2819 if (xfer->error == 0)
2820 (ep->methods->start) (xfer);
2822 /* check for transfer error */
2823 if (xfer->error) {
2824 /* some error has happened */
2825 usbd_transfer_done(xfer, 0);
2829 /*------------------------------------------------------------------------*
2830 * usbd_transfer_timeout_ms
2832 * This function is used to setup a timeout on the given USB
2833 * transfer. If the timeout has been deferred the callback given by
2834 * "cb" will get called after "ms" milliseconds.
2835 *------------------------------------------------------------------------*/
2836 void
2837 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2838 void (*cb) (void *arg), usb_timeout_t ms)
2840 USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2842 /* defer delay */
2843 usb_callout_reset(&xfer->timeout_handle,
2844 USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2847 /*------------------------------------------------------------------------*
2848 * usbd_callback_wrapper_sub
2850 * - This function will update variables in an USB transfer after
2851 * that the USB transfer is complete.
2853 * - This function is used to start the next USB transfer on the
2854 * ep transfer queue, if any.
2856 * NOTE: In some special cases the USB transfer will not be removed from
2857 * the pipe queue, but remain first. To enforce USB transfer removal call
2858 * this function passing the error code "USB_ERR_CANCELLED".
2860 * Return values:
2861 * 0: Success.
2862 * Else: The callback has been deferred.
2863 *------------------------------------------------------------------------*/
2864 static uint8_t
2865 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2867 struct usb_endpoint *ep;
2868 struct usb_bus *bus;
2869 usb_frcount_t x;
2871 bus = xfer->xroot->bus;
2873 if ((!xfer->flags_int.open) &&
2874 (!xfer->flags_int.did_close)) {
2875 DPRINTF("close\n");
2876 USB_BUS_LOCK(bus);
2877 (xfer->endpoint->methods->close) (xfer);
2878 USB_BUS_UNLOCK(bus);
2879 /* only close once */
2880 xfer->flags_int.did_close = 1;
2881 return (1); /* wait for new callback */
2884 * If we have a non-hardware induced error we
2885 * need to do the DMA delay!
2887 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2888 (xfer->error == USB_ERR_CANCELLED ||
2889 xfer->error == USB_ERR_TIMEOUT ||
2890 bus->methods->start_dma_delay != NULL)) {
2892 usb_timeout_t temp;
2894 /* only delay once */
2895 xfer->flags_int.did_dma_delay = 1;
2897 /* we can not cancel this delay */
2898 xfer->flags_int.can_cancel_immed = 0;
2900 temp = usbd_get_dma_delay(xfer->xroot->udev);
2902 DPRINTFN(3, "DMA delay, %u ms, "
2903 "on %p\n", temp, xfer);
2905 if (temp != 0) {
2906 USB_BUS_LOCK(bus);
2908 * Some hardware solutions have dedicated
2909 * events when it is safe to free DMA'ed
2910 * memory. For the other hardware platforms we
2911 * use a static delay.
2913 if (bus->methods->start_dma_delay != NULL) {
2914 (bus->methods->start_dma_delay) (xfer);
2915 } else {
2916 usbd_transfer_timeout_ms(xfer,
2917 (void (*)(void *))&usb_dma_delay_done_cb,
2918 temp);
2920 USB_BUS_UNLOCK(bus);
2921 return (1); /* wait for new callback */
2924 /* check actual number of frames */
2925 if (xfer->aframes > xfer->nframes) {
2926 if (xfer->error == 0) {
2927 panic("%s: actual number of frames, %d, is "
2928 "greater than initial number of frames, %d\n",
2929 __func__, xfer->aframes, xfer->nframes);
2930 } else {
2931 /* just set some valid value */
2932 xfer->aframes = xfer->nframes;
2935 /* compute actual length */
2936 xfer->actlen = 0;
2938 for (x = 0; x != xfer->aframes; x++) {
2939 xfer->actlen += xfer->frlengths[x];
2943 * Frames that were not transferred get zero actual length in
2944 * case the USB device driver does not check the actual number
2945 * of frames transferred, "xfer->aframes":
2947 for (; x < xfer->nframes; x++) {
2948 usbd_xfer_set_frame_len(xfer, x, 0);
2951 /* check actual length */
2952 if (xfer->actlen > xfer->sumlen) {
2953 if (xfer->error == 0) {
2954 panic("%s: actual length, %d, is greater than "
2955 "initial length, %d\n",
2956 __func__, xfer->actlen, xfer->sumlen);
2957 } else {
2958 /* just set some valid value */
2959 xfer->actlen = xfer->sumlen;
2962 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2963 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2964 xfer->aframes, xfer->nframes);
2966 if (xfer->error) {
2967 /* end of control transfer, if any */
2968 xfer->flags_int.control_act = 0;
2970 /* check if we should block the execution queue */
2971 if ((xfer->error != USB_ERR_CANCELLED) &&
2972 (xfer->flags.pipe_bof)) {
2973 DPRINTFN(2, "xfer=%p: Block On Failure "
2974 "on endpoint=%p\n", xfer, xfer->endpoint);
2975 goto done;
2977 } else {
2978 /* check for short transfers */
2979 if (xfer->actlen < xfer->sumlen) {
2981 /* end of control transfer, if any */
2982 xfer->flags_int.control_act = 0;
2984 if (!xfer->flags_int.short_xfer_ok) {
2985 xfer->error = USB_ERR_SHORT_XFER;
2986 if (xfer->flags.pipe_bof) {
2987 DPRINTFN(2, "xfer=%p: Block On Failure on "
2988 "Short Transfer on endpoint %p.\n",
2989 xfer, xfer->endpoint);
2990 goto done;
2993 } else {
2995 * Check if we are in the middle of a
2996 * control transfer:
2998 if (xfer->flags_int.control_act) {
2999 DPRINTFN(5, "xfer=%p: Control transfer "
3000 "active on endpoint=%p\n", xfer, xfer->endpoint);
3001 goto done;
3006 ep = xfer->endpoint;
3009 * If the current USB transfer is completing we need to start the
3010 * next one:
3012 USB_BUS_LOCK(bus);
3013 if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
3014 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
3016 if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
3017 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
3018 /* there is another USB transfer waiting */
3019 } else {
3020 /* this is the last USB transfer */
3021 /* clear isochronous sync flag */
3022 xfer->endpoint->is_synced = 0;
3025 USB_BUS_UNLOCK(bus);
3026 done:
3027 return (0);
3030 /*------------------------------------------------------------------------*
3031 * usb_command_wrapper
3033 * This function is used to execute commands non-recursivly on an USB
3034 * transfer.
3035 *------------------------------------------------------------------------*/
3036 void
3037 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3039 if (xfer) {
3041 * If the transfer is not already processing,
3042 * queue it!
3044 if (pq->curr != xfer) {
3045 usbd_transfer_enqueue(pq, xfer);
3046 if (pq->curr != NULL) {
3047 /* something is already processing */
3048 DPRINTFN(6, "busy %p\n", pq->curr);
3049 return;
3052 } else {
3053 /* Get next element in queue */
3054 pq->curr = NULL;
3057 if (!pq->recurse_1) {
3059 do {
3061 /* set both recurse flags */
3062 pq->recurse_1 = 1;
3063 pq->recurse_2 = 1;
3065 if (pq->curr == NULL) {
3066 xfer = TAILQ_FIRST(&pq->head);
3067 if (xfer) {
3068 TAILQ_REMOVE(&pq->head, xfer,
3069 wait_entry);
3070 xfer->wait_queue = NULL;
3071 pq->curr = xfer;
3072 } else {
3073 break;
3076 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3077 (pq->command) (pq);
3078 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3080 } while (!pq->recurse_2);
3082 /* clear first recurse flag */
3083 pq->recurse_1 = 0;
3085 } else {
3086 /* clear second recurse flag */
3087 pq->recurse_2 = 0;
3091 /*------------------------------------------------------------------------*
3092 * usbd_ctrl_transfer_setup
3094 * This function is used to setup the default USB control endpoint
3095 * transfer.
3096 *------------------------------------------------------------------------*/
3097 void
3098 usbd_ctrl_transfer_setup(struct usb_device *udev)
3100 struct usb_xfer *xfer;
3101 uint8_t no_resetup;
3102 uint8_t iface_index;
3104 /* check for root HUB */
3105 if (udev->parent_hub == NULL)
3106 return;
3107 repeat:
3109 xfer = udev->ctrl_xfer[0];
3110 if (xfer) {
3111 USB_XFER_LOCK(xfer);
3112 no_resetup =
3113 ((xfer->address == udev->address) &&
3114 (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3115 udev->ddesc.bMaxPacketSize));
3116 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3117 if (no_resetup) {
3119 * NOTE: checking "xfer->address" and
3120 * starting the USB transfer must be
3121 * atomic!
3123 usbd_transfer_start(xfer);
3126 USB_XFER_UNLOCK(xfer);
3127 } else {
3128 no_resetup = 0;
3131 if (no_resetup) {
3133 * All parameters are exactly the same like before.
3134 * Just return.
3136 return;
3139 * Update wMaxPacketSize for the default control endpoint:
3141 udev->ctrl_ep_desc.wMaxPacketSize[0] =
3142 udev->ddesc.bMaxPacketSize;
3145 * Unsetup any existing USB transfer:
3147 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3150 * Reset clear stall error counter.
3152 udev->clear_stall_errors = 0;
3155 * Try to setup a new USB transfer for the
3156 * default control endpoint:
3158 iface_index = 0;
3159 if (usbd_transfer_setup(udev, &iface_index,
3160 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3161 &udev->device_lock)) {
3162 DPRINTFN(0, "could not setup default "
3163 "USB transfer\n");
3164 } else {
3165 goto repeat;
3169 /*------------------------------------------------------------------------*
3170 * usbd_clear_data_toggle - factored out code
3172 * NOTE: the intention of this function is not to reset the hardware
3173 * data toggle.
3174 *------------------------------------------------------------------------*/
3175 void
3176 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3178 USB_BUS_LOCK_ASSERT(udev->bus);
3180 /* check that we have a valid case */
3181 if (udev->flags.usb_mode == USB_MODE_HOST &&
3182 udev->parent_hub != NULL &&
3183 udev->bus->methods->clear_stall != NULL &&
3184 ep->methods != NULL) {
3185 (udev->bus->methods->clear_stall) (udev, ep);
3189 /*------------------------------------------------------------------------*
3190 * usbd_clear_data_toggle - factored out code
3192 * NOTE: the intention of this function is not to reset the hardware
3193 * data toggle on the USB device side.
3194 *------------------------------------------------------------------------*/
3195 void
3196 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3198 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3200 USB_BUS_LOCK(udev->bus);
3201 ep->toggle_next = 0;
3202 /* some hardware needs a callback to clear the data toggle */
3203 usbd_clear_stall_locked(udev, ep);
3204 USB_BUS_UNLOCK(udev->bus);
3207 /*------------------------------------------------------------------------*
3208 * usbd_clear_stall_callback - factored out clear stall callback
3210 * Input parameters:
3211 * xfer1: Clear Stall Control Transfer
3212 * xfer2: Stalled USB Transfer
3214 * This function is NULL safe.
3216 * Return values:
3217 * 0: In progress
3218 * Else: Finished
3220 * Clear stall config example:
3222 * static const struct usb_config my_clearstall = {
3223 * .type = UE_CONTROL,
3224 * .endpoint = 0,
3225 * .direction = UE_DIR_ANY,
3226 * .interval = 50, //50 milliseconds
3227 * .bufsize = sizeof(struct usb_device_request),
3228 * .timeout = 1000, //1.000 seconds
3229 * .callback = &my_clear_stall_callback, // **
3230 * .usb_mode = USB_MODE_HOST,
3231 * };
3233 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3234 * passing the correct parameters.
3235 *------------------------------------------------------------------------*/
3236 uint8_t
3237 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3238 struct usb_xfer *xfer2)
3240 struct usb_device_request req;
3242 if (xfer2 == NULL) {
3243 /* looks like we are tearing down */
3244 DPRINTF("NULL input parameter\n");
3245 return (0);
3247 USB_XFER_LOCK_ASSERT(xfer1);
3248 USB_XFER_LOCK_ASSERT(xfer2);
3250 switch (USB_GET_STATE(xfer1)) {
3251 case USB_ST_SETUP:
3254 * pre-clear the data toggle to DATA0 ("umass.c" and
3255 * "ata-usb.c" depends on this)
3258 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3260 /* setup a clear-stall packet */
3262 req.bmRequestType = UT_WRITE_ENDPOINT;
3263 req.bRequest = UR_CLEAR_FEATURE;
3264 USETW(req.wValue, UF_ENDPOINT_HALT);
3265 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3266 req.wIndex[1] = 0;
3267 USETW(req.wLength, 0);
3270 * "usbd_transfer_setup_sub()" will ensure that
3271 * we have sufficient room in the buffer for
3272 * the request structure!
3275 /* copy in the transfer */
3277 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3279 /* set length */
3280 xfer1->frlengths[0] = sizeof(req);
3281 xfer1->nframes = 1;
3283 usbd_transfer_submit(xfer1);
3284 return (0);
3286 case USB_ST_TRANSFERRED:
3287 break;
3289 default: /* Error */
3290 if (xfer1->error == USB_ERR_CANCELLED) {
3291 return (0);
3293 break;
3295 return (1); /* Clear Stall Finished */
3298 /*------------------------------------------------------------------------*
3299 * usbd_transfer_poll
3301 * The following function gets called from the USB keyboard driver and
3302 * UMASS when the system has paniced.
3304 * NOTE: It is currently not possible to resume normal operation on
3305 * the USB controller which has been polled, due to clearing of the
3306 * "up_dsleep" and "up_msleep" flags.
3307 *------------------------------------------------------------------------*/
3308 void
3309 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3311 struct usb_xfer *xfer;
3312 struct usb_xfer_root *xroot;
3313 struct usb_device *udev;
3314 struct usb_proc_msg *pm;
3315 uint16_t n;
3316 uint16_t drop_bus;
3317 uint16_t drop_xfer;
3319 for (n = 0; n != max; n++) {
3320 /* Extra checks to avoid panic */
3321 xfer = ppxfer[n];
3322 if (xfer == NULL)
3323 continue; /* no USB transfer */
3324 xroot = xfer->xroot;
3325 if (xroot == NULL)
3326 continue; /* no USB root */
3327 udev = xroot->udev;
3328 if (udev == NULL)
3329 continue; /* no USB device */
3330 if (udev->bus == NULL)
3331 continue; /* no BUS structure */
3332 if (udev->bus->methods == NULL)
3333 continue; /* no BUS methods */
3334 if (udev->bus->methods->xfer_poll == NULL)
3335 continue; /* no poll method */
3337 /* make sure that the BUS mutex is not locked */
3338 drop_bus = 0;
3339 while (lockowned(&xroot->udev->bus->bus_lock)) {
3340 lockmgr(&xroot->udev->bus->bus_lock, LK_RELEASE);
3341 drop_bus++;
3344 /* make sure that the transfer mutex is not locked */
3345 drop_xfer = 0;
3346 while (lockowned(xroot->xfer_lock)) {
3347 lockmgr(xroot->xfer_lock, LK_RELEASE);
3348 drop_xfer++;
3351 /* Make sure cv_signal() and cv_broadcast() is not called */
3352 USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3353 USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3354 USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3355 USB_BUS_NON_GIANT_PROC(udev->bus)->up_msleep = 0;
3357 /* poll USB hardware */
3358 (udev->bus->methods->xfer_poll) (udev->bus);
3360 USB_BUS_LOCK(xroot->bus);
3362 /* check for clear stall */
3363 if (udev->ctrl_xfer[1] != NULL) {
3365 /* poll clear stall start */
3366 pm = &udev->cs_msg[0].hdr;
3367 (pm->pm_callback) (pm);
3368 /* poll clear stall done thread */
3369 pm = &udev->ctrl_xfer[1]->
3370 xroot->done_m[0].hdr;
3371 (pm->pm_callback) (pm);
3374 /* poll done thread */
3375 pm = &xroot->done_m[0].hdr;
3376 (pm->pm_callback) (pm);
3378 USB_BUS_UNLOCK(xroot->bus);
3380 /* restore transfer mutex */
3381 while (drop_xfer--)
3382 lockmgr(xroot->xfer_lock, LK_EXCLUSIVE);
3384 /* restore BUS mutex */
3385 while (drop_bus--)
3386 lockmgr(&xroot->udev->bus->bus_lock, LK_EXCLUSIVE);
3390 static void
3391 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3392 uint8_t type, enum usb_dev_speed speed)
3394 static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3395 [USB_SPEED_LOW] = 8,
3396 [USB_SPEED_FULL] = 64,
3397 [USB_SPEED_HIGH] = 1024,
3398 [USB_SPEED_VARIABLE] = 1024,
3399 [USB_SPEED_SUPER] = 1024,
3402 static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3403 [USB_SPEED_LOW] = 0, /* invalid */
3404 [USB_SPEED_FULL] = 1023,
3405 [USB_SPEED_HIGH] = 1024,
3406 [USB_SPEED_VARIABLE] = 3584,
3407 [USB_SPEED_SUPER] = 1024,
3410 static const uint16_t control_min[USB_SPEED_MAX] = {
3411 [USB_SPEED_LOW] = 8,
3412 [USB_SPEED_FULL] = 8,
3413 [USB_SPEED_HIGH] = 64,
3414 [USB_SPEED_VARIABLE] = 512,
3415 [USB_SPEED_SUPER] = 512,
3418 static const uint16_t bulk_min[USB_SPEED_MAX] = {
3419 [USB_SPEED_LOW] = 8,
3420 [USB_SPEED_FULL] = 8,
3421 [USB_SPEED_HIGH] = 512,
3422 [USB_SPEED_VARIABLE] = 512,
3423 [USB_SPEED_SUPER] = 1024,
3426 uint16_t temp;
3428 memset(ptr, 0, sizeof(*ptr));
3430 switch (type) {
3431 case UE_INTERRUPT:
3432 ptr->range.max = intr_range_max[speed];
3433 break;
3434 case UE_ISOCHRONOUS:
3435 ptr->range.max = isoc_range_max[speed];
3436 break;
3437 default:
3438 if (type == UE_BULK)
3439 temp = bulk_min[speed];
3440 else /* UE_CONTROL */
3441 temp = control_min[speed];
3443 /* default is fixed */
3444 ptr->fixed[0] = temp;
3445 ptr->fixed[1] = temp;
3446 ptr->fixed[2] = temp;
3447 ptr->fixed[3] = temp;
3449 if (speed == USB_SPEED_FULL) {
3450 /* multiple sizes */
3451 ptr->fixed[1] = 16;
3452 ptr->fixed[2] = 32;
3453 ptr->fixed[3] = 64;
3455 if ((speed == USB_SPEED_VARIABLE) &&
3456 (type == UE_BULK)) {
3457 /* multiple sizes */
3458 ptr->fixed[2] = 1024;
3459 ptr->fixed[3] = 1536;
3461 break;
3465 void *
3466 usbd_xfer_softc(struct usb_xfer *xfer)
3468 return (xfer->priv_sc);
3471 void *
3472 usbd_xfer_get_priv(struct usb_xfer *xfer)
3474 return (xfer->priv_fifo);
3477 void
3478 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3480 xfer->priv_fifo = ptr;
3483 uint8_t
3484 usbd_xfer_state(struct usb_xfer *xfer)
3486 return (xfer->usb_state);
3489 void
3490 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3492 switch (flag) {
3493 case USB_FORCE_SHORT_XFER:
3494 xfer->flags.force_short_xfer = 1;
3495 break;
3496 case USB_SHORT_XFER_OK:
3497 xfer->flags.short_xfer_ok = 1;
3498 break;
3499 case USB_MULTI_SHORT_OK:
3500 xfer->flags.short_frames_ok = 1;
3501 break;
3502 case USB_MANUAL_STATUS:
3503 xfer->flags.manual_status = 1;
3504 break;
3508 void
3509 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3511 switch (flag) {
3512 case USB_FORCE_SHORT_XFER:
3513 xfer->flags.force_short_xfer = 0;
3514 break;
3515 case USB_SHORT_XFER_OK:
3516 xfer->flags.short_xfer_ok = 0;
3517 break;
3518 case USB_MULTI_SHORT_OK:
3519 xfer->flags.short_frames_ok = 0;
3520 break;
3521 case USB_MANUAL_STATUS:
3522 xfer->flags.manual_status = 0;
3523 break;
3528 * The following function returns in milliseconds when the isochronous
3529 * transfer was completed by the hardware. The returned value wraps
3530 * around 65536 milliseconds.
3532 uint16_t
3533 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3535 return (xfer->isoc_time_complete);
3539 * The following function returns non-zero if the max packet size
3540 * field was clamped to a valid value. Else it returns zero.
3542 uint8_t
3543 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3545 return (xfer->flags_int.maxp_was_clamped);