Deleted Added
full compact
usb_transfer.c (187166) usb_transfer.c (187173)
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_transfer.c 187166 2009-01-13 19:01:56Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_transfer.c 187173 2009-01-13 19:03:12Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 228 unchanged lines hidden (view full) ---

238 parm->dma_page_cache_ptr += n_dma_pc;
239 parm->dma_page_ptr += count;
240 parm->xfer_page_cache_ptr += count;
241 return (0);
242 }
243 for (x = 0; x != n_dma_pc; x++) {
244 /* need to initialize the page cache */
245 parm->dma_page_cache_ptr[x].tag_parent =
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.

--- 228 unchanged lines hidden (view full) ---

238 parm->dma_page_cache_ptr += n_dma_pc;
239 parm->dma_page_ptr += count;
240 parm->xfer_page_cache_ptr += count;
241 return (0);
242 }
243 for (x = 0; x != n_dma_pc; x++) {
244 /* need to initialize the page cache */
245 parm->dma_page_cache_ptr[x].tag_parent =
246 &parm->curr_xfer->usb2_root->dma_parent_tag;
246 &parm->curr_xfer->xroot->dma_parent_tag;
247 }
248 for (x = 0; x != count; x++) {
249 /* need to initialize the page cache */
250 parm->xfer_page_cache_ptr[x].tag_parent =
247 }
248 for (x = 0; x != count; x++) {
249 /* need to initialize the page cache */
250 parm->xfer_page_cache_ptr[x].tag_parent =
251 &parm->curr_xfer->usb2_root->dma_parent_tag;
251 &parm->curr_xfer->xroot->dma_parent_tag;
252 }
253
254 if (ppc) {
255 *ppc = parm->xfer_page_cache_ptr;
256 }
257 r = count; /* set remainder count */
258 z = n_obj * size; /* set allocation size */
259 pc = parm->xfer_page_cache_ptr;

--- 405 unchanged lines hidden (view full) ---

