Lines Matching +full:transfer +full:- +full:function

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008-2021 Hans Petter Selasky. All rights reserved.
81 /* This transfer is used for generic control endpoint transfers */
93 /* This transfer is used for generic clear stall only */
108 /* This transfer is used for generic control endpoint transfers */
119 /* This transfer is used for generic clear stall only */
133 /* function prototypes */
147 /*------------------------------------------------------------------------*
149 *------------------------------------------------------------------------*/
153 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) in usb_request_callback()
159 /*------------------------------------------------------------------------*
162 * This function updates the maximum frame size, hence high speed USB
163 * can transfer multiple consecutive packets.
164 *------------------------------------------------------------------------*/
169 /* this computation should not overflow 16-bit */ in usbd_update_max_frame_size()
172 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count; in usbd_update_max_frame_size()
175 /*------------------------------------------------------------------------*
178 * The following function is called when we need to
184 *------------------------------------------------------------------------*/
191 mtod = udev->bus->methods; in usbd_get_dma_delay()
194 if (mtod->get_dma_delay) { in usbd_get_dma_delay()
195 (mtod->get_dma_delay) (udev, &temp); in usbd_get_dma_delay()
206 /*------------------------------------------------------------------------*
209 * This function will allocate one or more DMA'able memory chunks
213 * If the "align" argument is equal to "1" a non-contiguous allocation
220 *------------------------------------------------------------------------*/
248 size = -((-size) & (-align)); in usbd_transfer_setup_sub_malloc()
251 * Try multi-allocation chunks to reduce the number of DMA in usbd_transfer_setup_sub_malloc()
255 /* special case - non-cached multi page DMA memory */ in usbd_transfer_setup_sub_malloc()
280 * there is one list for auto-free and another list for in usbd_transfer_setup_sub_malloc()
281 * non-auto-free which only holds the mapping and not the in usbd_transfer_setup_sub_malloc()
284 if (parm->buf == NULL) { in usbd_transfer_setup_sub_malloc()
285 /* reserve memory (auto-free) */ in usbd_transfer_setup_sub_malloc()
286 parm->dma_page_ptr += n_dma_pc * n_dma_pg; in usbd_transfer_setup_sub_malloc()
287 parm->dma_page_cache_ptr += n_dma_pc; in usbd_transfer_setup_sub_malloc()
289 /* reserve memory (no-auto-free) */ in usbd_transfer_setup_sub_malloc()
290 parm->dma_page_ptr += count * n_dma_pg; in usbd_transfer_setup_sub_malloc()
291 parm->xfer_page_cache_ptr += count; in usbd_transfer_setup_sub_malloc()
296 parm->dma_page_cache_ptr[x].tag_parent = in usbd_transfer_setup_sub_malloc()
297 &parm->curr_xfer->xroot->dma_parent_tag; in usbd_transfer_setup_sub_malloc()
301 parm->xfer_page_cache_ptr[x].tag_parent = in usbd_transfer_setup_sub_malloc()
302 &parm->curr_xfer->xroot->dma_parent_tag; in usbd_transfer_setup_sub_malloc()
307 *ppc = parm->xfer_page_cache_ptr; in usbd_transfer_setup_sub_malloc()
309 *ppc = parm->dma_page_cache_ptr; in usbd_transfer_setup_sub_malloc()
313 pc = parm->xfer_page_cache_ptr; in usbd_transfer_setup_sub_malloc()
314 pg = parm->dma_page_ptr; in usbd_transfer_setup_sub_malloc()
322 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr, in usbd_transfer_setup_sub_malloc()
327 parm->dma_page_cache_ptr++; in usbd_transfer_setup_sub_malloc()
337 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr, in usbd_transfer_setup_sub_malloc()
342 buf = parm->dma_page_cache_ptr->buffer; in usbd_transfer_setup_sub_malloc()
344 parm->dma_page_cache_ptr++; in usbd_transfer_setup_sub_malloc()
347 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) { in usbd_transfer_setup_sub_malloc()
348 /* Load sub-chunk into DMA */ in usbd_transfer_setup_sub_malloc()
352 pc->buffer = USB_ADD_BYTES(buf, y * size); in usbd_transfer_setup_sub_malloc()
353 pc->page_start = pg; in usbd_transfer_setup_sub_malloc()
355 USB_MTX_LOCK(pc->tag_parent->mtx); in usbd_transfer_setup_sub_malloc()
357 USB_MTX_UNLOCK(pc->tag_parent->mtx); in usbd_transfer_setup_sub_malloc()
360 USB_MTX_UNLOCK(pc->tag_parent->mtx); in usbd_transfer_setup_sub_malloc()
365 parm->xfer_page_cache_ptr = pc; in usbd_transfer_setup_sub_malloc()
366 parm->dma_page_ptr = pg; in usbd_transfer_setup_sub_malloc()
371 /*------------------------------------------------------------------------*
374 * This function returns the maximum single frame length as computed by
378 *------------------------------------------------------------------------*/
388 max_packet_size = UGETW(edesc->wMaxPacketSize); in usbd_get_max_frame_length()
390 type = (edesc->bmAttributes & UE_XFERTYPE); in usbd_get_max_frame_length()
413 max_packet_count += ecomp->bMaxBurst; in usbd_get_max_frame_length()
428 ecomp->bmAttributes) + 1; in usbd_get_max_frame_length()
446 /*------------------------------------------------------------------------*
447 * usbd_transfer_setup_sub - transfer setup subroutine
449 * This function must be called from the "xfer_setup" callback of the
451 * transfer. This function will setup correct packet sizes, buffer
454 *------------------------------------------------------------------------*/
462 struct usb_xfer *xfer = parm->curr_xfer; in usbd_transfer_setup_sub()
463 const struct usb_config *setup = parm->curr_setup; in usbd_transfer_setup_sub()
476 * calling this function. in usbd_transfer_setup_sub()
478 if ((parm->hc_max_packet_size == 0) || in usbd_transfer_setup_sub()
479 (parm->hc_max_packet_count == 0) || in usbd_transfer_setup_sub()
480 (parm->hc_max_frame_size == 0)) { in usbd_transfer_setup_sub()
481 parm->err = USB_ERR_INVAL; in usbd_transfer_setup_sub()
484 edesc = xfer->endpoint->edesc; in usbd_transfer_setup_sub()
485 ecomp = xfer->endpoint->ecomp; in usbd_transfer_setup_sub()
487 type = (edesc->bmAttributes & UE_XFERTYPE); in usbd_transfer_setup_sub()
489 xfer->flags = setup->flags; in usbd_transfer_setup_sub()
490 xfer->nframes = setup->frames; in usbd_transfer_setup_sub()
491 xfer->timeout = setup->timeout; in usbd_transfer_setup_sub()
492 xfer->callback = setup->callback; in usbd_transfer_setup_sub()
493 xfer->interval = setup->interval; in usbd_transfer_setup_sub()
494 xfer->endpointno = edesc->bEndpointAddress; in usbd_transfer_setup_sub()
495 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); in usbd_transfer_setup_sub()
496 xfer->max_packet_count = 1; in usbd_transfer_setup_sub()
498 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode; in usbd_transfer_setup_sub()
500 parm->bufsize = setup->bufsize; in usbd_transfer_setup_sub()
502 switch (parm->speed) { in usbd_transfer_setup_sub()
507 xfer->max_packet_count += in usbd_transfer_setup_sub()
508 (xfer->max_packet_size >> 11) & 3; in usbd_transfer_setup_sub()
511 if (xfer->max_packet_count > 3) in usbd_transfer_setup_sub()
512 xfer->max_packet_count = 3; in usbd_transfer_setup_sub()
517 xfer->max_packet_size &= 0x7FF; in usbd_transfer_setup_sub()
520 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; in usbd_transfer_setup_sub()
523 xfer->max_packet_count += ecomp->bMaxBurst; in usbd_transfer_setup_sub()
525 if ((xfer->max_packet_count == 0) || in usbd_transfer_setup_sub()
526 (xfer->max_packet_count > 16)) in usbd_transfer_setup_sub()
527 xfer->max_packet_count = 16; in usbd_transfer_setup_sub()
531 xfer->max_packet_count = 1; in usbd_transfer_setup_sub()
538 ecomp->bmAttributes) + 1; in usbd_transfer_setup_sub()
542 xfer->max_packet_count *= mult; in usbd_transfer_setup_sub()
548 xfer->max_packet_size &= 0x7FF; in usbd_transfer_setup_sub()
555 if (xfer->max_packet_count > parm->hc_max_packet_count) { in usbd_transfer_setup_sub()
556 xfer->max_packet_count = parm->hc_max_packet_count; in usbd_transfer_setup_sub()
561 maxp_old = xfer->max_packet_size; in usbd_transfer_setup_sub()
565 if ((xfer->max_packet_size > parm->hc_max_packet_size) || in usbd_transfer_setup_sub()
566 (xfer->max_packet_size == 0)) { in usbd_transfer_setup_sub()
567 xfer->max_packet_size = parm->hc_max_packet_size; in usbd_transfer_setup_sub()
571 usbd_get_std_packet_size(&std_size, type, parm->speed); in usbd_transfer_setup_sub()
574 if (xfer->max_packet_size < std_size.range.min) { in usbd_transfer_setup_sub()
575 xfer->max_packet_size = std_size.range.min; in usbd_transfer_setup_sub()
577 if (xfer->max_packet_size > std_size.range.max) { in usbd_transfer_setup_sub()
578 xfer->max_packet_size = std_size.range.max; in usbd_transfer_setup_sub()
581 if (xfer->max_packet_size >= std_size.fixed[3]) { in usbd_transfer_setup_sub()
582 xfer->max_packet_size = std_size.fixed[3]; in usbd_transfer_setup_sub()
583 } else if (xfer->max_packet_size >= std_size.fixed[2]) { in usbd_transfer_setup_sub()
584 xfer->max_packet_size = std_size.fixed[2]; in usbd_transfer_setup_sub()
585 } else if (xfer->max_packet_size >= std_size.fixed[1]) { in usbd_transfer_setup_sub()
586 xfer->max_packet_size = std_size.fixed[1]; in usbd_transfer_setup_sub()
589 xfer->max_packet_size = std_size.fixed[0]; in usbd_transfer_setup_sub()
597 if (maxp_old != xfer->max_packet_size) in usbd_transfer_setup_sub()
598 xfer->flags_int.maxp_was_clamped = 1; in usbd_transfer_setup_sub()
604 /* check interrupt interval and transfer pre-delay */ in usbd_transfer_setup_sub()
609 xfer->interval = 0; /* not used, must be zero */ in usbd_transfer_setup_sub()
610 xfer->flags_int.isochronous_xfr = 1; /* set flag */ in usbd_transfer_setup_sub()
612 if (xfer->timeout == 0) { in usbd_transfer_setup_sub()
617 xfer->timeout = 1000 / 4; in usbd_transfer_setup_sub()
619 switch (parm->speed) { in usbd_transfer_setup_sub()
623 xfer->fps_shift = 0; in usbd_transfer_setup_sub()
627 xfer->fps_shift = edesc->bInterval; in usbd_transfer_setup_sub()
628 if (xfer->fps_shift > 0) in usbd_transfer_setup_sub()
629 xfer->fps_shift--; in usbd_transfer_setup_sub()
630 if (xfer->fps_shift > 3) in usbd_transfer_setup_sub()
631 xfer->fps_shift = 3; in usbd_transfer_setup_sub()
632 if (xfer->flags.pre_scale_frames != 0) in usbd_transfer_setup_sub()
633 xfer->nframes <<= (3 - xfer->fps_shift); in usbd_transfer_setup_sub()
637 if (xfer->nframes > frame_limit) { in usbd_transfer_setup_sub()
642 parm->err = USB_ERR_INVAL; in usbd_transfer_setup_sub()
645 if (xfer->nframes == 0) { in usbd_transfer_setup_sub()
649 parm->err = USB_ERR_ZERO_NFRAMES; in usbd_transfer_setup_sub()
660 if (xfer->interval == 0) { in usbd_transfer_setup_sub()
661 xfer->interval = edesc->bInterval; in usbd_transfer_setup_sub()
663 switch (parm->speed) { in usbd_transfer_setup_sub()
668 /* 125us -> 1ms */ in usbd_transfer_setup_sub()
669 if (xfer->interval < 4) in usbd_transfer_setup_sub()
670 xfer->interval = 1; in usbd_transfer_setup_sub()
671 else if (xfer->interval > 16) in usbd_transfer_setup_sub()
672 xfer->interval = (1 << (16 - 4)); in usbd_transfer_setup_sub()
674 xfer->interval = in usbd_transfer_setup_sub()
675 (1 << (xfer->interval - 4)); in usbd_transfer_setup_sub()
680 if (xfer->interval == 0) { in usbd_transfer_setup_sub()
685 xfer->interval = 1; in usbd_transfer_setup_sub()
688 xfer->fps_shift = 0; in usbd_transfer_setup_sub()
691 while ((temp != 0) && (temp < xfer->interval)) { in usbd_transfer_setup_sub()
692 xfer->fps_shift++; in usbd_transfer_setup_sub()
696 switch (parm->speed) { in usbd_transfer_setup_sub()
701 xfer->fps_shift += 3; in usbd_transfer_setup_sub()
713 if ((xfer->max_frame_size == 0) || in usbd_transfer_setup_sub()
714 (xfer->max_packet_size == 0)) { in usbd_transfer_setup_sub()
717 if ((parm->bufsize <= MIN_PKT) && in usbd_transfer_setup_sub()
721 xfer->max_packet_size = MIN_PKT; in usbd_transfer_setup_sub()
722 xfer->max_packet_count = 1; in usbd_transfer_setup_sub()
723 parm->bufsize = 0; /* automatic setup length */ in usbd_transfer_setup_sub()
727 parm->err = USB_ERR_ZERO_MAXP; in usbd_transfer_setup_sub()
740 if (parm->bufsize == 0) { in usbd_transfer_setup_sub()
741 parm->bufsize = xfer->max_frame_size; in usbd_transfer_setup_sub()
744 parm->bufsize *= xfer->nframes; in usbd_transfer_setup_sub()
752 if (xfer->flags.proxy_buffer) { in usbd_transfer_setup_sub()
755 parm->bufsize += (xfer->max_frame_size - 1); in usbd_transfer_setup_sub()
757 if (parm->bufsize < xfer->max_frame_size) { in usbd_transfer_setup_sub()
759 parm->err = USB_ERR_INVAL; in usbd_transfer_setup_sub()
764 parm->bufsize -= (parm->bufsize % xfer->max_frame_size); in usbd_transfer_setup_sub()
769 parm->bufsize += REQ_SIZE; /* SETUP message */ in usbd_transfer_setup_sub()
772 xfer->max_data_length = parm->bufsize; in usbd_transfer_setup_sub()
777 n_frlengths = xfer->nframes; in usbd_transfer_setup_sub()
781 xfer->flags_int.control_xfr = 1; in usbd_transfer_setup_sub()
782 if (xfer->nframes == 0) { in usbd_transfer_setup_sub()
783 if (parm->bufsize <= REQ_SIZE) { in usbd_transfer_setup_sub()
788 xfer->nframes = 1; in usbd_transfer_setup_sub()
790 xfer->nframes = 2; in usbd_transfer_setup_sub()
794 if (xfer->nframes == 0) { in usbd_transfer_setup_sub()
795 xfer->nframes = 1; in usbd_transfer_setup_sub()
799 n_frlengths = xfer->nframes; in usbd_transfer_setup_sub()
800 n_frbuffers = xfer->nframes; in usbd_transfer_setup_sub()
809 if (xfer->max_data_length < REQ_SIZE) { in usbd_transfer_setup_sub()
811 parm->err = USB_ERR_INVAL; in usbd_transfer_setup_sub()
814 xfer->max_data_length -= REQ_SIZE; in usbd_transfer_setup_sub()
818 * initial frame lengths when a USB transfer is complete. This in usbd_transfer_setup_sub()
821 xfer->frlengths = parm->xfer_length_ptr; in usbd_transfer_setup_sub()
822 parm->xfer_length_ptr += 2 * n_frlengths; in usbd_transfer_setup_sub()
825 xfer->frbuffers = parm->xfer_page_cache_ptr; in usbd_transfer_setup_sub()
826 parm->xfer_page_cache_ptr += n_frbuffers; in usbd_transfer_setup_sub()
829 xfer->max_frame_count = xfer->nframes; in usbd_transfer_setup_sub()
836 if (!xfer->flags.ext_buffer) { in usbd_transfer_setup_sub()
842 &pc, parm->bufsize, 1, 1)) { in usbd_transfer_setup_sub()
843 parm->err = USB_ERR_NOMEM; in usbd_transfer_setup_sub()
844 } else if (parm->buf != NULL) { in usbd_transfer_setup_sub()
847 xfer->local_buffer = page_info.buffer; in usbd_transfer_setup_sub()
857 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup_sub()
859 if (parm->buf != NULL) { in usbd_transfer_setup_sub()
860 xfer->local_buffer = in usbd_transfer_setup_sub()
861 USB_ADD_BYTES(parm->buf, parm->size[0]); in usbd_transfer_setup_sub()
869 parm->size[0] += parm->bufsize; in usbd_transfer_setup_sub()
872 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup_sub()
879 if (parm->bufsize_max < parm->bufsize) { in usbd_transfer_setup_sub()
880 parm->bufsize_max = parm->bufsize; in usbd_transfer_setup_sub()
883 if (xfer->flags_int.bdma_enable) { in usbd_transfer_setup_sub()
898 * (a + b + c) <= parm->bufsize in usbd_transfer_setup_sub()
902 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); in usbd_transfer_setup_sub()
906 xfer->dma_page_ptr = parm->dma_page_ptr; in usbd_transfer_setup_sub()
907 parm->dma_page_ptr += (2 * n_frbuffers); in usbd_transfer_setup_sub()
908 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); in usbd_transfer_setup_sub()
913 xfer->max_data_length = 0; in usbd_transfer_setup_sub()
917 xfer->max_hc_frame_size = in usbd_transfer_setup_sub()
918 (parm->hc_max_frame_size - in usbd_transfer_setup_sub()
919 (parm->hc_max_frame_size % xfer->max_frame_size)); in usbd_transfer_setup_sub()
921 if (xfer->max_hc_frame_size == 0) { in usbd_transfer_setup_sub()
922 parm->err = USB_ERR_INVAL; in usbd_transfer_setup_sub()
928 if (parm->buf) { in usbd_transfer_setup_sub()
930 xfer->frbuffers[x].tag_parent = in usbd_transfer_setup_sub()
931 &xfer->xroot->dma_parent_tag; in usbd_transfer_setup_sub()
933 if (xfer->flags_int.bdma_enable && in usbd_transfer_setup_sub()
934 (parm->bufsize_max > 0)) { in usbd_transfer_setup_sub()
936 xfer->frbuffers + x, in usbd_transfer_setup_sub()
937 parm->bufsize_max)) { in usbd_transfer_setup_sub()
938 parm->err = USB_ERR_NOMEM; in usbd_transfer_setup_sub()
946 if (parm->err) { in usbd_transfer_setup_sub()
950 xfer->max_hc_frame_size = 1; in usbd_transfer_setup_sub()
951 xfer->max_frame_size = 1; in usbd_transfer_setup_sub()
952 xfer->max_packet_size = 1; in usbd_transfer_setup_sub()
953 xfer->max_data_length = 0; in usbd_transfer_setup_sub()
954 xfer->nframes = 0; in usbd_transfer_setup_sub()
955 xfer->max_frame_count = 0; in usbd_transfer_setup_sub()
963 while (n_setup--) { in usbd_transfer_setup_has_bulk()
972 /*------------------------------------------------------------------------*
973 * usbd_transfer_setup - setup an array of USB transfers
978 * The idea is that the USB device driver should pre-allocate all its
979 * transfers by one call to this function.
984 *------------------------------------------------------------------------*/
1025 if (setup->bufsize == (usb_frlength_t)-1) { in usbd_transfer_setup()
1029 if (setup->callback == NULL) { in usbd_transfer_setup()
1045 parm = &udev->scratch.xfer_setup[0].parm; in usbd_transfer_setup()
1048 parm->udev = udev; in usbd_transfer_setup()
1049 parm->speed = usbd_get_speed(udev); in usbd_transfer_setup()
1050 parm->hc_max_packet_count = 1; in usbd_transfer_setup()
1052 if (parm->speed >= USB_SPEED_MAX) { in usbd_transfer_setup()
1053 parm->err = USB_ERR_INVAL; in usbd_transfer_setup()
1066 info->memory_base = buf; in usbd_transfer_setup()
1067 info->memory_size = parm->size[0]; in usbd_transfer_setup()
1070 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]); in usbd_transfer_setup()
1071 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]); in usbd_transfer_setup()
1073 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]); in usbd_transfer_setup()
1074 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]); in usbd_transfer_setup()
1076 cv_init(&info->cv_drain, "WDRAIN"); in usbd_transfer_setup()
1078 info->xfer_mtx = xfer_mtx; in usbd_transfer_setup()
1080 usb_dma_tag_setup(&info->dma_parent_tag, in usbd_transfer_setup()
1081 parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag, in usbd_transfer_setup()
1082 xfer_mtx, &usb_bdma_done_event, udev->bus->dma_bits, in usbd_transfer_setup()
1083 parm->dma_tag_max); in usbd_transfer_setup()
1086 info->bus = udev->bus; in usbd_transfer_setup()
1087 info->udev = udev; in usbd_transfer_setup()
1089 TAILQ_INIT(&info->done_q.head); in usbd_transfer_setup()
1090 info->done_q.command = &usbd_callback_wrapper; in usbd_transfer_setup()
1092 TAILQ_INIT(&info->dma_q.head); in usbd_transfer_setup()
1093 info->dma_q.command = &usb_bdma_work_loop; in usbd_transfer_setup()
1095 info->done_m[0].hdr.pm_callback = &usb_callback_proc; in usbd_transfer_setup()
1096 info->done_m[0].xroot = info; in usbd_transfer_setup()
1097 info->done_m[1].hdr.pm_callback = &usb_callback_proc; in usbd_transfer_setup()
1098 info->done_m[1].xroot = info; in usbd_transfer_setup()
1108 info->done_p = in usbd_transfer_setup()
1109 USB_BUS_CONTROL_XFER_PROC(udev->bus); in usbd_transfer_setup()
1111 info->done_p = in usbd_transfer_setup()
1112 USB_BUS_GIANT_PROC(udev->bus); in usbd_transfer_setup()
1114 info->done_p = in usbd_transfer_setup()
1115 USB_BUS_NON_GIANT_BULK_PROC(udev->bus); in usbd_transfer_setup()
1117 info->done_p = in usbd_transfer_setup()
1118 USB_BUS_NON_GIANT_ISOC_PROC(udev->bus); in usbd_transfer_setup()
1122 parm->size[0] = 0; in usbd_transfer_setup()
1123 parm->buf = buf; in usbd_transfer_setup()
1124 parm->size[0] += sizeof(info[0]); in usbd_transfer_setup()
1129 if (setup->callback == NULL) { in usbd_transfer_setup()
1134 ifaces[setup->if_index], setup); in usbd_transfer_setup()
1141 * transfer when such a combination is not in usbd_transfer_setup()
1144 if ((ep == NULL) || (ep->methods == NULL) || in usbd_transfer_setup()
1145 ((ep->ep_mode != USB_EP_MODE_STREAMS) && in usbd_transfer_setup()
1146 (ep->ep_mode != USB_EP_MODE_DEFAULT)) || in usbd_transfer_setup()
1147 (setup->stream_id != 0 && in usbd_transfer_setup()
1148 (setup->stream_id >= USB_MAX_EP_STREAMS || in usbd_transfer_setup()
1149 (ep->ep_mode != USB_EP_MODE_STREAMS)))) { in usbd_transfer_setup()
1150 if (setup->flags.no_pipe_ok) in usbd_transfer_setup()
1152 if ((setup->usb_mode != USB_MODE_DUAL) && in usbd_transfer_setup()
1153 (setup->usb_mode != udev->flags.usb_mode)) in usbd_transfer_setup()
1155 parm->err = USB_ERR_NO_PIPE; in usbd_transfer_setup()
1160 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1163 parm->curr_setup = setup; in usbd_transfer_setup()
1170 xfer = USB_ADD_BYTES(buf, parm->size[0]); in usbd_transfer_setup()
1171 xfer->address = udev->address; in usbd_transfer_setup()
1172 xfer->priv_sc = priv_sc; in usbd_transfer_setup()
1173 xfer->xroot = info; in usbd_transfer_setup()
1175 usb_callout_init_mtx(&xfer->timeout_handle, in usbd_transfer_setup()
1176 &udev->bus->bus_mtx, 0); in usbd_transfer_setup()
1185 xfer = &udev->scratch.xfer_setup[0].dummy; in usbd_transfer_setup()
1190 /* set transfer endpoint pointer */ in usbd_transfer_setup()
1191 xfer->endpoint = ep; in usbd_transfer_setup()
1193 /* set transfer stream ID */ in usbd_transfer_setup()
1194 xfer->stream_id = setup->stream_id; in usbd_transfer_setup()
1196 parm->size[0] += sizeof(xfer[0]); in usbd_transfer_setup()
1197 parm->methods = xfer->endpoint->methods; in usbd_transfer_setup()
1198 parm->curr_xfer = xfer; in usbd_transfer_setup()
1201 * Call the Host or Device controller transfer in usbd_transfer_setup()
1204 (udev->bus->methods->xfer_setup) (parm); in usbd_transfer_setup()
1207 if (parm->err) in usbd_transfer_setup()
1217 * code for "endpoint->refcount_alloc" if you in usbd_transfer_setup()
1220 USB_BUS_LOCK(info->bus); in usbd_transfer_setup()
1221 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX) in usbd_transfer_setup()
1222 parm->err = USB_ERR_INVAL; in usbd_transfer_setup()
1224 xfer->endpoint->refcount_alloc++; in usbd_transfer_setup()
1226 if (xfer->endpoint->refcount_alloc == 0) in usbd_transfer_setup()
1228 USB_BUS_UNLOCK(info->bus); in usbd_transfer_setup()
1235 info->setup_refcount++; in usbd_transfer_setup()
1238 * Transfer is successfully setup and in usbd_transfer_setup()
1245 if (parm->err) in usbd_transfer_setup()
1249 if (buf != NULL || parm->err != 0) in usbd_transfer_setup()
1257 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1260 parm->size[1] = parm->size[0]; in usbd_transfer_setup()
1271 parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX); in usbd_transfer_setup()
1276 parm->dma_tag_max += 8; in usbd_transfer_setup()
1278 parm->dma_tag_p += parm->dma_tag_max; in usbd_transfer_setup()
1280 parm->size[0] += ((uint8_t *)parm->dma_tag_p) - in usbd_transfer_setup()
1284 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1287 parm->size[3] = parm->size[0]; in usbd_transfer_setup()
1289 parm->size[0] += ((uint8_t *)parm->dma_page_ptr) - in usbd_transfer_setup()
1293 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1296 parm->size[4] = parm->size[0]; in usbd_transfer_setup()
1298 parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) - in usbd_transfer_setup()
1302 parm->size[5] = parm->size[0]; in usbd_transfer_setup()
1304 parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) - in usbd_transfer_setup()
1309 parm->size[2] = parm->size[0]; in usbd_transfer_setup()
1312 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1314 parm->size[6] = parm->size[0]; in usbd_transfer_setup()
1316 parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) - in usbd_transfer_setup()
1320 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); in usbd_transfer_setup()
1323 buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO); in usbd_transfer_setup()
1326 parm->err = USB_ERR_NOMEM; in usbd_transfer_setup()
1329 parm->size[0]); in usbd_transfer_setup()
1333 parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]); in usbd_transfer_setup()
1334 parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]); in usbd_transfer_setup()
1335 parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]); in usbd_transfer_setup()
1336 parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]); in usbd_transfer_setup()
1337 parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]); in usbd_transfer_setup()
1342 if (info->setup_refcount == 0) { in usbd_transfer_setup()
1347 USB_BUS_LOCK(info->bus); in usbd_transfer_setup()
1355 if (parm->err) in usbd_transfer_setup()
1358 error = parm->err; in usbd_transfer_setup()
1366 /*------------------------------------------------------------------------*
1367 * usbd_transfer_unsetup_sub - factored out code
1368 *------------------------------------------------------------------------*/
1376 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); in usbd_transfer_unsetup_sub()
1382 temp = usbd_get_dma_delay(info->udev); in usbd_transfer_unsetup_sub()
1384 usb_pause_mtx(&info->bus->bus_mtx, in usbd_transfer_unsetup_sub()
1390 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); in usbd_transfer_unsetup_sub()
1392 USB_BUS_UNLOCK(info->bus); in usbd_transfer_unsetup_sub()
1396 pc = info->dma_page_cache_start; in usbd_transfer_unsetup_sub()
1397 while (pc != info->dma_page_cache_end) { in usbd_transfer_unsetup_sub()
1402 /* free DMA maps in all "xfer->frbuffers" */ in usbd_transfer_unsetup_sub()
1403 pc = info->xfer_page_cache_start; in usbd_transfer_unsetup_sub()
1404 while (pc != info->xfer_page_cache_end) { in usbd_transfer_unsetup_sub()
1410 usb_dma_tag_unsetup(&info->dma_parent_tag); in usbd_transfer_unsetup_sub()
1413 cv_destroy(&info->cv_drain); in usbd_transfer_unsetup_sub()
1419 free(info->memory_base, M_USB); in usbd_transfer_unsetup_sub()
1422 /*------------------------------------------------------------------------*
1423 * usbd_transfer_unsetup - unsetup/free an array of USB transfers
1426 * the error code "USB_ERR_CANCELLED" before this function
1428 *------------------------------------------------------------------------*/
1439 while (n_setup--) { in usbd_transfer_unsetup()
1445 info = xfer->xroot; in usbd_transfer_unsetup()
1448 USB_BUS_LOCK(info->bus); in usbd_transfer_unsetup()
1451 * HINT: when you start/stop a transfer, it might be a in usbd_transfer_unsetup()
1454 * usbd_transfer_start(sc->pxfer[0]); in usbd_transfer_unsetup()
1455 * usbd_transfer_stop(sc->pxfer[0]); in usbd_transfer_unsetup()
1468 USB_BUS_UNLOCK(info->bus); in usbd_transfer_unsetup()
1474 if (xfer->flags_int.bdma_enable) in usbd_transfer_unsetup()
1479 * interface, even if endpoint->iface_index == 0 in usbd_transfer_unsetup()
1481 USB_BUS_LOCK(info->bus); in usbd_transfer_unsetup()
1482 xfer->endpoint->refcount_alloc--; in usbd_transfer_unsetup()
1483 USB_BUS_UNLOCK(info->bus); in usbd_transfer_unsetup()
1485 usb_callout_drain(&xfer->timeout_handle); in usbd_transfer_unsetup()
1487 USB_BUS_LOCK(info->bus); in usbd_transfer_unsetup()
1489 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " in usbd_transfer_unsetup()
1492 info->setup_refcount--; in usbd_transfer_unsetup()
1494 if (info->setup_refcount == 0) { in usbd_transfer_unsetup()
1498 USB_BUS_UNLOCK(info->bus); in usbd_transfer_unsetup()
1503 /*------------------------------------------------------------------------*
1504 * usbd_control_transfer_init - factored out code
1508 * transfer any data. In USB Host Mode we already have the SETUP
1509 * packet at the moment the USB transfer is started. This leads us to
1510 * having to setup the USB transfer at two different places in
1511 * time. This function just contains factored out control transfer
1513 *------------------------------------------------------------------------*/
1521 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); in usbd_control_transfer_init()
1525 xfer->flags_int.control_rem = UGETW(req.wLength); in usbd_control_transfer_init()
1529 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT); in usbd_control_transfer_init()
1530 xfer->endpointno |= in usbd_control_transfer_init()
1534 /*------------------------------------------------------------------------*
1537 * This function returns non-zero if a control endpoint has
1540 *------------------------------------------------------------------------*/
1547 if (xfer->flags_int.control_hdr != 0) in usbd_control_transfer_did_data()
1551 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); in usbd_control_transfer_did_data()
1554 return (xfer->flags_int.control_rem != UGETW(req.wLength)); in usbd_control_transfer_did_data()
1557 /*------------------------------------------------------------------------*
1560 * This function handles initialisation of control transfers. Control
1567 *------------------------------------------------------------------------*/
1574 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) { in usbd_setup_ctrl_transfer()
1575 /* the control transfer is no longer active */ in usbd_setup_ctrl_transfer()
1576 xfer->flags_int.control_stall = 1; in usbd_setup_ctrl_transfer()
1577 xfer->flags_int.control_act = 0; in usbd_setup_ctrl_transfer()
1579 /* don't stall control transfer by default */ in usbd_setup_ctrl_transfer()
1580 xfer->flags_int.control_stall = 0; in usbd_setup_ctrl_transfer()
1584 if (xfer->nframes > 2) { in usbd_setup_ctrl_transfer()
1586 * If you need to split a control transfer, you in usbd_setup_ctrl_transfer()
1588 * non-control transfers you can do multiple in usbd_setup_ctrl_transfer()
1592 (unsigned)xfer->nframes); in usbd_setup_ctrl_transfer()
1598 * transfer in progress: in usbd_setup_ctrl_transfer()
1600 if (xfer->flags_int.control_act) { in usbd_setup_ctrl_transfer()
1601 if (xfer->flags_int.control_hdr) { in usbd_setup_ctrl_transfer()
1604 xfer->flags_int.control_hdr = 0; in usbd_setup_ctrl_transfer()
1606 /* setup control transfer */ in usbd_setup_ctrl_transfer()
1607 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { in usbd_setup_ctrl_transfer()
1613 len = xfer->sumlen; in usbd_setup_ctrl_transfer()
1618 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) { in usbd_setup_ctrl_transfer()
1620 xfer->frlengths[0], sizeof(struct in usbd_setup_ctrl_transfer()
1625 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { in usbd_setup_ctrl_transfer()
1627 if (xfer->nframes != 1) { in usbd_setup_ctrl_transfer()
1633 DPRINTF("Misconfigured transfer\n"); in usbd_setup_ctrl_transfer()
1641 xfer->flags_int.control_rem = 0xFFFF; in usbd_setup_ctrl_transfer()
1648 /* set transfer-header flag */ in usbd_setup_ctrl_transfer()
1650 xfer->flags_int.control_hdr = 1; in usbd_setup_ctrl_transfer()
1654 len = (xfer->sumlen - sizeof(struct usb_device_request)); in usbd_setup_ctrl_transfer()
1659 xfer->flags_int.control_did_data = in usbd_setup_ctrl_transfer()
1664 if (len > xfer->flags_int.control_rem) { in usbd_setup_ctrl_transfer()
1667 xfer->flags_int.control_rem); in usbd_setup_ctrl_transfer()
1670 /* check if we are doing a short transfer */ in usbd_setup_ctrl_transfer()
1672 if (xfer->flags.force_short_xfer) { in usbd_setup_ctrl_transfer()
1673 xfer->flags_int.control_rem = 0; in usbd_setup_ctrl_transfer()
1675 if ((len != xfer->max_data_length) && in usbd_setup_ctrl_transfer()
1676 (len != xfer->flags_int.control_rem) && in usbd_setup_ctrl_transfer()
1677 (xfer->nframes != 1)) { in usbd_setup_ctrl_transfer()
1678 DPRINTFN(0, "Short control transfer without " in usbd_setup_ctrl_transfer()
1682 xfer->flags_int.control_rem -= len; in usbd_setup_ctrl_transfer()
1687 if ((xfer->flags_int.control_rem > 0) || in usbd_setup_ctrl_transfer()
1688 (xfer->flags.manual_status)) { in usbd_setup_ctrl_transfer()
1690 xfer->flags_int.control_act = 1; in usbd_setup_ctrl_transfer()
1693 if ((!xfer->flags_int.control_hdr) && in usbd_setup_ctrl_transfer()
1694 (xfer->nframes == 1)) { in usbd_setup_ctrl_transfer()
1704 xfer->flags_int.control_act = 0; in usbd_setup_ctrl_transfer()
1712 /*------------------------------------------------------------------------*
1713 * usbd_transfer_submit - start USB hardware for the given transfer
1715 * This function should only be called from the USB callback.
1716 *------------------------------------------------------------------------*/
1724 info = xfer->xroot; in usbd_transfer_submit()
1725 bus = info->bus; in usbd_transfer_submit()
1728 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? in usbd_transfer_submit()
1735 usb_dump_endpoint(xfer->endpoint); in usbd_transfer_submit()
1744 /* Only open the USB transfer once! */ in usbd_transfer_submit()
1745 if (!xfer->flags_int.open) { in usbd_transfer_submit()
1746 xfer->flags_int.open = 1; in usbd_transfer_submit()
1751 (xfer->endpoint->methods->open) (xfer); in usbd_transfer_submit()
1755 xfer->flags_int.transferring = 1; in usbd_transfer_submit()
1762 * Check if the transfer is waiting on a queue, most in usbd_transfer_submit()
1765 if (xfer->wait_queue) { in usbd_transfer_submit()
1771 xfer->flags_int.did_dma_delay = 0; in usbd_transfer_submit()
1774 xfer->flags_int.did_close = 0; in usbd_transfer_submit()
1778 xfer->flags_int.bdma_setup = 0; in usbd_transfer_submit()
1780 /* by default we cannot cancel any USB transfer immediately */ in usbd_transfer_submit()
1781 xfer->flags_int.can_cancel_immed = 0; in usbd_transfer_submit()
1784 xfer->sumlen = 0; in usbd_transfer_submit()
1785 xfer->actlen = 0; in usbd_transfer_submit()
1786 xfer->aframes = 0; in usbd_transfer_submit()
1789 xfer->error = 0; in usbd_transfer_submit()
1792 if (info->udev->state < USB_STATE_POWERED) { in usbd_transfer_submit()
1804 if (xfer->nframes == 0) { in usbd_transfer_submit()
1805 if (xfer->flags.stall_pipe) { in usbd_transfer_submit()
1807 * Special case - want to stall without transferring in usbd_transfer_submit()
1813 xfer->flags_int.can_cancel_immed = 1; in usbd_transfer_submit()
1814 /* start the transfer */ in usbd_transfer_submit()
1815 usb_command_wrapper(&xfer->endpoint-> in usbd_transfer_submit()
1816 endpoint_q[xfer->stream_id], xfer); in usbd_transfer_submit()
1827 for (x = 0; x != xfer->nframes; x++) { in usbd_transfer_submit()
1829 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x]; in usbd_transfer_submit()
1830 /* compute total transfer length */ in usbd_transfer_submit()
1831 xfer->sumlen += xfer->frlengths[x]; in usbd_transfer_submit()
1832 if (xfer->sumlen < xfer->frlengths[x]) { in usbd_transfer_submit()
1843 xfer->flags_int.short_xfer_ok = 0; in usbd_transfer_submit()
1844 xfer->flags_int.short_frames_ok = 0; in usbd_transfer_submit()
1846 /* check if this is a control transfer */ in usbd_transfer_submit()
1848 if (xfer->flags_int.control_xfr) { in usbd_transfer_submit()
1857 * Setup filtered version of some transfer flags, in usbd_transfer_submit()
1861 if (xfer->flags.short_frames_ok) { in usbd_transfer_submit()
1862 xfer->flags_int.short_xfer_ok = 1; in usbd_transfer_submit()
1863 xfer->flags_int.short_frames_ok = 1; in usbd_transfer_submit()
1864 } else if (xfer->flags.short_xfer_ok) { in usbd_transfer_submit()
1865 xfer->flags_int.short_xfer_ok = 1; in usbd_transfer_submit()
1867 /* check for control transfer */ in usbd_transfer_submit()
1868 if (xfer->flags_int.control_xfr) { in usbd_transfer_submit()
1882 xfer->flags_int.short_frames_ok = 1; in usbd_transfer_submit()
1887 * Check if BUS-DMA support is enabled and try to load virtual in usbd_transfer_submit()
1891 if (xfer->flags_int.bdma_enable) { in usbd_transfer_submit()
1892 /* insert the USB transfer last in the BUS-DMA queue */ in usbd_transfer_submit()
1893 usb_command_wrapper(&xfer->xroot->dma_q, xfer); in usbd_transfer_submit()
1898 * Enter the USB transfer into the Host Controller or in usbd_transfer_submit()
1904 /*------------------------------------------------------------------------*
1905 * usbd_pipe_enter - factored out code
1906 *------------------------------------------------------------------------*/
1914 USB_BUS_LOCK(xfer->xroot->bus); in usbd_pipe_enter()
1916 ep = xfer->endpoint; in usbd_pipe_enter()
1920 /* the transfer can now be cancelled */ in usbd_pipe_enter()
1921 xfer->flags_int.can_cancel_immed = 1; in usbd_pipe_enter()
1923 /* enter the transfer */ in usbd_pipe_enter()
1924 (ep->methods->enter) (xfer); in usbd_pipe_enter()
1926 /* check for transfer error */ in usbd_pipe_enter()
1927 if (xfer->error) { in usbd_pipe_enter()
1930 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_pipe_enter()
1934 /* start the transfer */ in usbd_pipe_enter()
1935 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer); in usbd_pipe_enter()
1936 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_pipe_enter()
1939 /*------------------------------------------------------------------------*
1940 * usbd_transfer_start - start an USB transfer
1942 * NOTE: Calling this function more than one time will only
1943 * result in a single transfer start, until the USB transfer
1945 *------------------------------------------------------------------------*/
1950 /* transfer is gone */ in usbd_transfer_start()
1955 /* mark the USB transfer started */ in usbd_transfer_start()
1957 if (!xfer->flags_int.started) { in usbd_transfer_start()
1959 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_start()
1960 xfer->flags_int.started = 1; in usbd_transfer_start()
1961 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_start()
1963 /* check if the USB transfer callback is already transferring */ in usbd_transfer_start()
1965 if (xfer->flags_int.transferring) { in usbd_transfer_start()
1968 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_start()
1969 /* call the USB transfer callback */ in usbd_transfer_start()
1971 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_start()
1974 /*------------------------------------------------------------------------*
1975 * usbd_transfer_stop - stop an USB transfer
1977 * NOTE: Calling this function more than one time will only
1978 * result in a single transfer stop.
1979 * NOTE: When this function returns it is not safe to free nor
1981 *------------------------------------------------------------------------*/
1988 /* transfer is gone */ in usbd_transfer_stop()
1993 /* check if the USB transfer was ever opened */ in usbd_transfer_stop()
1995 if (!xfer->flags_int.open) { in usbd_transfer_stop()
1996 if (xfer->flags_int.started) { in usbd_transfer_stop()
1999 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_stop()
2000 xfer->flags_int.started = 0; in usbd_transfer_stop()
2001 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_stop()
2005 /* try to stop the current USB transfer */ in usbd_transfer_stop()
2007 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_stop()
2009 xfer->error = USB_ERR_CANCELLED; in usbd_transfer_stop()
2015 xfer->flags_int.open = 0; in usbd_transfer_stop()
2016 xfer->flags_int.started = 0; in usbd_transfer_stop()
2019 * Check if we can cancel the USB transfer immediately. in usbd_transfer_stop()
2021 if (xfer->flags_int.transferring) { in usbd_transfer_stop()
2022 if (xfer->flags_int.can_cancel_immed && in usbd_transfer_stop()
2023 (!xfer->flags_int.did_close)) { in usbd_transfer_stop()
2029 (xfer->endpoint->methods->close) (xfer); in usbd_transfer_stop()
2031 xfer->flags_int.did_close = 1; in usbd_transfer_stop()
2039 (xfer->endpoint->methods->close) (xfer); in usbd_transfer_stop()
2050 ep = xfer->endpoint; in usbd_transfer_stop()
2053 * If the current USB transfer is completing we need in usbd_transfer_stop()
2056 if (ep->endpoint_q[xfer->stream_id].curr == xfer) { in usbd_transfer_stop()
2058 &ep->endpoint_q[xfer->stream_id], NULL); in usbd_transfer_stop()
2062 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_stop()
2065 /*------------------------------------------------------------------------*
2068 * This function will check if an USB transfer is pending which is a
2072 * 1: Pending: The USB transfer will receive a callback in the future.
2073 *------------------------------------------------------------------------*/
2081 /* transfer is gone */ in usbd_transfer_pending()
2086 if (xfer->flags_int.transferring) { in usbd_transfer_pending()
2090 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_pending()
2091 if (xfer->wait_queue) { in usbd_transfer_pending()
2093 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_pending()
2096 info = xfer->xroot; in usbd_transfer_pending()
2097 pq = &info->done_q; in usbd_transfer_pending()
2099 if (pq->curr == xfer) { in usbd_transfer_pending()
2101 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_pending()
2105 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_pending()
2109 /*------------------------------------------------------------------------*
2112 * This function will stop the USB transfer and wait for any
2113 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2115 * function has returned.
2116 *------------------------------------------------------------------------*/
2124 /* transfer is gone */ in usbd_transfer_drain()
2127 if (xfer->xroot->xfer_mtx != &Giant) { in usbd_transfer_drain()
2135 xfer->flags_int.doing_callback) { in usbd_transfer_drain()
2138 * transfer mutex. In that case checking only in usbd_transfer_drain()
2140 * the USB transfer is fully drained. We also need to in usbd_transfer_drain()
2143 xfer->flags_int.draining = 1; in usbd_transfer_drain()
2147 * transfer is complete ! in usbd_transfer_drain()
2149 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); in usbd_transfer_drain()
2157 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_get_frame()
2159 return (&xfer->frbuffers[frindex]); in usbd_xfer_get_frame()
2167 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_get_frame_buffer()
2169 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info); in usbd_xfer_get_frame_buffer()
2173 /*------------------------------------------------------------------------*
2176 * The following function is only useful for isochronous transfers. It
2183 *------------------------------------------------------------------------*/
2187 return (xfer->fps_shift); in usbd_xfer_get_fps_shift()
2193 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_frame_len()
2195 return (xfer->frlengths[frindex]); in usbd_xfer_frame_len()
2198 /*------------------------------------------------------------------------*
2201 * This function sets the pointer of the buffer that should
2205 *------------------------------------------------------------------------*/
2210 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_set_frame_data()
2213 xfer->frbuffers[frindex].buffer = ptr; in usbd_xfer_set_frame_data()
2221 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_frame_data()
2224 *ptr = xfer->frbuffers[frindex].buffer; in usbd_xfer_frame_data()
2226 *len = xfer->frlengths[frindex]; in usbd_xfer_frame_data()
2229 /*------------------------------------------------------------------------*
2232 * This function returns the framelength of the given frame at the
2233 * time the transfer was submitted. This function can be used to
2235 * when an isochronous transfer has completed.
2236 *------------------------------------------------------------------------*/
2240 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_old_frame_length()
2242 return (xfer->frlengths[frindex + xfer->max_frame_count]); in usbd_xfer_old_frame_length()
2250 *actlen = xfer->actlen; in usbd_xfer_status()
2252 *sumlen = xfer->sumlen; in usbd_xfer_status()
2254 *aframes = xfer->aframes; in usbd_xfer_status()
2256 *nframes = xfer->nframes; in usbd_xfer_status()
2259 /*------------------------------------------------------------------------*
2262 * This function sets the frame data buffer offset relative to the beginning
2263 * of the USB DMA buffer allocated for this USB transfer.
2264 *------------------------------------------------------------------------*/
2269 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " in usbd_xfer_set_frame_offset()
2271 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_set_frame_offset()
2274 xfer->frbuffers[frindex].buffer = in usbd_xfer_set_frame_offset()
2275 USB_ADD_BYTES(xfer->local_buffer, offset); in usbd_xfer_set_frame_offset()
2281 xfer->interval = i; in usbd_xfer_set_interval()
2287 xfer->timeout = t; in usbd_xfer_set_timeout()
2293 xfer->nframes = n; in usbd_xfer_set_frames()
2299 return (xfer->max_frame_count); in usbd_xfer_max_frames()
2305 return (xfer->max_data_length); in usbd_xfer_max_len()
2311 return (xfer->max_frame_size); in usbd_xfer_max_framelen()
2318 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); in usbd_xfer_set_frame_len()
2320 xfer->frlengths[frindex] = len; in usbd_xfer_set_frame_len()
2323 /*------------------------------------------------------------------------*
2324 * usb_callback_proc - factored out code
2326 * This function performs USB callbacks.
2327 *------------------------------------------------------------------------*/
2332 struct usb_xfer_root *info = pm->xroot; in usb_callback_proc()
2335 USB_BUS_UNLOCK(info->bus); in usb_callback_proc()
2341 USB_MTX_LOCK(info->xfer_mtx); in usb_callback_proc()
2342 USB_BUS_LOCK(info->bus); in usb_callback_proc()
2345 usb_command_wrapper(&info->done_q, in usb_callback_proc()
2346 info->done_q.curr); in usb_callback_proc()
2348 USB_MTX_UNLOCK(info->xfer_mtx); in usb_callback_proc()
2351 /*------------------------------------------------------------------------*
2354 * This function will defer the start, stop and done callback to the
2356 *------------------------------------------------------------------------*/
2360 struct usb_xfer_root *info = xfer->xroot; in usbd_callback_ss_done_defer()
2361 struct usb_xfer_queue *pq = &info->done_q; in usbd_callback_ss_done_defer()
2363 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); in usbd_callback_ss_done_defer()
2365 if (pq->curr != xfer) { in usbd_callback_ss_done_defer()
2368 if (!pq->recurse_1) { in usbd_callback_ss_done_defer()
2374 (void) usb_proc_msignal(info->done_p, in usbd_callback_ss_done_defer()
2375 &info->done_m[0], &info->done_m[1]); in usbd_callback_ss_done_defer()
2378 pq->recurse_2 = 0; in usbd_callback_ss_done_defer()
2384 /*------------------------------------------------------------------------*
2388 * auto-magic things like figuring out if we can call the callback
2391 *------------------------------------------------------------------------*/
2395 struct usb_xfer *xfer = pq->curr; in usbd_callback_wrapper()
2396 struct usb_xfer_root *info = xfer->xroot; in usbd_callback_wrapper()
2398 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); in usbd_callback_wrapper()
2399 if ((pq->recurse_3 != 0 || mtx_owned(info->xfer_mtx) == 0) && in usbd_callback_wrapper()
2405 * 6) HW completed transfer during callback in usbd_callback_wrapper()
2415 * transfer queues get a chance. in usbd_callback_wrapper()
2417 (void) usb_proc_msignal(info->done_p, in usbd_callback_wrapper()
2418 &info->done_m[0], &info->done_m[1]); in usbd_callback_wrapper()
2424 * 1) We are starting a transfer in usbd_callback_wrapper()
2425 * 2) We are prematurely calling back a transfer in usbd_callback_wrapper()
2426 * 3) We are stopping a transfer in usbd_callback_wrapper()
2429 DPRINTFN(3, "case 1-4\n"); in usbd_callback_wrapper()
2430 /* get next USB transfer in the queue */ in usbd_callback_wrapper()
2431 info->done_q.curr = NULL; in usbd_callback_wrapper()
2434 xfer->flags_int.doing_callback = 1; in usbd_callback_wrapper()
2436 USB_BUS_UNLOCK(info->bus); in usbd_callback_wrapper()
2437 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED); in usbd_callback_wrapper()
2440 if (!xfer->flags_int.transferring) { in usbd_callback_wrapper()
2441 xfer->usb_state = USB_ST_SETUP; in usbd_callback_wrapper()
2442 if (!xfer->flags_int.started) { in usbd_callback_wrapper()
2444 USB_BUS_LOCK(info->bus); in usbd_callback_wrapper()
2450 USB_BUS_LOCK(info->bus); in usbd_callback_wrapper()
2455 usbd_transfer_power_ref(xfer, -1); in usbd_callback_wrapper()
2457 xfer->flags_int.transferring = 0; in usbd_callback_wrapper()
2459 if (xfer->error) { in usbd_callback_wrapper()
2460 xfer->usb_state = USB_ST_ERROR; in usbd_callback_wrapper()
2463 xfer->usb_state = USB_ST_TRANSFERRED; in usbd_callback_wrapper()
2466 if (xfer->flags_int.bdma_enable && in usbd_callback_wrapper()
2467 (!xfer->flags_int.bdma_no_post_sync)) { in usbd_callback_wrapper()
2475 if (xfer->usb_state != USB_ST_SETUP) { in usbd_callback_wrapper()
2476 USB_BUS_LOCK(info->bus); in usbd_callback_wrapper()
2478 USB_BUS_UNLOCK(info->bus); in usbd_callback_wrapper()
2482 (xfer->callback) (xfer, xfer->error); in usbd_callback_wrapper()
2485 USB_BUS_LOCK(info->bus); in usbd_callback_wrapper()
2491 if ((!xfer->flags_int.open) && in usbd_callback_wrapper()
2492 (xfer->flags_int.started) && in usbd_callback_wrapper()
2493 (xfer->usb_state == USB_ST_ERROR)) { in usbd_callback_wrapper()
2495 xfer->flags_int.doing_callback = 0; in usbd_callback_wrapper()
2497 usb_command_wrapper(&info->done_q, xfer); in usbd_callback_wrapper()
2503 xfer->flags_int.doing_callback = 0; in usbd_callback_wrapper()
2508 if (xfer->flags_int.draining && in usbd_callback_wrapper()
2509 (!xfer->flags_int.transferring)) { in usbd_callback_wrapper()
2510 /* "usbd_transfer_drain()" is waiting for end of transfer */ in usbd_callback_wrapper()
2511 xfer->flags_int.draining = 0; in usbd_callback_wrapper()
2512 cv_broadcast(&info->cv_drain); in usbd_callback_wrapper()
2516 usb_command_wrapper(&info->done_q, in usbd_callback_wrapper()
2517 info->done_q.curr); in usbd_callback_wrapper()
2520 /*------------------------------------------------------------------------*
2523 * This function is called when the DMA delay has been exectuded, and
2525 * transfer. This code path is usually only used when there is an USB
2527 *------------------------------------------------------------------------*/
2531 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); in usb_dma_delay_done_cb()
2539 /*------------------------------------------------------------------------*
2542 * - This function is used to remove an USB transfer from a USB
2543 * transfer queue.
2545 * - This function can be called multiple times in a row.
2546 *------------------------------------------------------------------------*/
2552 pq = xfer->wait_queue; in usbd_transfer_dequeue()
2554 TAILQ_REMOVE(&pq->head, xfer, wait_entry); in usbd_transfer_dequeue()
2555 xfer->wait_queue = NULL; in usbd_transfer_dequeue()
2559 /*------------------------------------------------------------------------*
2562 * - This function is used to insert an USB transfer into a USB *
2563 * transfer queue.
2565 * - This function can be called multiple times in a row.
2566 *------------------------------------------------------------------------*/
2571 * Insert the USB transfer into the queue, if it is not in usbd_transfer_enqueue()
2572 * already on a USB transfer queue: in usbd_transfer_enqueue()
2574 if (xfer->wait_queue == NULL) { in usbd_transfer_enqueue()
2575 xfer->wait_queue = pq; in usbd_transfer_enqueue()
2576 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); in usbd_transfer_enqueue()
2580 /*------------------------------------------------------------------------*
2583 * - This function is used to remove an USB transfer from the busdma,
2586 * - This function is used to queue the USB transfer on the done
2589 * - This function is used to stop any USB transfer timeouts.
2590 *------------------------------------------------------------------------*/
2594 struct usb_xfer_root *info = xfer->xroot; in usbd_transfer_done()
2596 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); in usbd_transfer_done()
2602 * This can happen during transfer cancel. in usbd_transfer_done()
2604 if (!xfer->flags_int.transferring) { in usbd_transfer_done()
2606 /* end of control transfer, if any */ in usbd_transfer_done()
2607 xfer->flags_int.control_act = 0; in usbd_transfer_done()
2610 /* only set transfer error, if not already set */ in usbd_transfer_done()
2611 if (xfer->error == USB_ERR_NORMAL_COMPLETION) in usbd_transfer_done()
2612 xfer->error = error; in usbd_transfer_done()
2615 usb_callout_stop(&xfer->timeout_handle); in usbd_transfer_done()
2618 * If we are waiting on a queue, just remove the USB transfer in usbd_transfer_done()
2620 * locked to do the remove when this function is called. in usbd_transfer_done()
2625 if (mtx_owned(info->xfer_mtx)) { in usbd_transfer_done()
2630 * that the BUS-DMA load stage has been passed: in usbd_transfer_done()
2632 pq = &info->dma_q; in usbd_transfer_done()
2634 if (pq->curr == xfer) { in usbd_transfer_done()
2635 /* start the next BUS-DMA load, if any */ in usbd_transfer_done()
2641 if (xfer->error == USB_ERR_CANCELLED) { in usbd_transfer_done()
2642 info->udev->stats_cancelled.uds_requests in usbd_transfer_done()
2643 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; in usbd_transfer_done()
2644 } else if (xfer->error != USB_ERR_NORMAL_COMPLETION) { in usbd_transfer_done()
2645 info->udev->stats_err.uds_requests in usbd_transfer_done()
2646 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; in usbd_transfer_done()
2648 info->udev->stats_ok.uds_requests in usbd_transfer_done()
2649 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; in usbd_transfer_done()
2652 /* call the USB transfer callback */ in usbd_transfer_done()
2656 /*------------------------------------------------------------------------*
2659 * This function is called to start the USB transfer when
2660 * "xfer->interval" is greater than zero, and and the endpoint type is
2662 *------------------------------------------------------------------------*/
2667 struct usb_endpoint *ep = xfer->endpoint; in usbd_transfer_start_cb()
2669 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); in usbd_transfer_start_cb()
2677 /* the transfer can now be cancelled */ in usbd_transfer_start_cb()
2678 xfer->flags_int.can_cancel_immed = 1; in usbd_transfer_start_cb()
2680 /* start USB transfer, if no error */ in usbd_transfer_start_cb()
2681 if (xfer->error == 0) in usbd_transfer_start_cb()
2682 (ep->methods->start) (xfer); in usbd_transfer_start_cb()
2684 /* check for transfer error */ in usbd_transfer_start_cb()
2685 if (xfer->error) { in usbd_transfer_start_cb()
2691 /*------------------------------------------------------------------------*
2694 * This function sets the USB transfers ZLP flag.
2695 *------------------------------------------------------------------------*/
2706 USB_BUS_LOCK(xfer->xroot->bus); in usbd_xfer_set_zlp()
2707 xfer->flags.send_zlp = 1; in usbd_xfer_set_zlp()
2708 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_xfer_set_zlp()
2711 /*------------------------------------------------------------------------*
2714 * This function gets and clears the USB transfers ZLP flag and
2715 * queues a zero-length USB transfer if the flag was set.
2716 *------------------------------------------------------------------------*/
2728 retval = xfer->flags.send_zlp; in usbd_xfer_get_and_clr_zlp()
2731 DPRINTFN(1, "Sending zero-length packet.\n"); in usbd_xfer_get_and_clr_zlp()
2734 USB_BUS_LOCK(xfer->xroot->bus); in usbd_xfer_get_and_clr_zlp()
2735 xfer->flags.send_zlp = 0; in usbd_xfer_get_and_clr_zlp()
2736 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_xfer_get_and_clr_zlp()
2738 /* queue up a zero-length packet */ in usbd_xfer_get_and_clr_zlp()
2746 /*------------------------------------------------------------------------*
2749 * This function is used to set the stall flag outside the
2750 * callback. This function is NULL safe.
2751 *------------------------------------------------------------------------*/
2762 USB_BUS_LOCK(xfer->xroot->bus); in usbd_xfer_set_stall()
2763 xfer->flags.stall_pipe = 1; in usbd_xfer_set_stall()
2764 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_xfer_set_stall()
2770 return (xfer->endpoint->is_stalled); in usbd_xfer_is_stalled()
2773 /*------------------------------------------------------------------------*
2776 * This function is used to clear the stall flag outside the
2777 * callback. This function is NULL safe.
2778 *------------------------------------------------------------------------*/
2789 USB_BUS_LOCK(xfer->xroot->bus); in usbd_transfer_clear_stall()
2790 xfer->flags.stall_pipe = 0; in usbd_transfer_clear_stall()
2791 USB_BUS_UNLOCK(xfer->xroot->bus); in usbd_transfer_clear_stall()
2794 /*------------------------------------------------------------------------*
2797 * This function is used to add an USB transfer to the pipe transfer list.
2798 *------------------------------------------------------------------------*/
2806 xfer = pq->curr; in usbd_pipe_start()
2807 ep = xfer->endpoint; in usbd_pipe_start()
2809 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); in usbd_pipe_start()
2814 if (ep->is_stalled) { in usbd_pipe_start()
2820 if (xfer->flags.stall_pipe) { in usbd_pipe_start()
2825 xfer->flags.stall_pipe = 0; in usbd_pipe_start()
2828 info = xfer->xroot; in usbd_pipe_start()
2829 udev = info->udev; in usbd_pipe_start()
2834 type = (ep->edesc->bmAttributes & UE_XFERTYPE); in usbd_pipe_start()
2841 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usbd_pipe_start()
2842 (udev->bus->methods->set_stall) ( in usbd_pipe_start()
2844 } else if (udev->ctrl_xfer[1]) { in usbd_pipe_start()
2845 info = udev->ctrl_xfer[1]->xroot; in usbd_pipe_start()
2847 USB_BUS_CS_PROC(info->bus), in usbd_pipe_start()
2848 &udev->cs_msg[0], &udev->cs_msg[1]); in usbd_pipe_start()
2855 * handles set- and clear-stall in hardware. in usbd_pipe_start()
2859 * The transfer will be continued when in usbd_pipe_start()
2860 * the clear-stall control endpoint in usbd_pipe_start()
2863 ep->is_stalled = 1; in usbd_pipe_start()
2873 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usbd_pipe_start()
2874 (udev->bus->methods->clear_stall) (udev, ep); in usbd_pipe_start()
2878 /* Set or clear stall complete - special case */ in usbd_pipe_start()
2879 if (xfer->nframes == 0) { in usbd_pipe_start()
2881 xfer->aframes = 0; in usbd_pipe_start()
2888 * 1) Start the first transfer queued. in usbd_pipe_start()
2890 * 2) Re-start the current USB transfer. in usbd_pipe_start()
2894 * pre transfer start delay: in usbd_pipe_start()
2896 if (xfer->interval > 0) { in usbd_pipe_start()
2897 type = (ep->edesc->bmAttributes & UE_XFERTYPE); in usbd_pipe_start()
2902 xfer->interval); in usbd_pipe_start()
2911 /* the transfer can now be cancelled */ in usbd_pipe_start()
2912 xfer->flags_int.can_cancel_immed = 1; in usbd_pipe_start()
2914 /* start USB transfer, if no error */ in usbd_pipe_start()
2915 if (xfer->error == 0) in usbd_pipe_start()
2916 (ep->methods->start) (xfer); in usbd_pipe_start()
2918 /* check for transfer error */ in usbd_pipe_start()
2919 if (xfer->error) { in usbd_pipe_start()
2925 /*------------------------------------------------------------------------*
2928 * This function is used to setup a timeout on the given USB
2929 * transfer. If the timeout has been deferred the callback given by
2931 *------------------------------------------------------------------------*/
2936 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); in usbd_transfer_timeout_ms()
2939 usb_callout_reset(&xfer->timeout_handle, in usbd_transfer_timeout_ms()
2943 /*------------------------------------------------------------------------*
2946 * - This function will update variables in an USB transfer after
2947 * that the USB transfer is complete.
2949 * - This function is used to start the next USB transfer on the
2950 * ep transfer queue, if any.
2952 * NOTE: In some special cases the USB transfer will not be removed from
2953 * the pipe queue, but remain first. To enforce USB transfer removal call
2954 * this function passing the error code "USB_ERR_CANCELLED".
2959 *------------------------------------------------------------------------*/
2967 bus = xfer->xroot->bus; in usbd_callback_wrapper_sub()
2969 if ((!xfer->flags_int.open) && in usbd_callback_wrapper_sub()
2970 (!xfer->flags_int.did_close)) { in usbd_callback_wrapper_sub()
2973 (xfer->endpoint->methods->close) (xfer); in usbd_callback_wrapper_sub()
2976 xfer->flags_int.did_close = 1; in usbd_callback_wrapper_sub()
2980 * If we have a non-hardware induced error we in usbd_callback_wrapper_sub()
2983 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay && in usbd_callback_wrapper_sub()
2984 (xfer->error == USB_ERR_CANCELLED || in usbd_callback_wrapper_sub()
2985 xfer->error == USB_ERR_TIMEOUT || in usbd_callback_wrapper_sub()
2986 bus->methods->start_dma_delay != NULL)) { in usbd_callback_wrapper_sub()
2990 xfer->flags_int.did_dma_delay = 1; in usbd_callback_wrapper_sub()
2993 xfer->flags_int.can_cancel_immed = 0; in usbd_callback_wrapper_sub()
2995 temp = usbd_get_dma_delay(xfer->xroot->udev); in usbd_callback_wrapper_sub()
3008 if (bus->methods->start_dma_delay != NULL) { in usbd_callback_wrapper_sub()
3009 (bus->methods->start_dma_delay) (xfer); in usbd_callback_wrapper_sub()
3020 if (xfer->aframes > xfer->nframes) { in usbd_callback_wrapper_sub()
3021 if (xfer->error == 0) { in usbd_callback_wrapper_sub()
3024 __FUNCTION__, xfer->aframes, xfer->nframes); in usbd_callback_wrapper_sub()
3027 xfer->aframes = xfer->nframes; in usbd_callback_wrapper_sub()
3031 xfer->actlen = 0; in usbd_callback_wrapper_sub()
3033 for (x = 0; x != xfer->aframes; x++) { in usbd_callback_wrapper_sub()
3034 xfer->actlen += xfer->frlengths[x]; in usbd_callback_wrapper_sub()
3040 * of frames transferred, "xfer->aframes": in usbd_callback_wrapper_sub()
3042 for (; x < xfer->nframes; x++) { in usbd_callback_wrapper_sub()
3047 if (xfer->actlen > xfer->sumlen) { in usbd_callback_wrapper_sub()
3048 if (xfer->error == 0) { in usbd_callback_wrapper_sub()
3051 __FUNCTION__, xfer->actlen, xfer->sumlen); in usbd_callback_wrapper_sub()
3054 xfer->actlen = xfer->sumlen; in usbd_callback_wrapper_sub()
3058 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen, in usbd_callback_wrapper_sub()
3059 xfer->aframes, xfer->nframes); in usbd_callback_wrapper_sub()
3061 if (xfer->error) { in usbd_callback_wrapper_sub()
3062 /* end of control transfer, if any */ in usbd_callback_wrapper_sub()
3063 xfer->flags_int.control_act = 0; in usbd_callback_wrapper_sub()
3066 switch (xfer->error) { in usbd_callback_wrapper_sub()
3076 uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint); in usbd_callback_wrapper_sub()
3082 if ((xfer->error != USB_ERR_CANCELLED) && in usbd_callback_wrapper_sub()
3083 (xfer->flags.pipe_bof)) { in usbd_callback_wrapper_sub()
3085 "on endpoint=%p\n", xfer, xfer->endpoint); in usbd_callback_wrapper_sub()
3090 if (xfer->actlen < xfer->sumlen) { in usbd_callback_wrapper_sub()
3091 /* end of control transfer, if any */ in usbd_callback_wrapper_sub()
3092 xfer->flags_int.control_act = 0; in usbd_callback_wrapper_sub()
3094 if (!xfer->flags_int.short_xfer_ok) { in usbd_callback_wrapper_sub()
3095 xfer->error = USB_ERR_SHORT_XFER; in usbd_callback_wrapper_sub()
3096 if (xfer->flags.pipe_bof) { in usbd_callback_wrapper_sub()
3098 "Short Transfer on endpoint %p.\n", in usbd_callback_wrapper_sub()
3099 xfer, xfer->endpoint); in usbd_callback_wrapper_sub()
3106 * control transfer: in usbd_callback_wrapper_sub()
3108 if (xfer->flags_int.control_act) { in usbd_callback_wrapper_sub()
3109 DPRINTFN(5, "xfer=%p: Control transfer " in usbd_callback_wrapper_sub()
3110 "active on endpoint=%p\n", xfer, xfer->endpoint); in usbd_callback_wrapper_sub()
3116 ep = xfer->endpoint; in usbd_callback_wrapper_sub()
3119 * If the current USB transfer is completing we need to start the in usbd_callback_wrapper_sub()
3123 if (ep->endpoint_q[xfer->stream_id].curr == xfer) { in usbd_callback_wrapper_sub()
3124 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL); in usbd_callback_wrapper_sub()
3126 if (ep->endpoint_q[xfer->stream_id].curr != NULL || in usbd_callback_wrapper_sub()
3127 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) { in usbd_callback_wrapper_sub()
3128 /* there is another USB transfer waiting */ in usbd_callback_wrapper_sub()
3130 /* this is the last USB transfer */ in usbd_callback_wrapper_sub()
3132 xfer->endpoint->is_synced = 0; in usbd_callback_wrapper_sub()
3140 /*------------------------------------------------------------------------*
3143 * This function is used to execute commands non-recursivly on an USB
3144 * transfer.
3145 *------------------------------------------------------------------------*/
3151 * If the transfer is not already processing, in usb_command_wrapper()
3154 if (pq->curr != xfer) { in usb_command_wrapper()
3156 if (pq->curr != NULL) { in usb_command_wrapper()
3158 DPRINTFN(6, "busy %p\n", pq->curr); in usb_command_wrapper()
3164 pq->curr = NULL; in usb_command_wrapper()
3167 if (!pq->recurse_1) { in usb_command_wrapper()
3169 pq->recurse_3 = 0; in usb_command_wrapper()
3173 pq->recurse_1 = 1; in usb_command_wrapper()
3174 pq->recurse_2 = 1; in usb_command_wrapper()
3176 if (pq->curr == NULL) { in usb_command_wrapper()
3177 xfer = TAILQ_FIRST(&pq->head); in usb_command_wrapper()
3179 TAILQ_REMOVE(&pq->head, xfer, in usb_command_wrapper()
3181 xfer->wait_queue = NULL; in usb_command_wrapper()
3182 pq->curr = xfer; in usb_command_wrapper()
3187 DPRINTFN(6, "cb %p (enter)\n", pq->curr); in usb_command_wrapper()
3188 (pq->command) (pq); in usb_command_wrapper()
3189 DPRINTFN(6, "cb %p (leave)\n", pq->curr); in usb_command_wrapper()
3195 pq->recurse_3 = 1; in usb_command_wrapper()
3197 } while (!pq->recurse_2); in usb_command_wrapper()
3200 pq->recurse_1 = 0; in usb_command_wrapper()
3204 pq->recurse_2 = 0; in usb_command_wrapper()
3208 /*------------------------------------------------------------------------*
3211 * This function is used to setup the default USB control endpoint
3212 * transfer.
3213 *------------------------------------------------------------------------*/
3222 if (udev->parent_hub == NULL) in usbd_ctrl_transfer_setup()
3226 xfer = udev->ctrl_xfer[0]; in usbd_ctrl_transfer_setup()
3230 ((xfer->address == udev->address) && in usbd_ctrl_transfer_setup()
3231 (udev->ctrl_ep_desc.wMaxPacketSize[0] == in usbd_ctrl_transfer_setup()
3232 udev->ddesc.bMaxPacketSize)); in usbd_ctrl_transfer_setup()
3233 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usbd_ctrl_transfer_setup()
3236 * NOTE: checking "xfer->address" and in usbd_ctrl_transfer_setup()
3237 * starting the USB transfer must be in usbd_ctrl_transfer_setup()
3258 udev->ctrl_ep_desc.wMaxPacketSize[0] = in usbd_ctrl_transfer_setup()
3259 udev->ddesc.bMaxPacketSize; in usbd_ctrl_transfer_setup()
3262 * Unsetup any existing USB transfer: in usbd_ctrl_transfer_setup()
3264 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); in usbd_ctrl_transfer_setup()
3269 udev->clear_stall_errors = 0; in usbd_ctrl_transfer_setup()
3272 * Try to setup a new USB transfer for the in usbd_ctrl_transfer_setup()
3277 udev->ctrl_xfer, udev->bus->control_ep_quirk ? in usbd_ctrl_transfer_setup()
3279 &udev->device_mtx)) { in usbd_ctrl_transfer_setup()
3281 "USB transfer\n"); in usbd_ctrl_transfer_setup()
3287 /*------------------------------------------------------------------------*
3288 * usbd_clear_data_toggle - factored out code
3290 * NOTE: the intention of this function is not to reset the hardware
3292 *------------------------------------------------------------------------*/
3296 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); in usbd_clear_stall_locked()
3299 if (udev->flags.usb_mode == USB_MODE_HOST && in usbd_clear_stall_locked()
3300 udev->parent_hub != NULL && in usbd_clear_stall_locked()
3301 udev->bus->methods->clear_stall != NULL && in usbd_clear_stall_locked()
3302 ep->methods != NULL) { in usbd_clear_stall_locked()
3303 (udev->bus->methods->clear_stall) (udev, ep); in usbd_clear_stall_locked()
3307 /*------------------------------------------------------------------------*
3308 * usbd_clear_data_toggle - factored out code
3310 * NOTE: the intention of this function is not to reset the hardware
3312 *------------------------------------------------------------------------*/
3318 USB_BUS_LOCK(udev->bus); in usbd_clear_data_toggle()
3319 ep->toggle_next = 0; in usbd_clear_data_toggle()
3322 USB_BUS_UNLOCK(udev->bus); in usbd_clear_data_toggle()
3325 /*------------------------------------------------------------------------*
3326 * usbd_clear_stall_callback - factored out clear stall callback
3329 * xfer1: Clear Stall Control Transfer
3330 * xfer2: Stalled USB Transfer
3332 * This function is NULL safe.
3353 *------------------------------------------------------------------------*/
3372 * pre-clear the data toggle to DATA0 ("umass.c" and in usbd_clear_stall_callback()
3373 * "ata-usb.c" depends on this) in usbd_clear_stall_callback()
3376 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint); in usbd_clear_stall_callback()
3378 /* setup a clear-stall packet */ in usbd_clear_stall_callback()
3383 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress; in usbd_clear_stall_callback()
3393 /* copy in the transfer */ in usbd_clear_stall_callback()
3395 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); in usbd_clear_stall_callback()
3398 xfer1->frlengths[0] = sizeof(req); in usbd_clear_stall_callback()
3399 xfer1->nframes = 1; in usbd_clear_stall_callback()
3408 if (xfer1->error == USB_ERR_CANCELLED) { in usbd_clear_stall_callback()
3416 /*------------------------------------------------------------------------*
3419 * The following function gets called from the USB keyboard driver and
3425 *------------------------------------------------------------------------*/
3443 continue; /* no USB transfer */ in usbd_transfer_poll()
3444 xroot = xfer->xroot; in usbd_transfer_poll()
3447 udev = xroot->udev; in usbd_transfer_poll()
3450 bus = udev->bus; in usbd_transfer_poll()
3453 if (bus->methods == NULL) in usbd_transfer_poll()
3455 if (bus->methods->xfer_poll == NULL) in usbd_transfer_poll()
3464 while (mtx_owned(&bus->bus_spin_lock)) { in usbd_transfer_poll()
3465 mtx_unlock_spin(&bus->bus_spin_lock); in usbd_transfer_poll()
3470 while (mtx_owned(&bus->bus_mtx)) { in usbd_transfer_poll()
3471 mtx_unlock(&bus->bus_mtx); in usbd_transfer_poll()
3475 /* make sure that the transfer mutex is not locked */ in usbd_transfer_poll()
3476 while (mtx_owned(xroot->xfer_mtx)) { in usbd_transfer_poll()
3477 mtx_unlock(xroot->xfer_mtx); in usbd_transfer_poll()
3483 USB_BUS_CONTROL_XFER_PROC(bus)->up_msleep = 0; in usbd_transfer_poll()
3484 USB_BUS_EXPLORE_PROC(bus)->up_msleep = 0; in usbd_transfer_poll()
3485 USB_BUS_GIANT_PROC(bus)->up_msleep = 0; in usbd_transfer_poll()
3486 USB_BUS_NON_GIANT_ISOC_PROC(bus)->up_msleep = 0; in usbd_transfer_poll()
3487 USB_BUS_NON_GIANT_BULK_PROC(bus)->up_msleep = 0; in usbd_transfer_poll()
3490 (bus->methods->xfer_poll) (bus); in usbd_transfer_poll()
3492 USB_BUS_LOCK(xroot->bus); in usbd_transfer_poll()
3495 if (udev->ctrl_xfer[1] != NULL) { in usbd_transfer_poll()
3497 pm = &udev->cs_msg[0].hdr; in usbd_transfer_poll()
3498 (pm->pm_callback) (pm); in usbd_transfer_poll()
3500 pm = &udev->ctrl_xfer[1]-> in usbd_transfer_poll()
3501 xroot->done_m[0].hdr; in usbd_transfer_poll()
3502 (pm->pm_callback) (pm); in usbd_transfer_poll()
3506 pm = &xroot->done_m[0].hdr; in usbd_transfer_poll()
3507 (pm->pm_callback) (pm); in usbd_transfer_poll()
3509 USB_BUS_UNLOCK(xroot->bus); in usbd_transfer_poll()
3511 /* restore transfer mutex */ in usbd_transfer_poll()
3512 while (drop_xfer--) in usbd_transfer_poll()
3513 mtx_lock(xroot->xfer_mtx); in usbd_transfer_poll()
3516 while (drop_bus--) in usbd_transfer_poll()
3517 mtx_lock(&bus->bus_mtx); in usbd_transfer_poll()
3520 while (drop_bus_spin--) in usbd_transfer_poll()
3521 mtx_lock_spin(&bus->bus_spin_lock); in usbd_transfer_poll()
3567 ptr->range.max = intr_range_max[speed]; in usbd_get_std_packet_size()
3570 ptr->range.max = isoc_range_max[speed]; in usbd_get_std_packet_size()
3579 ptr->fixed[0] = temp; in usbd_get_std_packet_size()
3580 ptr->fixed[1] = temp; in usbd_get_std_packet_size()
3581 ptr->fixed[2] = temp; in usbd_get_std_packet_size()
3582 ptr->fixed[3] = temp; in usbd_get_std_packet_size()
3586 ptr->fixed[1] = 16; in usbd_get_std_packet_size()
3587 ptr->fixed[2] = 32; in usbd_get_std_packet_size()
3588 ptr->fixed[3] = 64; in usbd_get_std_packet_size()
3593 ptr->fixed[2] = 1024; in usbd_get_std_packet_size()
3594 ptr->fixed[3] = 1536; in usbd_get_std_packet_size()
3603 return (xfer->priv_sc); in usbd_xfer_softc()
3609 return (xfer->priv_fifo); in usbd_xfer_get_priv()
3615 xfer->priv_fifo = ptr; in usbd_xfer_set_priv()
3621 return (xfer->usb_state); in usbd_xfer_state()
3629 xfer->flags.force_short_xfer = 1; in usbd_xfer_set_flag()
3632 xfer->flags.short_xfer_ok = 1; in usbd_xfer_set_flag()
3635 xfer->flags.short_frames_ok = 1; in usbd_xfer_set_flag()
3638 xfer->flags.manual_status = 1; in usbd_xfer_set_flag()
3648 xfer->flags.force_short_xfer = 0; in usbd_xfer_clr_flag()
3651 xfer->flags.short_xfer_ok = 0; in usbd_xfer_clr_flag()
3654 xfer->flags.short_frames_ok = 0; in usbd_xfer_clr_flag()
3657 xfer->flags.manual_status = 0; in usbd_xfer_clr_flag()
3663 * The following function returns in milliseconds when the isochronous
3664 * transfer was completed by the hardware. The returned value wraps
3670 return (xfer->isoc_time_complete); in usbd_xfer_get_timestamp()
3674 * The following function returns non-zero if the max packet size
3680 return (xfer->flags_int.maxp_was_clamped); in usbd_xfer_maxp_was_clamped()
3684 * The following function computes the next isochronous frame number
3687 * The function returns non-zero if there was a discontinuity.
3700 delta = (xfer->endpoint->isoc_next - frame_curr) & frame_mask; in usbd_xfer_get_isochronous_start_frame()
3703 * Check if it is the first transfer or if the future frame in usbd_xfer_get_isochronous_start_frame()
3707 if (xfer->endpoint->is_synced == 0 || in usbd_xfer_get_isochronous_start_frame()
3710 /* Schedule transfer 2 milliseconds into the future. */ in usbd_xfer_get_isochronous_start_frame()
3711 xfer->endpoint->isoc_next = (frame_curr + 2 * frame_ms + frame_min) & frame_mask; in usbd_xfer_get_isochronous_start_frame()
3712 xfer->endpoint->is_synced = 1; in usbd_xfer_get_isochronous_start_frame()
3721 *p_frame_start = xfer->endpoint->isoc_next & frame_mask; in usbd_xfer_get_isochronous_start_frame()
3724 delta = xfer->endpoint->isoc_next - frame_curr + (frame_curr % frame_ms); in usbd_xfer_get_isochronous_start_frame()
3728 switch (usbd_get_speed(xfer->xroot->udev)) { in usbd_xfer_get_isochronous_start_frame()
3738 duration = ((xfer->nframes << shift) + 7) / 8; in usbd_xfer_get_isochronous_start_frame()
3740 /* Compute full 32-bit completion time, in milliseconds. */ in usbd_xfer_get_isochronous_start_frame()
3741 xfer->isoc_time_complete = in usbd_xfer_get_isochronous_start_frame()
3742 usb_isoc_time_expand(xfer->xroot->bus, frame_curr / frame_ms) + in usbd_xfer_get_isochronous_start_frame()
3746 xfer->endpoint->isoc_next += duration * frame_ms; in usbd_xfer_get_isochronous_start_frame()
3747 xfer->endpoint->isoc_next &= frame_mask; in usbd_xfer_get_isochronous_start_frame()