xref: /dragonfly/sys/bus/u4b/usb_transfer.c (revision 2b3f93ea6d1f70880f3e87f3c2cbe0dc0bfc9332)
1 /* $FreeBSD: head/sys/dev/usb/usb_transfer.c 276717 2015-01-05 20:22:18Z hselasky $ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
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 <sys/stdint.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/thread.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/unistd.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
42 #include <sys/caps.h>
43 #include <sys/proc.h>
44 
45 #include <sys/thread2.h>
46 
47 #include <bus/u4b/usb.h>
48 #include <bus/u4b/usbdi.h>
49 #include <bus/u4b/usbdi_util.h>
50 
51 #define   USB_DEBUG_VAR usb_debug
52 
53 #include <bus/u4b/usb_core.h>
54 #include <bus/u4b/usb_busdma.h>
55 #include <bus/u4b/usb_process.h>
56 #include <bus/u4b/usb_transfer.h>
57 #include <bus/u4b/usb_device.h>
58 #include <bus/u4b/usb_debug.h>
59 #include <bus/u4b/usb_util.h>
60 
61 #include <bus/u4b/usb_controller.h>
62 #include <bus/u4b/usb_bus.h>
63 #include <bus/u4b/usb_pf.h>
64 
65 struct usb_std_packet_size {
66           struct {
67                     uint16_t min;                 /* inclusive */
68                     uint16_t max;                 /* inclusive */
69           }         range;
70 
71           uint16_t fixed[4];
72 };
73 
74 static usb_callback_t usb_request_callback;
75 
76 static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
77 
78           /* This transfer is used for generic control endpoint transfers */
79 
80           [0] = {
81                     .type = UE_CONTROL,
82                     .endpoint = 0x00,   /* Control endpoint */
83                     .direction = UE_DIR_ANY,
84                     .bufsize = USB_EP0_BUFSIZE,   /* bytes */
85                     .flags = {.proxy_buffer = 1,},
86                     .callback = &usb_request_callback,
87                     .usb_mode = USB_MODE_DUAL,    /* both modes */
88           },
89 
90           /* This transfer is used for generic clear stall only */
91 
92           [1] = {
93                     .type = UE_CONTROL,
94                     .endpoint = 0x00,   /* Control pipe */
95                     .direction = UE_DIR_ANY,
96                     .bufsize = sizeof(struct usb_device_request),
97                     .callback = &usb_do_clear_stall_callback,
98                     .timeout = 1000,    /* 1 second */
99                     .interval = 50,     /* 50ms */
100                     .usb_mode = USB_MODE_HOST,
101           },
102 };
103 
104 /* function prototypes */
105 
106 static void         usbd_update_max_frame_size(struct usb_xfer *);
107 static void         usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
108 static void         usbd_delayed_free(void *data, struct malloc_type *mtype);
109 static void         usbd_control_transfer_init(struct usb_xfer *);
110 static int          usbd_setup_ctrl_transfer(struct usb_xfer *);
111 static void         usb_callback_proc(struct usb_proc_msg *);
112 static void         usbd_callback_ss_done_defer(struct usb_xfer *);
113 static void         usbd_callback_wrapper(struct usb_xfer_queue *);
114 static void         usbd_transfer_start_cb(void *);
115 static uint8_t      usbd_callback_wrapper_sub(struct usb_xfer *);
116 static void         usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
117                         uint8_t type, enum usb_dev_speed speed);
118 
119 /*------------------------------------------------------------------------*
120  *        usb_request_callback
121  *------------------------------------------------------------------------*/
122 static void
usb_request_callback(struct usb_xfer * xfer,usb_error_t error)123 usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
124 {
125           if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
126                     usb_handle_request_callback(xfer, error);
127           else
128                     usbd_do_request_callback(xfer, error);
129 }
130 
131 /*------------------------------------------------------------------------*
132  *        usbd_update_max_frame_size
133  *
134  * This function updates the maximum frame size, hence high speed USB
135  * can transfer multiple consecutive packets.
136  *------------------------------------------------------------------------*/
137 static void
usbd_update_max_frame_size(struct usb_xfer * xfer)138 usbd_update_max_frame_size(struct usb_xfer *xfer)
139 {
140           /* compute maximum frame size */
141           /* this computation should not overflow 16-bit */
142           /* max = 15 * 1024 */
143 
144           xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
145 }
146 
147 /*------------------------------------------------------------------------*
148  *        usbd_get_dma_delay
149  *
150  * The following function is called when we need to
151  * synchronize with DMA hardware.
152  *
153  * Returns:
154  *    0: no DMA delay required
155  * Else: milliseconds of DMA delay
156  *------------------------------------------------------------------------*/
157 usb_timeout_t
usbd_get_dma_delay(struct usb_device * udev)158 usbd_get_dma_delay(struct usb_device *udev)
159 {
160           const struct usb_bus_methods *mtod;
161           uint32_t temp;
162 
163           mtod = udev->bus->methods;
164           temp = 0;
165 
166           if (mtod->get_dma_delay) {
167                     (mtod->get_dma_delay) (udev, &temp);
168                     /*
169                      * Round up and convert to milliseconds. Note that we use
170                      * 1024 milliseconds per second. to save a division.
171                      */
172                     temp += 0x3FF;
173                     temp /= 0x400;
174           }
175           return (temp);
176 }
177 
178 /*------------------------------------------------------------------------*
179  *        usbd_transfer_setup_sub_malloc
180  *
181  * This function will allocate one or more DMA'able memory chunks
182  * according to "size", "align" and "count" arguments. "ppc" is
183  * pointed to a linear array of USB page caches afterwards.
184  *
185  * If the "align" argument is equal to "1" a non-contiguous allocation
186  * can happen. Else if the "align" argument is greater than "1", the
187  * allocation will always be contiguous in memory.
188  *
189  * Returns:
190  *    0: Success
191  * Else: Failure
192  *------------------------------------------------------------------------*/
193 #if USB_HAVE_BUSDMA
194 uint8_t
usbd_transfer_setup_sub_malloc(struct usb_setup_params * parm,struct usb_page_cache ** ppc,usb_size_t size,usb_size_t align,usb_size_t count)195 usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
196     struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
197     usb_size_t count)
198 {
199           struct usb_page_cache *pc;
200           struct usb_page *pg;
201           void *buf;
202           usb_size_t n_dma_pc;
203           usb_size_t n_dma_pg;
204           usb_size_t n_obj;
205           usb_size_t x;
206           usb_size_t y;
207           usb_size_t r;
208           usb_size_t z;
209 
210           USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
211               align));
212           USB_ASSERT(size > 0, ("Invalid size = 0\n"));
213 
214           if (count == 0) {
215                     return (0);                   /* nothing to allocate */
216           }
217           /*
218            * Make sure that the size is aligned properly.
219            */
220           size = -((-size) & (-align));
221 
222           /*
223            * Try multi-allocation chunks to reduce the number of DMA
224            * allocations, hence DMA allocations are slow.
225            */
226           if (align == 1) {
227                     /* special case - non-cached multi page DMA memory */
228                     n_dma_pc = count;
229                     n_dma_pg = (2 + (size / USB_PAGE_SIZE));
230                     n_obj = 1;
231           } else if (size >= USB_PAGE_SIZE) {
232                     n_dma_pc = count;
233                     n_dma_pg = 1;
234                     n_obj = 1;
235           } else {
236                     /* compute number of objects per page */
237 #ifdef USB_DMA_SINGLE_ALLOC
238                     n_obj = 1;
239 #else
240                     n_obj = (USB_PAGE_SIZE / size);
241 #endif
242                     /*
243                      * Compute number of DMA chunks, rounded up
244                      * to nearest one:
245                      */
246                     n_dma_pc = ((count + n_obj - 1) / n_obj);
247                     n_dma_pg = 1;
248           }
249 
250           /*
251            * DMA memory is allocated once, but mapped twice. That's why
252            * there is one list for auto-free and another list for
253            * non-auto-free which only holds the mapping and not the
254            * allocation.
255            */
256           if (parm->buf == NULL) {
257                     /* reserve memory (auto-free) */
258                     parm->dma_page_ptr += n_dma_pc * n_dma_pg;
259                     parm->dma_page_cache_ptr += n_dma_pc;
260 
261                     /* reserve memory (no-auto-free) */
262                     parm->dma_page_ptr += count * n_dma_pg;
263                     parm->xfer_page_cache_ptr += count;
264                     return (0);
265           }
266           for (x = 0; x != n_dma_pc; x++) {
267                     /* need to initialize the page cache */
268                     parm->dma_page_cache_ptr[x].tag_parent =
269                         &parm->curr_xfer->xroot->dma_parent_tag;
270           }
271           for (x = 0; x != count; x++) {
272                     /* need to initialize the page cache */
273                     parm->xfer_page_cache_ptr[x].tag_parent =
274                         &parm->curr_xfer->xroot->dma_parent_tag;
275           }
276 
277           if (ppc != NULL) {
278                     if (n_obj != 1)
279                               *ppc = parm->xfer_page_cache_ptr;
280                     else
281                               *ppc = parm->dma_page_cache_ptr;
282           }
283           r = count;                              /* set remainder count */
284           z = n_obj * size;             /* set allocation size */
285           pc = parm->xfer_page_cache_ptr;
286           pg = parm->dma_page_ptr;
287 
288           if (n_obj == 1) {
289               /*
290                * Avoid mapping memory twice if only a single object
291                * should be allocated per page cache:
292                */
293               for (x = 0; x != n_dma_pc; x++) {
294                     if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
295                         pg, z, align)) {
296                               return (1);         /* failure */
297                     }
298                     /* Make room for one DMA page cache and "n_dma_pg" pages */
299                     parm->dma_page_cache_ptr++;
300                     pg += n_dma_pg;
301               }
302           } else {
303               for (x = 0; x != n_dma_pc; x++) {
304 
305                     if (r < n_obj) {
306                               /* compute last remainder */
307                               z = r * size;
308                               n_obj = r;
309                     }
310                     if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
311                         pg, z, align)) {
312                               return (1);         /* failure */
313                     }
314                     /* Set beginning of current buffer */
315                     buf = parm->dma_page_cache_ptr->buffer;
316                     /* Make room for one DMA page cache and "n_dma_pg" pages */
317                     parm->dma_page_cache_ptr++;
318                     pg += n_dma_pg;
319 
320                     for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
321 
322                               /* Load sub-chunk into DMA */
323                               if (usb_pc_dmamap_create(pc, size)) {
324                                         return (1);         /* failure */
325                               }
326                               pc->buffer = USB_ADD_BYTES(buf, y * size);
327                               pc->page_start = pg;
328 
329                               lockmgr(pc->tag_parent->lock, LK_EXCLUSIVE);
330                               if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
331                                         lockmgr(pc->tag_parent->lock, LK_RELEASE);
332                                         return (1);         /* failure */
333                               }
334                               lockmgr(pc->tag_parent->lock, LK_RELEASE);
335                     }
336               }
337           }
338 
339           parm->xfer_page_cache_ptr = pc;
340           parm->dma_page_ptr = pg;
341           return (0);
342 }
343 #endif
344 
345 /*------------------------------------------------------------------------*
346  *        usbd_transfer_setup_sub - transfer setup subroutine
347  *
348  * This function must be called from the "xfer_setup" callback of the
349  * USB Host or Device controller driver when setting up an USB
350  * transfer. This function will setup correct packet sizes, buffer
351  * sizes, flags and more, that are stored in the "usb_xfer"
352  * structure.
353  *------------------------------------------------------------------------*/
354 void
usbd_transfer_setup_sub(struct usb_setup_params * parm)355 usbd_transfer_setup_sub(struct usb_setup_params *parm)
356 {
357           enum {
358                     REQ_SIZE = 8,
359                     MIN_PKT = 8,
360           };
361           struct usb_xfer *xfer = parm->curr_xfer;
362           const struct usb_config *setup = parm->curr_setup;
363           struct usb_endpoint_ss_comp_descriptor *ecomp;
364           struct usb_endpoint_descriptor *edesc;
365           struct usb_std_packet_size std_size;
366           usb_frcount_t n_frlengths;
367           usb_frcount_t n_frbuffers;
368           usb_frcount_t x;
369           uint16_t maxp_old;
370           uint8_t type;
371           uint8_t zmps;
372 
373           /*
374            * Sanity check. The following parameters must be initialized before
375            * calling this function.
376            */
377           if ((parm->hc_max_packet_size == 0) ||
378               (parm->hc_max_packet_count == 0) ||
379               (parm->hc_max_frame_size == 0)) {
380                     parm->err = USB_ERR_INVAL;
381                     goto done;
382           }
383           edesc = xfer->endpoint->edesc;
384           ecomp = xfer->endpoint->ecomp;
385 
386           type = (edesc->bmAttributes & UE_XFERTYPE);
387 
388           xfer->flags = setup->flags;
389           xfer->nframes = setup->frames;
390           xfer->timeout = setup->timeout;
391           xfer->callback = setup->callback;
392           xfer->interval = setup->interval;
393           xfer->endpointno = edesc->bEndpointAddress;
394           xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
395           xfer->max_packet_count = 1;
396           /* make a shadow copy: */
397           xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
398 
399           parm->bufsize = setup->bufsize;
400 
401           switch (parm->speed) {
402           case USB_SPEED_HIGH:
403                     switch (type) {
404                     case UE_ISOCHRONOUS:
405                     case UE_INTERRUPT:
406                               xfer->max_packet_count +=
407                                   (xfer->max_packet_size >> 11) & 3;
408 
409                               /* check for invalid max packet count */
410                               if (xfer->max_packet_count > 3)
411                                         xfer->max_packet_count = 3;
412                               break;
413                     default:
414                               break;
415                     }
416                     xfer->max_packet_size &= 0x7FF;
417                     break;
418           case USB_SPEED_SUPER:
419                     xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
420 
421                     if (ecomp != NULL)
422                               xfer->max_packet_count += ecomp->bMaxBurst;
423 
424                     if ((xfer->max_packet_count == 0) ||
425                         (xfer->max_packet_count > 16))
426                               xfer->max_packet_count = 16;
427 
428                     switch (type) {
429                     case UE_CONTROL:
430                               xfer->max_packet_count = 1;
431                               break;
432                     case UE_ISOCHRONOUS:
433                               if (ecomp != NULL) {
434                                         uint8_t mult;
435 
436                                         mult = UE_GET_SS_ISO_MULT(
437                                             ecomp->bmAttributes) + 1;
438                                         if (mult > 3)
439                                                   mult = 3;
440 
441                                         xfer->max_packet_count *= mult;
442                               }
443                               break;
444                     default:
445                               break;
446                     }
447                     xfer->max_packet_size &= 0x7FF;
448                     break;
449           default:
450                     break;
451           }
452           /* range check "max_packet_count" */
453 
454           if (xfer->max_packet_count > parm->hc_max_packet_count) {
455                     xfer->max_packet_count = parm->hc_max_packet_count;
456           }
457 
458           /* store max packet size value before filtering */
459 
460           maxp_old = xfer->max_packet_size;
461 
462           /* filter "wMaxPacketSize" according to HC capabilities */
463 
464           if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
465               (xfer->max_packet_size == 0)) {
466                     xfer->max_packet_size = parm->hc_max_packet_size;
467           }
468           /* filter "wMaxPacketSize" according to standard sizes */
469 
470           usbd_get_std_packet_size(&std_size, type, parm->speed);
471 
472           if (std_size.range.min || std_size.range.max) {
473 
474                     if (xfer->max_packet_size < std_size.range.min) {
475                               xfer->max_packet_size = std_size.range.min;
476                     }
477                     if (xfer->max_packet_size > std_size.range.max) {
478                               xfer->max_packet_size = std_size.range.max;
479                     }
480           } else {
481 
482                     if (xfer->max_packet_size >= std_size.fixed[3]) {
483                               xfer->max_packet_size = std_size.fixed[3];
484                     } else if (xfer->max_packet_size >= std_size.fixed[2]) {
485                               xfer->max_packet_size = std_size.fixed[2];
486                     } else if (xfer->max_packet_size >= std_size.fixed[1]) {
487                               xfer->max_packet_size = std_size.fixed[1];
488                     } else {
489                               /* only one possibility left */
490                               xfer->max_packet_size = std_size.fixed[0];
491                     }
492           }
493 
494           /*
495            * Check if the max packet size was outside its allowed range
496            * and clamped to a valid value:
497            */
498           if (maxp_old != xfer->max_packet_size)
499                     xfer->flags_int.maxp_was_clamped = 1;
500 
501           /* compute "max_frame_size" */
502 
503           usbd_update_max_frame_size(xfer);
504 
505           /* check interrupt interval and transfer pre-delay */
506 
507           if (type == UE_ISOCHRONOUS) {
508 
509                     uint16_t frame_limit;
510 
511                     xfer->interval = 0; /* not used, must be zero */
512                     xfer->flags_int.isochronous_xfr = 1;    /* set flag */
513 
514                     if (xfer->timeout == 0) {
515                               /*
516                                * set a default timeout in
517                                * case something goes wrong!
518                                */
519                               xfer->timeout = 1000 / 4;
520                     }
521                     switch (parm->speed) {
522                     case USB_SPEED_LOW:
523                     case USB_SPEED_FULL:
524                               frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
525                               xfer->fps_shift = 0;
526                               break;
527                     default:
528                               frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
529                               xfer->fps_shift = edesc->bInterval;
530                               if (xfer->fps_shift > 0)
531                                         xfer->fps_shift--;
532                               if (xfer->fps_shift > 3)
533                                         xfer->fps_shift = 3;
534                               if (xfer->flags.pre_scale_frames != 0)
535                                         xfer->nframes <<= (3 - xfer->fps_shift);
536                               break;
537                     }
538 
539                     if (xfer->nframes > frame_limit) {
540                               /*
541                                * this is not going to work
542                                * cross hardware
543                                */
544                               parm->err = USB_ERR_INVAL;
545                               goto done;
546                     }
547                     if (xfer->nframes == 0) {
548                               /*
549                                * this is not a valid value
550                                */
551                               parm->err = USB_ERR_ZERO_NFRAMES;
552                               goto done;
553                     }
554           } else {
555 
556                     /*
557                      * If a value is specified use that else check the
558                      * endpoint descriptor!
559                      */
560                     if (type == UE_INTERRUPT) {
561 
562                               uint32_t temp;
563 
564                               if (xfer->interval == 0) {
565 
566                                         xfer->interval = edesc->bInterval;
567 
568                                         switch (parm->speed) {
569                                         case USB_SPEED_LOW:
570                                         case USB_SPEED_FULL:
571                                                   break;
572                                         default:
573                                                   /* 125us -> 1ms */
574                                                   if (xfer->interval < 4)
575                                                             xfer->interval = 1;
576                                                   else if (xfer->interval > 16)
577                                                             xfer->interval = (1 << (16 - 4));
578                                                   else
579                                                             xfer->interval =
580                                                                 (1 << (xfer->interval - 4));
581                                                   break;
582                                         }
583                               }
584 
585                               if (xfer->interval == 0) {
586                                         /*
587                                          * One millisecond is the smallest
588                                          * interval we support:
589                                          */
590                                         xfer->interval = 1;
591                               }
592 
593                               xfer->fps_shift = 0;
594                               temp = 1;
595 
596                               while ((temp != 0) && (temp < xfer->interval)) {
597                                         xfer->fps_shift++;
598                                         temp *= 2;
599                               }
600 
601                               switch (parm->speed) {
602                               case USB_SPEED_LOW:
603                               case USB_SPEED_FULL:
604                                         break;
605                               default:
606                                         xfer->fps_shift += 3;
607                                         break;
608                               }
609                     }
610           }
611 
612           /*
613            * NOTE: we do not allow "max_packet_size" or "max_frame_size"
614            * to be equal to zero when setting up USB transfers, hence
615            * this leads to alot of extra code in the USB kernel.
616            */
617 
618           if ((xfer->max_frame_size == 0) ||
619               (xfer->max_packet_size == 0)) {
620 
621                     zmps = 1;
622 
623                     if ((parm->bufsize <= MIN_PKT) &&
624                         (type != UE_CONTROL) &&
625                         (type != UE_BULK)) {
626 
627                               /* workaround */
628                               xfer->max_packet_size = MIN_PKT;
629                               xfer->max_packet_count = 1;
630                               parm->bufsize = 0;  /* automatic setup length */
631                               usbd_update_max_frame_size(xfer);
632 
633                     } else {
634                               parm->err = USB_ERR_ZERO_MAXP;
635                               goto done;
636                     }
637 
638           } else {
639                     zmps = 0;
640           }
641 
642           /*
643            * check if we should setup a default
644            * length:
645            */
646 
647           if (parm->bufsize == 0) {
648 
649                     parm->bufsize = xfer->max_frame_size;
650 
651                     if (type == UE_ISOCHRONOUS) {
652                               parm->bufsize *= xfer->nframes;
653                     }
654           }
655           /*
656            * check if we are about to setup a proxy
657            * type of buffer:
658            */
659 
660           if (xfer->flags.proxy_buffer) {
661 
662                     /* round bufsize up */
663 
664                     parm->bufsize += (xfer->max_frame_size - 1);
665 
666                     if (parm->bufsize < xfer->max_frame_size) {
667                               /* length wrapped around */
668                               parm->err = USB_ERR_INVAL;
669                               goto done;
670                     }
671                     /* subtract remainder */
672 
673                     parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
674 
675                     /* add length of USB device request structure, if any */
676 
677                     if (type == UE_CONTROL) {
678                               parm->bufsize += REQ_SIZE;    /* SETUP message */
679                     }
680           }
681           xfer->max_data_length = parm->bufsize;
682 
683           /* Setup "n_frlengths" and "n_frbuffers" */
684 
685           if (type == UE_ISOCHRONOUS) {
686                     n_frlengths = xfer->nframes;
687                     n_frbuffers = 1;
688           } else {
689 
690                     if (type == UE_CONTROL) {
691                               xfer->flags_int.control_xfr = 1;
692                               if (xfer->nframes == 0) {
693                                         if (parm->bufsize <= REQ_SIZE) {
694                                                   /*
695                                                    * there will never be any data
696                                                    * stage
697                                                    */
698                                                   xfer->nframes = 1;
699                                         } else {
700                                                   xfer->nframes = 2;
701                                         }
702                               }
703                     } else {
704                               if (xfer->nframes == 0) {
705                                         xfer->nframes = 1;
706                               }
707                     }
708 
709                     n_frlengths = xfer->nframes;
710                     n_frbuffers = xfer->nframes;
711           }
712 
713           /*
714            * check if we have room for the
715            * USB device request structure:
716            */
717 
718           if (type == UE_CONTROL) {
719 
720                     if (xfer->max_data_length < REQ_SIZE) {
721                               /* length wrapped around or too small bufsize */
722                               parm->err = USB_ERR_INVAL;
723                               goto done;
724                     }
725                     xfer->max_data_length -= REQ_SIZE;
726           }
727           /*
728            * Setup "frlengths" and shadow "frlengths" for keeping the
729            * initial frame lengths when a USB transfer is complete. This
730            * information is useful when computing isochronous offsets.
731            */
732           xfer->frlengths = parm->xfer_length_ptr;
733           parm->xfer_length_ptr += 2 * n_frlengths;
734 
735           /* setup "frbuffers" */
736           xfer->frbuffers = parm->xfer_page_cache_ptr;
737           parm->xfer_page_cache_ptr += n_frbuffers;
738 
739           /* initialize max frame count */
740           xfer->max_frame_count = xfer->nframes;
741 
742           /*
743            * check if we need to setup
744            * a local buffer:
745            */
746 
747           if (!xfer->flags.ext_buffer) {
748 #if USB_HAVE_BUSDMA
749                     struct usb_page_search page_info;
750                     struct usb_page_cache *pc;
751 
752                     if (usbd_transfer_setup_sub_malloc(parm,
753                         &pc, parm->bufsize, 1, 1)) {
754                               parm->err = USB_ERR_NOMEM;
755                     } else if (parm->buf != NULL) {
756 
757                               usbd_get_page(pc, 0, &page_info);
758 
759                               xfer->local_buffer = page_info.buffer;
760 
761                               usbd_xfer_set_frame_offset(xfer, 0, 0);
762 
763                               if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
764                                         usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
765                               }
766                     }
767 #else
768                     /* align data */
769                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
770 
771                     if (parm->buf != NULL) {
772                               xfer->local_buffer =
773                                   USB_ADD_BYTES(parm->buf, parm->size[0]);
774 
775                               usbd_xfer_set_frame_offset(xfer, 0, 0);
776 
777                               if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
778                                         usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
779                               }
780                     }
781                     parm->size[0] += parm->bufsize;
782 
783                     /* align data again */
784                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
785 #endif
786           }
787           /*
788            * Compute maximum buffer size
789            */
790 
791           if (parm->bufsize_max < parm->bufsize) {
792                     parm->bufsize_max = parm->bufsize;
793           }
794 #if USB_HAVE_BUSDMA
795           if (xfer->flags_int.bdma_enable) {
796                     /*
797                      * Setup "dma_page_ptr".
798                      *
799                      * Proof for formula below:
800                      *
801                      * Assume there are three USB frames having length "a", "b" and
802                      * "c". These USB frames will at maximum need "z"
803                      * "usb_page" structures. "z" is given by:
804                      *
805                      * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
806                      * ((c / USB_PAGE_SIZE) + 2);
807                      *
808                      * Constraining "a", "b" and "c" like this:
809                      *
810                      * (a + b + c) <= parm->bufsize
811                      *
812                      * We know that:
813                      *
814                      * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
815                      *
816                      * Here is the general formula:
817                      */
818                     xfer->dma_page_ptr = parm->dma_page_ptr;
819                     parm->dma_page_ptr += (2 * n_frbuffers);
820                     parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
821           }
822 #endif
823           if (zmps) {
824                     /* correct maximum data length */
825                     xfer->max_data_length = 0;
826           }
827           /* subtract USB frame remainder from "hc_max_frame_size" */
828 
829           xfer->max_hc_frame_size =
830               (parm->hc_max_frame_size -
831               (parm->hc_max_frame_size % xfer->max_frame_size));
832 
833           if (xfer->max_hc_frame_size == 0) {
834                     parm->err = USB_ERR_INVAL;
835                     goto done;
836           }
837 
838           /* initialize frame buffers */
839 
840           if (parm->buf) {
841                     for (x = 0; x != n_frbuffers; x++) {
842                               xfer->frbuffers[x].tag_parent =
843                                   &xfer->xroot->dma_parent_tag;
844 #if USB_HAVE_BUSDMA
845                               if (xfer->flags_int.bdma_enable &&
846                                   (parm->bufsize_max > 0)) {
847 
848                                         if (usb_pc_dmamap_create(
849                                             xfer->frbuffers + x,
850                                             parm->bufsize_max)) {
851                                                   parm->err = USB_ERR_NOMEM;
852                                                   goto done;
853                                         }
854                               }
855 #endif
856                     }
857           }
858 done:
859           if (parm->err) {
860                     /*
861                      * Set some dummy values so that we avoid division by zero:
862                      */
863                     xfer->max_hc_frame_size = 1;
864                     xfer->max_frame_size = 1;
865                     xfer->max_packet_size = 1;
866                     xfer->max_data_length = 0;
867                     xfer->nframes = 0;
868                     xfer->max_frame_count = 0;
869           }
870 }
871 
872 /*------------------------------------------------------------------------*
873  *        usbd_transfer_setup - setup an array of USB transfers
874  *
875  * NOTE: You must always call "usbd_transfer_unsetup" after calling
876  * "usbd_transfer_setup" if success was returned.
877  *
878  * The idea is that the USB device driver should pre-allocate all its
879  * transfers by one call to this function.
880  *
881  * Return values:
882  *    0: Success
883  * Else: Failure
884  *------------------------------------------------------------------------*/
885 usb_error_t
usbd_transfer_setup(struct usb_device * udev,const uint8_t * ifaces,struct usb_xfer ** ppxfer,const struct usb_config * setup_start,uint16_t n_setup,void * priv_sc,struct lock * xfer_lock)886 usbd_transfer_setup(struct usb_device *udev,
887     const uint8_t *ifaces, struct usb_xfer **ppxfer,
888     const struct usb_config *setup_start, uint16_t n_setup,
889     void *priv_sc, struct lock *xfer_lock)
890 {
891           const struct usb_config *setup_end = setup_start + n_setup;
892           const struct usb_config *setup;
893           struct usb_setup_params *parm;
894           struct usb_endpoint *ep;
895           struct usb_xfer_root *info;
896           struct usb_xfer *xfer;
897           void *buf = NULL;
898           usb_error_t error = 0;
899           uint16_t n;
900           uint16_t refcount;
901           uint8_t do_unlock;
902 
903 #if 0
904           WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
905               "usbd_transfer_setup can sleep!");
906 #endif
907 
908           /* do some checking first */
909 
910           if (n_setup == 0) {
911                     DPRINTFN(6, "setup array has zero length!\n");
912                     return (USB_ERR_INVAL);
913           }
914           if (ifaces == NULL) {
915                     DPRINTFN(6, "ifaces array is NULL!\n");
916                     return (USB_ERR_INVAL);
917           }
918           if (xfer_lock == NULL) {
919                     panic("xfer without lock!\n");
920                     DPRINTFN(6, "using global lock\n");
921           }
922 
923           /* more sanity checks */
924 
925           for (setup = setup_start, n = 0;
926               setup != setup_end; setup++, n++) {
927                     if (setup->bufsize == (usb_frlength_t)-1) {
928                               error = USB_ERR_BAD_BUFSIZE;
929                               DPRINTF("invalid bufsize\n");
930                     }
931                     if (setup->callback == NULL) {
932                               error = USB_ERR_NO_CALLBACK;
933                               DPRINTF("no callback\n");
934                     }
935                     ppxfer[n] = NULL;
936           }
937 
938           if (error)
939                     return (error);
940 
941           /* Protect scratch area */
942           do_unlock = usbd_enum_lock(udev);
943 
944           refcount = 0;
945           info = NULL;
946 
947           parm = &udev->scratch.xfer_setup[0].parm;
948           memset(parm, 0, sizeof(*parm));
949 
950           parm->udev = udev;
951           parm->speed = usbd_get_speed(udev);
952           parm->hc_max_packet_count = 1;
953 
954           if (parm->speed >= USB_SPEED_MAX) {
955                     parm->err = USB_ERR_INVAL;
956                     goto done;
957           }
958           /* setup all transfers */
959 
960           while (1) {
961 
962                     if (buf) {
963                               /*
964                                * Initialize the "usb_xfer_root" structure,
965                                * which is common for all our USB transfers.
966                                */
967                               info = USB_ADD_BYTES(buf, 0);
968 
969                               info->memory_base = buf;
970                               info->memory_size = parm->size[0];
971 
972 #if USB_HAVE_BUSDMA
973                               info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
974                               info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
975 #endif
976                               info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
977                               info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
978 
979                               cv_init(&info->cv_drain, "WDRAIN");
980 
981                               info->xfer_lock = xfer_lock;
982 #if USB_HAVE_BUSDMA
983                               usb_dma_tag_setup(&info->dma_parent_tag,
984                                   parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
985                                   xfer_lock, &usb_bdma_done_event,
986                                   udev->bus->dma_bits, parm->dma_tag_max);
987 #endif
988 
989                               info->bus = udev->bus;
990                               info->udev = udev;
991 
992                               TAILQ_INIT(&info->done_q.head);
993                               info->done_q.command = &usbd_callback_wrapper;
994 #if USB_HAVE_BUSDMA
995                               TAILQ_INIT(&info->dma_q.head);
996                               info->dma_q.command = &usb_bdma_work_loop;
997 #endif
998                               info->done_m[0].hdr.pm_callback = &usb_callback_proc;
999                               info->done_m[0].xroot = info;
1000                               info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1001                               info->done_m[1].xroot = info;
1002 
1003                               /*
1004                                * In device side mode control endpoint
1005                                * requests need to run from a separate
1006                                * context, else there is a chance of
1007                                * deadlock!
1008                                */
1009                               if (setup_start == usb_control_ep_cfg)
1010                                         info->done_p =
1011                                             USB_BUS_CONTROL_XFER_PROC(udev->bus);
1012                               else
1013                                         info->done_p =
1014                                             USB_BUS_NON_GIANT_PROC(udev->bus);
1015                     }
1016                     /* reset sizes */
1017 
1018                     parm->size[0] = 0;
1019                     parm->buf = buf;
1020                     parm->size[0] += sizeof(info[0]);
1021 
1022                     for (setup = setup_start, n = 0;
1023                         setup != setup_end; setup++, n++) {
1024 
1025                               /* skip USB transfers without callbacks: */
1026                               if (setup->callback == NULL) {
1027                                         continue;
1028                               }
1029                               /* see if there is a matching endpoint */
1030                               ep = usbd_get_endpoint(udev,
1031                                   ifaces[setup->if_index], setup);
1032 
1033                               /*
1034                                * Check that the USB PIPE is valid and that
1035                                * the endpoint mode is proper.
1036                                *
1037                                * Make sure we don't allocate a streams
1038                                * transfer when such a combination is not
1039                                * valid.
1040                                */
1041                               if ((ep == NULL) || (ep->methods == NULL) ||
1042                                   ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1043                                   (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1044                                   (setup->stream_id != 0 &&
1045                                   (setup->stream_id >= USB_MAX_EP_STREAMS ||
1046                                   (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1047                                         if (setup->flags.no_pipe_ok)
1048                                                   continue;
1049                                         if ((setup->usb_mode != USB_MODE_DUAL) &&
1050                                             (setup->usb_mode != udev->flags.usb_mode))
1051                                                   continue;
1052                                         parm->err = USB_ERR_NO_PIPE;
1053                                         goto done;
1054                               }
1055 
1056                               /* align data properly */
1057                               parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1058 
1059                               /* store current setup pointer */
1060                               parm->curr_setup = setup;
1061 
1062                               if (buf) {
1063                                         /*
1064                                          * Common initialization of the
1065                                          * "usb_xfer" structure.
1066                                          */
1067                                         xfer = USB_ADD_BYTES(buf, parm->size[0]);
1068                                         xfer->address = udev->address;
1069                                         xfer->priv_sc = priv_sc;
1070                                         xfer->xroot = info;
1071 
1072                                         usb_callout_init_mtx(&xfer->timeout_handle,
1073                                             &udev->bus->bus_lock, 0);
1074                               } else {
1075                                         /*
1076                                          * Setup a dummy xfer, hence we are
1077                                          * writing to the "usb_xfer"
1078                                          * structure pointed to by "xfer"
1079                                          * before we have allocated any
1080                                          * memory:
1081                                          */
1082                                         xfer = &udev->scratch.xfer_setup[0].dummy;
1083                                         memset(xfer, 0, sizeof(*xfer));
1084                                         refcount++;
1085                               }
1086 
1087                               /* set transfer endpoint pointer */
1088                               xfer->endpoint = ep;
1089 
1090                               /* set transfer stream ID */
1091                               xfer->stream_id = setup->stream_id;
1092 
1093                               parm->size[0] += sizeof(xfer[0]);
1094                               parm->methods = xfer->endpoint->methods;
1095                               parm->curr_xfer = xfer;
1096 
1097                               /*
1098                                * Call the Host or Device controller transfer
1099                                * setup routine:
1100                                */
1101                               (udev->bus->methods->xfer_setup) (parm);
1102 
1103                               /* check for error */
1104                               if (parm->err)
1105                                         goto done;
1106 
1107                               if (buf) {
1108                                         /*
1109                                          * Increment the endpoint refcount. This
1110                                          * basically prevents setting a new
1111                                          * configuration and alternate setting
1112                                          * when USB transfers are in use on
1113                                          * the given interface. Search the USB
1114                                          * code for "endpoint->refcount_alloc" if you
1115                                          * want more information.
1116                                          */
1117                                         USB_BUS_LOCK(info->bus);
1118                                         if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1119                                                   parm->err = USB_ERR_INVAL;
1120 
1121                                         xfer->endpoint->refcount_alloc++;
1122 
1123                                         if (xfer->endpoint->refcount_alloc == 0)
1124                                                   panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1125                                         USB_BUS_UNLOCK(info->bus);
1126 
1127                                         /*
1128                                          * Whenever we set ppxfer[] then we
1129                                          * also need to increment the
1130                                          * "setup_refcount":
1131                                          */
1132                                         info->setup_refcount++;
1133 
1134                                         /*
1135                                          * Transfer is successfully setup and
1136                                          * can be used:
1137                                          */
1138                                         ppxfer[n] = xfer;
1139                               }
1140 
1141                               /* check for error */
1142                               if (parm->err)
1143                                         goto done;
1144                     }
1145 
1146                     if (buf != NULL || parm->err != 0)
1147                               goto done;
1148 
1149                     /* if no transfers, nothing to do */
1150                     if (refcount == 0)
1151                               goto done;
1152 
1153                     /* align data properly */
1154                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1155 
1156                     /* store offset temporarily */
1157                     parm->size[1] = parm->size[0];
1158 
1159                     /*
1160                      * The number of DMA tags required depends on
1161                      * the number of endpoints. The current estimate
1162                      * for maximum number of DMA tags per endpoint
1163                      * is three:
1164                      * 1) for loading memory
1165                      * 2) for allocating memory
1166                      * 3) for fixing memory [UHCI]
1167                      */
1168                     parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1169 
1170                     /*
1171                      * DMA tags for QH, TD, Data and more.
1172                      */
1173                     parm->dma_tag_max += 8;
1174 
1175                     parm->dma_tag_p += parm->dma_tag_max;
1176 
1177                     parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1178                         ((uint8_t *)0);
1179 
1180                     /* align data properly */
1181                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1182 
1183                     /* store offset temporarily */
1184                     parm->size[3] = parm->size[0];
1185 
1186                     parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1187                         ((uint8_t *)0);
1188 
1189                     /* align data properly */
1190                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1191 
1192                     /* store offset temporarily */
1193                     parm->size[4] = parm->size[0];
1194 
1195                     parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1196                         ((uint8_t *)0);
1197 
1198                     /* store end offset temporarily */
1199                     parm->size[5] = parm->size[0];
1200 
1201                     parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1202                         ((uint8_t *)0);
1203 
1204                     /* store end offset temporarily */
1205 
1206                     parm->size[2] = parm->size[0];
1207 
1208                     /* align data properly */
1209                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1210 
1211                     parm->size[6] = parm->size[0];
1212 
1213                     parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1214                         ((uint8_t *)0);
1215 
1216                     /* align data properly */
1217                     parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1218 
1219                     /* allocate zeroed memory */
1220                     buf = kmalloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1221 
1222                     if (buf == NULL) {
1223                               parm->err = USB_ERR_NOMEM;
1224                               DPRINTFN(0, "cannot allocate memory block for "
1225                                   "configuration (%d bytes)\n",
1226                                   parm->size[0]);
1227                               goto done;
1228                     }
1229                     parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1230                     parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1231                     parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1232                     parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1233                     parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1234           }
1235 
1236 done:
1237           if (buf) {
1238                     if (info->setup_refcount == 0) {
1239                               /*
1240                                * "usbd_transfer_unsetup_sub" will unlock
1241                                * the bus mutex before returning !
1242                                */
1243                               USB_BUS_LOCK(info->bus);
1244 
1245                               /* something went wrong */
1246                               usbd_transfer_unsetup_sub(info, 0);
1247                     }
1248           }
1249 
1250           /* check if any errors happened */
1251           if (parm->err)
1252                     usbd_transfer_unsetup(ppxfer, n_setup);
1253 
1254           error = parm->err;
1255 
1256           if (do_unlock)
1257                     usbd_enum_unlock(udev);
1258 
1259           return (error);
1260 }
1261 
1262 /*------------------------------------------------------------------------*
1263  *        usbd_transfer_unsetup_sub - factored out code
1264  *------------------------------------------------------------------------*/
1265 static void
usbd_transfer_unsetup_sub(struct usb_xfer_root * info,uint8_t needs_delay)1266 usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1267 {
1268 #if USB_HAVE_BUSDMA
1269           struct usb_page_cache *pc;
1270 #endif
1271 
1272           USB_BUS_LOCK_ASSERT(info->bus);
1273 
1274           /* wait for any outstanding DMA operations */
1275           /* This is insane */
1276           if (needs_delay) {
1277                     usb_timeout_t temp;
1278                     temp = usbd_get_dma_delay(info->udev);
1279                     if (temp != 0) {
1280                               usb_pause_mtx(&info->bus->bus_lock,
1281                                   USB_MS_TO_TICKS(temp));
1282                     }
1283           }
1284 
1285           /* make sure that our done messages are not queued anywhere */
1286           usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1287 
1288           USB_BUS_UNLOCK(info->bus);
1289 
1290 #if USB_HAVE_BUSDMA
1291           /* free DMA'able memory, if any */
1292           pc = info->dma_page_cache_start;
1293           while (pc != info->dma_page_cache_end) {
1294                     usb_pc_free_mem(pc);
1295                     pc++;
1296           }
1297 
1298           /* free DMA maps in all "xfer->frbuffers" */
1299           pc = info->xfer_page_cache_start;
1300           while (pc != info->xfer_page_cache_end) {
1301                     usb_pc_dmamap_destroy(pc);
1302                     pc++;
1303           }
1304 
1305           /* free all DMA tags */
1306           usb_dma_tag_unsetup(&info->dma_parent_tag);
1307 #endif
1308 
1309           cv_destroy(&info->cv_drain);
1310 
1311           /*
1312            * free the "memory_base" last, hence the "info" structure is
1313            * contained within the "memory_base"!
1314            */
1315           usbd_delayed_free(info->memory_base, M_USB);
1316 }
1317 
1318 /*
1319  * This is a horrible hack and workaround to a very bad decision by
1320  * the original U4B coder to integrate the QH/TD structures into the
1321  * xfer and then free the whole mess all at once.
1322  *
1323  * The problem is that the controller may still be accessing the QHs,
1324  * because it might have gotten side-tracked onto the removed QHs
1325  * chain link.  They have to remain intact long enough for the
1326  * controller to get out.
1327  *
1328  * This horrible hack basically just delays freeing by 256 slots.
1329  * It's not even time-based or door-bell based (which is the way
1330  * the linux driver does it)... but to fix it properly requires rewriting
1331  * too much of this driver.
1332  */
1333 #define DFREE_SLOTS 256
1334 #define DFREE_MASK  (DFREE_SLOTS - 1)
1335 
1336 static struct dfree_slot {
1337           void *data;
1338           struct malloc_type *mtype;
1339 } dfree_slots[DFREE_SLOTS];
1340 static int dfree_index;
1341 
1342 static void
usbd_delayed_free(void * data,struct malloc_type * mtype)1343 usbd_delayed_free(void *data, struct malloc_type *mtype)
1344 {
1345           struct dfree_slot slot;
1346           int index;
1347 
1348           crit_enter();
1349           index = atomic_fetchadd_int(&dfree_index, 1);
1350           index &= DFREE_MASK;
1351           slot = dfree_slots[index];
1352           dfree_slots[index].data = data;
1353           dfree_slots[index].mtype = mtype;
1354           crit_exit();
1355           if (slot.data)
1356                     kfree(slot.data, slot.mtype);
1357 }
1358 
1359 /*------------------------------------------------------------------------*
1360  *        usbd_transfer_unsetup - unsetup/free an array of USB transfers
1361  *
1362  * NOTE: All USB transfers in progress will get called back passing
1363  * the error code "USB_ERR_CANCELLED" before this function
1364  * returns.
1365  *------------------------------------------------------------------------*/
1366 void
usbd_transfer_unsetup(struct usb_xfer ** pxfer,uint16_t n_setup)1367 usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1368 {
1369           struct usb_xfer *xfer;
1370           struct usb_xfer_root *info;
1371           uint8_t needs_delay = 0;
1372 
1373 #if 0
1374           WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1375               "usbd_transfer_unsetup can sleep!");
1376 #endif
1377 
1378           while (n_setup--) {
1379                     xfer = pxfer[n_setup];
1380 
1381                     if (xfer == NULL)
1382                               continue;
1383 
1384                     info = xfer->xroot;
1385 
1386                     USB_XFER_LOCK(xfer);
1387                     USB_BUS_LOCK(info->bus);
1388 
1389                     /*
1390                      * HINT: when you start/stop a transfer, it might be a
1391                      * good idea to directly use the "pxfer[]" structure:
1392                      *
1393                      * usbd_transfer_start(sc->pxfer[0]);
1394                      * usbd_transfer_stop(sc->pxfer[0]);
1395                      *
1396                      * That way, if your code has many parts that will not
1397                      * stop running under the same lock, in other words
1398                      * "xfer_mtx", the usbd_transfer_start and
1399                      * usbd_transfer_stop functions will simply return
1400                      * when they detect a NULL pointer argument.
1401                      *
1402                      * To avoid any races we clear the "pxfer[]" pointer
1403                      * while holding the private mutex of the driver:
1404                      */
1405                     pxfer[n_setup] = NULL;
1406 
1407                     USB_BUS_UNLOCK(info->bus);
1408                     USB_XFER_UNLOCK(xfer);
1409 
1410                     usbd_transfer_drain(xfer);
1411 
1412 #if USB_HAVE_BUSDMA
1413                     if (xfer->flags_int.bdma_enable)
1414                               needs_delay = 1;
1415 #endif
1416                     /*
1417                      * NOTE: default endpoint does not have an
1418                      * interface, even if endpoint->iface_index == 0
1419                      */
1420                     USB_BUS_LOCK(info->bus);
1421                     xfer->endpoint->refcount_alloc--;
1422                     USB_BUS_UNLOCK(info->bus);
1423 
1424                     usb_callout_drain(&xfer->timeout_handle);
1425 
1426                     USB_BUS_LOCK(info->bus);
1427 
1428                     USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1429                         "reference count\n"));
1430 
1431                     info->setup_refcount--;
1432 
1433                     if (info->setup_refcount == 0) {
1434                               usbd_transfer_unsetup_sub(info,
1435                                   needs_delay);
1436                     } else {
1437                               USB_BUS_UNLOCK(info->bus);
1438                     }
1439           }
1440 }
1441 
1442 /*------------------------------------------------------------------------*
1443  *        usbd_control_transfer_init - factored out code
1444  *
1445  * In USB Device Mode we have to wait for the SETUP packet which
1446  * containst the "struct usb_device_request" structure, before we can
1447  * transfer any data. In USB Host Mode we already have the SETUP
1448  * packet at the moment the USB transfer is started. This leads us to
1449  * having to setup the USB transfer at two different places in
1450  * time. This function just contains factored out control transfer
1451  * initialisation code, so that we don't duplicate the code.
1452  *------------------------------------------------------------------------*/
1453 static void
usbd_control_transfer_init(struct usb_xfer * xfer)1454 usbd_control_transfer_init(struct usb_xfer *xfer)
1455 {
1456           struct usb_device_request req;
1457 
1458           /* copy out the USB request header */
1459 
1460           usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1461 
1462           /* setup remainder */
1463 
1464           xfer->flags_int.control_rem = UGETW(req.wLength);
1465 
1466           /* copy direction to endpoint variable */
1467 
1468           xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1469           xfer->endpointno |=
1470               (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1471 }
1472 
1473 /*------------------------------------------------------------------------*
1474  *        usbd_control_transfer_did_data
1475  *
1476  * This function returns non-zero if a control endpoint has
1477  * transferred the first DATA packet after the SETUP packet.
1478  * Else it returns zero.
1479  *------------------------------------------------------------------------*/
1480 static uint8_t
usbd_control_transfer_did_data(struct usb_xfer * xfer)1481 usbd_control_transfer_did_data(struct usb_xfer *xfer)
1482 {
1483           struct usb_device_request req;
1484 
1485           /* SETUP packet is not yet sent */
1486           if (xfer->flags_int.control_hdr != 0)
1487                     return (0);
1488 
1489           /* copy out the USB request header */
1490           usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1491 
1492           /* compare remainder to the initial value */
1493           return (xfer->flags_int.control_rem != UGETW(req.wLength));
1494 }
1495 
1496 /*------------------------------------------------------------------------*
1497  *        usbd_setup_ctrl_transfer
1498  *
1499  * This function handles initialisation of control transfers. Control
1500  * transfers are special in that regard that they can both transmit
1501  * and receive data.
1502  *
1503  * Return values:
1504  *    0: Success
1505  * Else: Failure
1506  *------------------------------------------------------------------------*/
1507 static int
usbd_setup_ctrl_transfer(struct usb_xfer * xfer)1508 usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1509 {
1510           usb_frlength_t len;
1511 
1512           /* Check for control endpoint stall */
1513           if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1514                     /* the control transfer is no longer active */
1515                     xfer->flags_int.control_stall = 1;
1516                     xfer->flags_int.control_act = 0;
1517           } else {
1518                     /* don't stall control transfer by default */
1519                     xfer->flags_int.control_stall = 0;
1520           }
1521 
1522           /* Check for invalid number of frames */
1523           if (xfer->nframes > 2) {
1524                     /*
1525                      * If you need to split a control transfer, you
1526                      * have to do one part at a time. Only with
1527                      * non-control transfers you can do multiple
1528                      * parts a time.
1529                      */
1530                     DPRINTFN(0, "Too many frames: %u\n",
1531                         (unsigned int)xfer->nframes);
1532                     goto error;
1533           }
1534 
1535           /*
1536          * Check if there is a control
1537          * transfer in progress:
1538          */
1539           if (xfer->flags_int.control_act) {
1540 
1541                     if (xfer->flags_int.control_hdr) {
1542 
1543                               /* clear send header flag */
1544 
1545                               xfer->flags_int.control_hdr = 0;
1546 
1547                               /* setup control transfer */
1548                               if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1549                                         usbd_control_transfer_init(xfer);
1550                               }
1551                     }
1552                     /* get data length */
1553 
1554                     len = xfer->sumlen;
1555 
1556           } else {
1557 
1558                     /* the size of the SETUP structure is hardcoded ! */
1559 
1560                     if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1561                               DPRINTFN(0, "Wrong framelength %u != %zu\n",
1562                                   xfer->frlengths[0], sizeof(struct
1563                                   usb_device_request));
1564                               goto error;
1565                     }
1566                     /* check USB mode */
1567                     if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1568 
1569                               /* check number of frames */
1570                               if (xfer->nframes != 1) {
1571                                         /*
1572                                        * We need to receive the setup
1573                                        * message first so that we know the
1574                                        * data direction!
1575                                        */
1576                                         DPRINTF("Misconfigured transfer\n");
1577                                         goto error;
1578                               }
1579                               /*
1580                                * Set a dummy "control_rem" value.  This
1581                                * variable will be overwritten later by a
1582                                * call to "usbd_control_transfer_init()" !
1583                                */
1584                               xfer->flags_int.control_rem = 0xFFFF;
1585                     } else {
1586 
1587                               /* setup "endpoint" and "control_rem" */
1588 
1589                               usbd_control_transfer_init(xfer);
1590                     }
1591 
1592                     /* set transfer-header flag */
1593 
1594                     xfer->flags_int.control_hdr = 1;
1595 
1596                     /* get data length */
1597 
1598                     len = (xfer->sumlen - sizeof(struct usb_device_request));
1599           }
1600 
1601           /* update did data flag */
1602 
1603           xfer->flags_int.control_did_data =
1604               usbd_control_transfer_did_data(xfer);
1605 
1606           /* check if there is a length mismatch */
1607 
1608           if (len > xfer->flags_int.control_rem) {
1609                     DPRINTFN(0, "Length (%d) greater than "
1610                         "remaining length (%d)\n", len,
1611                         xfer->flags_int.control_rem);
1612                     goto error;
1613           }
1614           /* check if we are doing a short transfer */
1615 
1616           if (xfer->flags.force_short_xfer) {
1617                     xfer->flags_int.control_rem = 0;
1618           } else {
1619                     if ((len != xfer->max_data_length) &&
1620                         (len != xfer->flags_int.control_rem) &&
1621                         (xfer->nframes != 1)) {
1622                               DPRINTFN(0, "Short control transfer without "
1623                                   "force_short_xfer set\n");
1624                               goto error;
1625                     }
1626                     xfer->flags_int.control_rem -= len;
1627           }
1628 
1629           /* the status part is executed when "control_act" is 0 */
1630 
1631           if ((xfer->flags_int.control_rem > 0) ||
1632               (xfer->flags.manual_status)) {
1633                     /* don't execute the STATUS stage yet */
1634                     xfer->flags_int.control_act = 1;
1635 
1636                     /* sanity check */
1637                     if ((!xfer->flags_int.control_hdr) &&
1638                         (xfer->nframes == 1)) {
1639                               /*
1640                              * This is not a valid operation!
1641                              */
1642                               DPRINTFN(0, "Invalid parameter "
1643                                   "combination\n");
1644                               goto error;
1645                     }
1646           } else {
1647                     /* time to execute the STATUS stage */
1648                     xfer->flags_int.control_act = 0;
1649           }
1650           return (0);                             /* success */
1651 
1652 error:
1653           return (1);                             /* failure */
1654 }
1655 
1656 /*------------------------------------------------------------------------*
1657  *        usbd_transfer_submit - start USB hardware for the given transfer
1658  *
1659  * This function should only be called from the USB callback.
1660  *------------------------------------------------------------------------*/
1661 void
usbd_transfer_submit(struct usb_xfer * xfer)1662 usbd_transfer_submit(struct usb_xfer *xfer)
1663 {
1664           struct usb_xfer_root *info;
1665           struct usb_bus *bus;
1666           usb_frcount_t x;
1667 
1668           info = xfer->xroot;
1669           bus = info->bus;
1670 
1671           DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1672               xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1673               "read" : "write");
1674 
1675 #ifdef USB_DEBUG
1676           if (USB_DEBUG_VAR > 0) {
1677                     USB_BUS_LOCK(bus);
1678 
1679                     usb_dump_endpoint(xfer->endpoint);
1680 
1681                     USB_BUS_UNLOCK(bus);
1682           }
1683 #endif
1684 
1685           USB_XFER_LOCK_ASSERT(xfer);
1686           USB_BUS_LOCK_ASSERT_NOTOWNED(bus);
1687 
1688           /* Only open the USB transfer once! */
1689           if (!xfer->flags_int.open) {
1690                     xfer->flags_int.open = 1;
1691 
1692                     DPRINTF("open\n");
1693 
1694                     USB_BUS_LOCK(bus);
1695                     (xfer->endpoint->methods->open) (xfer);
1696                     USB_BUS_UNLOCK(bus);
1697           }
1698           /* set "transferring" flag */
1699           xfer->flags_int.transferring = 1;
1700 
1701 #if USB_HAVE_POWERD
1702           /* increment power reference */
1703           usbd_transfer_power_ref(xfer, 1);
1704 #endif
1705           /*
1706            * Check if the transfer is waiting on a queue, most
1707            * frequently the "done_q":
1708            */
1709           if (xfer->wait_queue) {
1710                     USB_BUS_LOCK(bus);
1711                     usbd_transfer_dequeue(xfer);
1712                     USB_BUS_UNLOCK(bus);
1713           }
1714           /* clear "did_dma_delay" flag */
1715           xfer->flags_int.did_dma_delay = 0;
1716 
1717           /* clear "did_close" flag */
1718           xfer->flags_int.did_close = 0;
1719 
1720 #if USB_HAVE_BUSDMA
1721           /* clear "bdma_setup" flag */
1722           xfer->flags_int.bdma_setup = 0;
1723 #endif
1724           /* by default we cannot cancel any USB transfer immediately */
1725           xfer->flags_int.can_cancel_immed = 0;
1726 
1727           /* clear lengths and frame counts by default */
1728           xfer->sumlen = 0;
1729           xfer->actlen = 0;
1730           xfer->aframes = 0;
1731 
1732           /* clear any previous errors */
1733           xfer->error = 0;
1734 
1735           /* Check if the device is still alive */
1736           if (info->udev->state < USB_STATE_POWERED) {
1737                     USB_BUS_LOCK(bus);
1738                     /*
1739                      * Must return cancelled error code else
1740                      * device drivers can hang.
1741                      */
1742                     usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1743                     USB_BUS_UNLOCK(bus);
1744                     return;
1745           }
1746 
1747           /* sanity check */
1748           if (xfer->nframes == 0) {
1749                     if (xfer->flags.stall_pipe) {
1750                               /*
1751                                * Special case - want to stall without transferring
1752                                * any data:
1753                                */
1754                               DPRINTF("xfer=%p nframes=0: stall "
1755                                   "or clear stall!\n", xfer);
1756                               USB_BUS_LOCK(bus);
1757                               xfer->flags_int.can_cancel_immed = 1;
1758                               /* start the transfer */
1759                               usb_command_wrapper(&xfer->endpoint->
1760                                   endpoint_q[xfer->stream_id], xfer);
1761                               USB_BUS_UNLOCK(bus);
1762                               return;
1763                     }
1764                     USB_BUS_LOCK(bus);
1765                     usbd_transfer_done(xfer, USB_ERR_INVAL);
1766                     USB_BUS_UNLOCK(bus);
1767                     return;
1768           }
1769           /* compute some variables */
1770 
1771           for (x = 0; x != xfer->nframes; x++) {
1772                     /* make a copy of the frlenghts[] */
1773                     xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1774                     /* compute total transfer length */
1775                     xfer->sumlen += xfer->frlengths[x];
1776                     if (xfer->sumlen < xfer->frlengths[x]) {
1777                               /* length wrapped around */
1778                               USB_BUS_LOCK(bus);
1779                               usbd_transfer_done(xfer, USB_ERR_INVAL);
1780                               USB_BUS_UNLOCK(bus);
1781                               return;
1782                     }
1783           }
1784 
1785           /* clear some internal flags */
1786 
1787           xfer->flags_int.short_xfer_ok = 0;
1788           xfer->flags_int.short_frames_ok = 0;
1789 
1790           /* check if this is a control transfer */
1791 
1792           if (xfer->flags_int.control_xfr) {
1793 
1794                     if (usbd_setup_ctrl_transfer(xfer)) {
1795                               USB_BUS_LOCK(bus);
1796                               usbd_transfer_done(xfer, USB_ERR_STALLED);
1797                               USB_BUS_UNLOCK(bus);
1798                               return;
1799                     }
1800           }
1801           /*
1802            * Setup filtered version of some transfer flags,
1803            * in case of data read direction
1804            */
1805           if (USB_GET_DATA_ISREAD(xfer)) {
1806 
1807                     if (xfer->flags.short_frames_ok) {
1808                               xfer->flags_int.short_xfer_ok = 1;
1809                               xfer->flags_int.short_frames_ok = 1;
1810                     } else if (xfer->flags.short_xfer_ok) {
1811                               xfer->flags_int.short_xfer_ok = 1;
1812 
1813                               /* check for control transfer */
1814                               if (xfer->flags_int.control_xfr) {
1815                                         /*
1816                                          * 1) Control transfers do not support
1817                                          * reception of multiple short USB
1818                                          * frames in host mode and device side
1819                                          * mode, with exception of:
1820                                          *
1821                                          * 2) Due to sometimes buggy device
1822                                          * side firmware we need to do a
1823                                          * STATUS stage in case of short
1824                                          * control transfers in USB host mode.
1825                                          * The STATUS stage then becomes the
1826                                          * "alt_next" to the DATA stage.
1827                                          */
1828                                         xfer->flags_int.short_frames_ok = 1;
1829                               }
1830                     }
1831           }
1832           /*
1833            * Check if BUS-DMA support is enabled and try to load virtual
1834            * buffers into DMA, if any:
1835            */
1836 #if USB_HAVE_BUSDMA
1837           if (xfer->flags_int.bdma_enable) {
1838                     /* insert the USB transfer last in the BUS-DMA queue */
1839                     usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1840                     return;
1841           }
1842 #endif
1843           /*
1844            * Enter the USB transfer into the Host Controller or
1845            * Device Controller schedule:
1846            */
1847           usbd_pipe_enter(xfer);
1848 }
1849 
1850 /*------------------------------------------------------------------------*
1851  *        usbd_pipe_enter - factored out code
1852  *------------------------------------------------------------------------*/
1853 void
usbd_pipe_enter(struct usb_xfer * xfer)1854 usbd_pipe_enter(struct usb_xfer *xfer)
1855 {
1856           struct usb_endpoint *ep;
1857 
1858           USB_XFER_LOCK_ASSERT(xfer);
1859 
1860           USB_BUS_LOCK(xfer->xroot->bus);
1861 
1862           ep = xfer->endpoint;
1863 
1864           DPRINTF("enter\n");
1865 
1866           /* the transfer can now be cancelled */
1867           xfer->flags_int.can_cancel_immed = 1;
1868 
1869           /* enter the transfer */
1870           (ep->methods->enter) (xfer);
1871 
1872           /* check for transfer error */
1873           if (xfer->error) {
1874                     /* some error has happened */
1875                     usbd_transfer_done(xfer, 0);
1876                     USB_BUS_UNLOCK(xfer->xroot->bus);
1877                     return;
1878           }
1879 
1880           /* start the transfer */
1881           usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1882           USB_BUS_UNLOCK(xfer->xroot->bus);
1883 }
1884 
1885 /*------------------------------------------------------------------------*
1886  *        usbd_transfer_start - start an USB transfer
1887  *
1888  * NOTE: Calling this function more than one time will only
1889  *       result in a single transfer start, until the USB transfer
1890  *       completes.
1891  *------------------------------------------------------------------------*/
1892 void
usbd_transfer_start(struct usb_xfer * xfer)1893 usbd_transfer_start(struct usb_xfer *xfer)
1894 {
1895           if (xfer == NULL) {
1896                     /* transfer is gone */
1897                     return;
1898           }
1899           USB_XFER_LOCK_ASSERT(xfer);
1900 
1901           /* mark the USB transfer started */
1902 
1903           if (!xfer->flags_int.started) {
1904                     /* lock the BUS lock to avoid races updating flags_int */
1905                     USB_BUS_LOCK(xfer->xroot->bus);
1906                     xfer->flags_int.started = 1;
1907                     USB_BUS_UNLOCK(xfer->xroot->bus);
1908           }
1909           /* check if the USB transfer callback is already transferring */
1910 
1911           if (xfer->flags_int.transferring) {
1912                     return;
1913           }
1914           USB_BUS_LOCK(xfer->xroot->bus);
1915           /* call the USB transfer callback */
1916           usbd_callback_ss_done_defer(xfer);
1917           USB_BUS_UNLOCK(xfer->xroot->bus);
1918 }
1919 
1920 /*------------------------------------------------------------------------*
1921  *        usbd_transfer_stop - stop an USB transfer
1922  *
1923  * NOTE: Calling this function more than one time will only
1924  *       result in a single transfer stop.
1925  * NOTE: When this function returns it is not safe to free nor
1926  *       reuse any DMA buffers. See "usbd_transfer_drain()".
1927  *------------------------------------------------------------------------*/
1928 void
usbd_transfer_stop(struct usb_xfer * xfer)1929 usbd_transfer_stop(struct usb_xfer *xfer)
1930 {
1931           struct usb_endpoint *ep;
1932 
1933           if (xfer == NULL) {
1934                     /* transfer is gone */
1935                     return;
1936           }
1937 #if 0
1938           USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1939 #endif
1940 
1941           /* check if the USB transfer was ever opened */
1942 
1943           if (!xfer->flags_int.open) {
1944                     if (xfer->flags_int.started) {
1945                               /* nothing to do except clearing the "started" flag */
1946                               /* lock the BUS lock to avoid races updating flags_int */
1947                               USB_BUS_LOCK(xfer->xroot->bus);
1948                               xfer->flags_int.started = 0;
1949                               USB_BUS_UNLOCK(xfer->xroot->bus);
1950                     }
1951                     return;
1952           }
1953           /* try to stop the current USB transfer */
1954 
1955           USB_BUS_LOCK(xfer->xroot->bus);
1956           /* override any previous error */
1957           xfer->error = USB_ERR_CANCELLED;
1958 
1959           /*
1960            * Clear "open" and "started" when both private and USB lock
1961            * is locked so that we don't get a race updating "flags_int"
1962            */
1963           xfer->flags_int.open = 0;
1964           xfer->flags_int.started = 0;
1965 
1966           /*
1967            * Check if we can cancel the USB transfer immediately.
1968            */
1969           if (xfer->flags_int.transferring) {
1970                     if (xfer->flags_int.can_cancel_immed &&
1971                         (!xfer->flags_int.did_close)) {
1972                               DPRINTF("close\n");
1973                               /*
1974                                * The following will lead to an USB_ERR_CANCELLED
1975                                * error code being passed to the USB callback.
1976                                */
1977                               (xfer->endpoint->methods->close) (xfer);
1978                               /* only close once */
1979                               xfer->flags_int.did_close = 1;
1980                     } else {
1981                               /* need to wait for the next done callback */
1982                     }
1983           } else {
1984                     DPRINTF("close\n");
1985 
1986                     /* close here and now */
1987                     (xfer->endpoint->methods->close) (xfer);
1988 
1989                     /*
1990                      * Any additional DMA delay is done by
1991                      * "usbd_transfer_unsetup()".
1992                      */
1993 
1994                     /*
1995                      * Special case. Check if we need to restart a blocked
1996                      * endpoint.
1997                      */
1998                     ep = xfer->endpoint;
1999 
2000                     /*
2001                      * If the current USB transfer is completing we need
2002                      * to start the next one:
2003                      */
2004                     if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2005                               usb_command_wrapper(
2006                                   &ep->endpoint_q[xfer->stream_id], NULL);
2007                     }
2008           }
2009 
2010           USB_BUS_UNLOCK(xfer->xroot->bus);
2011 }
2012 
2013 /*------------------------------------------------------------------------*
2014  *        usbd_transfer_pending
2015  *
2016  * This function will check if an USB transfer is pending which is a
2017  * little bit complicated!
2018  * Return values:
2019  * 0: Not pending
2020  * 1: Pending: The USB transfer will receive a callback in the future.
2021  *------------------------------------------------------------------------*/
2022 uint8_t
usbd_transfer_pending(struct usb_xfer * xfer)2023 usbd_transfer_pending(struct usb_xfer *xfer)
2024 {
2025           struct usb_xfer_root *info;
2026           struct usb_xfer_queue *pq;
2027 
2028           if (xfer == NULL) {
2029                     /* transfer is gone */
2030                     return (0);
2031           }
2032 #if 0
2033           USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2034 #endif
2035 
2036           if (xfer->flags_int.transferring) {
2037                     /* trivial case */
2038                     return (1);
2039           }
2040           USB_BUS_LOCK(xfer->xroot->bus);
2041           if (xfer->wait_queue) {
2042                     /* we are waiting on a queue somewhere */
2043                     USB_BUS_UNLOCK(xfer->xroot->bus);
2044                     return (1);
2045           }
2046           info = xfer->xroot;
2047           pq = &info->done_q;
2048 
2049           if (pq->curr == xfer) {
2050                     /* we are currently scheduled for callback */
2051                     USB_BUS_UNLOCK(xfer->xroot->bus);
2052                     return (1);
2053           }
2054           /* we are not pending */
2055           USB_BUS_UNLOCK(xfer->xroot->bus);
2056           return (0);
2057 }
2058 
2059 /*------------------------------------------------------------------------*
2060  *        usbd_transfer_drain
2061  *
2062  * This function will stop the USB transfer and wait for any
2063  * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2064  * are loaded into DMA can safely be freed or reused after that this
2065  * function has returned.
2066  *------------------------------------------------------------------------*/
2067 void
usbd_transfer_drain(struct usb_xfer * xfer)2068 usbd_transfer_drain(struct usb_xfer *xfer)
2069 {
2070 #if 0
2071           WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2072               "usbd_transfer_drain can sleep!");
2073 #endif
2074 
2075           if (xfer == NULL) {
2076                     /* transfer is gone */
2077                     return;
2078           }
2079           USB_XFER_LOCK_ASSERT_NOTOWNED(xfer);
2080           USB_XFER_LOCK(xfer);
2081 
2082           usbd_transfer_stop(xfer);
2083 
2084           /*
2085            * It is allowed that the callback can drop its
2086            * transfer mutex. In that case checking only
2087            * "usbd_transfer_pending()" is not enough to tell if
2088            * the USB transfer is fully drained. We also need to
2089            * check the internal "doing_callback" flag.
2090            */
2091           xfer->flags_int.draining = 1;
2092 
2093           /*
2094            * XXX hack, the wakeup of xfer can race conditions which
2095            *     clear the pending status of the xfer.
2096            */
2097           while (usbd_transfer_pending(xfer) ||
2098               xfer->flags_int.doing_callback) {
2099 
2100                     /*
2101                      * Wait until the current outstanding USB
2102                      * transfer is complete !
2103                      */
2104                     /* cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_lock); */
2105                     lksleep(xfer, xfer->xroot->xfer_lock, 0, "DRAIN", hz);
2106           }
2107           xfer->flags_int.draining = 0;
2108           USB_XFER_UNLOCK(xfer);
2109 }
2110 
2111 struct usb_page_cache *
usbd_xfer_get_frame(struct usb_xfer * xfer,usb_frcount_t frindex)2112 usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2113 {
2114           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2115 
2116           return (&xfer->frbuffers[frindex]);
2117 }
2118 
2119 void *
usbd_xfer_get_frame_buffer(struct usb_xfer * xfer,usb_frcount_t frindex)2120 usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2121 {
2122           struct usb_page_search page_info;
2123 
2124           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2125 
2126           usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2127           return (page_info.buffer);
2128 }
2129 
2130 /*------------------------------------------------------------------------*
2131  *        usbd_xfer_get_fps_shift
2132  *
2133  * The following function is only useful for isochronous transfers. It
2134  * returns how many times the frame execution rate has been shifted
2135  * down.
2136  *
2137  * Return value:
2138  * Success: 0..3
2139  * Failure: 0
2140  *------------------------------------------------------------------------*/
2141 uint8_t
usbd_xfer_get_fps_shift(struct usb_xfer * xfer)2142 usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2143 {
2144           return (xfer->fps_shift);
2145 }
2146 
2147 usb_frlength_t
usbd_xfer_frame_len(struct usb_xfer * xfer,usb_frcount_t frindex)2148 usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2149 {
2150           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2151 
2152           return (xfer->frlengths[frindex]);
2153 }
2154 
2155 /*------------------------------------------------------------------------*
2156  *        usbd_xfer_set_frame_data
2157  *
2158  * This function sets the pointer of the buffer that should
2159  * loaded directly into DMA for the given USB frame. Passing "ptr"
2160  * equal to NULL while the corresponding "frlength" is greater
2161  * than zero gives undefined results!
2162  *------------------------------------------------------------------------*/
2163 void
usbd_xfer_set_frame_data(struct usb_xfer * xfer,usb_frcount_t frindex,void * ptr,usb_frlength_t len)2164 usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2165     void *ptr, usb_frlength_t len)
2166 {
2167           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2168 
2169           /* set virtual address to load and length */
2170           xfer->frbuffers[frindex].buffer = ptr;
2171           usbd_xfer_set_frame_len(xfer, frindex, len);
2172 }
2173 
2174 void
usbd_xfer_frame_data(struct usb_xfer * xfer,usb_frcount_t frindex,void ** ptr,int * len)2175 usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2176     void **ptr, int *len)
2177 {
2178           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2179 
2180           if (ptr != NULL)
2181                     *ptr = xfer->frbuffers[frindex].buffer;
2182           if (len != NULL)
2183                     *len = xfer->frlengths[frindex];
2184 }
2185 
2186 /*------------------------------------------------------------------------*
2187  *        usbd_xfer_old_frame_length
2188  *
2189  * This function returns the framelength of the given frame at the
2190  * time the transfer was submitted. This function can be used to
2191  * compute the starting data pointer of the next isochronous frame
2192  * when an isochronous transfer has completed.
2193  *------------------------------------------------------------------------*/
2194 usb_frlength_t
usbd_xfer_old_frame_length(struct usb_xfer * xfer,usb_frcount_t frindex)2195 usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2196 {
2197           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2198 
2199           return (xfer->frlengths[frindex + xfer->max_frame_count]);
2200 }
2201 
2202 void
usbd_xfer_status(struct usb_xfer * xfer,int * actlen,int * sumlen,int * aframes,int * nframes)2203 usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2204     int *nframes)
2205 {
2206           if (actlen != NULL)
2207                     *actlen = xfer->actlen;
2208           if (sumlen != NULL)
2209                     *sumlen = xfer->sumlen;
2210           if (aframes != NULL)
2211                     *aframes = xfer->aframes;
2212           if (nframes != NULL)
2213                     *nframes = xfer->nframes;
2214 }
2215 
2216 /*------------------------------------------------------------------------*
2217  *        usbd_xfer_set_frame_offset
2218  *
2219  * This function sets the frame data buffer offset relative to the beginning
2220  * of the USB DMA buffer allocated for this USB transfer.
2221  *------------------------------------------------------------------------*/
2222 void
usbd_xfer_set_frame_offset(struct usb_xfer * xfer,usb_frlength_t offset,usb_frcount_t frindex)2223 usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2224     usb_frcount_t frindex)
2225 {
2226           KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2227               "when the USB buffer is external\n"));
2228           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2229 
2230           /* set virtual address to load */
2231           xfer->frbuffers[frindex].buffer =
2232               USB_ADD_BYTES(xfer->local_buffer, offset);
2233 }
2234 
2235 void
usbd_xfer_set_interval(struct usb_xfer * xfer,int i)2236 usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2237 {
2238           xfer->interval = i;
2239 }
2240 
2241 void
usbd_xfer_set_timeout(struct usb_xfer * xfer,int t)2242 usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2243 {
2244           xfer->timeout = t;
2245 }
2246 
2247 void
usbd_xfer_set_frames(struct usb_xfer * xfer,usb_frcount_t n)2248 usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2249 {
2250           xfer->nframes = n;
2251 }
2252 
2253 usb_frcount_t
usbd_xfer_max_frames(struct usb_xfer * xfer)2254 usbd_xfer_max_frames(struct usb_xfer *xfer)
2255 {
2256           return (xfer->max_frame_count);
2257 }
2258 
2259 usb_frlength_t
usbd_xfer_max_len(struct usb_xfer * xfer)2260 usbd_xfer_max_len(struct usb_xfer *xfer)
2261 {
2262           return (xfer->max_data_length);
2263 }
2264 
2265 usb_frlength_t
usbd_xfer_max_framelen(struct usb_xfer * xfer)2266 usbd_xfer_max_framelen(struct usb_xfer *xfer)
2267 {
2268           return (xfer->max_frame_size);
2269 }
2270 
2271 void
usbd_xfer_set_frame_len(struct usb_xfer * xfer,usb_frcount_t frindex,usb_frlength_t len)2272 usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2273     usb_frlength_t len)
2274 {
2275           KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2276 
2277           xfer->frlengths[frindex] = len;
2278 }
2279 
2280 /*------------------------------------------------------------------------*
2281  *        usb_callback_proc - factored out code
2282  *
2283  * This function performs USB callbacks.
2284  *------------------------------------------------------------------------*/
2285 static void
usb_callback_proc(struct usb_proc_msg * _pm)2286 usb_callback_proc(struct usb_proc_msg *_pm)
2287 {
2288           struct usb_done_msg *pm = (void *)_pm;
2289           struct usb_xfer_root *info = pm->xroot;
2290 
2291           /* Change locking order */
2292           USB_BUS_UNLOCK(info->bus);
2293 
2294           /*
2295            * We exploit the fact that the mutex is the same for all
2296            * callbacks that will be called from this thread:
2297            */
2298           lockmgr(info->xfer_lock, LK_EXCLUSIVE);
2299           USB_BUS_LOCK(info->bus);
2300 
2301           /* Continue where we lost track */
2302           usb_command_wrapper(&info->done_q,
2303               info->done_q.curr);
2304 
2305           lockmgr(info->xfer_lock, LK_RELEASE);
2306 }
2307 
2308 /*------------------------------------------------------------------------*
2309  *        usbd_callback_ss_done_defer
2310  *
2311  * This function will defer the start, stop and done callback to the
2312  * correct thread.
2313  *------------------------------------------------------------------------*/
2314 static void
usbd_callback_ss_done_defer(struct usb_xfer * xfer)2315 usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2316 {
2317           struct usb_xfer_root *info = xfer->xroot;
2318           struct usb_xfer_queue *pq = &info->done_q;
2319 
2320           USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2321 
2322           if (pq->curr != xfer) {
2323                     usbd_transfer_enqueue(pq, xfer);
2324           }
2325           if (!pq->recurse_1) {
2326 
2327                     /*
2328                    * We have to postpone the callback due to the fact we
2329                    * will have a Lock Order Reversal, LOR, if we try to
2330                    * proceed !
2331                    */
2332                     if (usb_proc_msignal(info->done_p,
2333                         &info->done_m[0], &info->done_m[1])) {
2334                               /* ignore */
2335                     }
2336           } else {
2337                     /* clear second recurse flag */
2338                     pq->recurse_2 = 0;
2339           }
2340           return;
2341 
2342 }
2343 
2344 /*------------------------------------------------------------------------*
2345  *        usbd_callback_wrapper
2346  *
2347  * This is a wrapper for USB callbacks. This wrapper does some
2348  * auto-magic things like figuring out if we can call the callback
2349  * directly from the current context or if we need to wakeup the
2350  * interrupt process.
2351  *------------------------------------------------------------------------*/
2352 static void
usbd_callback_wrapper(struct usb_xfer_queue * pq)2353 usbd_callback_wrapper(struct usb_xfer_queue *pq)
2354 {
2355           struct usb_xfer *xfer = pq->curr;
2356           struct usb_xfer_root *info = xfer->xroot;
2357 
2358           USB_BUS_LOCK_ASSERT(info->bus);
2359           if (!lockowned(info->xfer_lock)) {
2360                     /*
2361                      * Cases that end up here:
2362                      *
2363                      * 5) HW interrupt done callback or other source.
2364                      */
2365                     DPRINTFN(3, "case 5\n");
2366 
2367                     /*
2368                    * We have to postpone the callback due to the fact we
2369                    * will have a Lock Order Reversal, LOR, if we try to
2370                    * proceed !
2371                    */
2372                     if (usb_proc_msignal(info->done_p,
2373                         &info->done_m[0], &info->done_m[1])) {
2374                               /* ignore */
2375                     }
2376                     return;
2377           }
2378           /*
2379            * Cases that end up here:
2380            *
2381            * 1) We are starting a transfer
2382            * 2) We are prematurely calling back a transfer
2383            * 3) We are stopping a transfer
2384            * 4) We are doing an ordinary callback
2385            */
2386           DPRINTFN(3, "case 1-4\n");
2387           /* get next USB transfer in the queue */
2388           info->done_q.curr = NULL;
2389 
2390           /* set flag in case of drain */
2391           xfer->flags_int.doing_callback = 1;
2392 
2393           USB_BUS_UNLOCK(info->bus);
2394           USB_BUS_LOCK_ASSERT_NOTOWNED(info->bus);
2395 
2396           /* set correct USB state for callback */
2397           if (!xfer->flags_int.transferring) {
2398                     xfer->usb_state = USB_ST_SETUP;
2399                     if (!xfer->flags_int.started) {
2400                               /* we got stopped before we even got started */
2401                               USB_BUS_LOCK(info->bus);
2402                               goto done;
2403                     }
2404           } else {
2405 
2406                     if (usbd_callback_wrapper_sub(xfer)) {
2407                               /* the callback has been deferred */
2408                               USB_BUS_LOCK(info->bus);
2409                               goto done;
2410                     }
2411 #if USB_HAVE_POWERD
2412                     /* decrement power reference */
2413                     usbd_transfer_power_ref(xfer, -1);
2414 #endif
2415                     xfer->flags_int.transferring = 0;
2416 
2417                     if (xfer->error) {
2418                               xfer->usb_state = USB_ST_ERROR;
2419                     } else {
2420                               /* set transferred state */
2421                               xfer->usb_state = USB_ST_TRANSFERRED;
2422 #if USB_HAVE_BUSDMA
2423                               /* sync DMA memory, if any */
2424                               if (xfer->flags_int.bdma_enable &&
2425                                   (!xfer->flags_int.bdma_no_post_sync)) {
2426                                         usb_bdma_post_sync(xfer);
2427                               }
2428 #endif
2429                     }
2430           }
2431 
2432 #if USB_HAVE_PF
2433           if (xfer->usb_state != USB_ST_SETUP)
2434                     usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2435 #endif
2436           /* call processing routine */
2437           (xfer->callback) (xfer, xfer->error);
2438 
2439           /* pickup the USB mutex again */
2440           USB_BUS_LOCK(info->bus);
2441 
2442           /*
2443            * Check if we got started after that we got cancelled, but
2444            * before we managed to do the callback.
2445            */
2446           if ((!xfer->flags_int.open) &&
2447               (xfer->flags_int.started) &&
2448               (xfer->usb_state == USB_ST_ERROR)) {
2449                     /* clear flag in case of drain */
2450                     xfer->flags_int.doing_callback = 0;
2451                     /* try to loop, but not recursivly */
2452                     usb_command_wrapper(&info->done_q, xfer);
2453                     return;
2454           }
2455 
2456 done:
2457           /* clear flag in case of drain */
2458           xfer->flags_int.doing_callback = 0;
2459 
2460           /*
2461            * Check if we are draining.
2462            */
2463           if (xfer->flags_int.draining &&
2464               (!xfer->flags_int.transferring)) {
2465                     /* "usbd_transfer_drain()" is waiting for end of transfer */
2466                     xfer->flags_int.draining = 0;
2467                     /* cv_broadcast(&info->cv_drain); */
2468                     wakeup(xfer);
2469           }
2470 
2471           /* do the next callback, if any */
2472           usb_command_wrapper(&info->done_q,
2473               info->done_q.curr);
2474 }
2475 
2476 /*------------------------------------------------------------------------*
2477  *        usb_dma_delay_done_cb
2478  *
2479  * This function is called when the DMA delay has been exectuded, and
2480  * will make sure that the callback is called to complete the USB
2481  * transfer. This code path is ususally only used when there is an USB
2482  * error like USB_ERR_CANCELLED.
2483  *------------------------------------------------------------------------*/
2484 void
usb_dma_delay_done_cb(struct usb_xfer * xfer)2485 usb_dma_delay_done_cb(struct usb_xfer *xfer)
2486 {
2487           USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2488 
2489           DPRINTFN(3, "Completed %p\n", xfer);
2490 
2491           /* queue callback for execution, again */
2492           usbd_transfer_done(xfer, 0);
2493 }
2494 
2495 /*------------------------------------------------------------------------*
2496  *        usbd_transfer_dequeue
2497  *
2498  *  - This function is used to remove an USB transfer from a USB
2499  *  transfer queue.
2500  *
2501  *  - This function can be called multiple times in a row.
2502  *------------------------------------------------------------------------*/
2503 void
usbd_transfer_dequeue(struct usb_xfer * xfer)2504 usbd_transfer_dequeue(struct usb_xfer *xfer)
2505 {
2506           struct usb_xfer_queue *pq;
2507 
2508           pq = xfer->wait_queue;
2509           if (pq) {
2510                     TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2511                     xfer->wait_queue = NULL;
2512           }
2513 }
2514 
2515 /*------------------------------------------------------------------------*
2516  *        usbd_transfer_enqueue
2517  *
2518  *  - This function is used to insert an USB transfer into a USB *
2519  *  transfer queue.
2520  *
2521  *  - This function can be called multiple times in a row.
2522  *------------------------------------------------------------------------*/
2523 void
usbd_transfer_enqueue(struct usb_xfer_queue * pq,struct usb_xfer * xfer)2524 usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2525 {
2526           /*
2527            * Insert the USB transfer into the queue, if it is not
2528            * already on a USB transfer queue:
2529            */
2530           /* mpf ?
2531           KKASSERT(xfer->wait_queue == NULL);
2532           */
2533           if (xfer->wait_queue == NULL) {
2534                     xfer->wait_queue = pq;
2535                     TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2536           }
2537 }
2538 
2539 /*------------------------------------------------------------------------*
2540  *        usbd_transfer_done
2541  *
2542  *  - This function is used to remove an USB transfer from the busdma,
2543  *  pipe or interrupt queue.
2544  *
2545  *  - This function is used to queue the USB transfer on the done
2546  *  queue.
2547  *
2548  *  - This function is used to stop any USB transfer timeouts.
2549  *------------------------------------------------------------------------*/
2550 void
usbd_transfer_done(struct usb_xfer * xfer,usb_error_t error)2551 usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2552 {
2553           struct usb_xfer_root *info = xfer->xroot;
2554 
2555           USB_BUS_LOCK_ASSERT(info->bus);
2556 
2557           DPRINTF("err=%s\n", usbd_errstr(error));
2558 
2559           /*
2560            * If we are not transferring then just return.
2561            * This can happen during transfer cancel.
2562            */
2563           if (!xfer->flags_int.transferring) {
2564                     DPRINTF("not transferring\n");
2565                     /* end of control transfer, if any */
2566                     xfer->flags_int.control_act = 0;
2567                     return;
2568           }
2569           /* only set transfer error, if not already set */
2570           if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2571                     xfer->error = error;
2572 
2573           /* stop any callouts */
2574           usb_callout_stop(&xfer->timeout_handle);
2575 
2576           /*
2577            * If we are waiting on a queue, just remove the USB transfer
2578            * from the queue, if any. We should have the required locks
2579            * locked to do the remove when this function is called.
2580            */
2581           usbd_transfer_dequeue(xfer);
2582 
2583 #if USB_HAVE_BUSDMA
2584           if (lockowned(xfer->xroot->xfer_lock)) {
2585                     struct usb_xfer_queue *pq;
2586 
2587                     /*
2588                      * If the private USB lock is not locked, then we assume
2589                      * that the BUS-DMA load stage has been passed:
2590                      */
2591                     pq = &info->dma_q;
2592 
2593                     if (pq->curr == xfer) {
2594                               /* start the next BUS-DMA load, if any */
2595                               usb_command_wrapper(pq, NULL);
2596                     }
2597           }
2598 #endif
2599           /* keep some statistics */
2600           if (xfer->error) {
2601                     info->bus->stats_err.uds_requests
2602                         [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2603           } else {
2604                     info->bus->stats_ok.uds_requests
2605                         [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2606           }
2607 
2608           /* call the USB transfer callback */
2609           usbd_callback_ss_done_defer(xfer);
2610 }
2611 
2612 /*------------------------------------------------------------------------*
2613  *        usbd_transfer_start_cb
2614  *
2615  * This function is called to start the USB transfer when
2616  * "xfer->interval" is greater than zero, and and the endpoint type is
2617  * BULK or CONTROL.
2618  *------------------------------------------------------------------------*/
2619 static void
usbd_transfer_start_cb(void * arg)2620 usbd_transfer_start_cb(void *arg)
2621 {
2622           struct usb_xfer *xfer = arg;
2623           struct usb_endpoint *ep = xfer->endpoint;
2624 
2625           USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2626 
2627           DPRINTF("start\n");
2628 
2629 #if USB_HAVE_PF
2630           usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2631 #endif
2632 
2633           /* the transfer can now be cancelled */
2634           xfer->flags_int.can_cancel_immed = 1;
2635 
2636           /* start USB transfer, if no error */
2637           if (xfer->error == 0)
2638                     (ep->methods->start) (xfer);
2639 
2640           /* check for transfer error */
2641           if (xfer->error) {
2642                     /* some error has happened */
2643                     usbd_transfer_done(xfer, 0);
2644           }
2645 }
2646 
2647 /*------------------------------------------------------------------------*
2648  *        usbd_xfer_set_stall
2649  *
2650  * This function is used to set the stall flag outside the
2651  * callback. This function is NULL safe.
2652  *------------------------------------------------------------------------*/
2653 void
usbd_xfer_set_stall(struct usb_xfer * xfer)2654 usbd_xfer_set_stall(struct usb_xfer *xfer)
2655 {
2656           if (xfer == NULL) {
2657                     /* tearing down */
2658                     return;
2659           }
2660           USB_XFER_LOCK_ASSERT(xfer);
2661 
2662           /* avoid any races by locking the USB mutex */
2663           USB_BUS_LOCK(xfer->xroot->bus);
2664           xfer->flags.stall_pipe = 1;
2665           USB_BUS_UNLOCK(xfer->xroot->bus);
2666 }
2667 
2668 int
usbd_xfer_is_stalled(struct usb_xfer * xfer)2669 usbd_xfer_is_stalled(struct usb_xfer *xfer)
2670 {
2671           return (xfer->endpoint->is_stalled);
2672 }
2673 
2674 /*------------------------------------------------------------------------*
2675  *        usbd_transfer_clear_stall
2676  *
2677  * This function is used to clear the stall flag outside the
2678  * callback. This function is NULL safe.
2679  *------------------------------------------------------------------------*/
2680 void
usbd_transfer_clear_stall(struct usb_xfer * xfer)2681 usbd_transfer_clear_stall(struct usb_xfer *xfer)
2682 {
2683           if (xfer == NULL) {
2684                     /* tearing down */
2685                     return;
2686           }
2687           USB_XFER_LOCK_ASSERT(xfer);
2688 
2689           /* avoid any races by locking the USB mutex */
2690           USB_BUS_LOCK(xfer->xroot->bus);
2691 
2692           xfer->flags.stall_pipe = 0;
2693 
2694           USB_BUS_UNLOCK(xfer->xroot->bus);
2695 }
2696 
2697 /*------------------------------------------------------------------------*
2698  *        usbd_pipe_start
2699  *
2700  * This function is used to add an USB transfer to the pipe transfer list.
2701  *------------------------------------------------------------------------*/
2702 void
usbd_pipe_start(struct usb_xfer_queue * pq)2703 usbd_pipe_start(struct usb_xfer_queue *pq)
2704 {
2705           struct usb_endpoint *ep;
2706           struct usb_xfer *xfer;
2707           uint8_t type;
2708 
2709           xfer = pq->curr;
2710           ep = xfer->endpoint;
2711 
2712           USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2713 
2714           /*
2715            * If the endpoint is already stalled we do nothing !
2716            */
2717           if (ep->is_stalled) {
2718                     return;
2719           }
2720           /*
2721            * Check if we are supposed to stall the endpoint:
2722            */
2723           if (xfer->flags.stall_pipe) {
2724                     struct usb_device *udev;
2725                     struct usb_xfer_root *info;
2726 
2727                     /* clear stall command */
2728                     xfer->flags.stall_pipe = 0;
2729 
2730                     /* get pointer to USB device */
2731                     info = xfer->xroot;
2732                     udev = info->udev;
2733 
2734                     /*
2735                      * Only stall BULK and INTERRUPT endpoints.
2736                      */
2737                     type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2738                     if ((type == UE_BULK) ||
2739                         (type == UE_INTERRUPT)) {
2740                               uint8_t did_stall;
2741 
2742                               did_stall = 1;
2743 
2744                               if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2745                                         (udev->bus->methods->set_stall) (
2746                                             udev, ep, &did_stall);
2747                               } else if (udev->ctrl_xfer[1]) {
2748                                         info = udev->ctrl_xfer[1]->xroot;
2749                                         usb_proc_msignal(
2750                                             USB_BUS_NON_GIANT_PROC(info->bus),
2751                                             &udev->cs_msg[0], &udev->cs_msg[1]);
2752                               } else {
2753                                         /* should not happen */
2754                                         DPRINTFN(0, "No stall handler\n");
2755                               }
2756                               /*
2757                                * Check if we should stall. Some USB hardware
2758                                * handles set- and clear-stall in hardware.
2759                                */
2760                               if (did_stall) {
2761                                         /*
2762                                          * The transfer will be continued when
2763                                          * the clear-stall control endpoint
2764                                          * message is received.
2765                                          */
2766                                         ep->is_stalled = 1;
2767                                         return;
2768                               }
2769                     } else if (type == UE_ISOCHRONOUS) {
2770 
2771                               /*
2772                                * Make sure any FIFO overflow or other FIFO
2773                                * error conditions go away by resetting the
2774                                * endpoint FIFO through the clear stall
2775                                * method.
2776                                */
2777                               if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2778                                         (udev->bus->methods->clear_stall) (udev, ep);
2779                               }
2780                     }
2781           }
2782           /* Set or clear stall complete - special case */
2783           if (xfer->nframes == 0) {
2784                     /* we are complete */
2785                     xfer->aframes = 0;
2786                     usbd_transfer_done(xfer, 0);
2787                     return;
2788           }
2789           /*
2790            * Handled cases:
2791            *
2792            * 1) Start the first transfer queued.
2793            *
2794            * 2) Re-start the current USB transfer.
2795            */
2796           /*
2797            * Check if there should be any
2798            * pre transfer start delay:
2799            */
2800           if (xfer->interval > 0) {
2801                     type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2802                     if ((type == UE_BULK) ||
2803                         (type == UE_CONTROL)) {
2804                               usbd_transfer_timeout_ms(xfer,
2805                                   &usbd_transfer_start_cb,
2806                                   xfer->interval);
2807                               return;
2808                     }
2809           }
2810           DPRINTF("start\n");
2811 
2812 #if USB_HAVE_PF
2813           usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2814 #endif
2815           /* the transfer can now be cancelled */
2816           xfer->flags_int.can_cancel_immed = 1;
2817 
2818           /* start USB transfer, if no error */
2819           if (xfer->error == 0)
2820                     (ep->methods->start) (xfer);
2821 
2822           /* check for transfer error */
2823           if (xfer->error) {
2824                     /* some error has happened */
2825                     usbd_transfer_done(xfer, 0);
2826           }
2827 }
2828 
2829 /*------------------------------------------------------------------------*
2830  *        usbd_transfer_timeout_ms
2831  *
2832  * This function is used to setup a timeout on the given USB
2833  * transfer. If the timeout has been deferred the callback given by
2834  * "cb" will get called after "ms" milliseconds.
2835  *------------------------------------------------------------------------*/
2836 void
usbd_transfer_timeout_ms(struct usb_xfer * xfer,void (* cb)(void * arg),usb_timeout_t ms)2837 usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2838     void (*cb) (void *arg), usb_timeout_t ms)
2839 {
2840           USB_BUS_LOCK_ASSERT(xfer->xroot->bus);
2841 
2842           /* defer delay */
2843           usb_callout_reset(&xfer->timeout_handle,
2844               USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2845 }
2846 
2847 /*------------------------------------------------------------------------*
2848  *        usbd_callback_wrapper_sub
2849  *
2850  *  - This function will update variables in an USB transfer after
2851  *  that the USB transfer is complete.
2852  *
2853  *  - This function is used to start the next USB transfer on the
2854  *  ep transfer queue, if any.
2855  *
2856  * NOTE: In some special cases the USB transfer will not be removed from
2857  * the pipe queue, but remain first. To enforce USB transfer removal call
2858  * this function passing the error code "USB_ERR_CANCELLED".
2859  *
2860  * Return values:
2861  * 0: Success.
2862  * Else: The callback has been deferred.
2863  *------------------------------------------------------------------------*/
2864 static uint8_t
usbd_callback_wrapper_sub(struct usb_xfer * xfer)2865 usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2866 {
2867           struct usb_endpoint *ep;
2868           struct usb_bus *bus;
2869           usb_frcount_t x;
2870 
2871           bus = xfer->xroot->bus;
2872 
2873           if ((!xfer->flags_int.open) &&
2874               (!xfer->flags_int.did_close)) {
2875                     DPRINTF("close\n");
2876                     USB_BUS_LOCK(bus);
2877                     (xfer->endpoint->methods->close) (xfer);
2878                     USB_BUS_UNLOCK(bus);
2879                     /* only close once */
2880                     xfer->flags_int.did_close = 1;
2881                     return (1);                   /* wait for new callback */
2882           }
2883           /*
2884            * If we have a non-hardware induced error we
2885            * need to do the DMA delay!
2886            */
2887           if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2888               (xfer->error == USB_ERR_CANCELLED ||
2889               xfer->error == USB_ERR_TIMEOUT ||
2890               bus->methods->start_dma_delay != NULL)) {
2891 
2892                     usb_timeout_t temp;
2893 
2894                     /* only delay once */
2895                     xfer->flags_int.did_dma_delay = 1;
2896 
2897                     /* we can not cancel this delay */
2898                     xfer->flags_int.can_cancel_immed = 0;
2899 
2900                     temp = usbd_get_dma_delay(xfer->xroot->udev);
2901 
2902                     DPRINTFN(3, "DMA delay, %u ms, "
2903                         "on %p\n", temp, xfer);
2904 
2905                     if (temp != 0) {
2906                               USB_BUS_LOCK(bus);
2907                               /*
2908                                * Some hardware solutions have dedicated
2909                                * events when it is safe to free DMA'ed
2910                                * memory. For the other hardware platforms we
2911                                * use a static delay.
2912                                */
2913                               if (bus->methods->start_dma_delay != NULL) {
2914                                         (bus->methods->start_dma_delay) (xfer);
2915                               } else {
2916                                         usbd_transfer_timeout_ms(xfer,
2917                                             (void (*)(void *))&usb_dma_delay_done_cb,
2918                                             temp);
2919                               }
2920                               USB_BUS_UNLOCK(bus);
2921                               return (1);         /* wait for new callback */
2922                     }
2923           }
2924           /* check actual number of frames */
2925           if (xfer->aframes > xfer->nframes) {
2926                     if (xfer->error == 0) {
2927                               panic("%s: actual number of frames, %d, is "
2928                                   "greater than initial number of frames, %d\n",
2929                                   __func__, xfer->aframes, xfer->nframes);
2930                     } else {
2931                               /* just set some valid value */
2932                               xfer->aframes = xfer->nframes;
2933                     }
2934           }
2935           /* compute actual length */
2936           xfer->actlen = 0;
2937 
2938           for (x = 0; x != xfer->aframes; x++) {
2939                     xfer->actlen += xfer->frlengths[x];
2940           }
2941 
2942           /*
2943            * Frames that were not transferred get zero actual length in
2944            * case the USB device driver does not check the actual number
2945            * of frames transferred, "xfer->aframes":
2946            */
2947           for (; x < xfer->nframes; x++) {
2948                     usbd_xfer_set_frame_len(xfer, x, 0);
2949           }
2950 
2951           /* check actual length */
2952           if (xfer->actlen > xfer->sumlen) {
2953                     if (xfer->error == 0) {
2954                               panic("%s: actual length, %d, is greater than "
2955                                   "initial length, %d\n",
2956                                   __func__, xfer->actlen, xfer->sumlen);
2957                     } else {
2958                               /* just set some valid value */
2959                               xfer->actlen = xfer->sumlen;
2960                     }
2961           }
2962           DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
2963               xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
2964               xfer->aframes, xfer->nframes);
2965 
2966           if (xfer->error) {
2967                     /* end of control transfer, if any */
2968                     xfer->flags_int.control_act = 0;
2969 
2970                     /* check if we should block the execution queue */
2971                     if ((xfer->error != USB_ERR_CANCELLED) &&
2972                         (xfer->flags.pipe_bof)) {
2973                               DPRINTFN(2, "xfer=%p: Block On Failure "
2974                                   "on endpoint=%p\n", xfer, xfer->endpoint);
2975                               goto done;
2976                     }
2977           } else {
2978                     /* check for short transfers */
2979                     if (xfer->actlen < xfer->sumlen) {
2980 
2981                               /* end of control transfer, if any */
2982                               xfer->flags_int.control_act = 0;
2983 
2984                               if (!xfer->flags_int.short_xfer_ok) {
2985                                         xfer->error = USB_ERR_SHORT_XFER;
2986                                         if (xfer->flags.pipe_bof) {
2987                                                   DPRINTFN(2, "xfer=%p: Block On Failure on "
2988                                                       "Short Transfer on endpoint %p.\n",
2989                                                       xfer, xfer->endpoint);
2990                                                   goto done;
2991                                         }
2992                               }
2993                     } else {
2994                               /*
2995                                * Check if we are in the middle of a
2996                                * control transfer:
2997                                */
2998                               if (xfer->flags_int.control_act) {
2999                                         DPRINTFN(5, "xfer=%p: Control transfer "
3000                                             "active on endpoint=%p\n", xfer, xfer->endpoint);
3001                                         goto done;
3002                               }
3003                     }
3004           }
3005 
3006           ep = xfer->endpoint;
3007 
3008           /*
3009            * If the current USB transfer is completing we need to start the
3010            * next one:
3011            */
3012           USB_BUS_LOCK(bus);
3013           if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
3014                     usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
3015 
3016                     if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
3017                         TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
3018                               /* there is another USB transfer waiting */
3019                     } else {
3020                               /* this is the last USB transfer */
3021                               /* clear isochronous sync flag */
3022                               xfer->endpoint->is_synced = 0;
3023                     }
3024           }
3025           USB_BUS_UNLOCK(bus);
3026 done:
3027           return (0);
3028 }
3029 
3030 /*------------------------------------------------------------------------*
3031  *        usb_command_wrapper
3032  *
3033  * This function is used to execute commands non-recursivly on an USB
3034  * transfer.
3035  *------------------------------------------------------------------------*/
3036 void
usb_command_wrapper(struct usb_xfer_queue * pq,struct usb_xfer * xfer)3037 usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3038 {
3039           if (xfer) {
3040                     /*
3041                      * If the transfer is not already processing,
3042                      * queue it!
3043                      */
3044                     if (pq->curr != xfer) {
3045                               usbd_transfer_enqueue(pq, xfer);
3046                               if (pq->curr != NULL) {
3047                                         /* something is already processing */
3048                                         DPRINTFN(6, "busy %p\n", pq->curr);
3049                                         return;
3050                               }
3051                     }
3052           } else {
3053                     /* Get next element in queue */
3054                     pq->curr = NULL;
3055           }
3056 
3057           if (!pq->recurse_1) {
3058 
3059                     do {
3060 
3061                               /* set both recurse flags */
3062                               pq->recurse_1 = 1;
3063                               pq->recurse_2 = 1;
3064 
3065                               if (pq->curr == NULL) {
3066                                         xfer = TAILQ_FIRST(&pq->head);
3067                                         if (xfer) {
3068                                                   TAILQ_REMOVE(&pq->head, xfer,
3069                                                       wait_entry);
3070                                                   xfer->wait_queue = NULL;
3071                                                   pq->curr = xfer;
3072                                         } else {
3073                                                   break;
3074                                         }
3075                               }
3076                               DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3077                               (pq->command) (pq);
3078                               DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3079 
3080                     } while (!pq->recurse_2);
3081 
3082                     /* clear first recurse flag */
3083                     pq->recurse_1 = 0;
3084 
3085           } else {
3086                     /* clear second recurse flag */
3087                     pq->recurse_2 = 0;
3088           }
3089 }
3090 
3091 /*------------------------------------------------------------------------*
3092  *        usbd_ctrl_transfer_setup
3093  *
3094  * This function is used to setup the default USB control endpoint
3095  * transfer.
3096  *------------------------------------------------------------------------*/
3097 void
usbd_ctrl_transfer_setup(struct usb_device * udev)3098 usbd_ctrl_transfer_setup(struct usb_device *udev)
3099 {
3100           struct usb_xfer *xfer;
3101           uint8_t no_resetup;
3102           uint8_t iface_index;
3103 
3104           /* check for root HUB */
3105           if (udev->parent_hub == NULL)
3106                     return;
3107 repeat:
3108 
3109           xfer = udev->ctrl_xfer[0];
3110           if (xfer) {
3111                     USB_XFER_LOCK(xfer);
3112                     no_resetup =
3113                         ((xfer->address == udev->address) &&
3114                         (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3115                         udev->ddesc.bMaxPacketSize));
3116                     if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3117                               if (no_resetup) {
3118                                         /*
3119                                          * NOTE: checking "xfer->address" and
3120                                          * starting the USB transfer must be
3121                                          * atomic!
3122                                          */
3123                                         usbd_transfer_start(xfer);
3124                               }
3125                     }
3126                     USB_XFER_UNLOCK(xfer);
3127           } else {
3128                     no_resetup = 0;
3129           }
3130 
3131           if (no_resetup) {
3132                     /*
3133                    * All parameters are exactly the same like before.
3134                    * Just return.
3135                    */
3136                     return;
3137           }
3138           /*
3139            * Update wMaxPacketSize for the default control endpoint:
3140            */
3141           udev->ctrl_ep_desc.wMaxPacketSize[0] =
3142               udev->ddesc.bMaxPacketSize;
3143 
3144           /*
3145            * Unsetup any existing USB transfer:
3146            */
3147           usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3148 
3149           /*
3150            * Reset clear stall error counter.
3151            */
3152           udev->clear_stall_errors = 0;
3153 
3154           /*
3155            * Try to setup a new USB transfer for the
3156            * default control endpoint:
3157            */
3158           iface_index = 0;
3159           if (usbd_transfer_setup(udev, &iface_index,
3160               udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3161               &udev->device_lock)) {
3162                     DPRINTFN(0, "could not setup default "
3163                         "USB transfer\n");
3164           } else {
3165                     goto repeat;
3166           }
3167 }
3168 
3169 /*------------------------------------------------------------------------*
3170  *        usbd_clear_data_toggle - factored out code
3171  *
3172  * NOTE: the intention of this function is not to reset the hardware
3173  * data toggle.
3174  *------------------------------------------------------------------------*/
3175 void
usbd_clear_stall_locked(struct usb_device * udev,struct usb_endpoint * ep)3176 usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3177 {
3178           USB_BUS_LOCK_ASSERT(udev->bus);
3179 
3180           /* check that we have a valid case */
3181           if (udev->flags.usb_mode == USB_MODE_HOST &&
3182               udev->parent_hub != NULL &&
3183               udev->bus->methods->clear_stall != NULL &&
3184               ep->methods != NULL) {
3185                     (udev->bus->methods->clear_stall) (udev, ep);
3186           }
3187 }
3188 
3189 /*------------------------------------------------------------------------*
3190  *        usbd_clear_data_toggle - factored out code
3191  *
3192  * NOTE: the intention of this function is not to reset the hardware
3193  * data toggle on the USB device side.
3194  *------------------------------------------------------------------------*/
3195 void
usbd_clear_data_toggle(struct usb_device * udev,struct usb_endpoint * ep)3196 usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3197 {
3198           DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3199 
3200           USB_BUS_LOCK(udev->bus);
3201           ep->toggle_next = 0;
3202           /* some hardware needs a callback to clear the data toggle */
3203           usbd_clear_stall_locked(udev, ep);
3204           USB_BUS_UNLOCK(udev->bus);
3205 }
3206 
3207 /*------------------------------------------------------------------------*
3208  *        usbd_clear_stall_callback - factored out clear stall callback
3209  *
3210  * Input parameters:
3211  *  xfer1: Clear Stall Control Transfer
3212  *  xfer2: Stalled USB Transfer
3213  *
3214  * This function is NULL safe.
3215  *
3216  * Return values:
3217  *   0: In progress
3218  *   Else: Finished
3219  *
3220  * Clear stall config example:
3221  *
3222  * static const struct usb_config my_clearstall =  {
3223  *        .type = UE_CONTROL,
3224  *        .endpoint = 0,
3225  *        .direction = UE_DIR_ANY,
3226  *        .interval = 50, //50 milliseconds
3227  *        .bufsize = sizeof(struct usb_device_request),
3228  *        .timeout = 1000, //1.000 seconds
3229  *        .callback = &my_clear_stall_callback, // **
3230  *        .usb_mode = USB_MODE_HOST,
3231  * };
3232  *
3233  * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3234  * passing the correct parameters.
3235  *------------------------------------------------------------------------*/
3236 uint8_t
usbd_clear_stall_callback(struct usb_xfer * xfer1,struct usb_xfer * xfer2)3237 usbd_clear_stall_callback(struct usb_xfer *xfer1,
3238     struct usb_xfer *xfer2)
3239 {
3240           struct usb_device_request req;
3241 
3242           if (xfer2 == NULL) {
3243                     /* looks like we are tearing down */
3244                     DPRINTF("NULL input parameter\n");
3245                     return (0);
3246           }
3247           USB_XFER_LOCK_ASSERT(xfer1);
3248           USB_XFER_LOCK_ASSERT(xfer2);
3249 
3250           switch (USB_GET_STATE(xfer1)) {
3251           case USB_ST_SETUP:
3252 
3253                     /*
3254                      * pre-clear the data toggle to DATA0 ("umass.c" and
3255                      * "ata-usb.c" depends on this)
3256                      */
3257 
3258                     usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3259 
3260                     /* setup a clear-stall packet */
3261 
3262                     req.bmRequestType = UT_WRITE_ENDPOINT;
3263                     req.bRequest = UR_CLEAR_FEATURE;
3264                     USETW(req.wValue, UF_ENDPOINT_HALT);
3265                     req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3266                     req.wIndex[1] = 0;
3267                     USETW(req.wLength, 0);
3268 
3269                     /*
3270                      * "usbd_transfer_setup_sub()" will ensure that
3271                      * we have sufficient room in the buffer for
3272                      * the request structure!
3273                      */
3274 
3275                     /* copy in the transfer */
3276 
3277                     usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3278 
3279                     /* set length */
3280                     xfer1->frlengths[0] = sizeof(req);
3281                     xfer1->nframes = 1;
3282 
3283                     usbd_transfer_submit(xfer1);
3284                     return (0);
3285 
3286           case USB_ST_TRANSFERRED:
3287                     break;
3288 
3289           default:                      /* Error */
3290                     if (xfer1->error == USB_ERR_CANCELLED) {
3291                               return (0);
3292                     }
3293                     break;
3294           }
3295           return (1);                             /* Clear Stall Finished */
3296 }
3297 
3298 /*------------------------------------------------------------------------*
3299  *        usbd_transfer_poll
3300  *
3301  * The following function gets called from the USB keyboard driver and
3302  * UMASS when the system has paniced.
3303  *
3304  * NOTE: It is currently not possible to resume normal operation on
3305  * the USB controller which has been polled, due to clearing of the
3306  * "up_dsleep" and "up_msleep" flags.
3307  *------------------------------------------------------------------------*/
3308 void
usbd_transfer_poll(struct usb_xfer ** ppxfer,uint16_t max)3309 usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3310 {
3311           struct usb_xfer *xfer;
3312           struct usb_xfer_root *xroot;
3313           struct usb_device *udev;
3314           struct usb_proc_msg *pm;
3315           uint16_t n;
3316           uint16_t drop_bus;
3317           uint16_t drop_xfer;
3318 
3319           for (n = 0; n != max; n++) {
3320                     /* Extra checks to avoid panic */
3321                     xfer = ppxfer[n];
3322                     if (xfer == NULL)
3323                               continue; /* no USB transfer */
3324                     xroot = xfer->xroot;
3325                     if (xroot == NULL)
3326                               continue; /* no USB root */
3327                     udev = xroot->udev;
3328                     if (udev == NULL)
3329                               continue; /* no USB device */
3330                     if (udev->bus == NULL)
3331                               continue; /* no BUS structure */
3332                     if (udev->bus->methods == NULL)
3333                               continue; /* no BUS methods */
3334                     if (udev->bus->methods->xfer_poll == NULL)
3335                               continue; /* no poll method */
3336 
3337                     /* make sure that the BUS mutex is not locked */
3338                     drop_bus = 0;
3339                     while (lockowned(&xroot->udev->bus->bus_lock)) {
3340                               lockmgr(&xroot->udev->bus->bus_lock, LK_RELEASE);
3341                               drop_bus++;
3342                     }
3343 
3344                     /* make sure that the transfer mutex is not locked */
3345                     drop_xfer = 0;
3346                     while (lockowned(xroot->xfer_lock)) {
3347                               lockmgr(xroot->xfer_lock, LK_RELEASE);
3348                               drop_xfer++;
3349                     }
3350 
3351                     /* Make sure cv_signal() and cv_broadcast() is not called */
3352                     USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3353                     USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3354                     USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3355                     USB_BUS_NON_GIANT_PROC(udev->bus)->up_msleep = 0;
3356 
3357                     /* poll USB hardware */
3358                     (udev->bus->methods->xfer_poll) (udev->bus);
3359 
3360                     USB_BUS_LOCK(xroot->bus);
3361 
3362                     /* check for clear stall */
3363                     if (udev->ctrl_xfer[1] != NULL) {
3364 
3365                               /* poll clear stall start */
3366                               pm = &udev->cs_msg[0].hdr;
3367                               (pm->pm_callback) (pm);
3368                               /* poll clear stall done thread */
3369                               pm = &udev->ctrl_xfer[1]->
3370                                   xroot->done_m[0].hdr;
3371                               (pm->pm_callback) (pm);
3372                     }
3373 
3374                     /* poll done thread */
3375                     pm = &xroot->done_m[0].hdr;
3376                     (pm->pm_callback) (pm);
3377 
3378                     USB_BUS_UNLOCK(xroot->bus);
3379 
3380                     /* restore transfer mutex */
3381                     while (drop_xfer--)
3382                               lockmgr(xroot->xfer_lock, LK_EXCLUSIVE);
3383 
3384                     /* restore BUS mutex */
3385                     while (drop_bus--)
3386                               lockmgr(&xroot->udev->bus->bus_lock, LK_EXCLUSIVE);
3387           }
3388 }
3389 
3390 static void
usbd_get_std_packet_size(struct usb_std_packet_size * ptr,uint8_t type,enum usb_dev_speed speed)3391 usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3392     uint8_t type, enum usb_dev_speed speed)
3393 {
3394           static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3395                     [USB_SPEED_LOW] = 8,
3396                     [USB_SPEED_FULL] = 64,
3397                     [USB_SPEED_HIGH] = 1024,
3398                     [USB_SPEED_VARIABLE] = 1024,
3399                     [USB_SPEED_SUPER] = 1024,
3400           };
3401 
3402           static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3403                     [USB_SPEED_LOW] = 0,          /* invalid */
3404                     [USB_SPEED_FULL] = 1023,
3405                     [USB_SPEED_HIGH] = 1024,
3406                     [USB_SPEED_VARIABLE] = 3584,
3407                     [USB_SPEED_SUPER] = 1024,
3408           };
3409 
3410           static const uint16_t control_min[USB_SPEED_MAX] = {
3411                     [USB_SPEED_LOW] = 8,
3412                     [USB_SPEED_FULL] = 8,
3413                     [USB_SPEED_HIGH] = 64,
3414                     [USB_SPEED_VARIABLE] = 512,
3415                     [USB_SPEED_SUPER] = 512,
3416           };
3417 
3418           static const uint16_t bulk_min[USB_SPEED_MAX] = {
3419                     [USB_SPEED_LOW] = 8,
3420                     [USB_SPEED_FULL] = 8,
3421                     [USB_SPEED_HIGH] = 512,
3422                     [USB_SPEED_VARIABLE] = 512,
3423                     [USB_SPEED_SUPER] = 1024,
3424           };
3425 
3426           uint16_t temp;
3427 
3428           memset(ptr, 0, sizeof(*ptr));
3429 
3430           switch (type) {
3431           case UE_INTERRUPT:
3432                     ptr->range.max = intr_range_max[speed];
3433                     break;
3434           case UE_ISOCHRONOUS:
3435                     ptr->range.max = isoc_range_max[speed];
3436                     break;
3437           default:
3438                     if (type == UE_BULK)
3439                               temp = bulk_min[speed];
3440                     else /* UE_CONTROL */
3441                               temp = control_min[speed];
3442 
3443                     /* default is fixed */
3444                     ptr->fixed[0] = temp;
3445                     ptr->fixed[1] = temp;
3446                     ptr->fixed[2] = temp;
3447                     ptr->fixed[3] = temp;
3448 
3449                     if (speed == USB_SPEED_FULL) {
3450                               /* multiple sizes */
3451                               ptr->fixed[1] = 16;
3452                               ptr->fixed[2] = 32;
3453                               ptr->fixed[3] = 64;
3454                     }
3455                     if ((speed == USB_SPEED_VARIABLE) &&
3456                         (type == UE_BULK)) {
3457                               /* multiple sizes */
3458                               ptr->fixed[2] = 1024;
3459                               ptr->fixed[3] = 1536;
3460                     }
3461                     break;
3462           }
3463 }
3464 
3465 void      *
usbd_xfer_softc(struct usb_xfer * xfer)3466 usbd_xfer_softc(struct usb_xfer *xfer)
3467 {
3468           return (xfer->priv_sc);
3469 }
3470 
3471 void *
usbd_xfer_get_priv(struct usb_xfer * xfer)3472 usbd_xfer_get_priv(struct usb_xfer *xfer)
3473 {
3474           return (xfer->priv_fifo);
3475 }
3476 
3477 void
usbd_xfer_set_priv(struct usb_xfer * xfer,void * ptr)3478 usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3479 {
3480           xfer->priv_fifo = ptr;
3481 }
3482 
3483 uint8_t
usbd_xfer_state(struct usb_xfer * xfer)3484 usbd_xfer_state(struct usb_xfer *xfer)
3485 {
3486           return (xfer->usb_state);
3487 }
3488 
3489 void
usbd_xfer_set_flag(struct usb_xfer * xfer,int flag)3490 usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3491 {
3492           switch (flag) {
3493                     case USB_FORCE_SHORT_XFER:
3494                               xfer->flags.force_short_xfer = 1;
3495                               break;
3496                     case USB_SHORT_XFER_OK:
3497                               xfer->flags.short_xfer_ok = 1;
3498                               break;
3499                     case USB_MULTI_SHORT_OK:
3500                               xfer->flags.short_frames_ok = 1;
3501                               break;
3502                     case USB_MANUAL_STATUS:
3503                               xfer->flags.manual_status = 1;
3504                               break;
3505           }
3506 }
3507 
3508 void
usbd_xfer_clr_flag(struct usb_xfer * xfer,int flag)3509 usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3510 {
3511           switch (flag) {
3512                     case USB_FORCE_SHORT_XFER:
3513                               xfer->flags.force_short_xfer = 0;
3514                               break;
3515                     case USB_SHORT_XFER_OK:
3516                               xfer->flags.short_xfer_ok = 0;
3517                               break;
3518                     case USB_MULTI_SHORT_OK:
3519                               xfer->flags.short_frames_ok = 0;
3520                               break;
3521                     case USB_MANUAL_STATUS:
3522                               xfer->flags.manual_status = 0;
3523                               break;
3524           }
3525 }
3526 
3527 /*
3528  * The following function returns in milliseconds when the isochronous
3529  * transfer was completed by the hardware. The returned value wraps
3530  * around 65536 milliseconds.
3531  */
3532 uint16_t
usbd_xfer_get_timestamp(struct usb_xfer * xfer)3533 usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3534 {
3535           return (xfer->isoc_time_complete);
3536 }
3537 
3538 /*
3539  * The following function returns non-zero if the max packet size
3540  * field was clamped to a valid value. Else it returns zero.
3541  */
3542 uint8_t
usbd_xfer_maxp_was_clamped(struct usb_xfer * xfer)3543 usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3544 {
3545           return (xfer->flags_int.maxp_was_clamped);
3546 }
3547