665
666 xfer->max_frame_count = xfer->nframes;
667
668 /* initialize frame buffers */
669
670 if (parm->buf) {
671 for (x = 0; x != n_frbuffers; x++) {
672 xfer->frbuffers[x].tag_parent =
252 }
253
254 if (ppc) {
255 *ppc = parm->xfer_page_cache_ptr;
256 }
257 r = count; /* set remainder count */
258 z = n_obj * size; /* set allocation size */
259 pc = parm->xfer_page_cache_ptr;

--- 405 unchanged lines hidden (view full) ---

665
666 xfer->max_frame_count = xfer->nframes;
667
668 /* initialize frame buffers */
669
670 if (parm->buf) {
671 for (x = 0; x != n_frbuffers; x++) {
672 xfer->frbuffers[x].tag_parent =
673 &xfer->usb2_root->dma_parent_tag;
673 &xfer->xroot->dma_parent_tag;
674
675 if (xfer->flags_int.bdma_enable &&
676 (parm->bufsize_max > 0)) {
677
678 if (usb2_pc_dmamap_create(
679 xfer->frbuffers + x,
680 parm->bufsize_max)) {
681 parm->err = USB_ERR_NOMEM;

--- 28 unchanged lines hidden (view full) ---

710 * Return values:
711 * 0: Success
712 * Else: Failure
713 *------------------------------------------------------------------------*/
714usb2_error_t
715usb2_transfer_setup(struct usb2_device *udev,
716 const uint8_t *ifaces, struct usb2_xfer **ppxfer,
717 const struct usb2_config *setup_start, uint16_t n_setup,
674
675 if (xfer->flags_int.bdma_enable &&
676 (parm->bufsize_max > 0)) {
677
678 if (usb2_pc_dmamap_create(
679 xfer->frbuffers + x,
680 parm->bufsize_max)) {
681 parm->err = USB_ERR_NOMEM;

--- 28 unchanged lines hidden (view full) ---

710 * Return values:
711 * 0: Success
712 * Else: Failure
713 *------------------------------------------------------------------------*/
714usb2_error_t
715usb2_transfer_setup(struct usb2_device *udev,
716 const uint8_t *ifaces, struct usb2_xfer **ppxfer,
717 const struct usb2_config *setup_start, uint16_t n_setup,
718 void *priv_sc, struct mtx *priv_mtx)
718 void *priv_sc, struct mtx *xfer_mtx)
719{
720 struct usb2_xfer dummy;
721 struct usb2_setup_params parm;
722 const struct usb2_config *setup_end = setup_start + n_setup;
723 const struct usb2_config *setup;
724 struct usb2_pipe *pipe;
725 struct usb2_xfer_root *info;
726 struct usb2_xfer *xfer;

--- 13 unchanged lines hidden (view full) ---

740 if (n_setup == 0) {
741 DPRINTFN(6, "setup array has zero length!\n");
742 return (USB_ERR_INVAL);
743 }
744 if (ifaces == 0) {
745 DPRINTFN(6, "ifaces array is NULL!\n");
746 return (USB_ERR_INVAL);
747 }
719{
720 struct usb2_xfer dummy;
721 struct usb2_setup_params parm;
722 const struct usb2_config *setup_end = setup_start + n_setup;
723 const struct usb2_config *setup;
724 struct usb2_pipe *pipe;
725 struct usb2_xfer_root *info;
726 struct usb2_xfer *xfer;

--- 13 unchanged lines hidden (view full) ---

740 if (n_setup == 0) {
741 DPRINTFN(6, "setup array has zero length!\n");
742 return (USB_ERR_INVAL);
743 }
744 if (ifaces == 0) {
745 DPRINTFN(6, "ifaces array is NULL!\n");
746 return (USB_ERR_INVAL);
747 }
748 if (priv_mtx == NULL) {
748 if (xfer_mtx == NULL) {
749 DPRINTFN(6, "using global lock\n");
749 DPRINTFN(6, "using global lock\n");
750 priv_mtx = &Giant;
750 xfer_mtx = &Giant;
751 }
752 /* sanity checks */
753 for (setup = setup_start, n = 0;
754 setup != setup_end; setup++, n++) {
755 if ((setup->mh.bufsize == 0xffffffff) ||
756 (setup->md.bufsize == 0xffffffff)) {
757 parm.err = USB_ERR_BAD_BUFSIZE;
758 DPRINTF("invalid bufsize\n");

--- 35 unchanged lines hidden (view full) ---

794
795 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
796 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
797 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
798 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
799
800 usb2_cv_init(&info->cv_drain, "WDRAIN");
801
751 }
752 /* sanity checks */
753 for (setup = setup_start, n = 0;
754 setup != setup_end; setup++, n++) {
755 if ((setup->mh.bufsize == 0xffffffff) ||
756 (setup->md.bufsize == 0xffffffff)) {
757 parm.err = USB_ERR_BAD_BUFSIZE;
758 DPRINTF("invalid bufsize\n");

--- 35 unchanged lines hidden (view full) ---

794
795 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
796 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
797 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
798 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
799
800 usb2_cv_init(&info->cv_drain, "WDRAIN");
801
802 info->priv_mtx = priv_mtx;
802 info->xfer_mtx = xfer_mtx;
803
804 usb2_dma_tag_setup(&info->dma_parent_tag,
805 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
803
804 usb2_dma_tag_setup(&info->dma_parent_tag,
805 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
806 priv_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
806 xfer_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
807
808 info->bus = udev->bus;
807
808 info->bus = udev->bus;
809 info->udev = udev;
809
810 TAILQ_INIT(&info->done_q.head);
811 info->done_q.command = &usb2_callback_wrapper;
812
813 TAILQ_INIT(&info->dma_q.head);
814 info->dma_q.command = &usb2_bdma_work_loop;
815
816 info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
810
811 TAILQ_INIT(&info->done_q.head);
812 info->done_q.command = &usb2_callback_wrapper;
813
814 TAILQ_INIT(&info->dma_q.head);
815 info->dma_q.command = &usb2_bdma_work_loop;
816
817 info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
817 info->done_m[0].usb2_root = info;
818 info->done_m[0].xroot = info;
818 info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
819 info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
819 info->done_m[1].usb2_root = info;
820 info->done_m[1].xroot = info;
820
821 /* create a callback thread */
822
823 if (usb2_proc_setup(&info->done_p,
824 &udev->bus->bus_mtx, USB_PRI_HIGH)) {
825 parm.err = USB_ERR_NO_INTR_THREAD;
826 goto done;
827 }

--- 38 unchanged lines hidden (view full) ---

866
867 /*
868 * Common initialization of the
869 * "usb2_xfer" structure.
870 */
871 xfer = USB_ADD_BYTES(buf, parm.size[0]);
872
873 ppxfer[n] = xfer;
821
822 /* create a callback thread */
823
824 if (usb2_proc_setup(&info->done_p,
825 &udev->bus->bus_mtx, USB_PRI_HIGH)) {
826 parm.err = USB_ERR_NO_INTR_THREAD;
827 goto done;
828 }

--- 38 unchanged lines hidden (view full) ---

867
868 /*
869 * Common initialization of the
870 * "usb2_xfer" structure.
871 */
872 xfer = USB_ADD_BYTES(buf, parm.size[0]);
873
874 ppxfer[n] = xfer;
874 xfer->udev = udev;
875 xfer->address = udev->address;
876 xfer->priv_sc = priv_sc;
875 xfer->address = udev->address;
876 xfer->priv_sc = priv_sc;
877 xfer->xfer_mtx = priv_mtx;
878 xfer->usb2_root = info;
877 xfer->xroot = info;
879 info->setup_refcount++;
880
881 usb2_callout_init_mtx(&xfer->timeout_handle,
882 &udev->bus->bus_mtx, 0);
883 } else {
884 /*
885 * Setup a dummy xfer, hence we are
886 * writing to the "usb2_xfer"

--- 208 unchanged lines hidden (view full) ---

1095 "usb2_transfer_unsetup can sleep!");
1096
1097 while (n_setup--) {
1098 xfer = pxfer[n_setup];
1099
1100 if (xfer) {
1101 if (xfer->pipe) {
1102 USB_XFER_LOCK(xfer);
878 info->setup_refcount++;
879
880 usb2_callout_init_mtx(&xfer->timeout_handle,
881 &udev->bus->bus_mtx, 0);
882 } else {
883 /*
884 * Setup a dummy xfer, hence we are
885 * writing to the "usb2_xfer"

--- 208 unchanged lines hidden (view full) ---

1094 "usb2_transfer_unsetup can sleep!");
1095
1096 while (n_setup--) {
1097 xfer = pxfer[n_setup];
1098
1099 if (xfer) {
1100 if (xfer->pipe) {
1101 USB_XFER_LOCK(xfer);
1103 USB_BUS_LOCK(xfer->udev->bus);
1102 USB_BUS_LOCK(xfer->xroot->bus);
1104
1105 /*
1106 * HINT: when you start/stop a transfer, it
1107 * might be a good idea to directly use the
1108 * "pxfer[]" structure:
1109 *
1110 * usb2_transfer_start(sc->pxfer[0]);
1111 * usb2_transfer_stop(sc->pxfer[0]);
1112 *
1113 * That way, if your code has many parts that
1114 * will not stop running under the same
1103
1104 /*
1105 * HINT: when you start/stop a transfer, it
1106 * might be a good idea to directly use the
1107 * "pxfer[]" structure:
1108 *
1109 * usb2_transfer_start(sc->pxfer[0]);
1110 * usb2_transfer_stop(sc->pxfer[0]);
1111 *
1112 * That way, if your code has many parts that
1113 * will not stop running under the same
1115 * lock, in other words "priv_mtx", the
1114 * lock, in other words "xfer_mtx", the
1116 * usb2_transfer_start and
1117 * usb2_transfer_stop functions will simply
1118 * return when they detect a NULL pointer
1119 * argument.
1120 *
1121 * To avoid any races we clear the "pxfer[]"
1122 * pointer while holding the private mutex
1123 * of the driver:
1124 */
1125 pxfer[n_setup] = NULL;
1126
1115 * usb2_transfer_start and
1116 * usb2_transfer_stop functions will simply
1117 * return when they detect a NULL pointer
1118 * argument.
1119 *
1120 * To avoid any races we clear the "pxfer[]"
1121 * pointer while holding the private mutex
1122 * of the driver:
1123 */
1124 pxfer[n_setup] = NULL;
1125
1127 USB_BUS_UNLOCK(xfer->udev->bus);
1126 USB_BUS_UNLOCK(xfer->xroot->bus);
1128 USB_XFER_UNLOCK(xfer);
1129
1130 usb2_transfer_drain(xfer);
1131
1132 if (xfer->flags_int.bdma_enable) {
1133 needs_delay = 1;
1134 }
1135 /*

--- 4 unchanged lines hidden (view full) ---

1140
1141 } else {
1142 /* clear the transfer pointer */
1143 pxfer[n_setup] = NULL;
1144 }
1145
1146 usb2_callout_drain(&xfer->timeout_handle);
1147
1127 USB_XFER_UNLOCK(xfer);
1128
1129 usb2_transfer_drain(xfer);
1130
1131 if (xfer->flags_int.bdma_enable) {
1132 needs_delay = 1;
1133 }
1134 /*

--- 4 unchanged lines hidden (view full) ---

1139
1140 } else {
1141 /* clear the transfer pointer */
1142 pxfer[n_setup] = NULL;
1143 }
1144
1145 usb2_callout_drain(&xfer->timeout_handle);
1146
1148 if (xfer->usb2_root) {
1149 info = xfer->usb2_root;
1147 if (xfer->xroot) {
1148 info = xfer->xroot;
1150
1151 USB_BUS_LOCK(info->bus);
1152
1153 USB_ASSERT(info->setup_refcount != 0,
1154 ("Invalid setup "
1155 "reference count!\n"));
1156
1157 info->setup_refcount--;

--- 186 unchanged lines hidden (view full) ---

1344 uint32_t x;
1345
1346 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
1347 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1348 "read" : "write");
1349
1350#if USB_DEBUG
1351 if (USB_DEBUG_VAR > 0) {
1149
1150 USB_BUS_LOCK(info->bus);
1151
1152 USB_ASSERT(info->setup_refcount != 0,
1153 ("Invalid setup "
1154 "reference count!\n"));
1155
1156 info->setup_refcount--;

--- 186 unchanged lines hidden (view full) ---

1343 uint32_t x;
1344
1345 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
1346 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1347 "read" : "write");
1348
1349#if USB_DEBUG
1350 if (USB_DEBUG_VAR > 0) {
1352 USB_BUS_LOCK(xfer->udev->bus);
1351 USB_BUS_LOCK(xfer->xroot->bus);
1353
1354 usb2_dump_pipe(xfer->pipe);
1355
1352
1353 usb2_dump_pipe(xfer->pipe);
1354
1356 USB_BUS_UNLOCK(xfer->udev->bus);
1355 USB_BUS_UNLOCK(xfer->xroot->bus);
1357 }
1358#endif
1359
1360 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1356 }
1357#endif
1358
1359 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1361 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_NOTOWNED);
1360 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
1362
1363 /* Only open the USB transfer once! */
1364 if (!xfer->flags_int.open) {
1365 xfer->flags_int.open = 1;
1366
1367 DPRINTF("open\n");
1368
1361
1362 /* Only open the USB transfer once! */
1363 if (!xfer->flags_int.open) {
1364 xfer->flags_int.open = 1;
1365
1366 DPRINTF("open\n");
1367
1369 USB_BUS_LOCK(xfer->udev->bus);
1368 USB_BUS_LOCK(xfer->xroot->bus);
1370 (xfer->pipe->methods->open) (xfer);
1369 (xfer->pipe->methods->open) (xfer);
1371 USB_BUS_UNLOCK(xfer->udev->bus);
1370 USB_BUS_UNLOCK(xfer->xroot->bus);
1372 }
1373 /* set "transferring" flag */
1374 xfer->flags_int.transferring = 1;
1375
1376 /* increment power reference */
1377 usb2_transfer_power_ref(xfer, 1);
1378
1379 /*
1380 * Check if the transfer is waiting on a queue, most
1381 * frequently the "done_q":
1382 */
1383 if (xfer->wait_queue) {
1371 }
1372 /* set "transferring" flag */
1373 xfer->flags_int.transferring = 1;
1374
1375 /* increment power reference */
1376 usb2_transfer_power_ref(xfer, 1);
1377
1378 /*
1379 * Check if the transfer is waiting on a queue, most
1380 * frequently the "done_q":
1381 */
1382 if (xfer->wait_queue) {
1384 USB_BUS_LOCK(xfer->udev->bus);
1383 USB_BUS_LOCK(xfer->xroot->bus);
1385 usb2_transfer_dequeue(xfer);
1384 usb2_transfer_dequeue(xfer);
1386 USB_BUS_UNLOCK(xfer->udev->bus);
1385 USB_BUS_UNLOCK(xfer->xroot->bus);
1387 }
1388 /* clear "did_dma_delay" flag */
1389 xfer->flags_int.did_dma_delay = 0;
1390
1391 /* clear "did_close" flag */
1392 xfer->flags_int.did_close = 0;
1393
1394 /* clear "bdma_setup" flag */

--- 15 unchanged lines hidden (view full) ---

1410 if (xfer->nframes == 0) {
1411 if (xfer->flags.stall_pipe) {
1412 /*
1413 * Special case - want to stall without transferring
1414 * any data:
1415 */
1416 DPRINTF("xfer=%p nframes=0: stall "
1417 "or clear stall!\n", xfer);
1386 }
1387 /* clear "did_dma_delay" flag */
1388 xfer->flags_int.did_dma_delay = 0;
1389
1390 /* clear "did_close" flag */
1391 xfer->flags_int.did_close = 0;
1392
1393 /* clear "bdma_setup" flag */

--- 15 unchanged lines hidden (view full) ---

1409 if (xfer->nframes == 0) {
1410 if (xfer->flags.stall_pipe) {
1411 /*
1412 * Special case - want to stall without transferring
1413 * any data:
1414 */
1415 DPRINTF("xfer=%p nframes=0: stall "
1416 "or clear stall!\n", xfer);
1418 USB_BUS_LOCK(xfer->udev->bus);
1417 USB_BUS_LOCK(xfer->xroot->bus);
1419 xfer->flags_int.can_cancel_immed = 1;
1420 /* start the transfer */
1421 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
1418 xfer->flags_int.can_cancel_immed = 1;
1419 /* start the transfer */
1420 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
1422 USB_BUS_UNLOCK(xfer->udev->bus);
1421 USB_BUS_UNLOCK(xfer->xroot->bus);
1423 return;
1424 }
1422 return;
1423 }
1425 USB_BUS_LOCK(xfer->udev->bus);
1424 USB_BUS_LOCK(xfer->xroot->bus);
1426 usb2_transfer_done(xfer, USB_ERR_INVAL);
1425 usb2_transfer_done(xfer, USB_ERR_INVAL);
1427 USB_BUS_UNLOCK(xfer->udev->bus);
1426 USB_BUS_UNLOCK(xfer->xroot->bus);
1428 return;
1429 }
1430 /* compute total transfer length */
1431
1432 for (x = 0; x != xfer->nframes; x++) {
1433 xfer->sumlen += xfer->frlengths[x];
1434 if (xfer->sumlen < xfer->frlengths[x]) {
1435 /* length wrapped around */
1427 return;
1428 }
1429 /* compute total transfer length */
1430
1431 for (x = 0; x != xfer->nframes; x++) {
1432 xfer->sumlen += xfer->frlengths[x];
1433 if (xfer->sumlen < xfer->frlengths[x]) {
1434 /* length wrapped around */
1436 USB_BUS_LOCK(xfer->udev->bus);
1435 USB_BUS_LOCK(xfer->xroot->bus);
1437 usb2_transfer_done(xfer, USB_ERR_INVAL);
1436 usb2_transfer_done(xfer, USB_ERR_INVAL);
1438 USB_BUS_UNLOCK(xfer->udev->bus);
1437 USB_BUS_UNLOCK(xfer->xroot->bus);
1439 return;
1440 }
1441 }
1442
1443 /* clear some internal flags */
1444
1445 xfer->flags_int.short_xfer_ok = 0;
1446 xfer->flags_int.short_frames_ok = 0;
1447
1448 /* check if this is a control transfer */
1449
1450 if (xfer->flags_int.control_xfr) {
1451
1452 if (usb2_start_hardware_sub(xfer)) {
1438 return;
1439 }
1440 }
1441
1442 /* clear some internal flags */
1443
1444 xfer->flags_int.short_xfer_ok = 0;
1445 xfer->flags_int.short_frames_ok = 0;
1446
1447 /* check if this is a control transfer */
1448
1449 if (xfer->flags_int.control_xfr) {
1450
1451 if (usb2_start_hardware_sub(xfer)) {
1453 USB_BUS_LOCK(xfer->udev->bus);
1452 USB_BUS_LOCK(xfer->xroot->bus);
1454 usb2_transfer_done(xfer, USB_ERR_STALLED);
1453 usb2_transfer_done(xfer, USB_ERR_STALLED);
1455 USB_BUS_UNLOCK(xfer->udev->bus);
1454 USB_BUS_UNLOCK(xfer->xroot->bus);
1456 return;
1457 }
1458 }
1459 /*
1460 * Setup filtered version of some transfer flags,
1461 * in case of data read direction
1462 */
1463 if (USB_GET_DATA_ISREAD(xfer)) {

--- 19 unchanged lines hidden (view full) ---

1483 }
1484 }
1485 /*
1486 * Check if BUS-DMA support is enabled and try to load virtual
1487 * buffers into DMA, if any:
1488 */
1489 if (xfer->flags_int.bdma_enable) {
1490 /* insert the USB transfer last in the BUS-DMA queue */
1455 return;
1456 }
1457 }
1458 /*
1459 * Setup filtered version of some transfer flags,
1460 * in case of data read direction
1461 */
1462 if (USB_GET_DATA_ISREAD(xfer)) {

--- 19 unchanged lines hidden (view full) ---

1482 }
1483 }
1484 /*
1485 * Check if BUS-DMA support is enabled and try to load virtual
1486 * buffers into DMA, if any:
1487 */
1488 if (xfer->flags_int.bdma_enable) {
1489 /* insert the USB transfer last in the BUS-DMA queue */
1491 usb2_command_wrapper(&xfer->usb2_root->dma_q, xfer);
1490 usb2_command_wrapper(&xfer->xroot->dma_q, xfer);
1492 return;
1493 }
1494 /*
1495 * Enter the USB transfer into the Host Controller or
1496 * Device Controller schedule:
1497 */
1498 usb2_pipe_enter(xfer);
1499}
1500
1501/*------------------------------------------------------------------------*
1502 * usb2_pipe_enter - factored out code
1503 *------------------------------------------------------------------------*/
1504void
1505usb2_pipe_enter(struct usb2_xfer *xfer)
1506{
1507 struct usb2_pipe *pipe;
1508
1509 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1510
1491 return;
1492 }
1493 /*
1494 * Enter the USB transfer into the Host Controller or
1495 * Device Controller schedule:
1496 */
1497 usb2_pipe_enter(xfer);
1498}
1499
1500/*------------------------------------------------------------------------*
1501 * usb2_pipe_enter - factored out code
1502 *------------------------------------------------------------------------*/
1503void
1504usb2_pipe_enter(struct usb2_xfer *xfer)
1505{
1506 struct usb2_pipe *pipe;
1507
1508 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1509
1511 USB_BUS_LOCK(xfer->udev->bus);
1510 USB_BUS_LOCK(xfer->xroot->bus);
1512
1513 pipe = xfer->pipe;
1514
1515 DPRINTF("enter\n");
1516
1517 /* enter the transfer */
1518 (pipe->methods->enter) (xfer);
1519
1520 /* check cancelability */
1521 if (pipe->methods->enter_is_cancelable) {
1522 xfer->flags_int.can_cancel_immed = 1;
1523 /* check for transfer error */
1524 if (xfer->error) {
1525 /* some error has happened */
1526 usb2_transfer_done(xfer, 0);
1511
1512 pipe = xfer->pipe;
1513
1514 DPRINTF("enter\n");
1515
1516 /* enter the transfer */
1517 (pipe->methods->enter) (xfer);
1518
1519 /* check cancelability */
1520 if (pipe->methods->enter_is_cancelable) {
1521 xfer->flags_int.can_cancel_immed = 1;
1522 /* check for transfer error */
1523 if (xfer->error) {
1524 /* some error has happened */
1525 usb2_transfer_done(xfer, 0);
1527 USB_BUS_UNLOCK(xfer->udev->bus);
1526 USB_BUS_UNLOCK(xfer->xroot->bus);
1528 return;
1529 }
1530 } else {
1531 xfer->flags_int.can_cancel_immed = 0;
1532 }
1533
1534 /* start the transfer */
1535 usb2_command_wrapper(&pipe->pipe_q, xfer);
1527 return;
1528 }
1529 } else {
1530 xfer->flags_int.can_cancel_immed = 0;
1531 }
1532
1533 /* start the transfer */
1534 usb2_command_wrapper(&pipe->pipe_q, xfer);
1536 USB_BUS_UNLOCK(xfer->udev->bus);
1535 USB_BUS_UNLOCK(xfer->xroot->bus);
1537}
1538
1539/*------------------------------------------------------------------------*
1540 * usb2_transfer_start - start an USB transfer
1541 *
1542 * NOTE: Calling this function more than one time will only
1543 * result in a single transfer start, until the USB transfer
1544 * completes.

--- 12 unchanged lines hidden (view full) ---

1557 if (!xfer->flags_int.started) {
1558 xfer->flags_int.started = 1;
1559 }
1560 /* check if the USB transfer callback is already transferring */
1561
1562 if (xfer->flags_int.transferring) {
1563 return;
1564 }
1536}
1537
1538/*------------------------------------------------------------------------*
1539 * usb2_transfer_start - start an USB transfer
1540 *
1541 * NOTE: Calling this function more than one time will only
1542 * result in a single transfer start, until the USB transfer
1543 * completes.

--- 12 unchanged lines hidden (view full) ---

1556 if (!xfer->flags_int.started) {
1557 xfer->flags_int.started = 1;
1558 }
1559 /* check if the USB transfer callback is already transferring */
1560
1561 if (xfer->flags_int.transferring) {
1562 return;
1563 }
1565 USB_BUS_LOCK(xfer->udev->bus);
1564 USB_BUS_LOCK(xfer->xroot->bus);
1566 /* call the USB transfer callback */
1567 usb2_callback_ss_done_defer(xfer);
1565 /* call the USB transfer callback */
1566 usb2_callback_ss_done_defer(xfer);
1568 USB_BUS_UNLOCK(xfer->udev->bus);
1567 USB_BUS_UNLOCK(xfer->xroot->bus);
1569}
1570
1571/*------------------------------------------------------------------------*
1572 * usb2_transfer_stop - stop an USB transfer
1573 *
1574 * NOTE: Calling this function more than one time will only
1575 * result in a single transfer stop.
1576 * NOTE: When this function returns it is not safe to free nor

--- 14 unchanged lines hidden (view full) ---

1591
1592 if (!xfer->flags_int.open) {
1593 /* nothing to do except clearing the "started" flag */
1594 xfer->flags_int.started = 0;
1595 return;
1596 }
1597 /* try to stop the current USB transfer */
1598
1568}
1569
1570/*------------------------------------------------------------------------*
1571 * usb2_transfer_stop - stop an USB transfer
1572 *
1573 * NOTE: Calling this function more than one time will only
1574 * result in a single transfer stop.
1575 * NOTE: When this function returns it is not safe to free nor

--- 14 unchanged lines hidden (view full) ---

1590
1591 if (!xfer->flags_int.open) {
1592 /* nothing to do except clearing the "started" flag */
1593 xfer->flags_int.started = 0;
1594 return;
1595 }
1596 /* try to stop the current USB transfer */
1597
1599 USB_BUS_LOCK(xfer->udev->bus);
1598 USB_BUS_LOCK(xfer->xroot->bus);
1600 xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1601 /*
1602 * Clear "open" and "started" when both private and USB lock
1603 * is locked so that we don't get a race updating "flags_int"
1604 */
1605 xfer->flags_int.open = 0;
1606 xfer->flags_int.started = 0;
1607

--- 35 unchanged lines hidden (view full) ---

1643 * If the current USB transfer is completing we need
1644 * to start the next one:
1645 */
1646 if (pipe->pipe_q.curr == xfer) {
1647 usb2_command_wrapper(&pipe->pipe_q, NULL);
1648 }
1649 }
1650
1599 xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1600 /*
1601 * Clear "open" and "started" when both private and USB lock
1602 * is locked so that we don't get a race updating "flags_int"
1603 */
1604 xfer->flags_int.open = 0;
1605 xfer->flags_int.started = 0;
1606

--- 35 unchanged lines hidden (view full) ---

1642 * If the current USB transfer is completing we need
1643 * to start the next one:
1644 */
1645 if (pipe->pipe_q.curr == xfer) {
1646 usb2_command_wrapper(&pipe->pipe_q, NULL);
1647 }
1648 }
1649
1651 USB_BUS_UNLOCK(xfer->udev->bus);
1650 USB_BUS_UNLOCK(xfer->xroot->bus);
1652}
1653
1654/*------------------------------------------------------------------------*
1655 * usb2_transfer_pending
1656 *
1657 * This function will check if an USB transfer is pending which is a
1658 * little bit complicated!
1659 * Return values:

--- 7 unchanged lines hidden (view full) ---

1667 struct usb2_xfer_queue *pq;
1668
1669 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1670
1671 if (xfer->flags_int.transferring) {
1672 /* trivial case */
1673 return (1);
1674 }
1651}
1652
1653/*------------------------------------------------------------------------*
1654 * usb2_transfer_pending
1655 *
1656 * This function will check if an USB transfer is pending which is a
1657 * little bit complicated!
1658 * Return values:

--- 7 unchanged lines hidden (view full) ---

1666 struct usb2_xfer_queue *pq;
1667
1668 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1669
1670 if (xfer->flags_int.transferring) {
1671 /* trivial case */
1672 return (1);
1673 }
1675 USB_BUS_LOCK(xfer->udev->bus);
1674 USB_BUS_LOCK(xfer->xroot->bus);
1676 if (xfer->wait_queue) {
1677 /* we are waiting on a queue somewhere */
1675 if (xfer->wait_queue) {
1676 /* we are waiting on a queue somewhere */
1678 USB_BUS_UNLOCK(xfer->udev->bus);
1677 USB_BUS_UNLOCK(xfer->xroot->bus);
1679 return (1);
1680 }
1678 return (1);
1679 }
1681 info = xfer->usb2_root;
1680 info = xfer->xroot;
1682 pq = &info->done_q;
1683
1684 if (pq->curr == xfer) {
1685 /* we are currently scheduled for callback */
1681 pq = &info->done_q;
1682
1683 if (pq->curr == xfer) {
1684 /* we are currently scheduled for callback */
1686 USB_BUS_UNLOCK(xfer->udev->bus);
1685 USB_BUS_UNLOCK(xfer->xroot->bus);
1687 return (1);
1688 }
1689 /* we are not pending */
1686 return (1);
1687 }
1688 /* we are not pending */
1690 USB_BUS_UNLOCK(xfer->udev->bus);
1689 USB_BUS_UNLOCK(xfer->xroot->bus);
1691 return (0);
1692}
1693
1694/*------------------------------------------------------------------------*
1695 * usb2_transfer_drain
1696 *
1697 * This function will stop the USB transfer and wait for any
1698 * additional BUS-DMA and HW-DMA operations to complete. Buffers that

--- 5 unchanged lines hidden (view full) ---

1704{
1705 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1706 "usb2_transfer_drain can sleep!");
1707
1708 if (xfer == NULL) {
1709 /* transfer is gone */
1710 return;
1711 }
1690 return (0);
1691}
1692
1693/*------------------------------------------------------------------------*
1694 * usb2_transfer_drain
1695 *
1696 * This function will stop the USB transfer and wait for any
1697 * additional BUS-DMA and HW-DMA operations to complete. Buffers that

--- 5 unchanged lines hidden (view full) ---

1703{
1704 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1705 "usb2_transfer_drain can sleep!");
1706
1707 if (xfer == NULL) {
1708 /* transfer is gone */
1709 return;
1710 }
1712 if (xfer->xfer_mtx != &Giant) {
1711 if (xfer->xroot->xfer_mtx != &Giant) {
1713 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1714 }
1715 USB_XFER_LOCK(xfer);
1716
1717 usb2_transfer_stop(xfer);
1718
1719 while (usb2_transfer_pending(xfer)) {
1720 xfer->flags_int.draining = 1;
1721 /*
1722 * Wait until the current outstanding USB
1723 * transfer is complete !
1724 */
1712 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1713 }
1714 USB_XFER_LOCK(xfer);
1715
1716 usb2_transfer_stop(xfer);
1717
1718 while (usb2_transfer_pending(xfer)) {
1719 xfer->flags_int.draining = 1;
1720 /*
1721 * Wait until the current outstanding USB
1722 * transfer is complete !
1723 */
1725 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->xfer_mtx);
1724 usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
1726 }
1727 USB_XFER_UNLOCK(xfer);
1728}
1729
1730/*------------------------------------------------------------------------*
1731 * usb2_set_frame_data
1732 *
1733 * This function sets the pointer of the buffer that should

--- 30 unchanged lines hidden (view full) ---

1764 * usb2_callback_proc - factored out code
1765 *
1766 * This function performs USB callbacks.
1767 *------------------------------------------------------------------------*/
1768static void
1769usb2_callback_proc(struct usb2_proc_msg *_pm)
1770{
1771 struct usb2_done_msg *pm = (void *)_pm;
1725 }
1726 USB_XFER_UNLOCK(xfer);
1727}
1728
1729/*------------------------------------------------------------------------*
1730 * usb2_set_frame_data
1731 *
1732 * This function sets the pointer of the buffer that should

--- 30 unchanged lines hidden (view full) ---

1763 * usb2_callback_proc - factored out code
1764 *
1765 * This function performs USB callbacks.
1766 *------------------------------------------------------------------------*/
1767static void
1768usb2_callback_proc(struct usb2_proc_msg *_pm)
1769{
1770 struct usb2_done_msg *pm = (void *)_pm;
1772 struct usb2_xfer_root *info = pm->usb2_root;
1771 struct usb2_xfer_root *info = pm->xroot;
1773
1774 /* Change locking order */
1775 USB_BUS_UNLOCK(info->bus);
1776
1777 /*
1778 * We exploit the fact that the mutex is the same for all
1779 * callbacks that will be called from this thread:
1780 */
1772
1773 /* Change locking order */
1774 USB_BUS_UNLOCK(info->bus);
1775
1776 /*
1777 * We exploit the fact that the mutex is the same for all
1778 * callbacks that will be called from this thread:
1779 */
1781 mtx_lock(info->priv_mtx);
1780 mtx_lock(info->xfer_mtx);
1782 USB_BUS_LOCK(info->bus);
1783
1784 /* Continue where we lost track */
1785 usb2_command_wrapper(&info->done_q,
1786 info->done_q.curr);
1787
1781 USB_BUS_LOCK(info->bus);
1782
1783 /* Continue where we lost track */
1784 usb2_command_wrapper(&info->done_q,
1785 info->done_q.curr);
1786
1788 mtx_unlock(info->priv_mtx);
1787 mtx_unlock(info->xfer_mtx);
1789}
1790
1791/*------------------------------------------------------------------------*
1792 * usb2_callback_ss_done_defer
1793 *
1794 * This function will defer the start, stop and done callback to the
1795 * correct thread.
1796 *------------------------------------------------------------------------*/
1797static void
1798usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1799{
1788}
1789
1790/*------------------------------------------------------------------------*
1791 * usb2_callback_ss_done_defer
1792 *
1793 * This function will defer the start, stop and done callback to the
1794 * correct thread.
1795 *------------------------------------------------------------------------*/
1796static void
1797usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1798{
1800 struct usb2_xfer_root *info = xfer->usb2_root;
1799 struct usb2_xfer_root *info = xfer->xroot;
1801 struct usb2_xfer_queue *pq = &info->done_q;
1802
1800 struct usb2_xfer_queue *pq = &info->done_q;
1801
1803 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1802 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1804
1805 if (pq->curr != xfer) {
1806 usb2_transfer_enqueue(pq, xfer);
1807 }
1808 if (!pq->recurse_1) {
1809
1810 /*
1811 * We have to postpone the callback due to the fact we

--- 19 unchanged lines hidden (view full) ---

1831 * auto-magic things like figuring out if we can call the callback
1832 * directly from the current context or if we need to wakeup the
1833 * interrupt process.
1834 *------------------------------------------------------------------------*/
1835static void
1836usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1837{
1838 struct usb2_xfer *xfer = pq->curr;
1803
1804 if (pq->curr != xfer) {
1805 usb2_transfer_enqueue(pq, xfer);
1806 }
1807 if (!pq->recurse_1) {
1808
1809 /*
1810 * We have to postpone the callback due to the fact we

--- 19 unchanged lines hidden (view full) ---

1830 * auto-magic things like figuring out if we can call the callback
1831 * directly from the current context or if we need to wakeup the
1832 * interrupt process.
1833 *------------------------------------------------------------------------*/
1834static void
1835usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1836{
1837 struct usb2_xfer *xfer = pq->curr;
1839 struct usb2_xfer_root *info = xfer->usb2_root;
1838 struct usb2_xfer_root *info = xfer->xroot;
1840
1839
1841 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1842 if (!mtx_owned(xfer->xfer_mtx)) {
1840 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1841 if (!mtx_owned(xfer->xroot->xfer_mtx)) {
1843 /*
1844 * Cases that end up here:
1845 *
1846 * 5) HW interrupt done callback or other source.
1847 */
1848 DPRINTFN(3, "case 5\n");
1849
1850 /*

--- 14 unchanged lines hidden (view full) ---

1865 * 2) We are prematurely calling back a transfer
1866 * 3) We are stopping a transfer
1867 * 4) We are doing an ordinary callback
1868 */
1869 DPRINTFN(3, "case 1-4\n");
1870 /* get next USB transfer in the queue */
1871 info->done_q.curr = NULL;
1872
1842 /*
1843 * Cases that end up here:
1844 *
1845 * 5) HW interrupt done callback or other source.
1846 */
1847 DPRINTFN(3, "case 5\n");
1848
1849 /*

--- 14 unchanged lines hidden (view full) ---

1864 * 2) We are prematurely calling back a transfer
1865 * 3) We are stopping a transfer
1866 * 4) We are doing an ordinary callback
1867 */
1868 DPRINTFN(3, "case 1-4\n");
1869 /* get next USB transfer in the queue */
1870 info->done_q.curr = NULL;
1871
1873 USB_BUS_UNLOCK(xfer->udev->bus);
1874 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_NOTOWNED);
1872 USB_BUS_UNLOCK(xfer->xroot->bus);
1873 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED);
1875
1876 /* set correct USB state for callback */
1877 if (!xfer->flags_int.transferring) {
1878 xfer->usb2_state = USB_ST_SETUP;
1879 if (!xfer->flags_int.started) {
1880 /* we got stopped before we even got started */
1874
1875 /* set correct USB state for callback */
1876 if (!xfer->flags_int.transferring) {
1877 xfer->usb2_state = USB_ST_SETUP;
1878 if (!xfer->flags_int.started) {
1879 /* we got stopped before we even got started */
1881 USB_BUS_LOCK(xfer->udev->bus);
1880 USB_BUS_LOCK(xfer->xroot->bus);
1882 goto done;
1883 }
1884 } else {
1885
1886 if (usb2_callback_wrapper_sub(xfer)) {
1887 /* the callback has been deferred */
1881 goto done;
1882 }
1883 } else {
1884
1885 if (usb2_callback_wrapper_sub(xfer)) {
1886 /* the callback has been deferred */
1888 USB_BUS_LOCK(xfer->udev->bus);
1887 USB_BUS_LOCK(xfer->xroot->bus);
1889 goto done;
1890 }
1891 /* decrement power reference */
1892 usb2_transfer_power_ref(xfer, -1);
1893
1894 xfer->flags_int.transferring = 0;
1895
1896 if (xfer->error) {

--- 9 unchanged lines hidden (view full) ---

1906 }
1907 }
1908 }
1909
1910 /* call processing routine */
1911 (xfer->callback) (xfer);
1912
1913 /* pickup the USB mutex again */
1888 goto done;
1889 }
1890 /* decrement power reference */
1891 usb2_transfer_power_ref(xfer, -1);
1892
1893 xfer->flags_int.transferring = 0;
1894
1895 if (xfer->error) {

--- 9 unchanged lines hidden (view full) ---

1905 }
1906 }
1907 }
1908
1909 /* call processing routine */
1910 (xfer->callback) (xfer);
1911
1912 /* pickup the USB mutex again */
1914 USB_BUS_LOCK(xfer->udev->bus);
1913 USB_BUS_LOCK(xfer->xroot->bus);
1915
1916 /*
1917 * Check if we got started after that we got cancelled, but
1918 * before we managed to do the callback.
1919 */
1920 if ((!xfer->flags_int.open) &&
1921 (xfer->flags_int.started) &&
1922 (xfer->usb2_state == USB_ST_ERROR)) {

--- 5 unchanged lines hidden (view full) ---

1928done:
1929 /*
1930 * Check if we are draining.
1931 */
1932 if (xfer->flags_int.draining &&
1933 (!xfer->flags_int.transferring)) {
1934 /* "usb2_transfer_drain()" is waiting for end of transfer */
1935 xfer->flags_int.draining = 0;
1914
1915 /*
1916 * Check if we got started after that we got cancelled, but
1917 * before we managed to do the callback.
1918 */
1919 if ((!xfer->flags_int.open) &&
1920 (xfer->flags_int.started) &&
1921 (xfer->usb2_state == USB_ST_ERROR)) {

--- 5 unchanged lines hidden (view full) ---

1927done:
1928 /*
1929 * Check if we are draining.
1930 */
1931 if (xfer->flags_int.draining &&
1932 (!xfer->flags_int.transferring)) {
1933 /* "usb2_transfer_drain()" is waiting for end of transfer */
1934 xfer->flags_int.draining = 0;
1936 usb2_cv_broadcast(&xfer->usb2_root->cv_drain);
1935 usb2_cv_broadcast(&xfer->xroot->cv_drain);
1937 }
1938
1939 /* do the next callback, if any */
1940 usb2_command_wrapper(&info->done_q,
1941 info->done_q.curr);
1942}
1943
1944/*------------------------------------------------------------------------*

--- 4 unchanged lines hidden (view full) ---

1949 * transfer. This code path is ususally only used when there is an USB
1950 * error like USB_ERR_CANCELLED.
1951 *------------------------------------------------------------------------*/
1952static void
1953usb2_dma_delay_done_cb(void *arg)
1954{
1955 struct usb2_xfer *xfer = arg;
1956
1936 }
1937
1938 /* do the next callback, if any */
1939 usb2_command_wrapper(&info->done_q,
1940 info->done_q.curr);
1941}
1942
1943/*------------------------------------------------------------------------*

--- 4 unchanged lines hidden (view full) ---

1948 * transfer. This code path is ususally only used when there is an USB
1949 * error like USB_ERR_CANCELLED.
1950 *------------------------------------------------------------------------*/
1951static void
1952usb2_dma_delay_done_cb(void *arg)
1953{
1954 struct usb2_xfer *xfer = arg;
1955
1957 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1956 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1958
1959 DPRINTFN(3, "Completed %p\n", xfer);
1960
1961 /* queue callback for execution, again */
1962 usb2_transfer_done(xfer, 0);
1963}
1964
1965/*------------------------------------------------------------------------*

--- 48 unchanged lines hidden (view full) ---

2014 *
2015 * - This function is used to stop any USB transfer timeouts.
2016 *------------------------------------------------------------------------*/
2017void
2018usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2019{
2020 struct usb2_xfer_queue *pq;
2021
1957
1958 DPRINTFN(3, "Completed %p\n", xfer);
1959
1960 /* queue callback for execution, again */
1961 usb2_transfer_done(xfer, 0);
1962}
1963
1964/*------------------------------------------------------------------------*

--- 48 unchanged lines hidden (view full) ---

2013 *
2014 * - This function is used to stop any USB transfer timeouts.
2015 *------------------------------------------------------------------------*/
2016void
2017usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2018{
2019 struct usb2_xfer_queue *pq;
2020
2022 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2021 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2023
2024 DPRINTF("err=%s\n", usb2_errstr(error));
2025
2026 /*
2027 * If we are not transferring then just return.
2028 * This can happen during transfer cancel.
2029 */
2030 if (!xfer->flags_int.transferring) {

--- 9 unchanged lines hidden (view full) ---

2040
2041 /*
2042 * If we are waiting on a queue, just remove the USB transfer
2043 * from the queue, if any. We should have the required locks
2044 * locked to do the remove when this function is called.
2045 */
2046 usb2_transfer_dequeue(xfer);
2047
2022
2023 DPRINTF("err=%s\n", usb2_errstr(error));
2024
2025 /*
2026 * If we are not transferring then just return.
2027 * This can happen during transfer cancel.
2028 */
2029 if (!xfer->flags_int.transferring) {

--- 9 unchanged lines hidden (view full) ---

2039
2040 /*
2041 * If we are waiting on a queue, just remove the USB transfer
2042 * from the queue, if any. We should have the required locks
2043 * locked to do the remove when this function is called.
2044 */
2045 usb2_transfer_dequeue(xfer);
2046
2048 if (mtx_owned(xfer->xfer_mtx)) {
2047 if (mtx_owned(xfer->xroot->xfer_mtx)) {
2049 /*
2050 * If the private USB lock is not locked, then we assume
2051 * that the BUS-DMA load stage has been passed:
2052 */
2048 /*
2049 * If the private USB lock is not locked, then we assume
2050 * that the BUS-DMA load stage has been passed:
2051 */
2053 pq = &xfer->usb2_root->dma_q;
2052 pq = &xfer->xroot->dma_q;
2054
2055 if (pq->curr == xfer) {
2056 /* start the next BUS-DMA load, if any */
2057 usb2_command_wrapper(pq, NULL);
2058 }
2059 }
2060 /* keep some statistics */
2061 if (xfer->error) {
2053
2054 if (pq->curr == xfer) {
2055 /* start the next BUS-DMA load, if any */
2056 usb2_command_wrapper(pq, NULL);
2057 }
2058 }
2059 /* keep some statistics */
2060 if (xfer->error) {
2062 xfer->udev->bus->stats_err.uds_requests
2061 xfer->xroot->bus->stats_err.uds_requests
2063 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2064 } else {
2062 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2063 } else {
2065 xfer->udev->bus->stats_ok.uds_requests
2064 xfer->xroot->bus->stats_ok.uds_requests
2066 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2067 }
2068
2069 /* call the USB transfer callback */
2070 usb2_callback_ss_done_defer(xfer);
2071}
2072
2073/*------------------------------------------------------------------------*

--- 4 unchanged lines hidden (view full) ---

2078 * BULK or CONTROL.
2079 *------------------------------------------------------------------------*/
2080static void
2081usb2_transfer_start_cb(void *arg)
2082{
2083 struct usb2_xfer *xfer = arg;
2084 struct usb2_pipe *pipe = xfer->pipe;
2085
2065 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++;
2066 }
2067
2068 /* call the USB transfer callback */
2069 usb2_callback_ss_done_defer(xfer);
2070}
2071
2072/*------------------------------------------------------------------------*

--- 4 unchanged lines hidden (view full) ---

2077 * BULK or CONTROL.
2078 *------------------------------------------------------------------------*/
2079static void
2080usb2_transfer_start_cb(void *arg)
2081{
2082 struct usb2_xfer *xfer = arg;
2083 struct usb2_pipe *pipe = xfer->pipe;
2084
2086 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2085 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2087
2088 DPRINTF("start\n");
2089
2090 /* start the transfer */
2091 (pipe->methods->start) (xfer);
2092
2093 /* check cancelability */
2094 if (pipe->methods->start_is_cancelable) {

--- 18 unchanged lines hidden (view full) ---

2113{
2114 if (xfer == NULL) {
2115 /* tearing down */
2116 return;
2117 }
2118 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2119
2120 /* avoid any races by locking the USB mutex */
2086
2087 DPRINTF("start\n");
2088
2089 /* start the transfer */
2090 (pipe->methods->start) (xfer);
2091
2092 /* check cancelability */
2093 if (pipe->methods->start_is_cancelable) {

--- 18 unchanged lines hidden (view full) ---

2112{
2113 if (xfer == NULL) {
2114 /* tearing down */
2115 return;
2116 }
2117 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2118
2119 /* avoid any races by locking the USB mutex */
2121 USB_BUS_LOCK(xfer->udev->bus);
2120 USB_BUS_LOCK(xfer->xroot->bus);
2122
2123 xfer->flags.stall_pipe = 1;
2124
2121
2122 xfer->flags.stall_pipe = 1;
2123
2125 USB_BUS_UNLOCK(xfer->udev->bus);
2124 USB_BUS_UNLOCK(xfer->xroot->bus);
2126}
2127
2128/*------------------------------------------------------------------------*
2129 * usb2_transfer_clear_stall
2130 *
2131 * This function is used to clear the stall flag outside the
2132 * callback. This function is NULL safe.
2133 *------------------------------------------------------------------------*/
2134void
2135usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2136{
2137 if (xfer == NULL) {
2138 /* tearing down */
2139 return;
2140 }
2141 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2142
2143 /* avoid any races by locking the USB mutex */
2125}
2126
2127/*------------------------------------------------------------------------*
2128 * usb2_transfer_clear_stall
2129 *
2130 * This function is used to clear the stall flag outside the
2131 * callback. This function is NULL safe.
2132 *------------------------------------------------------------------------*/
2133void
2134usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2135{
2136 if (xfer == NULL) {
2137 /* tearing down */
2138 return;
2139 }
2140 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2141
2142 /* avoid any races by locking the USB mutex */
2144 USB_BUS_LOCK(xfer->udev->bus);
2143 USB_BUS_LOCK(xfer->xroot->bus);
2145
2146 xfer->flags.stall_pipe = 0;
2147
2144
2145 xfer->flags.stall_pipe = 0;
2146
2148 USB_BUS_UNLOCK(xfer->udev->bus);
2147 USB_BUS_UNLOCK(xfer->xroot->bus);
2149}
2150
2151/*------------------------------------------------------------------------*
2152 * usb2_pipe_start
2153 *
2154 * This function is used to add an USB transfer to the pipe transfer list.
2155 *------------------------------------------------------------------------*/
2156void
2157usb2_pipe_start(struct usb2_xfer_queue *pq)
2158{
2159 struct usb2_pipe *pipe;
2160 struct usb2_xfer *xfer;
2161 uint8_t type;
2162
2163 xfer = pq->curr;
2164 pipe = xfer->pipe;
2165
2148}
2149
2150/*------------------------------------------------------------------------*
2151 * usb2_pipe_start
2152 *
2153 * This function is used to add an USB transfer to the pipe transfer list.
2154 *------------------------------------------------------------------------*/
2155void
2156usb2_pipe_start(struct usb2_xfer_queue *pq)
2157{
2158 struct usb2_pipe *pipe;
2159 struct usb2_xfer *xfer;
2160 uint8_t type;
2161
2162 xfer = pq->curr;
2163 pipe = xfer->pipe;
2164
2166 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2165 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2167
2168 /*
2169 * If the pipe is already stalled we do nothing !
2170 */
2171 if (pipe->is_stalled) {
2172 return;
2173 }
2174 /*

--- 7 unchanged lines hidden (view full) ---

2182 * Only stall BULK and INTERRUPT endpoints.
2183 */
2184 type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2185 if ((type == UE_BULK) ||
2186 (type == UE_INTERRUPT)) {
2187 struct usb2_device *udev;
2188 struct usb2_xfer_root *info;
2189
2166
2167 /*
2168 * If the pipe is already stalled we do nothing !
2169 */
2170 if (pipe->is_stalled) {
2171 return;
2172 }
2173 /*

--- 7 unchanged lines hidden (view full) ---

2181 * Only stall BULK and INTERRUPT endpoints.
2182 */
2183 type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
2184 if ((type == UE_BULK) ||
2185 (type == UE_INTERRUPT)) {
2186 struct usb2_device *udev;
2187 struct usb2_xfer_root *info;
2188
2190 udev = xfer->udev;
2189 info = xfer->xroot;
2190 udev = info->udev;
2191 pipe->is_stalled = 1;
2192
2193 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2194 (udev->bus->methods->set_stall) (
2195 udev, NULL, pipe);
2196 } else if (udev->default_xfer[1]) {
2191 pipe->is_stalled = 1;
2192
2193 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2194 (udev->bus->methods->set_stall) (
2195 udev, NULL, pipe);
2196 } else if (udev->default_xfer[1]) {
2197 info = udev->default_xfer[1]->usb2_root;
2197 info = udev->default_xfer[1]->xroot;
2198 if (usb2_proc_msignal(&info->done_p,
2199 &udev->cs_msg[0], &udev->cs_msg[1])) {
2200 /* ignore */
2201 }
2202 } else {
2203 /* should not happen */
2204 DPRINTFN(0, "No stall handler!\n");
2205 }

--- 54 unchanged lines hidden (view full) ---

2260 * This function is used to setup a timeout on the given USB
2261 * transfer. If the timeout has been deferred the callback given by
2262 * "cb" will get called after "ms" milliseconds.
2263 *------------------------------------------------------------------------*/
2264void
2265usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2266 void (*cb) (void *arg), uint32_t ms)
2267{
2198 if (usb2_proc_msignal(&info->done_p,
2199 &udev->cs_msg[0], &udev->cs_msg[1])) {
2200 /* ignore */
2201 }
2202 } else {
2203 /* should not happen */
2204 DPRINTFN(0, "No stall handler!\n");
2205 }

--- 54 unchanged lines hidden (view full) ---

2260 * This function is used to setup a timeout on the given USB
2261 * transfer. If the timeout has been deferred the callback given by
2262 * "cb" will get called after "ms" milliseconds.
2263 *------------------------------------------------------------------------*/
2264void
2265usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2266 void (*cb) (void *arg), uint32_t ms)
2267{
2268 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2268 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2269
2270 /* defer delay */
2271 usb2_callout_reset(&xfer->timeout_handle,
2272 USB_MS_TO_TICKS(ms), cb, xfer);
2273}
2274
2275/*------------------------------------------------------------------------*
2276 * usb2_callback_wrapper_sub

--- 16 unchanged lines hidden (view full) ---

2293usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2294{
2295 struct usb2_pipe *pipe;
2296 uint32_t x;
2297
2298 if ((!xfer->flags_int.open) &&
2299 (!xfer->flags_int.did_close)) {
2300 DPRINTF("close\n");
2269
2270 /* defer delay */
2271 usb2_callout_reset(&xfer->timeout_handle,
2272 USB_MS_TO_TICKS(ms), cb, xfer);
2273}
2274
2275/*------------------------------------------------------------------------*
2276 * usb2_callback_wrapper_sub

--- 16 unchanged lines hidden (view full) ---

2293usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2294{
2295 struct usb2_pipe *pipe;
2296 uint32_t x;
2297
2298 if ((!xfer->flags_int.open) &&
2299 (!xfer->flags_int.did_close)) {
2300 DPRINTF("close\n");
2301 USB_BUS_LOCK(xfer->udev->bus);
2301 USB_BUS_LOCK(xfer->xroot->bus);
2302 (xfer->pipe->methods->close) (xfer);
2302 (xfer->pipe->methods->close) (xfer);
2303 USB_BUS_UNLOCK(xfer->udev->bus);
2303 USB_BUS_UNLOCK(xfer->xroot->bus);
2304 /* only close once */
2305 xfer->flags_int.did_close = 1;
2306 return (1); /* wait for new callback */
2307 }
2308 /*
2309 * If we have a non-hardware induced error we
2310 * need to do the DMA delay!
2311 */

--- 4 unchanged lines hidden (view full) ---

2316 uint32_t temp;
2317
2318 /* only delay once */
2319 xfer->flags_int.did_dma_delay = 1;
2320
2321 /* we can not cancel this delay */
2322 xfer->flags_int.can_cancel_immed = 0;
2323
2304 /* only close once */
2305 xfer->flags_int.did_close = 1;
2306 return (1); /* wait for new callback */
2307 }
2308 /*
2309 * If we have a non-hardware induced error we
2310 * need to do the DMA delay!
2311 */

--- 4 unchanged lines hidden (view full) ---

2316 uint32_t temp;
2317
2318 /* only delay once */
2319 xfer->flags_int.did_dma_delay = 1;
2320
2321 /* we can not cancel this delay */
2322 xfer->flags_int.can_cancel_immed = 0;
2323
2324 temp = usb2_get_dma_delay(xfer->udev->bus);
2324 temp = usb2_get_dma_delay(xfer->xroot->bus);
2325
2326 DPRINTFN(3, "DMA delay, %u ms, "
2327 "on %p\n", temp, xfer);
2328
2329 if (temp != 0) {
2325
2326 DPRINTFN(3, "DMA delay, %u ms, "
2327 "on %p\n", temp, xfer);
2328
2329 if (temp != 0) {
2330 USB_BUS_LOCK(xfer->udev->bus);
2330 USB_BUS_LOCK(xfer->xroot->bus);
2331 usb2_transfer_timeout_ms(xfer,
2332 &usb2_dma_delay_done_cb, temp);
2331 usb2_transfer_timeout_ms(xfer,
2332 &usb2_dma_delay_done_cb, temp);
2333 USB_BUS_UNLOCK(xfer->udev->bus);
2333 USB_BUS_UNLOCK(xfer->xroot->bus);
2334 return (1); /* wait for new callback */
2335 }
2336 }
2337 /* check actual number of frames */
2338 if (xfer->aframes > xfer->nframes) {
2339 if (xfer->error == 0) {
2340 panic("%s: actual number of frames, %d, is "
2341 "greater than initial number of frames, %d!\n",

--- 75 unchanged lines hidden (view full) ---

2417 }
2418
2419 pipe = xfer->pipe;
2420
2421 /*
2422 * If the current USB transfer is completing we need to start the
2423 * next one:
2424 */
2334 return (1); /* wait for new callback */
2335 }
2336 }
2337 /* check actual number of frames */
2338 if (xfer->aframes > xfer->nframes) {
2339 if (xfer->error == 0) {
2340 panic("%s: actual number of frames, %d, is "
2341 "greater than initial number of frames, %d!\n",

--- 75 unchanged lines hidden (view full) ---

2417 }
2418
2419 pipe = xfer->pipe;
2420
2421 /*
2422 * If the current USB transfer is completing we need to start the
2423 * next one:
2424 */
2425 USB_BUS_LOCK(xfer->udev->bus);
2425 USB_BUS_LOCK(xfer->xroot->bus);
2426 if (pipe->pipe_q.curr == xfer) {
2427 usb2_command_wrapper(&pipe->pipe_q, NULL);
2428
2429 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2430 /* there is another USB transfer waiting */
2431 } else {
2432 /* this is the last USB transfer */
2433 /* clear isochronous sync flag */
2434 xfer->pipe->is_synced = 0;
2435 }
2436 }
2426 if (pipe->pipe_q.curr == xfer) {
2427 usb2_command_wrapper(&pipe->pipe_q, NULL);
2428
2429 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2430 /* there is another USB transfer waiting */
2431 } else {
2432 /* this is the last USB transfer */
2433 /* clear isochronous sync flag */
2434 xfer->pipe->is_synced = 0;
2435 }
2436 }
2437 USB_BUS_UNLOCK(xfer->udev->bus);
2437 USB_BUS_UNLOCK(xfer->xroot->bus);
2438done:
2439 return (0);
2440}
2441
2442/*------------------------------------------------------------------------*
2443 * usb2_command_wrapper
2444 *
2445 * This function is used to execute commands non-recursivly on an USB

--- 186 unchanged lines hidden (view full) ---

2632 switch (USB_GET_STATE(xfer1)) {
2633 case USB_ST_SETUP:
2634
2635 /*
2636 * pre-clear the data toggle to DATA0 ("umass.c" and
2637 * "ata-usb.c" depends on this)
2638 */
2639
2438done:
2439 return (0);
2440}
2441
2442/*------------------------------------------------------------------------*
2443 * usb2_command_wrapper
2444 *
2445 * This function is used to execute commands non-recursivly on an USB

--- 186 unchanged lines hidden (view full) ---

2632 switch (USB_GET_STATE(xfer1)) {
2633 case USB_ST_SETUP:
2634
2635 /*
2636 * pre-clear the data toggle to DATA0 ("umass.c" and
2637 * "ata-usb.c" depends on this)
2638 */
2639
2640 usb2_clear_data_toggle(xfer2->udev, xfer2->pipe);
2640 usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe);
2641
2642 /* setup a clear-stall packet */
2643
2644 req.bmRequestType = UT_WRITE_ENDPOINT;
2645 req.bRequest = UR_CLEAR_FEATURE;
2646 USETW(req.wValue, UF_ENDPOINT_HALT);
2647 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress;
2648 req.wIndex[1] = 0;

--- 76 unchanged lines hidden (view full) ---

2725 *
2726 * This function is called from keyboard driver when in polling
2727 * mode.
2728 *------------------------------------------------------------------------*/
2729void
2730usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2731{
2732 struct usb2_xfer *xfer;
2641
2642 /* setup a clear-stall packet */
2643
2644 req.bmRequestType = UT_WRITE_ENDPOINT;
2645 req.bRequest = UR_CLEAR_FEATURE;
2646 USETW(req.wValue, UF_ENDPOINT_HALT);
2647 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress;
2648 req.wIndex[1] = 0;

--- 76 unchanged lines hidden (view full) ---

2725 *
2726 * This function is called from keyboard driver when in polling
2727 * mode.
2728 *------------------------------------------------------------------------*/
2729void
2730usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2731{
2732 struct usb2_xfer *xfer;
2733 struct usb2_xfer_root *usb2_root;
2733 struct usb2_xfer_root *xroot;
2734 struct usb2_device *udev;
2735 struct usb2_proc_msg *pm;
2736 uint32_t to;
2737 uint16_t n;
2738
2739 /* compute system tick delay */
2740 to = ((uint32_t)(1000000)) / ((uint32_t)(hz));
2741 DELAY(to);
2742 atomic_add_int((volatile int *)&ticks, 1);
2743
2744 for (n = 0; n != max; n++) {
2745 xfer = ppxfer[n];
2746 if (xfer) {
2734 struct usb2_device *udev;
2735 struct usb2_proc_msg *pm;
2736 uint32_t to;
2737 uint16_t n;
2738
2739 /* compute system tick delay */
2740 to = ((uint32_t)(1000000)) / ((uint32_t)(hz));
2741 DELAY(to);
2742 atomic_add_int((volatile int *)&ticks, 1);
2743
2744 for (n = 0; n != max; n++) {
2745 xfer = ppxfer[n];
2746 if (xfer) {
2747 usb2_root = xfer->usb2_root;
2748 udev = xfer->udev;
2747 xroot = xfer->xroot;
2748 udev = xroot->udev;
2749
2750 /*
2751 * Poll hardware - signal that we are polling by
2752 * locking the private mutex:
2753 */
2754 USB_XFER_LOCK(xfer);
2755 (udev->bus->methods->do_poll) (udev->bus);
2756 USB_XFER_UNLOCK(xfer);
2757
2758 /* poll clear stall start */
2749
2750 /*
2751 * Poll hardware - signal that we are polling by
2752 * locking the private mutex:
2753 */
2754 USB_XFER_LOCK(xfer);
2755 (udev->bus->methods->do_poll) (udev->bus);
2756 USB_XFER_UNLOCK(xfer);
2757
2758 /* poll clear stall start */
2759 USB_BUS_LOCK(xfer->udev->bus);
2759 USB_BUS_LOCK(xfer->xroot->bus);
2760 pm = &udev->cs_msg[0].hdr;
2761 (pm->pm_callback) (pm);
2760 pm = &udev->cs_msg[0].hdr;
2761 (pm->pm_callback) (pm);
2762 USB_BUS_UNLOCK(xfer->udev->bus);
2762 USB_BUS_UNLOCK(xfer->xroot->bus);
2763
2764 if (udev->default_xfer[1]) {
2765
2766 /* poll timeout */
2767 usb2_callout_poll(udev->default_xfer[1]);
2768
2769 /* poll clear stall done thread */
2763
2764 if (udev->default_xfer[1]) {
2765
2766 /* poll timeout */
2767 usb2_callout_poll(udev->default_xfer[1]);
2768
2769 /* poll clear stall done thread */
2770 USB_BUS_LOCK(xfer->udev->bus);
2770 USB_BUS_LOCK(xfer->xroot->bus);
2771 pm = &udev->default_xfer[1]->
2771 pm = &udev->default_xfer[1]->
2772 usb2_root->done_m[0].hdr;
2772 xroot->done_m[0].hdr;
2773 (pm->pm_callback) (pm);
2773 (pm->pm_callback) (pm);
2774 USB_BUS_UNLOCK(xfer->udev->bus);
2774 USB_BUS_UNLOCK(xfer->xroot->bus);
2775 }
2776 /* poll timeout */
2777 usb2_callout_poll(xfer);
2778
2779 /* poll done thread */
2775 }
2776 /* poll timeout */
2777 usb2_callout_poll(xfer);
2778
2779 /* poll done thread */
2780 USB_BUS_LOCK(xfer->udev->bus);
2781 pm = &usb2_root->done_m[0].hdr;
2780 USB_BUS_LOCK(xfer->xroot->bus);
2781 pm = &xroot->done_m[0].hdr;
2782 (pm->pm_callback) (pm);
2782 (pm->pm_callback) (pm);
2783 USB_BUS_UNLOCK(xfer->udev->bus);
2783 USB_BUS_UNLOCK(xfer->xroot->bus);
2784 }
2785 }
2786}
2787
2788#else
2789
2790void
2791usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2792{
2793 /* polling not supported */
2794}
2795
2796#endif
2784 }
2785 }
2786}
2787
2788#else
2789
2790void
2791usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2792{
2793 /* polling not supported */
2794}
2795
2796#endif