Deleted Added
sdiff udiff text old ( 184610 ) new ( 184824 )
full compact
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_transfer.c 184824 2008-11-10 20:54:31Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
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.
13 *
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 */
26
27#include <dev/usb2/include/usb2_mfunc.h>
28#include <dev/usb2/include/usb2_error.h>
29#include <dev/usb2/include/usb2_standard.h>
30#include <dev/usb2/include/usb2_defs.h>
31
32#define USB_DEBUG_VAR usb2_debug
33
34#include <dev/usb2/core/usb2_core.h>
35#include <dev/usb2/core/usb2_busdma.h>
36#include <dev/usb2/core/usb2_process.h>
37#include <dev/usb2/core/usb2_transfer.h>
38#include <dev/usb2/core/usb2_device.h>
39#include <dev/usb2/core/usb2_debug.h>
40#include <dev/usb2/core/usb2_util.h>
41
42#include <dev/usb2/controller/usb2_controller.h>
43#include <dev/usb2/controller/usb2_bus.h>
44
45struct usb2_std_packet_size {
46 struct {
47 uint16_t min; /* inclusive */
48 uint16_t max; /* inclusive */
49 } range;
50
51 uint16_t fixed[4];
52};
53
54/*
55 * This table stores the all the allowed packet sizes based on
56 * endpoint type and USB speed:
57 */
58static const struct usb2_std_packet_size
59 usb2_std_packet_size[4][USB_SPEED_MAX] = {
60
61 [UE_INTERRUPT] = {
62 [USB_SPEED_LOW] = {.range = {0, 8}},
63 [USB_SPEED_FULL] = {.range = {0, 64}},
64 [USB_SPEED_HIGH] = {.range = {0, 1024}},
65 [USB_SPEED_VARIABLE] = {.range = {0, 1024}},
66 },
67
68 [UE_CONTROL] = {
69 [USB_SPEED_LOW] = {.fixed = {8, 8, 8, 8}},
70 [USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
71 [USB_SPEED_HIGH] = {.fixed = {64, 64, 64, 64}},
72 [USB_SPEED_VARIABLE] = {.fixed = {512, 512, 512, 512}},
73 },
74
75 [UE_BULK] = {
76 [USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}}, /* invalid */
77 [USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}},
78 [USB_SPEED_HIGH] = {.fixed = {512, 512, 512, 512}},
79 [USB_SPEED_VARIABLE] = {.fixed = {512, 512, 1024, 1536}},
80 },
81
82 [UE_ISOCHRONOUS] = {
83 [USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}}, /* invalid */
84 [USB_SPEED_FULL] = {.range = {0, 1023}},
85 [USB_SPEED_HIGH] = {.range = {0, 1024}},
86 [USB_SPEED_VARIABLE] = {.range = {0, 3584}},
87 },
88};
89
90static const struct usb2_config usb2_control_ep_cfg[USB_DEFAULT_XFER_MAX] = {
91
92 /* This transfer is used for generic control endpoint transfers */
93
94 [0] = {
95 .type = UE_CONTROL,
96 .endpoint = 0x00, /* Control endpoint */
97 .direction = UE_DIR_ANY,
98 .mh.bufsize = 1024, /* bytes */
99 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
100 .mh.callback = &usb2_do_request_callback,
101 .md.bufsize = 1024, /* bytes */
102 .md.flags = {.proxy_buffer = 1,.short_xfer_ok = 0,},
103 .md.callback = &usb2_handle_request_callback,
104 },
105
106 /* This transfer is used for generic clear stall only */
107
108 [1] = {
109 .type = UE_CONTROL,
110 .endpoint = 0x00, /* Control pipe */
111 .direction = UE_DIR_ANY,
112 .mh.bufsize = sizeof(struct usb2_device_request),
113 .mh.flags = {},
114 .mh.callback = &usb2_do_clear_stall_callback,
115 .mh.timeout = 1000, /* 1 second */
116 .mh.interval = 50, /* 50ms */
117 },
118};
119
120/* function prototypes */
121
122static void usb2_update_max_frame_size(struct usb2_xfer *xfer);
123static uint32_t usb2_get_dma_delay(struct usb2_bus *bus);
124static void usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay);
125static void usb2_control_transfer_init(struct usb2_xfer *xfer);
126static uint8_t usb2_start_hardware_sub(struct usb2_xfer *xfer);
127static void usb2_callback_proc(struct usb2_proc_msg *_pm);
128static void usb2_callback_ss_done_defer(struct usb2_xfer *xfer);
129static void usb2_callback_wrapper(struct usb2_xfer_queue *pq);
130static void usb2_dma_delay_done_cb(void *arg);
131static void usb2_transfer_start_cb(void *arg);
132static uint8_t usb2_callback_wrapper_sub(struct usb2_xfer *xfer);
133
134/*------------------------------------------------------------------------*
135 * usb2_update_max_frame_size
136 *
137 * This function updates the maximum frame size, hence high speed USB
138 * can transfer multiple consecutive packets.
139 *------------------------------------------------------------------------*/
140static void
141usb2_update_max_frame_size(struct usb2_xfer *xfer)
142{
143 /* compute maximum frame size */
144
145 if (xfer->max_packet_count == 2) {
146 xfer->max_frame_size = 2 * xfer->max_packet_size;
147 } else if (xfer->max_packet_count == 3) {
148 xfer->max_frame_size = 3 * xfer->max_packet_size;
149 } else {
150 xfer->max_frame_size = xfer->max_packet_size;
151 }
152 return;
153}
154
155/*------------------------------------------------------------------------*
156 * usb2_get_dma_delay
157 *
158 * The following function is called when we need to
159 * synchronize with DMA hardware.
160 *
161 * Returns:
162 * 0: no DMA delay required
163 * Else: milliseconds of DMA delay
164 *------------------------------------------------------------------------*/
165static uint32_t
166usb2_get_dma_delay(struct usb2_bus *bus)
167{
168 uint32_t temp = 0;
169
170 if (bus->methods->get_dma_delay) {
171 (bus->methods->get_dma_delay) (bus, &temp);
172 /*
173 * Round up and convert to milliseconds. Note that we use
174 * 1024 milliseconds per second. to save a division.
175 */
176 temp += 0x3FF;
177 temp /= 0x400;
178 }
179 return (temp);
180}
181
182/*------------------------------------------------------------------------*
183 * usb2_transfer_setup_sub_malloc
184 *
185 * This function will allocate one or more DMA'able memory chunks
186 * according to "size", "align" and "count" arguments. "ppc" is
187 * pointed to a linear array of USB page caches afterwards.
188 *
189 * Returns:
190 * 0: Success
191 * Else: Failure
192 *------------------------------------------------------------------------*/
193uint8_t
194usb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm,
195 struct usb2_page_cache **ppc, uint32_t size, uint32_t align,
196 uint32_t count)
197{
198 struct usb2_page_cache *pc;
199 struct usb2_page *pg;
200 void *buf;
201 uint32_t n_dma_pc;
202 uint32_t n_obj;
203 uint32_t x;
204 uint32_t y;
205 uint32_t r;
206 uint32_t z;
207
208 USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n",
209 align));
210 USB_ASSERT(size > 0, ("Invalid size = 0!\n"));
211
212 if (count == 0) {
213 return (0); /* nothing to allocate */
214 }
215 /*
216 * Make sure that the size is aligned properly.
217 */
218 size = -((-size) & (-align));
219
220 /*
221 * Try multi-allocation chunks to reduce the number of DMA
222 * allocations, hence DMA allocations are slow.
223 */
224 if (size >= PAGE_SIZE) {
225 n_dma_pc = count;
226 n_obj = 1;
227 } else {
228 /* compute number of objects per page */
229 n_obj = (PAGE_SIZE / size);
230 /*
231 * Compute number of DMA chunks, rounded up
232 * to nearest one:
233 */
234 n_dma_pc = ((count + n_obj - 1) / n_obj);
235 }
236
237 if (parm->buf == NULL) {
238 /* for the future */
239 parm->dma_page_ptr += n_dma_pc;
240 parm->dma_page_cache_ptr += n_dma_pc;
241 parm->dma_page_ptr += count;
242 parm->xfer_page_cache_ptr += count;
243 return (0);
244 }
245 for (x = 0; x != n_dma_pc; x++) {
246 /* need to initialize the page cache */
247 parm->dma_page_cache_ptr[x].tag_parent =
248 &parm->curr_xfer->usb2_root->dma_parent_tag;
249 }
250 for (x = 0; x != count; x++) {
251 /* need to initialize the page cache */
252 parm->xfer_page_cache_ptr[x].tag_parent =
253 &parm->curr_xfer->usb2_root->dma_parent_tag;
254 }
255
256 if (ppc) {
257 *ppc = parm->xfer_page_cache_ptr;
258 }
259 r = count; /* set remainder count */
260 z = n_obj * size; /* set allocation size */
261 pc = parm->xfer_page_cache_ptr;
262 pg = parm->dma_page_ptr;
263
264 for (x = 0; x != n_dma_pc; x++) {
265
266 if (r < n_obj) {
267 /* compute last remainder */
268 z = r * size;
269 n_obj = r;
270 }
271 if (usb2_pc_alloc_mem(parm->dma_page_cache_ptr,
272 pg, z, align)) {
273 return (1); /* failure */
274 }
275 /* Set beginning of current buffer */
276 buf = parm->dma_page_cache_ptr->buffer;
277 /* Make room for one DMA page cache and one page */
278 parm->dma_page_cache_ptr++;
279 pg++;
280
281 for (y = 0; (y != n_obj); y++, r--, pc++, pg++) {
282
283 /* Load sub-chunk into DMA */
284 if (usb2_pc_dmamap_create(pc, size)) {
285 return (1); /* failure */
286 }
287 pc->buffer = USB_ADD_BYTES(buf, y * size);
288 pc->page_start = pg;
289
290 mtx_lock(pc->tag_parent->mtx);
291 if (usb2_pc_load_mem(pc, size, 1 /* synchronous */ )) {
292 mtx_unlock(pc->tag_parent->mtx);
293 return (1); /* failure */
294 }
295 mtx_unlock(pc->tag_parent->mtx);
296 }
297 }
298
299 parm->xfer_page_cache_ptr = pc;
300 parm->dma_page_ptr = pg;
301 return (0);
302}
303
304/*------------------------------------------------------------------------*
305 * usb2_transfer_setup_sub - transfer setup subroutine
306 *
307 * This function must be called from the "xfer_setup" callback of the
308 * USB Host or Device controller driver when setting up an USB
309 * transfer. This function will setup correct packet sizes, buffer
310 * sizes, flags and more, that are stored in the "usb2_xfer"
311 * structure.
312 *------------------------------------------------------------------------*/
313void
314usb2_transfer_setup_sub(struct usb2_setup_params *parm)
315{
316 enum {
317 REQ_SIZE = 8,
318 MIN_PKT = 8,
319 };
320 struct usb2_xfer *xfer = parm->curr_xfer;
321 const struct usb2_config_sub *setup_sub = parm->curr_setup_sub;
322 struct usb2_endpoint_descriptor *edesc;
323 struct usb2_std_packet_size std_size;
324 uint32_t n_frlengths;
325 uint32_t n_frbuffers;
326 uint32_t x;
327 uint8_t type;
328 uint8_t zmps;
329
330 /*
331 * Sanity check. The following parameters must be initialized before
332 * calling this function.
333 */
334 if ((parm->hc_max_packet_size == 0) ||
335 (parm->hc_max_packet_count == 0) ||
336 (parm->hc_max_frame_size == 0)) {
337 parm->err = USB_ERR_INVAL;
338 goto done;
339 }
340 edesc = xfer->pipe->edesc;
341
342 type = (edesc->bmAttributes & UE_XFERTYPE);
343
344 xfer->flags = setup_sub->flags;
345 xfer->nframes = setup_sub->frames;
346 xfer->timeout = setup_sub->timeout;
347 xfer->callback = setup_sub->callback;
348 xfer->interval = setup_sub->interval;
349 xfer->endpoint = edesc->bEndpointAddress;
350 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
351 xfer->max_packet_count = 1;
352 /* make a shadow copy: */
353 xfer->flags_int.usb2_mode = parm->udev->flags.usb2_mode;
354
355 parm->bufsize = setup_sub->bufsize;
356
357 if (parm->speed == USB_SPEED_HIGH) {
358 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
359 xfer->max_packet_size &= 0x7FF;
360 }
361 /* range check "max_packet_count" */
362
363 if (xfer->max_packet_count > parm->hc_max_packet_count) {
364 xfer->max_packet_count = parm->hc_max_packet_count;
365 }
366 /* filter "wMaxPacketSize" according to HC capabilities */
367
368 if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
369 (xfer->max_packet_size == 0)) {
370 xfer->max_packet_size = parm->hc_max_packet_size;
371 }
372 /* filter "wMaxPacketSize" according to standard sizes */
373
374 std_size = usb2_std_packet_size[type][parm->speed];
375
376 if (std_size.range.min || std_size.range.max) {
377
378 if (xfer->max_packet_size < std_size.range.min) {
379 xfer->max_packet_size = std_size.range.min;
380 }
381 if (xfer->max_packet_size > std_size.range.max) {
382 xfer->max_packet_size = std_size.range.max;
383 }
384 } else {
385
386 if (xfer->max_packet_size >= std_size.fixed[3]) {
387 xfer->max_packet_size = std_size.fixed[3];
388 } else if (xfer->max_packet_size >= std_size.fixed[2]) {
389 xfer->max_packet_size = std_size.fixed[2];
390 } else if (xfer->max_packet_size >= std_size.fixed[1]) {
391 xfer->max_packet_size = std_size.fixed[1];
392 } else {
393 /* only one possibility left */
394 xfer->max_packet_size = std_size.fixed[0];
395 }
396 }
397
398 /* compute "max_frame_size" */
399
400 usb2_update_max_frame_size(xfer);
401
402 /* check interrupt interval and transfer pre-delay */
403
404 if (type == UE_ISOCHRONOUS) {
405
406 uint32_t frame_limit;
407
408 xfer->interval = 0; /* not used, must be zero */
409 xfer->flags_int.isochronous_xfr = 1; /* set flag */
410
411 if (xfer->timeout == 0) {
412 /*
413 * set a default timeout in
414 * case something goes wrong!
415 */
416 xfer->timeout = 1000 / 4;
417 }
418 if (parm->speed == USB_SPEED_HIGH) {
419 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
420 } else {
421 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
422 }
423
424 if (xfer->nframes > frame_limit) {
425 /*
426 * this is not going to work
427 * cross hardware
428 */
429 parm->err = USB_ERR_INVAL;
430 goto done;
431 }
432 if (xfer->nframes == 0) {
433 /*
434 * this is not a valid value
435 */
436 parm->err = USB_ERR_ZERO_NFRAMES;
437 goto done;
438 }
439 } else {
440
441 /*
442 * if a value is specified use that else check the endpoint
443 * descriptor
444 */
445 if (xfer->interval == 0) {
446
447 if (type == UE_INTERRUPT) {
448
449 xfer->interval = edesc->bInterval;
450
451 if (parm->speed == USB_SPEED_HIGH) {
452 xfer->interval /= 8; /* 125us -> 1ms */
453 }
454 if (xfer->interval == 0) {
455 /*
456 * one millisecond is the smallest
457 * interval
458 */
459 xfer->interval = 1;
460 }
461 }
462 }
463 }
464
465 /*
466 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
467 * to be equal to zero when setting up USB transfers, hence
468 * this leads to alot of extra code in the USB kernel.
469 */
470
471 if ((xfer->max_frame_size == 0) ||
472 (xfer->max_packet_size == 0)) {
473
474 zmps = 1;
475
476 if ((parm->bufsize <= MIN_PKT) &&
477 (type != UE_CONTROL) &&
478 (type != UE_BULK)) {
479
480 /* workaround */
481 xfer->max_packet_size = MIN_PKT;
482 xfer->max_packet_count = 1;
483 parm->bufsize = 0; /* automatic setup length */
484 usb2_update_max_frame_size(xfer);
485
486 } else {
487 parm->err = USB_ERR_ZERO_MAXP;
488 goto done;
489 }
490
491 } else {
492 zmps = 0;
493 }
494
495 /*
496 * check if we should setup a default
497 * length:
498 */
499
500 if (parm->bufsize == 0) {
501
502 parm->bufsize = xfer->max_frame_size;
503
504 if (type == UE_ISOCHRONOUS) {
505 parm->bufsize *= xfer->nframes;
506 }
507 }
508 /*
509 * check if we are about to setup a proxy
510 * type of buffer:
511 */
512
513 if (xfer->flags.proxy_buffer) {
514
515 /* round bufsize up */
516
517 parm->bufsize += (xfer->max_frame_size - 1);
518
519 if (parm->bufsize < xfer->max_frame_size) {
520 /* length wrapped around */
521 parm->err = USB_ERR_INVAL;
522 goto done;
523 }
524 /* subtract remainder */
525
526 parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
527
528 /* add length of USB device request structure, if any */
529
530 if (type == UE_CONTROL) {
531 parm->bufsize += REQ_SIZE; /* SETUP message */
532 }
533 }
534 xfer->max_data_length = parm->bufsize;
535
536 /* Setup "n_frlengths" and "n_frbuffers" */
537
538 if (type == UE_ISOCHRONOUS) {
539 n_frlengths = xfer->nframes;
540 n_frbuffers = 1;
541 } else {
542
543 if (type == UE_CONTROL) {
544 xfer->flags_int.control_xfr = 1;
545 if (xfer->nframes == 0) {
546 if (parm->bufsize <= REQ_SIZE) {
547 /*
548 * there will never be any data
549 * stage
550 */
551 xfer->nframes = 1;
552 } else {
553 xfer->nframes = 2;
554 }
555 }
556 } else {
557 if (xfer->nframes == 0) {
558 xfer->nframes = 1;
559 }
560 }
561
562 n_frlengths = xfer->nframes;
563 n_frbuffers = xfer->nframes;
564 }
565
566 /*
567 * check if we have room for the
568 * USB device request structure:
569 */
570
571 if (type == UE_CONTROL) {
572
573 if (xfer->max_data_length < REQ_SIZE) {
574 /* length wrapped around or too small bufsize */
575 parm->err = USB_ERR_INVAL;
576 goto done;
577 }
578 xfer->max_data_length -= REQ_SIZE;
579 }
580 /* setup "frlengths" */
581
582 xfer->frlengths = parm->xfer_length_ptr;
583
584 parm->xfer_length_ptr += n_frlengths;
585
586 /* setup "frbuffers" */
587
588 xfer->frbuffers = parm->xfer_page_cache_ptr;
589
590 parm->xfer_page_cache_ptr += n_frbuffers;
591
592 /*
593 * check if we need to setup
594 * a local buffer:
595 */
596
597 if (!xfer->flags.ext_buffer) {
598
599 /* align data */
600 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
601
602 if (parm->buf) {
603
604 xfer->local_buffer =
605 USB_ADD_BYTES(parm->buf, parm->size[0]);
606
607 usb2_set_frame_offset(xfer, 0, 0);
608
609 if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
610 usb2_set_frame_offset(xfer, REQ_SIZE, 1);
611 }
612 }
613 parm->size[0] += parm->bufsize;
614
615 /* align data again */
616 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
617 }
618 /*
619 * Compute maximum buffer size
620 */
621
622 if (parm->bufsize_max < parm->bufsize) {
623 parm->bufsize_max = parm->bufsize;
624 }
625 if (xfer->flags_int.bdma_enable) {
626 /*
627 * Setup "dma_page_ptr".
628 *
629 * Proof for formula below:
630 *
631 * Assume there are three USB frames having length "a", "b" and
632 * "c". These USB frames will at maximum need "z"
633 * "usb2_page" structures. "z" is given by:
634 *
635 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
636 * ((c / USB_PAGE_SIZE) + 2);
637 *
638 * Constraining "a", "b" and "c" like this:
639 *
640 * (a + b + c) <= parm->bufsize
641 *
642 * We know that:
643 *
644 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
645 *
646 * Here is the general formula:
647 */
648 xfer->dma_page_ptr = parm->dma_page_ptr;
649 parm->dma_page_ptr += (2 * n_frbuffers);
650 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
651 }
652 if (zmps) {
653 /* correct maximum data length */
654 xfer->max_data_length = 0;
655 }
656 /* subtract USB frame remainder from "hc_max_frame_size" */
657
658 xfer->max_usb2_frame_size =
659 (parm->hc_max_frame_size -
660 (parm->hc_max_frame_size % xfer->max_frame_size));
661
662 if (xfer->max_usb2_frame_size == 0) {
663 parm->err = USB_ERR_INVAL;
664 goto done;
665 }
666 /* initialize max frame count */
667
668 xfer->max_frame_count = xfer->nframes;
669
670 /* initialize frame buffers */
671
672 if (parm->buf) {
673 for (x = 0; x != n_frbuffers; x++) {
674 xfer->frbuffers[x].tag_parent =
675 &xfer->usb2_root->dma_parent_tag;
676
677 if (xfer->flags_int.bdma_enable &&
678 (parm->bufsize_max > 0)) {
679
680 if (usb2_pc_dmamap_create(
681 xfer->frbuffers + x,
682 parm->bufsize_max)) {
683 parm->err = USB_ERR_NOMEM;
684 goto done;
685 }
686 }
687 }
688 }
689done:
690 if (parm->err) {
691 /*
692 * Set some dummy values so that we avoid division by zero:
693 */
694 xfer->max_usb2_frame_size = 1;
695 xfer->max_frame_size = 1;
696 xfer->max_packet_size = 1;
697 xfer->max_data_length = 0;
698 xfer->nframes = 0;
699 xfer->max_frame_count = 0;
700 }
701 return;
702}
703
704/*------------------------------------------------------------------------*
705 * usb2_transfer_setup - setup an array of USB transfers
706 *
707 * NOTE: You must always call "usb2_transfer_unsetup" after calling
708 * "usb2_transfer_setup" if success was returned.
709 *
710 * The idea is that the USB device driver should pre-allocate all its
711 * transfers by one call to this function.
712 *
713 * Return values:
714 * 0: Success
715 * Else: Failure
716 *------------------------------------------------------------------------*/
717usb2_error_t
718usb2_transfer_setup(struct usb2_device *udev,
719 const uint8_t *ifaces, struct usb2_xfer **ppxfer,
720 const struct usb2_config *setup_start, uint16_t n_setup,
721 void *priv_sc, struct mtx *priv_mtx)
722{
723 struct usb2_xfer dummy;
724 struct usb2_setup_params parm;
725 const struct usb2_config *setup_end = setup_start + n_setup;
726 const struct usb2_config *setup;
727 struct usb2_pipe *pipe;
728 struct usb2_xfer_root *info;
729 struct usb2_xfer *xfer;
730 void *buf = NULL;
731 uint16_t n;
732 uint16_t refcount;
733
734 parm.err = 0;
735 refcount = 0;
736 info = NULL;
737
738 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
739 "usb2_transfer_setup can sleep!");
740
741 /* do some checking first */
742
743 if (n_setup == 0) {
744 DPRINTFN(6, "setup array has zero length!\n");
745 return (USB_ERR_INVAL);
746 }
747 if (ifaces == 0) {
748 DPRINTFN(6, "ifaces array is NULL!\n");
749 return (USB_ERR_INVAL);
750 }
751 if (priv_mtx == NULL) {
752 DPRINTFN(6, "using global lock\n");
753 priv_mtx = &Giant;
754 }
755 /* sanity checks */
756 for (setup = setup_start, n = 0;
757 setup != setup_end; setup++, n++) {
758 if ((setup->mh.bufsize == 0xffffffff) ||
759 (setup->md.bufsize == 0xffffffff)) {
760 parm.err = USB_ERR_BAD_BUFSIZE;
761 DPRINTF("invalid bufsize\n");
762 }
763 if ((setup->mh.callback == NULL) &&
764 (setup->md.callback == NULL)) {
765 parm.err = USB_ERR_NO_CALLBACK;
766 DPRINTF("no callback\n");
767 }
768 ppxfer[n] = NULL;
769 }
770
771 if (parm.err) {
772 goto done;
773 }
774 bzero(&parm, sizeof(parm));
775
776 parm.udev = udev;
777 parm.speed = usb2_get_speed(udev);
778 parm.hc_max_packet_count = 1;
779
780 if (parm.speed >= USB_SPEED_MAX) {
781 parm.err = USB_ERR_INVAL;
782 goto done;
783 }
784 /* setup all transfers */
785
786 while (1) {
787
788 if (buf) {
789 /*
790 * Initialize the "usb2_xfer_root" structure,
791 * which is common for all our USB transfers.
792 */
793 info = USB_ADD_BYTES(buf, 0);
794
795 info->memory_base = buf;
796 info->memory_size = parm.size[0];
797
798 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
799 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
800 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
801 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
802
803 usb2_cv_init(&info->cv_drain, "WDRAIN");
804
805 info->priv_mtx = priv_mtx;
806
807 usb2_dma_tag_setup(&info->dma_parent_tag,
808 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
809 priv_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
810
811 info->bus = udev->bus;
812
813 TAILQ_INIT(&info->done_q.head);
814 info->done_q.command = &usb2_callback_wrapper;
815
816 TAILQ_INIT(&info->dma_q.head);
817 info->dma_q.command = &usb2_bdma_work_loop;
818
819 info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
820 info->done_m[0].usb2_root = info;
821 info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
822 info->done_m[1].usb2_root = info;
823
824 /* create a callback thread */
825
826 if (usb2_proc_setup(&info->done_p,
827 &udev->bus->bus_mtx, USB_PRI_HIGH)) {
828 parm.err = USB_ERR_NO_INTR_THREAD;
829 goto done;
830 }
831 }
832 /* reset sizes */
833
834 parm.size[0] = 0;
835 parm.buf = buf;
836 parm.size[0] += sizeof(info[0]);
837
838 for (setup = setup_start, n = 0;
839 setup != setup_end; setup++, n++) {
840
841 /* select mode specific structure */
842 if (udev->flags.usb2_mode == USB_MODE_HOST) {
843 parm.curr_setup_sub = &setup->mh;
844 } else {
845 parm.curr_setup_sub = &setup->md;
846 }
847 /* skip USB transfers without callbacks: */
848 if (parm.curr_setup_sub->callback == NULL) {
849 continue;
850 }
851 /* see if there is a matching endpoint */
852 pipe = usb2_get_pipe(udev,
853 ifaces[setup->if_index], setup);
854
855 if (!pipe) {
856 if (parm.curr_setup_sub->flags.no_pipe_ok) {
857 continue;
858 }
859 parm.err = USB_ERR_NO_PIPE;
860 goto done;
861 }
862 /* store current setup pointer */
863 parm.curr_setup = setup;
864
865 /* align data properly */
866 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
867
868 if (buf) {
869
870 /*
871 * Common initialization of the
872 * "usb2_xfer" structure.
873 */
874 xfer = USB_ADD_BYTES(buf, parm.size[0]);
875
876 ppxfer[n] = xfer;
877 xfer->udev = udev;
878 xfer->address = udev->address;
879 xfer->priv_sc = priv_sc;
880 xfer->xfer_mtx = priv_mtx;
881 xfer->usb2_root = info;
882 info->setup_refcount++;
883
884 usb2_callout_init_mtx(&xfer->timeout_handle,
885 &udev->bus->bus_mtx, CALLOUT_RETURNUNLOCKED);
886 } else {
887 /*
888 * Setup a dummy xfer, hence we are
889 * writing to the "usb2_xfer"
890 * structure pointed to by "xfer"
891 * before we have allocated any
892 * memory:
893 */
894 xfer = &dummy;
895 bzero(&dummy, sizeof(dummy));
896 refcount++;
897 }
898
899 parm.size[0] += sizeof(xfer[0]);
900
901 xfer->pipe = pipe;
902
903 if (buf) {
904 /*
905 * Increment the pipe refcount. This
906 * basically prevents setting a new
907 * configuration and alternate setting
908 * when USB transfers are in use on
909 * the given interface. Search the USB
910 * code for "pipe->refcount" if you
911 * want more information.
912 */
913 xfer->pipe->refcount++;
914 }
915 parm.methods = xfer->pipe->methods;
916 parm.curr_xfer = xfer;
917
918 /*
919 * Call the Host or Device controller transfer setup
920 * routine:
921 */
922 (udev->bus->methods->xfer_setup) (&parm);
923
924 if (parm.err) {
925 goto done;
926 }
927 }
928
929 if (buf || parm.err) {
930 goto done;
931 }
932 if (refcount == 0) {
933 /* no transfers - nothing to do ! */
934 goto done;
935 }
936 /* align data properly */
937 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
938
939 /* store offset temporarily */
940 parm.size[1] = parm.size[0];
941
942 /*
943 * The number of DMA tags required depends on
944 * the number of endpoints. The current estimate
945 * for maximum number of DMA tags per endpoint
946 * is two.
947 */
948 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX);
949
950 /*
951 * DMA tags for QH, TD, Data and more.
952 */
953 parm.dma_tag_max += 8;
954
955 parm.dma_tag_p += parm.dma_tag_max;
956
957 parm.size[0] += ((uint8_t *)parm.dma_tag_p) -
958 ((uint8_t *)0);
959
960 /* align data properly */
961 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
962
963 /* store offset temporarily */
964 parm.size[3] = parm.size[0];
965
966 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) -
967 ((uint8_t *)0);
968
969 /* align data properly */
970 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
971
972 /* store offset temporarily */
973 parm.size[4] = parm.size[0];
974
975 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) -
976 ((uint8_t *)0);
977
978 /* store end offset temporarily */
979 parm.size[5] = parm.size[0];
980
981 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) -
982 ((uint8_t *)0);
983
984 /* store end offset temporarily */
985
986 parm.size[2] = parm.size[0];
987
988 /* align data properly */
989 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
990
991 parm.size[6] = parm.size[0];
992
993 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) -
994 ((uint8_t *)0);
995
996 /* align data properly */
997 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1));
998
999 /* allocate zeroed memory */
1000 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO);
1001
1002 if (buf == NULL) {
1003 parm.err = USB_ERR_NOMEM;
1004 DPRINTFN(0, "cannot allocate memory block for "
1005 "configuration (%d bytes)\n",
1006 parm.size[0]);
1007 goto done;
1008 }
1009 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]);
1010 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]);
1011 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]);
1012 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]);
1013 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1014 }
1015
1016done:
1017 if (buf) {
1018 if (info->setup_refcount == 0) {
1019 /*
1020 * "usb2_transfer_unsetup_sub" will unlock
1021 * the bus mutex before returning !
1022 */
1023 USB_BUS_LOCK(info->bus);
1024
1025 /* something went wrong */
1026 usb2_transfer_unsetup_sub(info, 0);
1027 }
1028 }
1029 if (parm.err) {
1030 usb2_transfer_unsetup(ppxfer, n_setup);
1031 }
1032 return (parm.err);
1033}
1034
1035/*------------------------------------------------------------------------*
1036 * usb2_transfer_unsetup_sub - factored out code
1037 *------------------------------------------------------------------------*/
1038static void
1039usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay)
1040{
1041 struct usb2_page_cache *pc;
1042 uint32_t temp;
1043
1044 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1045
1046 /* wait for any outstanding DMA operations */
1047
1048 if (needs_delay) {
1049 temp = usb2_get_dma_delay(info->bus);
1050 usb2_pause_mtx(&info->bus->bus_mtx, temp);
1051 }
1052 USB_BUS_UNLOCK(info->bus);
1053
1054 /* wait for interrupt thread to exit */
1055 usb2_proc_unsetup(&info->done_p);
1056
1057 /* free DMA'able memory, if any */
1058 pc = info->dma_page_cache_start;
1059 while (pc != info->dma_page_cache_end) {
1060 usb2_pc_free_mem(pc);
1061 pc++;
1062 }
1063
1064 /* free DMA maps in all "xfer->frbuffers" */
1065 pc = info->xfer_page_cache_start;
1066 while (pc != info->xfer_page_cache_end) {
1067 usb2_pc_dmamap_destroy(pc);
1068 pc++;
1069 }
1070
1071 /* free all DMA tags */
1072 usb2_dma_tag_unsetup(&info->dma_parent_tag);
1073
1074 usb2_cv_destroy(&info->cv_drain);
1075
1076 /*
1077 * free the "memory_base" last, hence the "info" structure is
1078 * contained within the "memory_base"!
1079 */
1080 free(info->memory_base, M_USB);
1081 return;
1082}
1083
1084/*------------------------------------------------------------------------*
1085 * usb2_transfer_unsetup - unsetup/free an array of USB transfers
1086 *
1087 * NOTE: All USB transfers in progress will get called back passing
1088 * the error code "USB_ERR_CANCELLED" before this function
1089 * returns.
1090 *------------------------------------------------------------------------*/
1091void
1092usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup)
1093{
1094 struct usb2_xfer *xfer;
1095 struct usb2_xfer_root *info;
1096 uint8_t needs_delay = 0;
1097
1098 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1099 "usb2_transfer_unsetup can sleep!");
1100
1101 while (n_setup--) {
1102 xfer = pxfer[n_setup];
1103
1104 if (xfer) {
1105 if (xfer->pipe) {
1106 USB_XFER_LOCK(xfer);
1107 USB_BUS_LOCK(xfer->udev->bus);
1108
1109 /*
1110 * HINT: when you start/stop a transfer, it
1111 * might be a good idea to directly use the
1112 * "pxfer[]" structure:
1113 *
1114 * usb2_transfer_start(sc->pxfer[0]);
1115 * usb2_transfer_stop(sc->pxfer[0]);
1116 *
1117 * That way, if your code has many parts that
1118 * will not stop running under the same
1119 * lock, in other words "priv_mtx", the
1120 * usb2_transfer_start and
1121 * usb2_transfer_stop functions will simply
1122 * return when they detect a NULL pointer
1123 * argument.
1124 *
1125 * To avoid any races we clear the "pxfer[]"
1126 * pointer while holding the private mutex
1127 * of the driver:
1128 */
1129 pxfer[n_setup] = NULL;
1130
1131 USB_BUS_UNLOCK(xfer->udev->bus);
1132 USB_XFER_UNLOCK(xfer);
1133
1134 usb2_transfer_drain(xfer);
1135
1136 if (xfer->flags_int.bdma_enable) {
1137 needs_delay = 1;
1138 }
1139 /*
1140 * NOTE: default pipe does not have an
1141 * interface, even if pipe->iface_index == 0
1142 */
1143 xfer->pipe->refcount--;
1144
1145 } else {
1146 /* clear the transfer pointer */
1147 pxfer[n_setup] = NULL;
1148 }
1149
1150 usb2_callout_drain(&xfer->timeout_handle);
1151
1152 if (xfer->usb2_root) {
1153 info = xfer->usb2_root;
1154
1155 USB_BUS_LOCK(info->bus);
1156
1157 USB_ASSERT(info->setup_refcount != 0,
1158 ("Invalid setup "
1159 "reference count!\n"));
1160
1161 info->setup_refcount--;
1162
1163 if (info->setup_refcount == 0) {
1164 usb2_transfer_unsetup_sub(info,
1165 needs_delay);
1166 } else {
1167 USB_BUS_UNLOCK(info->bus);
1168 }
1169 }
1170 }
1171 }
1172 return;
1173}
1174
1175/*------------------------------------------------------------------------*
1176 * usb2_control_transfer_init - factored out code
1177 *
1178 * In USB Device Mode we have to wait for the SETUP packet which
1179 * containst the "struct usb2_device_request" structure, before we can
1180 * transfer any data. In USB Host Mode we already have the SETUP
1181 * packet at the moment the USB transfer is started. This leads us to
1182 * having to setup the USB transfer at two different places in
1183 * time. This function just contains factored out control transfer
1184 * initialisation code, so that we don't duplicate the code.
1185 *------------------------------------------------------------------------*/
1186static void
1187usb2_control_transfer_init(struct usb2_xfer *xfer)
1188{
1189 struct usb2_device_request req;
1190
1191 /* copy out the USB request header */
1192
1193 usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1194
1195 /* setup remainder */
1196
1197 xfer->flags_int.control_rem = UGETW(req.wLength);
1198
1199 /* copy direction to endpoint variable */
1200
1201 xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT);
1202 xfer->endpoint |=
1203 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1204
1205 return;
1206}
1207
1208/*------------------------------------------------------------------------*
1209 * usb2_start_hardware_sub
1210 *
1211 * This function handles initialisation of control transfers. Control
1212 * transfers are special in that regard that they can both transmit
1213 * and receive data.
1214 *
1215 * Return values:
1216 * 0: Success
1217 * Else: Failure
1218 *------------------------------------------------------------------------*/
1219static uint8_t
1220usb2_start_hardware_sub(struct usb2_xfer *xfer)
1221{
1222 uint32_t len;
1223
1224 /* Check for control endpoint stall */
1225 if (xfer->flags.stall_pipe) {
1226 /* no longer active */
1227 xfer->flags_int.control_act = 0;
1228 }
1229 /*
1230 * Check if there is a control
1231 * transfer in progress:
1232 */
1233 if (xfer->flags_int.control_act) {
1234
1235 if (xfer->flags_int.control_hdr) {
1236
1237 /* clear send header flag */
1238
1239 xfer->flags_int.control_hdr = 0;
1240
1241 /* setup control transfer */
1242 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
1243 usb2_control_transfer_init(xfer);
1244 }
1245 }
1246 /* get data length */
1247
1248 len = xfer->sumlen;
1249
1250 } else {
1251
1252 /* the size of the SETUP structure is hardcoded ! */
1253
1254 if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) {
1255 DPRINTFN(0, "Wrong framelength %u != %zu\n",
1256 xfer->frlengths[0], sizeof(struct
1257 usb2_device_request));
1258 goto error;
1259 }
1260 /* check USB mode */
1261 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
1262
1263 /* check number of frames */
1264 if (xfer->nframes != 1) {
1265 /*
1266 * We need to receive the setup
1267 * message first so that we know the
1268 * data direction!
1269 */
1270 DPRINTF("Misconfigured transfer\n");
1271 goto error;
1272 }
1273 /*
1274 * Set a dummy "control_rem" value. This
1275 * variable will be overwritten later by a
1276 * call to "usb2_control_transfer_init()" !
1277 */
1278 xfer->flags_int.control_rem = 0xFFFF;
1279 } else {
1280
1281 /* setup "endpoint" and "control_rem" */
1282
1283 usb2_control_transfer_init(xfer);
1284 }
1285
1286 /* set transfer-header flag */
1287
1288 xfer->flags_int.control_hdr = 1;
1289
1290 /* get data length */
1291
1292 len = (xfer->sumlen - sizeof(struct usb2_device_request));
1293 }
1294
1295 /* check if there is a length mismatch */
1296
1297 if (len > xfer->flags_int.control_rem) {
1298 DPRINTFN(0, "Length greater than remaining length!\n");
1299 goto error;
1300 }
1301 /* check if we are doing a short transfer */
1302
1303 if (xfer->flags.force_short_xfer) {
1304 xfer->flags_int.control_rem = 0;
1305 } else {
1306 if ((len != xfer->max_data_length) &&
1307 (len != xfer->flags_int.control_rem) &&
1308 (xfer->nframes != 1)) {
1309 DPRINTFN(0, "Short control transfer without "
1310 "force_short_xfer set!\n");
1311 goto error;
1312 }
1313 xfer->flags_int.control_rem -= len;
1314 }
1315
1316 /* the status part is executed when "control_act" is 0 */
1317
1318 if ((xfer->flags_int.control_rem > 0) ||
1319 (xfer->flags.manual_status)) {
1320 /* don't execute the STATUS stage yet */
1321 xfer->flags_int.control_act = 1;
1322
1323 /* sanity check */
1324 if ((!xfer->flags_int.control_hdr) &&
1325 (xfer->nframes == 1)) {
1326 /*
1327 * This is not a valid operation!
1328 */
1329 DPRINTFN(0, "Invalid parameter "
1330 "combination\n");
1331 goto error;
1332 }
1333 } else {
1334 /* time to execute the STATUS stage */
1335 xfer->flags_int.control_act = 0;
1336 }
1337 return (0); /* success */
1338
1339error:
1340 return (1); /* failure */
1341}
1342
1343/*------------------------------------------------------------------------*
1344 * usb2_start_hardware - start USB hardware for the given transfer
1345 *
1346 * This function should only be called from the USB callback.
1347 *------------------------------------------------------------------------*/
1348void
1349usb2_start_hardware(struct usb2_xfer *xfer)
1350{
1351 uint32_t x;
1352
1353 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
1354 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1355 "read" : "write");
1356
1357#if USB_DEBUG
1358 if (USB_DEBUG_VAR > 0) {
1359 USB_BUS_LOCK(xfer->udev->bus);
1360
1361 usb2_dump_pipe(xfer->pipe);
1362
1363 USB_BUS_UNLOCK(xfer->udev->bus);
1364 }
1365#endif
1366
1367 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1368 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_NOTOWNED);
1369
1370 /* Only open the USB transfer once! */
1371 if (!xfer->flags_int.open) {
1372 xfer->flags_int.open = 1;
1373
1374 DPRINTF("open\n");
1375
1376 USB_BUS_LOCK(xfer->udev->bus);
1377 (xfer->pipe->methods->open) (xfer);
1378 USB_BUS_UNLOCK(xfer->udev->bus);
1379 }
1380 /* set "transferring" flag */
1381 xfer->flags_int.transferring = 1;
1382
1383 /*
1384 * Check if the transfer is waiting on a queue, most
1385 * frequently the "done_q":
1386 */
1387 if (xfer->wait_queue) {
1388 USB_BUS_LOCK(xfer->udev->bus);
1389 usb2_transfer_dequeue(xfer);
1390 USB_BUS_UNLOCK(xfer->udev->bus);
1391 }
1392 /* clear "did_dma_delay" flag */
1393 xfer->flags_int.did_dma_delay = 0;
1394
1395 /* clear "did_close" flag */
1396 xfer->flags_int.did_close = 0;
1397
1398 /* clear "bdma_setup" flag */
1399 xfer->flags_int.bdma_setup = 0;
1400
1401 /* by default we cannot cancel any USB transfer immediately */
1402 xfer->flags_int.can_cancel_immed = 0;
1403
1404 /* clear lengths and frame counts by default */
1405 xfer->sumlen = 0;
1406 xfer->actlen = 0;
1407 xfer->aframes = 0;
1408
1409 /* clear any previous errors */
1410 xfer->error = 0;
1411
1412 /* sanity check */
1413
1414 if (xfer->nframes == 0) {
1415 if (xfer->flags.stall_pipe) {
1416 /*
1417 * Special case - want to stall without transferring
1418 * any data:
1419 */
1420 DPRINTF("xfer=%p nframes=0: stall "
1421 "or clear stall!\n", xfer);
1422 USB_BUS_LOCK(xfer->udev->bus);
1423 xfer->flags_int.can_cancel_immed = 1;
1424 /* start the transfer */
1425 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
1426 USB_BUS_UNLOCK(xfer->udev->bus);
1427 return;
1428 }
1429 USB_BUS_LOCK(xfer->udev->bus);
1430 usb2_transfer_done(xfer, USB_ERR_INVAL);
1431 USB_BUS_UNLOCK(xfer->udev->bus);
1432 return;
1433 }
1434 /* compute total transfer length */
1435
1436 for (x = 0; x != xfer->nframes; x++) {
1437 xfer->sumlen += xfer->frlengths[x];
1438 if (xfer->sumlen < xfer->frlengths[x]) {
1439 /* length wrapped around */
1440 USB_BUS_LOCK(xfer->udev->bus);
1441 usb2_transfer_done(xfer, USB_ERR_INVAL);
1442 USB_BUS_UNLOCK(xfer->udev->bus);
1443 return;
1444 }
1445 }
1446
1447 /* clear some internal flags */
1448
1449 xfer->flags_int.short_xfer_ok = 0;
1450 xfer->flags_int.short_frames_ok = 0;
1451
1452 /* check if this is a control transfer */
1453
1454 if (xfer->flags_int.control_xfr) {
1455
1456 if (usb2_start_hardware_sub(xfer)) {
1457 USB_BUS_LOCK(xfer->udev->bus);
1458 usb2_transfer_done(xfer, USB_ERR_STALLED);
1459 USB_BUS_UNLOCK(xfer->udev->bus);
1460 return;
1461 }
1462 }
1463 /*
1464 * Setup filtered version of some transfer flags,
1465 * in case of data read direction
1466 */
1467 if (USB_GET_DATA_ISREAD(xfer)) {
1468
1469 if (xfer->flags_int.control_xfr) {
1470
1471 /*
1472 * Control transfers do not support reception
1473 * of multiple short USB frames !
1474 */
1475
1476 if (xfer->flags.short_xfer_ok) {
1477 xfer->flags_int.short_xfer_ok = 1;
1478 }
1479 } else {
1480
1481 if (xfer->flags.short_frames_ok) {
1482 xfer->flags_int.short_xfer_ok = 1;
1483 xfer->flags_int.short_frames_ok = 1;
1484 } else if (xfer->flags.short_xfer_ok) {
1485 xfer->flags_int.short_xfer_ok = 1;
1486 }
1487 }
1488 }
1489 /*
1490 * Check if BUS-DMA support is enabled and try to load virtual
1491 * buffers into DMA, if any:
1492 */
1493 if (xfer->flags_int.bdma_enable) {
1494 /* insert the USB transfer last in the BUS-DMA queue */
1495 usb2_command_wrapper(&xfer->usb2_root->dma_q, xfer);
1496 return;
1497 }
1498 /*
1499 * Enter the USB transfer into the Host Controller or
1500 * Device Controller schedule:
1501 */
1502 usb2_pipe_enter(xfer);
1503 return;
1504}
1505
1506/*------------------------------------------------------------------------*
1507 * usb2_pipe_enter - factored out code
1508 *------------------------------------------------------------------------*/
1509void
1510usb2_pipe_enter(struct usb2_xfer *xfer)
1511{
1512 struct usb2_pipe *pipe;
1513
1514 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1515
1516 USB_BUS_LOCK(xfer->udev->bus);
1517
1518 pipe = xfer->pipe;
1519
1520 DPRINTF("enter\n");
1521
1522 /* enter the transfer */
1523 (pipe->methods->enter) (xfer);
1524
1525 /* check cancelability */
1526 if (pipe->methods->enter_is_cancelable) {
1527 xfer->flags_int.can_cancel_immed = 1;
1528 /* check for transfer error */
1529 if (xfer->error) {
1530 /* some error has happened */
1531 usb2_transfer_done(xfer, 0);
1532 USB_BUS_UNLOCK(xfer->udev->bus);
1533 return;
1534 }
1535 } else {
1536 xfer->flags_int.can_cancel_immed = 0;
1537 }
1538
1539 /* start the transfer */
1540 usb2_command_wrapper(&pipe->pipe_q, xfer);
1541 USB_BUS_UNLOCK(xfer->udev->bus);
1542 return;
1543}
1544
1545/*------------------------------------------------------------------------*
1546 * usb2_transfer_start - start an USB transfer
1547 *
1548 * NOTE: Calling this function more than one time will only
1549 * result in a single transfer start, until the USB transfer
1550 * completes.
1551 *------------------------------------------------------------------------*/
1552void
1553usb2_transfer_start(struct usb2_xfer *xfer)
1554{
1555 if (xfer == NULL) {
1556 /* transfer is gone */
1557 return;
1558 }
1559 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1560
1561 /* mark the USB transfer started */
1562
1563 if (!xfer->flags_int.started) {
1564 xfer->flags_int.started = 1;
1565 }
1566 /* check if the USB transfer callback is already transferring */
1567
1568 if (xfer->flags_int.transferring) {
1569 return;
1570 }
1571 USB_BUS_LOCK(xfer->udev->bus);
1572 /* call the USB transfer callback */
1573 usb2_callback_ss_done_defer(xfer);
1574 USB_BUS_UNLOCK(xfer->udev->bus);
1575 return;
1576}
1577
1578/*------------------------------------------------------------------------*
1579 * usb2_transfer_stop - stop an USB transfer
1580 *
1581 * NOTE: Calling this function more than one time will only
1582 * result in a single transfer stop.
1583 * NOTE: When this function returns it is not safe to free nor
1584 * reuse any DMA buffers. See "usb2_transfer_drain()".
1585 *------------------------------------------------------------------------*/
1586void
1587usb2_transfer_stop(struct usb2_xfer *xfer)
1588{
1589 struct usb2_pipe *pipe;
1590
1591 if (xfer == NULL) {
1592 /* transfer is gone */
1593 return;
1594 }
1595 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1596
1597 /* check if the USB transfer was ever opened */
1598
1599 if (!xfer->flags_int.open) {
1600 /* nothing to do except clearing the "started" flag */
1601 xfer->flags_int.started = 0;
1602 return;
1603 }
1604 /* try to stop the current USB transfer */
1605
1606 USB_BUS_LOCK(xfer->udev->bus);
1607 xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1608 /*
1609 * Clear "open" and "started" when both private and USB lock
1610 * is locked so that we don't get a race updating "flags_int"
1611 */
1612 xfer->flags_int.open = 0;
1613 xfer->flags_int.started = 0;
1614
1615 /*
1616 * Check if we can cancel the USB transfer immediately.
1617 */
1618 if (xfer->flags_int.transferring) {
1619 if (xfer->flags_int.can_cancel_immed &&
1620 (!xfer->flags_int.did_close)) {
1621 DPRINTF("close\n");
1622 /*
1623 * The following will lead to an USB_ERR_CANCELLED
1624 * error code being passed to the USB callback.
1625 */
1626 (xfer->pipe->methods->close) (xfer);
1627 /* only close once */
1628 xfer->flags_int.did_close = 1;
1629 } else {
1630 /* need to wait for the next done callback */
1631 }
1632 } else {
1633 DPRINTF("close\n");
1634
1635 /* close here and now */
1636 (xfer->pipe->methods->close) (xfer);
1637
1638 /*
1639 * Any additional DMA delay is done by
1640 * "usb2_transfer_unsetup()".
1641 */
1642
1643 /*
1644 * Special case. Check if we need to restart a blocked
1645 * pipe.
1646 */
1647 pipe = xfer->pipe;
1648
1649 /*
1650 * If the current USB transfer is completing we need
1651 * to start the next one:
1652 */
1653 if (pipe->pipe_q.curr == xfer) {
1654 usb2_command_wrapper(&pipe->pipe_q, NULL);
1655 }
1656 }
1657
1658 USB_BUS_UNLOCK(xfer->udev->bus);
1659 return;
1660}
1661
1662/*------------------------------------------------------------------------*
1663 * usb2_transfer_pending
1664 *
1665 * This function will check if an USB transfer is pending which is a
1666 * little bit complicated!
1667 * Return values:
1668 * 0: Not pending
1669 * 1: Pending: The USB transfer will receive a callback in the future.
1670 *------------------------------------------------------------------------*/
1671uint8_t
1672usb2_transfer_pending(struct usb2_xfer *xfer)
1673{
1674 struct usb2_xfer_root *info;
1675 struct usb2_xfer_queue *pq;
1676
1677 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1678
1679 if (xfer->flags_int.transferring) {
1680 /* trivial case */
1681 return (1);
1682 }
1683 USB_BUS_LOCK(xfer->udev->bus);
1684 if (xfer->wait_queue) {
1685 /* we are waiting on a queue somewhere */
1686 USB_BUS_UNLOCK(xfer->udev->bus);
1687 return (1);
1688 }
1689 info = xfer->usb2_root;
1690 pq = &info->done_q;
1691
1692 if (pq->curr == xfer) {
1693 /* we are currently scheduled for callback */
1694 USB_BUS_UNLOCK(xfer->udev->bus);
1695 return (1);
1696 }
1697 /* we are not pending */
1698 USB_BUS_UNLOCK(xfer->udev->bus);
1699 return (0);
1700}
1701
1702/*------------------------------------------------------------------------*
1703 * usb2_transfer_drain
1704 *
1705 * This function will stop the USB transfer and wait for any
1706 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
1707 * are loaded into DMA can safely be freed or reused after that this
1708 * function has returned.
1709 *------------------------------------------------------------------------*/
1710void
1711usb2_transfer_drain(struct usb2_xfer *xfer)
1712{
1713 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1714 "usb2_transfer_drain can sleep!");
1715
1716 if (xfer == NULL) {
1717 /* transfer is gone */
1718 return;
1719 }
1720 if (xfer->xfer_mtx != &Giant) {
1721 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1722 }
1723 USB_XFER_LOCK(xfer);
1724
1725 usb2_transfer_stop(xfer);
1726
1727 while (usb2_transfer_pending(xfer)) {
1728 xfer->flags_int.draining = 1;
1729 /*
1730 * Wait until the current outstanding USB
1731 * transfer is complete !
1732 */
1733 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->xfer_mtx);
1734 }
1735 USB_XFER_UNLOCK(xfer);
1736
1737 return;
1738}
1739
1740/*------------------------------------------------------------------------*
1741 * usb2_set_frame_data
1742 *
1743 * This function sets the pointer of the buffer that should
1744 * loaded directly into DMA for the given USB frame. Passing "ptr"
1745 * equal to NULL while the corresponding "frlength" is greater
1746 * than zero gives undefined results!
1747 *------------------------------------------------------------------------*/
1748void
1749usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, uint32_t frindex)
1750{
1751 /* set virtual address to load and length */
1752 xfer->frbuffers[frindex].buffer = ptr;
1753 return;
1754}
1755
1756/*------------------------------------------------------------------------*
1757 * usb2_set_frame_offset
1758 *
1759 * This function sets the frame data buffer offset relative to the beginning
1760 * of the USB DMA buffer allocated for this USB transfer.
1761 *------------------------------------------------------------------------*/
1762void
1763usb2_set_frame_offset(struct usb2_xfer *xfer, uint32_t offset,
1764 uint32_t frindex)
1765{
1766 USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
1767 "when the USB buffer is external!\n"));
1768
1769 /* set virtual address to load */
1770 xfer->frbuffers[frindex].buffer =
1771 USB_ADD_BYTES(xfer->local_buffer, offset);
1772 return;
1773}
1774
1775/*------------------------------------------------------------------------*
1776 * usb2_callback_proc - factored out code
1777 *
1778 * This function performs USB callbacks.
1779 *------------------------------------------------------------------------*/
1780static void
1781usb2_callback_proc(struct usb2_proc_msg *_pm)
1782{
1783 struct usb2_done_msg *pm = (void *)_pm;
1784 struct usb2_xfer_root *info = pm->usb2_root;
1785
1786 /* Change locking order */
1787 USB_BUS_UNLOCK(info->bus);
1788
1789 /*
1790 * We exploit the fact that the mutex is the same for all
1791 * callbacks that will be called from this thread:
1792 */
1793 mtx_lock(info->priv_mtx);
1794 USB_BUS_LOCK(info->bus);
1795
1796 /* Continue where we lost track */
1797 usb2_command_wrapper(&info->done_q,
1798 info->done_q.curr);
1799
1800 mtx_unlock(info->priv_mtx);
1801 return;
1802}
1803
1804/*------------------------------------------------------------------------*
1805 * usb2_callback_ss_done_defer
1806 *
1807 * This function will defer the start, stop and done callback to the
1808 * correct thread.
1809 *------------------------------------------------------------------------*/
1810static void
1811usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1812{
1813 struct usb2_xfer_root *info = xfer->usb2_root;
1814 struct usb2_xfer_queue *pq = &info->done_q;
1815
1816 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1817
1818 if (pq->curr != xfer) {
1819 usb2_transfer_enqueue(pq, xfer);
1820 }
1821 if (!pq->recurse_1) {
1822
1823 /*
1824 * We have to postpone the callback due to the fact we
1825 * will have a Lock Order Reversal, LOR, if we try to
1826 * proceed !
1827 */
1828 if (usb2_proc_msignal(&info->done_p,
1829 &info->done_m[0], &info->done_m[1])) {
1830 /* ignore */
1831 }
1832 } else {
1833 /* clear second recurse flag */
1834 pq->recurse_2 = 0;
1835 }
1836 return;
1837
1838}
1839
1840/*------------------------------------------------------------------------*
1841 * usb2_callback_wrapper
1842 *
1843 * This is a wrapper for USB callbacks. This wrapper does some
1844 * auto-magic things like figuring out if we can call the callback
1845 * directly from the current context or if we need to wakeup the
1846 * interrupt process.
1847 *------------------------------------------------------------------------*/
1848static void
1849usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1850{
1851 struct usb2_xfer *xfer = pq->curr;
1852 struct usb2_xfer_root *info = xfer->usb2_root;
1853
1854 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1855 if (!mtx_owned(xfer->xfer_mtx)) {
1856 /*
1857 * Cases that end up here:
1858 *
1859 * 5) HW interrupt done callback or other source.
1860 */
1861 DPRINTFN(3, "case 5\n");
1862
1863 /*
1864 * We have to postpone the callback due to the fact we
1865 * will have a Lock Order Reversal, LOR, if we try to
1866 * proceed !
1867 */
1868 if (usb2_proc_msignal(&info->done_p,
1869 &info->done_m[0], &info->done_m[1])) {
1870 /* ignore */
1871 }
1872 return;
1873 }
1874 /*
1875 * Cases that end up here:
1876 *
1877 * 1) We are starting a transfer
1878 * 2) We are prematurely calling back a transfer
1879 * 3) We are stopping a transfer
1880 * 4) We are doing an ordinary callback
1881 */
1882 DPRINTFN(3, "case 1-4\n");
1883 /* get next USB transfer in the queue */
1884 info->done_q.curr = NULL;
1885
1886 USB_BUS_UNLOCK(xfer->udev->bus);
1887 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_NOTOWNED);
1888
1889 /* set correct USB state for callback */
1890 if (!xfer->flags_int.transferring) {
1891 xfer->usb2_state = USB_ST_SETUP;
1892 if (!xfer->flags_int.started) {
1893 /* we got stopped before we even got started */
1894 USB_BUS_LOCK(xfer->udev->bus);
1895 goto done;
1896 }
1897 } else {
1898
1899 if (usb2_callback_wrapper_sub(xfer)) {
1900 /* the callback has been deferred */
1901 USB_BUS_LOCK(xfer->udev->bus);
1902 goto done;
1903 }
1904 xfer->flags_int.transferring = 0;
1905
1906 if (xfer->error) {
1907 xfer->usb2_state = USB_ST_ERROR;
1908 } else {
1909 /* set transferred state */
1910 xfer->usb2_state = USB_ST_TRANSFERRED;
1911
1912 /* sync DMA memory, if any */
1913 if (xfer->flags_int.bdma_enable &&
1914 (!xfer->flags_int.bdma_no_post_sync)) {
1915 usb2_bdma_post_sync(xfer);
1916 }
1917 }
1918 }
1919
1920 /* call processing routine */
1921 (xfer->callback) (xfer);
1922
1923 /* pickup the USB mutex again */
1924 USB_BUS_LOCK(xfer->udev->bus);
1925
1926 /*
1927 * Check if we got started after that we got cancelled, but
1928 * before we managed to do the callback. Check if we are
1929 * draining.
1930 */
1931 if ((!xfer->flags_int.open) &&
1932 (xfer->flags_int.started) &&
1933 (xfer->usb2_state == USB_ST_ERROR)) {
1934 /* try to loop, but not recursivly */
1935 usb2_command_wrapper(&info->done_q, xfer);
1936 return;
1937 } else if (xfer->flags_int.draining &&
1938 (!xfer->flags_int.transferring)) {
1939 /* "usb2_transfer_drain()" is waiting for end of transfer */
1940 xfer->flags_int.draining = 0;
1941 usb2_cv_broadcast(&xfer->usb2_root->cv_drain);
1942 }
1943done:
1944 /* do the next callback, if any */
1945 usb2_command_wrapper(&info->done_q,
1946 info->done_q.curr);
1947 return;
1948}
1949
1950/*------------------------------------------------------------------------*
1951 * usb2_dma_delay_done_cb
1952 *
1953 * This function is called when the DMA delay has been exectuded, and
1954 * will make sure that the callback is called to complete the USB
1955 * transfer. This code path is ususally only used when there is an USB
1956 * error like USB_ERR_CANCELLED.
1957 *------------------------------------------------------------------------*/
1958static void
1959usb2_dma_delay_done_cb(void *arg)
1960{
1961 struct usb2_xfer *xfer = arg;
1962
1963 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1964
1965 DPRINTFN(3, "Completed %p\n", xfer);
1966
1967 /* queue callback for execution, again */
1968 usb2_transfer_done(xfer, 0);
1969
1970 USB_BUS_UNLOCK(xfer->udev->bus);
1971 return;
1972}
1973
1974/*------------------------------------------------------------------------*
1975 * usb2_transfer_dequeue
1976 *
1977 * - This function is used to remove an USB transfer from a USB
1978 * transfer queue.
1979 *
1980 * - This function can be called multiple times in a row.
1981 *------------------------------------------------------------------------*/
1982void
1983usb2_transfer_dequeue(struct usb2_xfer *xfer)
1984{
1985 struct usb2_xfer_queue *pq;
1986
1987 pq = xfer->wait_queue;
1988 if (pq) {
1989 TAILQ_REMOVE(&pq->head, xfer, wait_entry);
1990 xfer->wait_queue = NULL;
1991 }
1992 return;
1993}
1994
1995/*------------------------------------------------------------------------*
1996 * usb2_transfer_enqueue
1997 *
1998 * - This function is used to insert an USB transfer into a USB *
1999 * transfer queue.
2000 *
2001 * - This function can be called multiple times in a row.
2002 *------------------------------------------------------------------------*/
2003void
2004usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
2005{
2006 /*
2007 * Insert the USB transfer into the queue, if it is not
2008 * already on a USB transfer queue:
2009 */
2010 if (xfer->wait_queue == NULL) {
2011 xfer->wait_queue = pq;
2012 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2013 }
2014 return;
2015}
2016
2017/*------------------------------------------------------------------------*
2018 * usb2_transfer_done
2019 *
2020 * - This function is used to remove an USB transfer from the busdma,
2021 * pipe or interrupt queue.
2022 *
2023 * - This function is used to queue the USB transfer on the done
2024 * queue.
2025 *
2026 * - This function is used to stop any USB transfer timeouts.
2027 *------------------------------------------------------------------------*/
2028void
2029usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2030{
2031 struct usb2_xfer_queue *pq;
2032
2033 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2034
2035 DPRINTF("err=%s\n", usb2_errstr(error));
2036
2037 /*
2038 * If we are not transferring then just return.
2039 * This can happen during transfer cancel.
2040 */
2041 if (!xfer->flags_int.transferring) {
2042 DPRINTF("not transferring\n");
2043 return;
2044 }
2045 /* only set transfer error if not already set */
2046 if (!xfer->error) {
2047 xfer->error = error;
2048 }
2049 /* stop any callouts */
2050 usb2_callout_stop(&xfer->timeout_handle);
2051
2052 /*
2053 * If we are waiting on a queue, just remove the USB transfer
2054 * from the queue, if any. We should have the required locks
2055 * locked to do the remove when this function is called.
2056 */
2057 usb2_transfer_dequeue(xfer);
2058
2059 if (mtx_owned(xfer->xfer_mtx)) {
2060 /*
2061 * If the private USB lock is not locked, then we assume
2062 * that the BUS-DMA load stage has been passed:
2063 */
2064 pq = &xfer->usb2_root->dma_q;
2065
2066 if (pq->curr == xfer) {
2067 /* start the next BUS-DMA load, if any */
2068 usb2_command_wrapper(pq, NULL);
2069 }
2070 }
2071 /* keep some statistics */
2072 if (xfer->error) {
2073 xfer->udev->bus->stats_err.uds_requests
2074 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2075 } else {
2076 xfer->udev->bus->stats_ok.uds_requests
2077 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2078 }
2079
2080 /* call the USB transfer callback */
2081 usb2_callback_ss_done_defer(xfer);
2082 return;
2083}
2084
2085/*------------------------------------------------------------------------*
2086 * usb2_transfer_start_cb
2087 *
2088 * This function is called to start the USB transfer when
2089 * "xfer->interval" is greater than zero, and and the endpoint type is
2090 * BULK or CONTROL.
2091 *------------------------------------------------------------------------*/
2092static void
2093usb2_transfer_start_cb(void *arg)
2094{
2095 struct usb2_xfer *xfer = arg;
2096 struct usb2_pipe *pipe = xfer->pipe;
2097
2098 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2099
2100 DPRINTF("start\n");
2101
2102 /* start the transfer */
2103 (pipe->methods->start) (xfer);
2104
2105 /* check cancelability */
2106 if (pipe->methods->start_is_cancelable) {
2107 xfer->flags_int.can_cancel_immed = 1;
2108 if (xfer->error) {
2109 /* some error has happened */
2110 usb2_transfer_done(xfer, 0);
2111 }
2112 } else {
2113 xfer->flags_int.can_cancel_immed = 0;
2114 }
2115 USB_BUS_UNLOCK(xfer->udev->bus);
2116
2117 return;
2118}
2119
2120/*------------------------------------------------------------------------*
2121 * usb2_transfer_set_stall
2122 *
2123 * This function is used to set the stall flag outside the
2124 * callback. This function is NULL safe.
2125 *------------------------------------------------------------------------*/
2126void
2127usb2_transfer_set_stall(struct usb2_xfer *xfer)
2128{
2129 if (xfer == NULL) {
2130 /* tearing down */
2131 return;
2132 }
2133 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2134
2135 /* avoid any races by locking the USB mutex */
2136 USB_BUS_LOCK(xfer->udev->bus);
2137
2138 xfer->flags.stall_pipe = 1;
2139
2140 USB_BUS_UNLOCK(xfer->udev->bus);
2141
2142 return;
2143}
2144
2145/*------------------------------------------------------------------------*
2146 * usb2_transfer_clear_stall
2147 *
2148 * This function is used to clear the stall flag outside the
2149 * callback. This function is NULL safe.
2150 *------------------------------------------------------------------------*/
2151void
2152usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2153{
2154 if (xfer == NULL) {
2155 /* tearing down */
2156 return;
2157 }
2158 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2159
2160 /* avoid any races by locking the USB mutex */
2161 USB_BUS_LOCK(xfer->udev->bus);
2162
2163 xfer->flags.stall_pipe = 0;
2164
2165 USB_BUS_UNLOCK(xfer->udev->bus);
2166
2167 return;
2168}
2169
2170/*------------------------------------------------------------------------*
2171 * usb2_pipe_start
2172 *
2173 * This function is used to add an USB transfer to the pipe transfer list.
2174 *------------------------------------------------------------------------*/
2175void
2176usb2_pipe_start(struct usb2_xfer_queue *pq)
2177{
2178 struct usb2_pipe *pipe;
2179 struct usb2_xfer *xfer;
2180 uint8_t type;
2181
2182 xfer = pq->curr;
2183 pipe = xfer->pipe;
2184
2185 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2186
2187 /*
2188 * If the pipe is already stalled we do nothing !
2189 */
2190 if (pipe->is_stalled) {
2191 return;
2192 }
2193 /*
2194 * Check if we are supposed to stall the pipe:
2195 */
2196 if (xfer->flags.stall_pipe) {
2197 /* clear stall command */
2198 xfer->flags.stall_pipe = 0;
2199
2200 /*
2201 * Only stall BULK and INTERRUPT endpoints.
2202 */
2203 type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2204 if ((type == UE_BULK) ||
2205 (type == UE_INTERRUPT)) {
2206 struct usb2_device *udev;
2207 struct usb2_xfer_root *info;
2208
2209 udev = xfer->udev;
2210 pipe->is_stalled = 1;
2211
2212 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2213 (udev->bus->methods->set_stall) (
2214 udev, NULL, pipe);
2215 } else if (udev->default_xfer[1]) {
2216 info = udev->default_xfer[1]->usb2_root;
2217 if (usb2_proc_msignal(&info->done_p,
2218 &udev->cs_msg[0], &udev->cs_msg[1])) {
2219 /* ignore */
2220 }
2221 } else {
2222 /* should not happen */
2223 DPRINTFN(0, "No stall handler!\n");
2224 }
2225 /*
2226 * We get started again when the stall is cleared!
2227 */
2228 return;
2229 }
2230 }
2231 /* Set or clear stall complete - special case */
2232 if (xfer->nframes == 0) {
2233 /* we are complete */
2234 xfer->aframes = 0;
2235 usb2_transfer_done(xfer, 0);
2236 return;
2237 }
2238 /*
2239 * Handled cases:
2240 *
2241 * 1) Start the first transfer queued.
2242 *
2243 * 2) Re-start the current USB transfer.
2244 */
2245 /*
2246 * Check if there should be any
2247 * pre transfer start delay:
2248 */
2249 if (xfer->interval > 0) {
2250 type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2251 if ((type == UE_BULK) ||
2252 (type == UE_CONTROL)) {
2253 usb2_transfer_timeout_ms(xfer,
2254 &usb2_transfer_start_cb,
2255 xfer->interval);
2256 return;
2257 }
2258 }
2259 DPRINTF("start\n");
2260
2261 /* start USB transfer */
2262 (pipe->methods->start) (xfer);
2263
2264 /* check cancelability */
2265 if (pipe->methods->start_is_cancelable) {
2266 xfer->flags_int.can_cancel_immed = 1;
2267 if (xfer->error) {
2268 /* some error has happened */
2269 usb2_transfer_done(xfer, 0);
2270 }
2271 } else {
2272 xfer->flags_int.can_cancel_immed = 0;
2273 }
2274 return;
2275}
2276
2277/*------------------------------------------------------------------------*
2278 * usb2_transfer_timeout_ms
2279 *
2280 * This function is used to setup a timeout on the given USB
2281 * transfer. If the timeout has been deferred the callback given by
2282 * "cb" will get called after "ms" milliseconds.
2283 *------------------------------------------------------------------------*/
2284void
2285usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2286 void (*cb) (void *arg), uint32_t ms)
2287{
2288 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2289
2290 /* defer delay */
2291 usb2_callout_reset(&xfer->timeout_handle,
2292 USB_MS_TO_TICKS(ms), cb, xfer);
2293 return;
2294}
2295
2296/*------------------------------------------------------------------------*
2297 * usb2_callback_wrapper_sub
2298 *
2299 * - This function will update variables in an USB transfer after
2300 * that the USB transfer is complete.
2301 *
2302 * - This function is used to start the next USB transfer on the
2303 * pipe transfer queue, if any.
2304 *
2305 * NOTE: In some special cases the USB transfer will not be removed from
2306 * the pipe queue, but remain first. To enforce USB transfer removal call
2307 * this function passing the error code "USB_ERR_CANCELLED".
2308 *
2309 * Return values:
2310 * 0: Success.
2311 * Else: The callback has been deferred.
2312 *------------------------------------------------------------------------*/
2313static uint8_t
2314usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2315{
2316 struct usb2_pipe *pipe;
2317 uint32_t x;
2318
2319 if ((!xfer->flags_int.open) &&
2320 (!xfer->flags_int.did_close)) {
2321 DPRINTF("close\n");
2322 USB_BUS_LOCK(xfer->udev->bus);
2323 (xfer->pipe->methods->close) (xfer);
2324 USB_BUS_UNLOCK(xfer->udev->bus);
2325 /* only close once */
2326 xfer->flags_int.did_close = 1;
2327 return (1); /* wait for new callback */
2328 }
2329 /*
2330 * If we have a non-hardware induced error we
2331 * need to do the DMA delay!
2332 */
2333 if (((xfer->error == USB_ERR_CANCELLED) ||
2334 (xfer->error == USB_ERR_TIMEOUT)) &&
2335 (!xfer->flags_int.did_dma_delay)) {
2336
2337 uint32_t temp;
2338
2339 /* only delay once */
2340 xfer->flags_int.did_dma_delay = 1;
2341
2342 /* we can not cancel this delay */
2343 xfer->flags_int.can_cancel_immed = 0;
2344
2345 temp = usb2_get_dma_delay(xfer->udev->bus);
2346
2347 DPRINTFN(3, "DMA delay, %u ms, "
2348 "on %p\n", temp, xfer);
2349
2350 if (temp != 0) {
2351 USB_BUS_LOCK(xfer->udev->bus);
2352 usb2_transfer_timeout_ms(xfer,
2353 &usb2_dma_delay_done_cb, temp);
2354 USB_BUS_UNLOCK(xfer->udev->bus);
2355 return (1); /* wait for new callback */
2356 }
2357 }
2358 /* check actual number of frames */
2359 if (xfer->aframes > xfer->nframes) {
2360 if (xfer->error == 0) {
2361 panic("%s: actual number of frames, %d, is "
2362 "greater than initial number of frames, %d!\n",
2363 __FUNCTION__, xfer->aframes, xfer->nframes);
2364 } else {
2365 /* just set some valid value */
2366 xfer->aframes = xfer->nframes;
2367 }
2368 }
2369 /* compute actual length */
2370 xfer->actlen = 0;
2371
2372 for (x = 0; x != xfer->aframes; x++) {
2373 xfer->actlen += xfer->frlengths[x];
2374 }
2375
2376 /*
2377 * Frames that were not transferred get zero actual length in
2378 * case the USB device driver does not check the actual number
2379 * of frames transferred, "xfer->aframes":
2380 */
2381 for (; x < xfer->nframes; x++) {
2382 xfer->frlengths[x] = 0;
2383 }
2384
2385 /* check actual length */
2386 if (xfer->actlen > xfer->sumlen) {
2387 if (xfer->error == 0) {
2388 panic("%s: actual length, %d, is greater than "
2389 "initial length, %d!\n",
2390 __FUNCTION__, xfer->actlen, xfer->sumlen);
2391 } else {
2392 /* just set some valid value */
2393 xfer->actlen = xfer->sumlen;
2394 }
2395 }
2396 DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2397 xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen,
2398 xfer->aframes, xfer->nframes);
2399
2400 if (xfer->error) {
2401 /* end of control transfer, if any */
2402 xfer->flags_int.control_act = 0;
2403
2404 /* check if we should block the execution queue */
2405 if ((xfer->error != USB_ERR_CANCELLED) &&
2406 (xfer->flags.pipe_bof)) {
2407 DPRINTFN(2, "xfer=%p: Block On Failure "
2408 "on pipe=%p\n", xfer, xfer->pipe);
2409 goto done;
2410 }
2411 } else {
2412 /* check for short transfers */
2413 if (xfer->actlen < xfer->sumlen) {
2414
2415 /* end of control transfer, if any */
2416 xfer->flags_int.control_act = 0;
2417
2418 if (!xfer->flags_int.short_xfer_ok) {
2419 xfer->error = USB_ERR_SHORT_XFER;
2420 if (xfer->flags.pipe_bof) {
2421 DPRINTFN(2, "xfer=%p: Block On Failure on "
2422 "Short Transfer on pipe %p.\n",
2423 xfer, xfer->pipe);
2424 goto done;
2425 }
2426 }
2427 } else {
2428 /*
2429 * Check if we are in the middle of a
2430 * control transfer:
2431 */
2432 if (xfer->flags_int.control_act) {
2433 DPRINTFN(5, "xfer=%p: Control transfer "
2434 "active on pipe=%p\n", xfer, xfer->pipe);
2435 goto done;
2436 }
2437 }
2438 }
2439
2440 pipe = xfer->pipe;
2441
2442 /*
2443 * If the current USB transfer is completing we need to start the
2444 * next one:
2445 */
2446 USB_BUS_LOCK(xfer->udev->bus);
2447 if (pipe->pipe_q.curr == xfer) {
2448 usb2_command_wrapper(&pipe->pipe_q, NULL);
2449
2450 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2451 /* there is another USB transfer waiting */
2452 } else {
2453 /* this is the last USB transfer */
2454 /* clear isochronous sync flag */
2455 xfer->pipe->is_synced = 0;
2456 }
2457 }
2458 USB_BUS_UNLOCK(xfer->udev->bus);
2459done:
2460 return (0);
2461}
2462
2463/*------------------------------------------------------------------------*
2464 * usb2_command_wrapper
2465 *
2466 * This function is used to execute commands non-recursivly on an USB
2467 * transfer.
2468 *------------------------------------------------------------------------*/
2469void
2470usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer)
2471{
2472 if (xfer) {
2473 /*
2474 * If the transfer is not already processing,
2475 * queue it!
2476 */
2477 if (pq->curr != xfer) {
2478 usb2_transfer_enqueue(pq, xfer);
2479 if (pq->curr != NULL) {
2480 /* something is already processing */
2481 DPRINTFN(6, "busy %p\n", pq->curr);
2482 return;
2483 }
2484 }
2485 } else {
2486 /* Get next element in queue */
2487 pq->curr = NULL;
2488 }
2489
2490 if (!pq->recurse_1) {
2491
2492 do {
2493
2494 /* set both recurse flags */
2495 pq->recurse_1 = 1;
2496 pq->recurse_2 = 1;
2497
2498 if (pq->curr == NULL) {
2499 xfer = TAILQ_FIRST(&pq->head);
2500 if (xfer) {
2501 TAILQ_REMOVE(&pq->head, xfer,
2502 wait_entry);
2503 xfer->wait_queue = NULL;
2504 pq->curr = xfer;
2505 } else {
2506 break;
2507 }
2508 }
2509 DPRINTFN(6, "cb %p (enter)\n", pq->curr);
2510 (pq->command) (pq);
2511 DPRINTFN(6, "cb %p (leave)\n", pq->curr);
2512
2513 } while (!pq->recurse_2);
2514
2515 /* clear first recurse flag */
2516 pq->recurse_1 = 0;
2517
2518 } else {
2519 /* clear second recurse flag */
2520 pq->recurse_2 = 0;
2521 }
2522 return;
2523}
2524
2525/*------------------------------------------------------------------------*
2526 * usb2_default_transfer_setup
2527 *
2528 * This function is used to setup the default USB control endpoint
2529 * transfer.
2530 *------------------------------------------------------------------------*/
2531void
2532usb2_default_transfer_setup(struct usb2_device *udev)
2533{
2534 struct usb2_xfer *xfer;
2535 uint8_t no_resetup;
2536 uint8_t iface_index;
2537
2538repeat:
2539
2540 xfer = udev->default_xfer[0];
2541 if (xfer) {
2542 USB_XFER_LOCK(xfer);
2543 no_resetup =
2544 ((xfer->address == udev->address) &&
2545 (udev->default_ep_desc.wMaxPacketSize[0] ==
2546 udev->ddesc.bMaxPacketSize));
2547 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2548 if (no_resetup) {
2549 /*
2550 * NOTE: checking "xfer->address" and
2551 * starting the USB transfer must be
2552 * atomic!
2553 */
2554 usb2_transfer_start(xfer);
2555 }
2556 }
2557 USB_XFER_UNLOCK(xfer);
2558 } else {
2559 no_resetup = 0;
2560 }
2561
2562 if (no_resetup) {
2563 /*
2564 * All parameters are exactly the same like before.
2565 * Just return.
2566 */
2567 return;
2568 }
2569 /*
2570 * Update wMaxPacketSize for the default control endpoint:
2571 */
2572 udev->default_ep_desc.wMaxPacketSize[0] =
2573 udev->ddesc.bMaxPacketSize;
2574
2575 /*
2576 * Unsetup any existing USB transfer:
2577 */
2578 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
2579
2580 /*
2581 * Try to setup a new USB transfer for the
2582 * default control endpoint:
2583 */
2584 iface_index = 0;
2585 if (usb2_transfer_setup(udev, &iface_index,
2586 udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL,
2587 udev->default_mtx)) {
2588 DPRINTFN(0, "could not setup default "
2589 "USB transfer!\n");
2590 } else {
2591 goto repeat;
2592 }
2593 return;
2594}
2595
2596/*------------------------------------------------------------------------*
2597 * usb2_clear_data_toggle - factored out code
2598 *
2599 * NOTE: the intention of this function is not to reset the hardware
2600 * data toggle.
2601 *------------------------------------------------------------------------*/
2602void
2603usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe)
2604{
2605 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe);
2606
2607 USB_BUS_LOCK(udev->bus);
2608 pipe->toggle_next = 0;
2609 USB_BUS_UNLOCK(udev->bus);
2610 return;
2611}
2612
2613/*------------------------------------------------------------------------*
2614 * usb2_clear_stall_callback - factored out clear stall callback
2615 *
2616 * Input parameters:
2617 * xfer1: Clear Stall Control Transfer
2618 * xfer2: Stalled USB Transfer
2619 *
2620 * This function is NULL safe.
2621 *
2622 * Return values:
2623 * 0: In progress
2624 * Else: Finished
2625 *
2626 * Clear stall config example:
2627 *
2628 * static const struct usb2_config my_clearstall = {
2629 * .type = UE_CONTROL,
2630 * .endpoint = 0,
2631 * .direction = UE_DIR_ANY,
2632 * .interval = 50, //50 milliseconds
2633 * .bufsize = sizeof(struct usb2_device_request),
2634 * .mh.timeout = 1000, //1.000 seconds
2635 * .mh.flags = { },
2636 * .mh.callback = &my_clear_stall_callback, // **
2637 * };
2638 *
2639 * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback"
2640 * passing the correct parameters.
2641 *------------------------------------------------------------------------*/
2642uint8_t
2643usb2_clear_stall_callback(struct usb2_xfer *xfer1,
2644 struct usb2_xfer *xfer2)
2645{
2646 struct usb2_device_request req;
2647
2648 if (xfer2 == NULL) {
2649 /* looks like we are tearing down */
2650 DPRINTF("NULL input parameter\n");
2651 return (0);
2652 }
2653 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
2654 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
2655
2656 switch (USB_GET_STATE(xfer1)) {
2657 case USB_ST_SETUP:
2658
2659 /*
2660 * pre-clear the data toggle to DATA0 ("umass.c" and
2661 * "ata-usb.c" depends on this)
2662 */
2663
2664 usb2_clear_data_toggle(xfer2->udev, xfer2->pipe);
2665
2666 /* setup a clear-stall packet */
2667
2668 req.bmRequestType = UT_WRITE_ENDPOINT;
2669 req.bRequest = UR_CLEAR_FEATURE;
2670 USETW(req.wValue, UF_ENDPOINT_HALT);
2671 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress;
2672 req.wIndex[1] = 0;
2673 USETW(req.wLength, 0);
2674
2675 /*
2676 * "usb2_transfer_setup_sub()" will ensure that
2677 * we have sufficient room in the buffer for
2678 * the request structure!
2679 */
2680
2681 /* copy in the transfer */
2682
2683 usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
2684
2685 /* set length */
2686 xfer1->frlengths[0] = sizeof(req);
2687 xfer1->nframes = 1;
2688
2689 usb2_start_hardware(xfer1);
2690 return (0);
2691
2692 case USB_ST_TRANSFERRED:
2693 break;
2694
2695 default: /* Error */
2696 if (xfer1->error == USB_ERR_CANCELLED) {
2697 return (0);
2698 }
2699 break;
2700 }
2701 return (1); /* Clear Stall Finished */
2702}
2703
2704#if (USB_NO_POLL == 0)
2705
2706/*------------------------------------------------------------------------*
2707 * usb2_callout_poll
2708 *------------------------------------------------------------------------*/
2709static void
2710usb2_callout_poll(struct usb2_xfer *xfer)
2711{
2712 struct usb2_callout *co;
2713 void (*cb) (void *);
2714 void *arg;
2715 struct mtx *mtx;
2716 uint32_t delta;
2717
2718 if (xfer == NULL) {
2719 return;
2720 }
2721 co = &xfer->timeout_handle;
2722
2723#if __FreeBSD_version >= 800000
2724 mtx = (void *)(co->co.c_lock);
2725#else
2726 mtx = co->co.c_mtx;
2727#endif
2728 mtx_lock(mtx);
2729
2730 if (usb2_callout_pending(co)) {
2731 delta = ticks - co->co.c_time;
2732 if (!(delta & 0x80000000)) {
2733
2734 cb = co->co.c_func;
2735 arg = co->co.c_arg;
2736
2737 /* timed out */
2738 usb2_callout_stop(co);
2739
2740 (cb) (arg);
2741
2742 /* the callback should drop the mutex */
2743 } else {
2744 mtx_unlock(mtx);
2745 }
2746 } else {
2747 mtx_unlock(mtx);
2748 }
2749 return;
2750}
2751
2752
2753/*------------------------------------------------------------------------*
2754 * usb2_do_poll
2755 *
2756 * This function is called from keyboard driver when in polling
2757 * mode.
2758 *------------------------------------------------------------------------*/
2759void
2760usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2761{
2762 struct usb2_xfer *xfer;
2763 struct usb2_xfer_root *usb2_root;
2764 struct usb2_device *udev;
2765 struct usb2_proc_msg *pm;
2766 uint32_t to;
2767 uint16_t n;
2768
2769 /* compute system tick delay */
2770 to = ((uint32_t)(1000000)) / ((uint32_t)(hz));
2771 DELAY(to);
2772 atomic_add_int((volatile int *)&ticks, 1);
2773
2774 for (n = 0; n != max; n++) {
2775 xfer = ppxfer[n];
2776 if (xfer) {
2777 usb2_root = xfer->usb2_root;
2778 udev = xfer->udev;
2779
2780 /*
2781 * Poll hardware - signal that we are polling by
2782 * locking the private mutex:
2783 */
2784 USB_XFER_LOCK(xfer);
2785 (udev->bus->methods->do_poll) (udev->bus);
2786 USB_XFER_UNLOCK(xfer);
2787
2788 /* poll clear stall start */
2789 USB_BUS_LOCK(xfer->udev->bus);
2790 pm = &udev->cs_msg[0].hdr;
2791 (pm->pm_callback) (pm);
2792 USB_BUS_UNLOCK(xfer->udev->bus);
2793
2794 if (udev->default_xfer[1]) {
2795
2796 /* poll timeout */
2797 usb2_callout_poll(udev->default_xfer[1]);
2798
2799 /* poll clear stall done thread */
2800 USB_BUS_LOCK(xfer->udev->bus);
2801 pm = &udev->default_xfer[1]->
2802 usb2_root->done_m[0].hdr;
2803 (pm->pm_callback) (pm);
2804 USB_BUS_UNLOCK(xfer->udev->bus);
2805 }
2806 /* poll timeout */
2807 usb2_callout_poll(xfer);
2808
2809 /* poll done thread */
2810 USB_BUS_LOCK(xfer->udev->bus);
2811 pm = &usb2_root->done_m[0].hdr;
2812 (pm->pm_callback) (pm);
2813 USB_BUS_UNLOCK(xfer->udev->bus);
2814 }
2815 }
2816 return;
2817}
2818
2819#else
2820
2821void
2822usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2823{
2824 /* polling not supported */
2825 return;
2826}
2827
2828#endif