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