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

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

797
798 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
799 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
800 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
801 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
802
803 usb2_cv_init(&info->cv_drain, "WDRAIN");
804
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.

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

797
798 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]);
799 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]);
800 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]);
801 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]);
802
803 usb2_cv_init(&info->cv_drain, "WDRAIN");
804
805 info->usb2_mtx = &udev->bus->mtx;
806 info->priv_mtx = priv_mtx;
807
808 usb2_dma_tag_setup(&info->dma_parent_tag,
809 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
810 priv_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
811
812 info->bus = udev->bus;
813

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

820 info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
821 info->done_m[0].usb2_root = info;
822 info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
823 info->done_m[1].usb2_root = info;
824
825 /* create a callback thread */
826
827 if (usb2_proc_setup(&info->done_p,
805 info->priv_mtx = priv_mtx;
806
807 usb2_dma_tag_setup(&info->dma_parent_tag,
808 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag,
809 priv_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max);
810
811 info->bus = udev->bus;
812

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

819 info->done_m[0].hdr.pm_callback = &usb2_callback_proc;
820 info->done_m[0].usb2_root = info;
821 info->done_m[1].hdr.pm_callback = &usb2_callback_proc;
822 info->done_m[1].usb2_root = info;
823
824 /* create a callback thread */
825
826 if (usb2_proc_setup(&info->done_p,
828 &udev->bus->mtx, USB_PRI_HIGH)) {
827 &udev->bus->bus_mtx, USB_PRI_HIGH)) {
829 parm.err = USB_ERR_NO_INTR_THREAD;
830 goto done;
831 }
832 }
833 /* reset sizes */
834
835 parm.size[0] = 0;
836 parm.buf = buf;

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

873 * "usb2_xfer" structure.
874 */
875 xfer = USB_ADD_BYTES(buf, parm.size[0]);
876
877 ppxfer[n] = xfer;
878 xfer->udev = udev;
879 xfer->address = udev->address;
880 xfer->priv_sc = priv_sc;
828 parm.err = USB_ERR_NO_INTR_THREAD;
829 goto done;
830 }
831 }
832 /* reset sizes */
833
834 parm.size[0] = 0;
835 parm.buf = buf;

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

872 * "usb2_xfer" structure.
873 */
874 xfer = USB_ADD_BYTES(buf, parm.size[0]);
875
876 ppxfer[n] = xfer;
877 xfer->udev = udev;
878 xfer->address = udev->address;
879 xfer->priv_sc = priv_sc;
881 xfer->priv_mtx = priv_mtx;
882 xfer->usb2_mtx = &udev->bus->mtx;
880 xfer->xfer_mtx = priv_mtx;
883 xfer->usb2_root = info;
884 info->setup_refcount++;
885
881 xfer->usb2_root = info;
882 info->setup_refcount++;
883
886 usb2_callout_init_mtx(&xfer->timeout_handle, xfer->usb2_mtx,
887 CALLOUT_RETURNUNLOCKED);
884 usb2_callout_init_mtx(&xfer->timeout_handle,
885 &udev->bus->bus_mtx, CALLOUT_RETURNUNLOCKED);
888 } else {
889 /*
890 * Setup a dummy xfer, hence we are
891 * writing to the "usb2_xfer"
892 * structure pointed to by "xfer"
893 * before we have allocated any
894 * memory:
895 */

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

1015 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1016 }
1017
1018done:
1019 if (buf) {
1020 if (info->setup_refcount == 0) {
1021 /*
1022 * "usb2_transfer_unsetup_sub" will unlock
886 } else {
887 /*
888 * Setup a dummy xfer, hence we are
889 * writing to the "usb2_xfer"
890 * structure pointed to by "xfer"
891 * before we have allocated any
892 * memory:
893 */

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

1013 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]);
1014 }
1015
1016done:
1017 if (buf) {
1018 if (info->setup_refcount == 0) {
1019 /*
1020 * "usb2_transfer_unsetup_sub" will unlock
1023 * "usb2_mtx" before returning !
1021 * the bus mutex before returning !
1024 */
1022 */
1025 mtx_lock(info->usb2_mtx);
1023 USB_BUS_LOCK(info->bus);
1026
1027 /* something went wrong */
1028 usb2_transfer_unsetup_sub(info, 0);
1029 }
1030 }
1031 if (parm.err) {
1032 usb2_transfer_unsetup(ppxfer, n_setup);
1033 }

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

1038 * usb2_transfer_unsetup_sub - factored out code
1039 *------------------------------------------------------------------------*/
1040static void
1041usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay)
1042{
1043 struct usb2_page_cache *pc;
1044 uint32_t temp;
1045
1024
1025 /* something went wrong */
1026 usb2_transfer_unsetup_sub(info, 0);
1027 }
1028 }
1029 if (parm.err) {
1030 usb2_transfer_unsetup(ppxfer, n_setup);
1031 }

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

1036 * usb2_transfer_unsetup_sub - factored out code
1037 *------------------------------------------------------------------------*/
1038static void
1039usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay)
1040{
1041 struct usb2_page_cache *pc;
1042 uint32_t temp;
1043
1046 mtx_assert(info->usb2_mtx, MA_OWNED);
1044 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1047
1048 /* wait for any outstanding DMA operations */
1049
1050 if (needs_delay) {
1051 temp = usb2_get_dma_delay(info->bus);
1045
1046 /* wait for any outstanding DMA operations */
1047
1048 if (needs_delay) {
1049 temp = usb2_get_dma_delay(info->bus);
1052 usb2_pause_mtx(info->usb2_mtx, temp);
1050 usb2_pause_mtx(&info->bus->bus_mtx, temp);
1053 }
1051 }
1054 mtx_unlock(info->usb2_mtx);
1052 USB_BUS_UNLOCK(info->bus);
1055
1056 /* wait for interrupt thread to exit */
1057 usb2_proc_unsetup(&info->done_p);
1058
1059 /* free DMA'able memory, if any */
1060 pc = info->dma_page_cache_start;
1061 while (pc != info->dma_page_cache_end) {
1062 usb2_pc_free_mem(pc);

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

1100 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1101 "usb2_transfer_unsetup can sleep!");
1102
1103 while (n_setup--) {
1104 xfer = pxfer[n_setup];
1105
1106 if (xfer) {
1107 if (xfer->pipe) {
1053
1054 /* wait for interrupt thread to exit */
1055 usb2_proc_unsetup(&info->done_p);
1056
1057 /* free DMA'able memory, if any */
1058 pc = info->dma_page_cache_start;
1059 while (pc != info->dma_page_cache_end) {
1060 usb2_pc_free_mem(pc);

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

1098 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1099 "usb2_transfer_unsetup can sleep!");
1100
1101 while (n_setup--) {
1102 xfer = pxfer[n_setup];
1103
1104 if (xfer) {
1105 if (xfer->pipe) {
1108 mtx_lock(xfer->priv_mtx);
1109 mtx_lock(xfer->usb2_mtx);
1106 USB_XFER_LOCK(xfer);
1107 USB_BUS_LOCK(xfer->udev->bus);
1110
1111 /*
1112 * HINT: when you start/stop a transfer, it
1113 * might be a good idea to directly use the
1114 * "pxfer[]" structure:
1115 *
1116 * usb2_transfer_start(sc->pxfer[0]);
1117 * usb2_transfer_stop(sc->pxfer[0]);

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

1125 * argument.
1126 *
1127 * To avoid any races we clear the "pxfer[]"
1128 * pointer while holding the private mutex
1129 * of the driver:
1130 */
1131 pxfer[n_setup] = NULL;
1132
1108
1109 /*
1110 * HINT: when you start/stop a transfer, it
1111 * might be a good idea to directly use the
1112 * "pxfer[]" structure:
1113 *
1114 * usb2_transfer_start(sc->pxfer[0]);
1115 * usb2_transfer_stop(sc->pxfer[0]);

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

1123 * argument.
1124 *
1125 * To avoid any races we clear the "pxfer[]"
1126 * pointer while holding the private mutex
1127 * of the driver:
1128 */
1129 pxfer[n_setup] = NULL;
1130
1133 mtx_unlock(xfer->usb2_mtx);
1134 mtx_unlock(xfer->priv_mtx);
1131 USB_BUS_UNLOCK(xfer->udev->bus);
1132 USB_XFER_UNLOCK(xfer);
1135
1136 usb2_transfer_drain(xfer);
1137
1138 if (xfer->flags_int.bdma_enable) {
1139 needs_delay = 1;
1140 }
1141 /*
1142 * NOTE: default pipe does not have an

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

1149 pxfer[n_setup] = NULL;
1150 }
1151
1152 usb2_callout_drain(&xfer->timeout_handle);
1153
1154 if (xfer->usb2_root) {
1155 info = xfer->usb2_root;
1156
1133
1134 usb2_transfer_drain(xfer);
1135
1136 if (xfer->flags_int.bdma_enable) {
1137 needs_delay = 1;
1138 }
1139 /*
1140 * NOTE: default pipe does not have an

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

1147 pxfer[n_setup] = NULL;
1148 }
1149
1150 usb2_callout_drain(&xfer->timeout_handle);
1151
1152 if (xfer->usb2_root) {
1153 info = xfer->usb2_root;
1154
1157 mtx_lock(info->usb2_mtx);
1155 USB_BUS_LOCK(info->bus);
1158
1159 USB_ASSERT(info->setup_refcount != 0,
1160 ("Invalid setup "
1161 "reference count!\n"));
1162
1163 info->setup_refcount--;
1164
1165 if (info->setup_refcount == 0) {
1166 usb2_transfer_unsetup_sub(info,
1167 needs_delay);
1168 } else {
1156
1157 USB_ASSERT(info->setup_refcount != 0,
1158 ("Invalid setup "
1159 "reference count!\n"));
1160
1161 info->setup_refcount--;
1162
1163 if (info->setup_refcount == 0) {
1164 usb2_transfer_unsetup_sub(info,
1165 needs_delay);
1166 } else {
1169 mtx_unlock(info->usb2_mtx);
1167 USB_BUS_UNLOCK(info->bus);
1170 }
1171 }
1172 }
1173 }
1174 return;
1175}
1176
1177/*------------------------------------------------------------------------*

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

1353 uint32_t x;
1354
1355 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n",
1356 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1357 "read" : "write");
1358
1359#if USB_DEBUG
1360 if (USB_DEBUG_VAR > 0) {
1168 }
1169 }
1170 }
1171 }
1172 return;
1173}
1174
1175/*------------------------------------------------------------------------*

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

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

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

1416 if (xfer->nframes == 0) {
1417 if (xfer->flags.stall_pipe) {
1418 /*
1419 * Special case - want to stall without transferring
1420 * any data:
1421 */
1422 DPRINTF("xfer=%p nframes=0: stall "
1423 "or clear stall!\n", xfer);
1391 }
1392 /* clear "did_dma_delay" flag */
1393 xfer->flags_int.did_dma_delay = 0;
1394
1395 /* clear "did_close" flag */
1396 xfer->flags_int.did_close = 0;
1397
1398 /* clear "bdma_setup" flag */

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

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

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

1508/*------------------------------------------------------------------------*
1509 * usb2_pipe_enter - factored out code
1510 *------------------------------------------------------------------------*/
1511void
1512usb2_pipe_enter(struct usb2_xfer *xfer)
1513{
1514 struct usb2_pipe *pipe;
1515
1460 return;
1461 }
1462 }
1463 /*
1464 * Setup filtered version of some transfer flags,
1465 * in case of data read direction
1466 */
1467 if (USB_GET_DATA_ISREAD(xfer)) {

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

1506/*------------------------------------------------------------------------*
1507 * usb2_pipe_enter - factored out code
1508 *------------------------------------------------------------------------*/
1509void
1510usb2_pipe_enter(struct usb2_xfer *xfer)
1511{
1512 struct usb2_pipe *pipe;
1513
1516 mtx_assert(xfer->priv_mtx, MA_OWNED);
1514 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1517
1515
1518 mtx_lock(xfer->usb2_mtx);
1516 USB_BUS_LOCK(xfer->udev->bus);
1519
1520 pipe = xfer->pipe;
1521
1522 DPRINTF("enter\n");
1523
1524 /* enter the transfer */
1525 (pipe->methods->enter) (xfer);
1526
1527 /* check cancelability */
1528 if (pipe->methods->enter_is_cancelable) {
1529 xfer->flags_int.can_cancel_immed = 1;
1530 /* check for transfer error */
1531 if (xfer->error) {
1532 /* some error has happened */
1533 usb2_transfer_done(xfer, 0);
1517
1518 pipe = xfer->pipe;
1519
1520 DPRINTF("enter\n");
1521
1522 /* enter the transfer */
1523 (pipe->methods->enter) (xfer);
1524
1525 /* check cancelability */
1526 if (pipe->methods->enter_is_cancelable) {
1527 xfer->flags_int.can_cancel_immed = 1;
1528 /* check for transfer error */
1529 if (xfer->error) {
1530 /* some error has happened */
1531 usb2_transfer_done(xfer, 0);
1534 mtx_unlock(xfer->usb2_mtx);
1532 USB_BUS_UNLOCK(xfer->udev->bus);
1535 return;
1536 }
1537 } else {
1538 xfer->flags_int.can_cancel_immed = 0;
1539 }
1540
1541 /* start the transfer */
1542 usb2_command_wrapper(&pipe->pipe_q, xfer);
1533 return;
1534 }
1535 } else {
1536 xfer->flags_int.can_cancel_immed = 0;
1537 }
1538
1539 /* start the transfer */
1540 usb2_command_wrapper(&pipe->pipe_q, xfer);
1543 mtx_unlock(xfer->usb2_mtx);
1541 USB_BUS_UNLOCK(xfer->udev->bus);
1544 return;
1545}
1546
1547/*------------------------------------------------------------------------*
1548 * usb2_transfer_start - start an USB transfer
1549 *
1550 * NOTE: Calling this function more than one time will only
1551 * result in a single transfer start, until the USB transfer
1552 * completes.
1553 *------------------------------------------------------------------------*/
1554void
1555usb2_transfer_start(struct usb2_xfer *xfer)
1556{
1557 if (xfer == NULL) {
1558 /* transfer is gone */
1559 return;
1560 }
1542 return;
1543}
1544
1545/*------------------------------------------------------------------------*
1546 * usb2_transfer_start - start an USB transfer
1547 *
1548 * NOTE: Calling this function more than one time will only
1549 * result in a single transfer start, until the USB transfer
1550 * completes.
1551 *------------------------------------------------------------------------*/
1552void
1553usb2_transfer_start(struct usb2_xfer *xfer)
1554{
1555 if (xfer == NULL) {
1556 /* transfer is gone */
1557 return;
1558 }
1561 mtx_assert(xfer->priv_mtx, MA_OWNED);
1559 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1562
1563 /* mark the USB transfer started */
1564
1565 if (!xfer->flags_int.started) {
1566 xfer->flags_int.started = 1;
1567 }
1568 /* check if the USB transfer callback is already transferring */
1569
1570 if (xfer->flags_int.transferring) {
1571 return;
1572 }
1560
1561 /* mark the USB transfer started */
1562
1563 if (!xfer->flags_int.started) {
1564 xfer->flags_int.started = 1;
1565 }
1566 /* check if the USB transfer callback is already transferring */
1567
1568 if (xfer->flags_int.transferring) {
1569 return;
1570 }
1573 mtx_lock(xfer->usb2_mtx);
1571 USB_BUS_LOCK(xfer->udev->bus);
1574 /* call the USB transfer callback */
1575 usb2_callback_ss_done_defer(xfer);
1572 /* call the USB transfer callback */
1573 usb2_callback_ss_done_defer(xfer);
1576 mtx_unlock(xfer->usb2_mtx);
1574 USB_BUS_UNLOCK(xfer->udev->bus);
1577 return;
1578}
1579
1580/*------------------------------------------------------------------------*
1581 * usb2_transfer_stop - stop an USB transfer
1582 *
1583 * NOTE: Calling this function more than one time will only
1584 * result in a single transfer stop.

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

1589usb2_transfer_stop(struct usb2_xfer *xfer)
1590{
1591 struct usb2_pipe *pipe;
1592
1593 if (xfer == NULL) {
1594 /* transfer is gone */
1595 return;
1596 }
1575 return;
1576}
1577
1578/*------------------------------------------------------------------------*
1579 * usb2_transfer_stop - stop an USB transfer
1580 *
1581 * NOTE: Calling this function more than one time will only
1582 * result in a single transfer stop.

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

1587usb2_transfer_stop(struct usb2_xfer *xfer)
1588{
1589 struct usb2_pipe *pipe;
1590
1591 if (xfer == NULL) {
1592 /* transfer is gone */
1593 return;
1594 }
1597 mtx_assert(xfer->priv_mtx, MA_OWNED);
1595 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1598
1599 /* check if the USB transfer was ever opened */
1600
1601 if (!xfer->flags_int.open) {
1602 /* nothing to do except clearing the "started" flag */
1603 xfer->flags_int.started = 0;
1604 return;
1605 }
1606 /* try to stop the current USB transfer */
1607
1596
1597 /* check if the USB transfer was ever opened */
1598
1599 if (!xfer->flags_int.open) {
1600 /* nothing to do except clearing the "started" flag */
1601 xfer->flags_int.started = 0;
1602 return;
1603 }
1604 /* try to stop the current USB transfer */
1605
1608 mtx_lock(xfer->usb2_mtx);
1606 USB_BUS_LOCK(xfer->udev->bus);
1609 xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1610 /*
1611 * Clear "open" and "started" when both private and USB lock
1612 * is locked so that we don't get a race updating "flags_int"
1613 */
1614 xfer->flags_int.open = 0;
1615 xfer->flags_int.started = 0;
1616

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

1652 * If the current USB transfer is completing we need
1653 * to start the next one:
1654 */
1655 if (pipe->pipe_q.curr == xfer) {
1656 usb2_command_wrapper(&pipe->pipe_q, NULL);
1657 }
1658 }
1659
1607 xfer->error = USB_ERR_CANCELLED;/* override any previous error */
1608 /*
1609 * Clear "open" and "started" when both private and USB lock
1610 * is locked so that we don't get a race updating "flags_int"
1611 */
1612 xfer->flags_int.open = 0;
1613 xfer->flags_int.started = 0;
1614

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

1650 * If the current USB transfer is completing we need
1651 * to start the next one:
1652 */
1653 if (pipe->pipe_q.curr == xfer) {
1654 usb2_command_wrapper(&pipe->pipe_q, NULL);
1655 }
1656 }
1657
1660 mtx_unlock(xfer->usb2_mtx);
1658 USB_BUS_UNLOCK(xfer->udev->bus);
1661 return;
1662}
1663
1664/*------------------------------------------------------------------------*
1665 * usb2_transfer_pending
1666 *
1667 * This function will check if an USB transfer is pending which is a
1668 * little bit complicated!
1669 * Return values:
1670 * 0: Not pending
1671 * 1: Pending: The USB transfer will receive a callback in the future.
1672 *------------------------------------------------------------------------*/
1673uint8_t
1674usb2_transfer_pending(struct usb2_xfer *xfer)
1675{
1676 struct usb2_xfer_root *info;
1677 struct usb2_xfer_queue *pq;
1678
1659 return;
1660}
1661
1662/*------------------------------------------------------------------------*
1663 * usb2_transfer_pending
1664 *
1665 * This function will check if an USB transfer is pending which is a
1666 * little bit complicated!
1667 * Return values:
1668 * 0: Not pending
1669 * 1: Pending: The USB transfer will receive a callback in the future.
1670 *------------------------------------------------------------------------*/
1671uint8_t
1672usb2_transfer_pending(struct usb2_xfer *xfer)
1673{
1674 struct usb2_xfer_root *info;
1675 struct usb2_xfer_queue *pq;
1676
1679 mtx_assert(xfer->priv_mtx, MA_OWNED);
1677 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1680
1681 if (xfer->flags_int.transferring) {
1682 /* trivial case */
1683 return (1);
1684 }
1678
1679 if (xfer->flags_int.transferring) {
1680 /* trivial case */
1681 return (1);
1682 }
1685 mtx_lock(xfer->usb2_mtx);
1683 USB_BUS_LOCK(xfer->udev->bus);
1686 if (xfer->wait_queue) {
1687 /* we are waiting on a queue somewhere */
1684 if (xfer->wait_queue) {
1685 /* we are waiting on a queue somewhere */
1688 mtx_unlock(xfer->usb2_mtx);
1686 USB_BUS_UNLOCK(xfer->udev->bus);
1689 return (1);
1690 }
1691 info = xfer->usb2_root;
1692 pq = &info->done_q;
1693
1694 if (pq->curr == xfer) {
1695 /* we are currently scheduled for callback */
1687 return (1);
1688 }
1689 info = xfer->usb2_root;
1690 pq = &info->done_q;
1691
1692 if (pq->curr == xfer) {
1693 /* we are currently scheduled for callback */
1696 mtx_unlock(xfer->usb2_mtx);
1694 USB_BUS_UNLOCK(xfer->udev->bus);
1697 return (1);
1698 }
1699 /* we are not pending */
1695 return (1);
1696 }
1697 /* we are not pending */
1700 mtx_unlock(xfer->usb2_mtx);
1698 USB_BUS_UNLOCK(xfer->udev->bus);
1701 return (0);
1702}
1703
1704/*------------------------------------------------------------------------*
1705 * usb2_transfer_drain
1706 *
1707 * This function will stop the USB transfer and wait for any
1708 * additional BUS-DMA and HW-DMA operations to complete. Buffers that

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

1714{
1715 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1716 "usb2_transfer_drain can sleep!");
1717
1718 if (xfer == NULL) {
1719 /* transfer is gone */
1720 return;
1721 }
1699 return (0);
1700}
1701
1702/*------------------------------------------------------------------------*
1703 * usb2_transfer_drain
1704 *
1705 * This function will stop the USB transfer and wait for any
1706 * additional BUS-DMA and HW-DMA operations to complete. Buffers that

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

1712{
1713 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1714 "usb2_transfer_drain can sleep!");
1715
1716 if (xfer == NULL) {
1717 /* transfer is gone */
1718 return;
1719 }
1722 if (xfer->priv_mtx != &Giant) {
1723 mtx_assert(xfer->priv_mtx, MA_NOTOWNED);
1720 if (xfer->xfer_mtx != &Giant) {
1721 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
1724 }
1722 }
1725 mtx_lock(xfer->priv_mtx);
1723 USB_XFER_LOCK(xfer);
1726
1727 usb2_transfer_stop(xfer);
1728
1729 while (usb2_transfer_pending(xfer)) {
1730 xfer->flags_int.draining = 1;
1731 /*
1732 * Wait until the current outstanding USB
1733 * transfer is complete !
1734 */
1724
1725 usb2_transfer_stop(xfer);
1726
1727 while (usb2_transfer_pending(xfer)) {
1728 xfer->flags_int.draining = 1;
1729 /*
1730 * Wait until the current outstanding USB
1731 * transfer is complete !
1732 */
1735 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->priv_mtx);
1733 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->xfer_mtx);
1736 }
1734 }
1737 mtx_unlock(xfer->priv_mtx);
1735 USB_XFER_UNLOCK(xfer);
1738
1739 return;
1740}
1741
1742/*------------------------------------------------------------------------*
1743 * usb2_set_frame_data
1744 *
1745 * This function sets the pointer of the buffer that should

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

1781 *------------------------------------------------------------------------*/
1782static void
1783usb2_callback_proc(struct usb2_proc_msg *_pm)
1784{
1785 struct usb2_done_msg *pm = (void *)_pm;
1786 struct usb2_xfer_root *info = pm->usb2_root;
1787
1788 /* Change locking order */
1736
1737 return;
1738}
1739
1740/*------------------------------------------------------------------------*
1741 * usb2_set_frame_data
1742 *
1743 * This function sets the pointer of the buffer that should

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

1779 *------------------------------------------------------------------------*/
1780static void
1781usb2_callback_proc(struct usb2_proc_msg *_pm)
1782{
1783 struct usb2_done_msg *pm = (void *)_pm;
1784 struct usb2_xfer_root *info = pm->usb2_root;
1785
1786 /* Change locking order */
1789 mtx_unlock(info->usb2_mtx);
1787 USB_BUS_UNLOCK(info->bus);
1790
1791 /*
1792 * We exploit the fact that the mutex is the same for all
1793 * callbacks that will be called from this thread:
1794 */
1795 mtx_lock(info->priv_mtx);
1788
1789 /*
1790 * We exploit the fact that the mutex is the same for all
1791 * callbacks that will be called from this thread:
1792 */
1793 mtx_lock(info->priv_mtx);
1796 mtx_lock(info->usb2_mtx);
1794 USB_BUS_LOCK(info->bus);
1797
1798 /* Continue where we lost track */
1799 usb2_command_wrapper(&info->done_q,
1800 info->done_q.curr);
1801
1802 mtx_unlock(info->priv_mtx);
1803 return;
1804}

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

1810 * correct thread.
1811 *------------------------------------------------------------------------*/
1812static void
1813usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1814{
1815 struct usb2_xfer_root *info = xfer->usb2_root;
1816 struct usb2_xfer_queue *pq = &info->done_q;
1817
1795
1796 /* Continue where we lost track */
1797 usb2_command_wrapper(&info->done_q,
1798 info->done_q.curr);
1799
1800 mtx_unlock(info->priv_mtx);
1801 return;
1802}

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

1808 * correct thread.
1809 *------------------------------------------------------------------------*/
1810static void
1811usb2_callback_ss_done_defer(struct usb2_xfer *xfer)
1812{
1813 struct usb2_xfer_root *info = xfer->usb2_root;
1814 struct usb2_xfer_queue *pq = &info->done_q;
1815
1818 if (!mtx_owned(xfer->usb2_mtx)) {
1819 panic("%s: called unlocked!\n", __FUNCTION__);
1820 }
1816 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1817
1821 if (pq->curr != xfer) {
1822 usb2_transfer_enqueue(pq, xfer);
1823 }
1824 if (!pq->recurse_1) {
1825
1826 /*
1827 * We have to postpone the callback due to the fact we
1828 * will have a Lock Order Reversal, LOR, if we try to

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

1849 * interrupt process.
1850 *------------------------------------------------------------------------*/
1851static void
1852usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1853{
1854 struct usb2_xfer *xfer = pq->curr;
1855 struct usb2_xfer_root *info = xfer->usb2_root;
1856
1818 if (pq->curr != xfer) {
1819 usb2_transfer_enqueue(pq, xfer);
1820 }
1821 if (!pq->recurse_1) {
1822
1823 /*
1824 * We have to postpone the callback due to the fact we
1825 * will have a Lock Order Reversal, LOR, if we try to

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

1846 * interrupt process.
1847 *------------------------------------------------------------------------*/
1848static void
1849usb2_callback_wrapper(struct usb2_xfer_queue *pq)
1850{
1851 struct usb2_xfer *xfer = pq->curr;
1852 struct usb2_xfer_root *info = xfer->usb2_root;
1853
1857 if (!mtx_owned(xfer->usb2_mtx)) {
1858 panic("%s: called unlocked!\n", __FUNCTION__);
1859 }
1860 if (!mtx_owned(xfer->priv_mtx)) {
1854 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1855 if (!mtx_owned(xfer->xfer_mtx)) {
1861 /*
1862 * Cases that end up here:
1863 *
1864 * 5) HW interrupt done callback or other source.
1865 */
1866 DPRINTFN(3, "case 5\n");
1867
1868 /*

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

1883 * 2) We are prematurely calling back a transfer
1884 * 3) We are stopping a transfer
1885 * 4) We are doing an ordinary callback
1886 */
1887 DPRINTFN(3, "case 1-4\n");
1888 /* get next USB transfer in the queue */
1889 info->done_q.curr = NULL;
1890
1856 /*
1857 * Cases that end up here:
1858 *
1859 * 5) HW interrupt done callback or other source.
1860 */
1861 DPRINTFN(3, "case 5\n");
1862
1863 /*

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

1878 * 2) We are prematurely calling back a transfer
1879 * 3) We are stopping a transfer
1880 * 4) We are doing an ordinary callback
1881 */
1882 DPRINTFN(3, "case 1-4\n");
1883 /* get next USB transfer in the queue */
1884 info->done_q.curr = NULL;
1885
1891 mtx_unlock(xfer->usb2_mtx);
1892 mtx_assert(xfer->usb2_mtx, MA_NOTOWNED);
1886 USB_BUS_UNLOCK(xfer->udev->bus);
1887 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_NOTOWNED);
1893
1894 /* set correct USB state for callback */
1895 if (!xfer->flags_int.transferring) {
1896 xfer->usb2_state = USB_ST_SETUP;
1897 if (!xfer->flags_int.started) {
1898 /* we got stopped before we even got started */
1888
1889 /* set correct USB state for callback */
1890 if (!xfer->flags_int.transferring) {
1891 xfer->usb2_state = USB_ST_SETUP;
1892 if (!xfer->flags_int.started) {
1893 /* we got stopped before we even got started */
1899 mtx_lock(xfer->usb2_mtx);
1894 USB_BUS_LOCK(xfer->udev->bus);
1900 goto done;
1901 }
1902 } else {
1903
1904 if (usb2_callback_wrapper_sub(xfer)) {
1905 /* the callback has been deferred */
1895 goto done;
1896 }
1897 } else {
1898
1899 if (usb2_callback_wrapper_sub(xfer)) {
1900 /* the callback has been deferred */
1906 mtx_lock(xfer->usb2_mtx);
1901 USB_BUS_LOCK(xfer->udev->bus);
1907 goto done;
1908 }
1909 xfer->flags_int.transferring = 0;
1910
1911 if (xfer->error) {
1912 xfer->usb2_state = USB_ST_ERROR;
1913 } else {
1914 /* set transferred state */

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

1921 }
1922 }
1923 }
1924
1925 /* call processing routine */
1926 (xfer->callback) (xfer);
1927
1928 /* pickup the USB mutex again */
1902 goto done;
1903 }
1904 xfer->flags_int.transferring = 0;
1905
1906 if (xfer->error) {
1907 xfer->usb2_state = USB_ST_ERROR;
1908 } else {
1909 /* set transferred state */

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

1916 }
1917 }
1918 }
1919
1920 /* call processing routine */
1921 (xfer->callback) (xfer);
1922
1923 /* pickup the USB mutex again */
1929 mtx_lock(xfer->usb2_mtx);
1924 USB_BUS_LOCK(xfer->udev->bus);
1930
1931 /*
1932 * Check if we got started after that we got cancelled, but
1933 * before we managed to do the callback. Check if we are
1934 * draining.
1935 */
1936 if ((!xfer->flags_int.open) &&
1937 (xfer->flags_int.started) &&

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

1960 * transfer. This code path is ususally only used when there is an USB
1961 * error like USB_ERR_CANCELLED.
1962 *------------------------------------------------------------------------*/
1963static void
1964usb2_dma_delay_done_cb(void *arg)
1965{
1966 struct usb2_xfer *xfer = arg;
1967
1925
1926 /*
1927 * Check if we got started after that we got cancelled, but
1928 * before we managed to do the callback. Check if we are
1929 * draining.
1930 */
1931 if ((!xfer->flags_int.open) &&
1932 (xfer->flags_int.started) &&

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

1955 * transfer. This code path is ususally only used when there is an USB
1956 * error like USB_ERR_CANCELLED.
1957 *------------------------------------------------------------------------*/
1958static void
1959usb2_dma_delay_done_cb(void *arg)
1960{
1961 struct usb2_xfer *xfer = arg;
1962
1968 mtx_assert(xfer->usb2_mtx, MA_OWNED);
1963 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
1969
1970 DPRINTFN(3, "Completed %p\n", xfer);
1971
1972 /* queue callback for execution, again */
1973 usb2_transfer_done(xfer, 0);
1974
1964
1965 DPRINTFN(3, "Completed %p\n", xfer);
1966
1967 /* queue callback for execution, again */
1968 usb2_transfer_done(xfer, 0);
1969
1975 mtx_unlock(xfer->usb2_mtx);
1970 USB_BUS_UNLOCK(xfer->udev->bus);
1976 return;
1977}
1978
1979/*------------------------------------------------------------------------*
1980 * usb2_transfer_dequeue
1981 *
1982 * - This function is used to remove an USB transfer from a USB
1983 * transfer queue.

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

2030 *
2031 * - This function is used to stop any USB transfer timeouts.
2032 *------------------------------------------------------------------------*/
2033void
2034usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2035{
2036 struct usb2_xfer_queue *pq;
2037
1971 return;
1972}
1973
1974/*------------------------------------------------------------------------*
1975 * usb2_transfer_dequeue
1976 *
1977 * - This function is used to remove an USB transfer from a USB
1978 * transfer queue.

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

2025 *
2026 * - This function is used to stop any USB transfer timeouts.
2027 *------------------------------------------------------------------------*/
2028void
2029usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error)
2030{
2031 struct usb2_xfer_queue *pq;
2032
2038 mtx_assert(xfer->usb2_mtx, MA_OWNED);
2033 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2039
2040 DPRINTF("err=%s\n", usb2_errstr(error));
2041
2042 /*
2043 * If we are not transferring then just return.
2044 * This can happen during transfer cancel.
2045 */
2046 if (!xfer->flags_int.transferring) {

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

2056
2057 /*
2058 * If we are waiting on a queue, just remove the USB transfer
2059 * from the queue, if any. We should have the required locks
2060 * locked to do the remove when this function is called.
2061 */
2062 usb2_transfer_dequeue(xfer);
2063
2034
2035 DPRINTF("err=%s\n", usb2_errstr(error));
2036
2037 /*
2038 * If we are not transferring then just return.
2039 * This can happen during transfer cancel.
2040 */
2041 if (!xfer->flags_int.transferring) {

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

2051
2052 /*
2053 * If we are waiting on a queue, just remove the USB transfer
2054 * from the queue, if any. We should have the required locks
2055 * locked to do the remove when this function is called.
2056 */
2057 usb2_transfer_dequeue(xfer);
2058
2064 if (mtx_owned(xfer->priv_mtx)) {
2059 if (mtx_owned(xfer->xfer_mtx)) {
2065 /*
2066 * If the private USB lock is not locked, then we assume
2067 * that the BUS-DMA load stage has been passed:
2068 */
2069 pq = &xfer->usb2_root->dma_q;
2070
2071 if (pq->curr == xfer) {
2072 /* start the next BUS-DMA load, if any */

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

2095 * BULK or CONTROL.
2096 *------------------------------------------------------------------------*/
2097static void
2098usb2_transfer_start_cb(void *arg)
2099{
2100 struct usb2_xfer *xfer = arg;
2101 struct usb2_pipe *pipe = xfer->pipe;
2102
2060 /*
2061 * If the private USB lock is not locked, then we assume
2062 * that the BUS-DMA load stage has been passed:
2063 */
2064 pq = &xfer->usb2_root->dma_q;
2065
2066 if (pq->curr == xfer) {
2067 /* start the next BUS-DMA load, if any */

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

2090 * BULK or CONTROL.
2091 *------------------------------------------------------------------------*/
2092static void
2093usb2_transfer_start_cb(void *arg)
2094{
2095 struct usb2_xfer *xfer = arg;
2096 struct usb2_pipe *pipe = xfer->pipe;
2097
2103 mtx_assert(xfer->usb2_mtx, MA_OWNED);
2098 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2104
2105 DPRINTF("start\n");
2106
2107 /* start the transfer */
2108 (pipe->methods->start) (xfer);
2109
2110 /* check cancelability */
2111 if (pipe->methods->start_is_cancelable) {
2112 xfer->flags_int.can_cancel_immed = 1;
2113 if (xfer->error) {
2114 /* some error has happened */
2115 usb2_transfer_done(xfer, 0);
2116 }
2117 } else {
2118 xfer->flags_int.can_cancel_immed = 0;
2119 }
2099
2100 DPRINTF("start\n");
2101
2102 /* start the transfer */
2103 (pipe->methods->start) (xfer);
2104
2105 /* check cancelability */
2106 if (pipe->methods->start_is_cancelable) {
2107 xfer->flags_int.can_cancel_immed = 1;
2108 if (xfer->error) {
2109 /* some error has happened */
2110 usb2_transfer_done(xfer, 0);
2111 }
2112 } else {
2113 xfer->flags_int.can_cancel_immed = 0;
2114 }
2120 mtx_unlock(xfer->usb2_mtx);
2115 USB_BUS_UNLOCK(xfer->udev->bus);
2121
2122 return;
2123}
2124
2125/*------------------------------------------------------------------------*
2126 * usb2_transfer_set_stall
2127 *
2128 * This function is used to set the stall flag outside the
2129 * callback. This function is NULL safe.
2130 *------------------------------------------------------------------------*/
2131void
2132usb2_transfer_set_stall(struct usb2_xfer *xfer)
2133{
2134 if (xfer == NULL) {
2135 /* tearing down */
2136 return;
2137 }
2116
2117 return;
2118}
2119
2120/*------------------------------------------------------------------------*
2121 * usb2_transfer_set_stall
2122 *
2123 * This function is used to set the stall flag outside the
2124 * callback. This function is NULL safe.
2125 *------------------------------------------------------------------------*/
2126void
2127usb2_transfer_set_stall(struct usb2_xfer *xfer)
2128{
2129 if (xfer == NULL) {
2130 /* tearing down */
2131 return;
2132 }
2138 mtx_assert(xfer->priv_mtx, MA_OWNED);
2133 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2139
2140 /* avoid any races by locking the USB mutex */
2134
2135 /* avoid any races by locking the USB mutex */
2141 mtx_lock(xfer->usb2_mtx);
2136 USB_BUS_LOCK(xfer->udev->bus);
2142
2143 xfer->flags.stall_pipe = 1;
2144
2137
2138 xfer->flags.stall_pipe = 1;
2139
2145 mtx_unlock(xfer->usb2_mtx);
2140 USB_BUS_UNLOCK(xfer->udev->bus);
2146
2147 return;
2148}
2149
2150/*------------------------------------------------------------------------*
2151 * usb2_transfer_clear_stall
2152 *
2153 * This function is used to clear the stall flag outside the
2154 * callback. This function is NULL safe.
2155 *------------------------------------------------------------------------*/
2156void
2157usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2158{
2159 if (xfer == NULL) {
2160 /* tearing down */
2161 return;
2162 }
2141
2142 return;
2143}
2144
2145/*------------------------------------------------------------------------*
2146 * usb2_transfer_clear_stall
2147 *
2148 * This function is used to clear the stall flag outside the
2149 * callback. This function is NULL safe.
2150 *------------------------------------------------------------------------*/
2151void
2152usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2153{
2154 if (xfer == NULL) {
2155 /* tearing down */
2156 return;
2157 }
2163 mtx_assert(xfer->priv_mtx, MA_OWNED);
2158 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2164
2165 /* avoid any races by locking the USB mutex */
2159
2160 /* avoid any races by locking the USB mutex */
2166 mtx_lock(xfer->usb2_mtx);
2161 USB_BUS_LOCK(xfer->udev->bus);
2167
2168 xfer->flags.stall_pipe = 0;
2169
2162
2163 xfer->flags.stall_pipe = 0;
2164
2170 mtx_unlock(xfer->usb2_mtx);
2165 USB_BUS_UNLOCK(xfer->udev->bus);
2171
2172 return;
2173}
2174
2175/*------------------------------------------------------------------------*
2176 * usb2_pipe_start
2177 *
2178 * This function is used to add an USB transfer to the pipe transfer list.
2179 *------------------------------------------------------------------------*/
2180void
2181usb2_pipe_start(struct usb2_xfer_queue *pq)
2182{
2183 struct usb2_pipe *pipe;
2184 struct usb2_xfer *xfer;
2185 uint8_t type;
2186
2187 xfer = pq->curr;
2188 pipe = xfer->pipe;
2189
2166
2167 return;
2168}
2169
2170/*------------------------------------------------------------------------*
2171 * usb2_pipe_start
2172 *
2173 * This function is used to add an USB transfer to the pipe transfer list.
2174 *------------------------------------------------------------------------*/
2175void
2176usb2_pipe_start(struct usb2_xfer_queue *pq)
2177{
2178 struct usb2_pipe *pipe;
2179 struct usb2_xfer *xfer;
2180 uint8_t type;
2181
2182 xfer = pq->curr;
2183 pipe = xfer->pipe;
2184
2190 mtx_assert(xfer->usb2_mtx, MA_OWNED);
2185 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2191
2192 /*
2193 * If the pipe is already stalled we do nothing !
2194 */
2195 if (pipe->is_stalled) {
2196 return;
2197 }
2198 /*

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

2285 * This function is used to setup a timeout on the given USB
2286 * transfer. If the timeout has been deferred the callback given by
2287 * "cb" will get called after "ms" milliseconds.
2288 *------------------------------------------------------------------------*/
2289void
2290usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2291 void (*cb) (void *arg), uint32_t ms)
2292{
2186
2187 /*
2188 * If the pipe is already stalled we do nothing !
2189 */
2190 if (pipe->is_stalled) {
2191 return;
2192 }
2193 /*

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

2280 * This function is used to setup a timeout on the given USB
2281 * transfer. If the timeout has been deferred the callback given by
2282 * "cb" will get called after "ms" milliseconds.
2283 *------------------------------------------------------------------------*/
2284void
2285usb2_transfer_timeout_ms(struct usb2_xfer *xfer,
2286 void (*cb) (void *arg), uint32_t ms)
2287{
2293 mtx_assert(xfer->usb2_mtx, MA_OWNED);
2288 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2294
2295 /* defer delay */
2296 usb2_callout_reset(&xfer->timeout_handle,
2297 USB_MS_TO_TICKS(ms), cb, xfer);
2298 return;
2299}
2300
2301/*------------------------------------------------------------------------*

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

2319usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2320{
2321 struct usb2_pipe *pipe;
2322 uint32_t x;
2323
2324 if ((!xfer->flags_int.open) &&
2325 (!xfer->flags_int.did_close)) {
2326 DPRINTF("close\n");
2289
2290 /* defer delay */
2291 usb2_callout_reset(&xfer->timeout_handle,
2292 USB_MS_TO_TICKS(ms), cb, xfer);
2293 return;
2294}
2295
2296/*------------------------------------------------------------------------*

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

2314usb2_callback_wrapper_sub(struct usb2_xfer *xfer)
2315{
2316 struct usb2_pipe *pipe;
2317 uint32_t x;
2318
2319 if ((!xfer->flags_int.open) &&
2320 (!xfer->flags_int.did_close)) {
2321 DPRINTF("close\n");
2327 mtx_lock(xfer->usb2_mtx);
2322 USB_BUS_LOCK(xfer->udev->bus);
2328 (xfer->pipe->methods->close) (xfer);
2323 (xfer->pipe->methods->close) (xfer);
2329 mtx_unlock(xfer->usb2_mtx);
2324 USB_BUS_UNLOCK(xfer->udev->bus);
2330 /* only close once */
2331 xfer->flags_int.did_close = 1;
2332 return (1); /* wait for new callback */
2333 }
2334 /*
2335 * If we have a non-hardware induced error we
2336 * need to do the DMA delay!
2337 */

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

2348 xfer->flags_int.can_cancel_immed = 0;
2349
2350 temp = usb2_get_dma_delay(xfer->udev->bus);
2351
2352 DPRINTFN(3, "DMA delay, %u ms, "
2353 "on %p\n", temp, xfer);
2354
2355 if (temp != 0) {
2325 /* only close once */
2326 xfer->flags_int.did_close = 1;
2327 return (1); /* wait for new callback */
2328 }
2329 /*
2330 * If we have a non-hardware induced error we
2331 * need to do the DMA delay!
2332 */

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

2343 xfer->flags_int.can_cancel_immed = 0;
2344
2345 temp = usb2_get_dma_delay(xfer->udev->bus);
2346
2347 DPRINTFN(3, "DMA delay, %u ms, "
2348 "on %p\n", temp, xfer);
2349
2350 if (temp != 0) {
2356 mtx_lock(xfer->usb2_mtx);
2351 USB_BUS_LOCK(xfer->udev->bus);
2357 usb2_transfer_timeout_ms(xfer,
2358 &usb2_dma_delay_done_cb, temp);
2352 usb2_transfer_timeout_ms(xfer,
2353 &usb2_dma_delay_done_cb, temp);
2359 mtx_unlock(xfer->usb2_mtx);
2354 USB_BUS_UNLOCK(xfer->udev->bus);
2360 return (1); /* wait for new callback */
2361 }
2362 }
2363 /* check actual number of frames */
2364 if (xfer->aframes > xfer->nframes) {
2365 if (xfer->error == 0) {
2366 panic("%s: actual number of frames, %d, is "
2367 "greater than initial number of frames, %d!\n",

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

2443 }
2444
2445 pipe = xfer->pipe;
2446
2447 /*
2448 * If the current USB transfer is completing we need to start the
2449 * next one:
2450 */
2355 return (1); /* wait for new callback */
2356 }
2357 }
2358 /* check actual number of frames */
2359 if (xfer->aframes > xfer->nframes) {
2360 if (xfer->error == 0) {
2361 panic("%s: actual number of frames, %d, is "
2362 "greater than initial number of frames, %d!\n",

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

2438 }
2439
2440 pipe = xfer->pipe;
2441
2442 /*
2443 * If the current USB transfer is completing we need to start the
2444 * next one:
2445 */
2451 mtx_lock(xfer->usb2_mtx);
2446 USB_BUS_LOCK(xfer->udev->bus);
2452 if (pipe->pipe_q.curr == xfer) {
2453 usb2_command_wrapper(&pipe->pipe_q, NULL);
2454
2455 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2456 /* there is another USB transfer waiting */
2457 } else {
2458 /* this is the last USB transfer */
2459 /* clear isochronous sync flag */
2460 xfer->pipe->is_synced = 0;
2461 }
2462 }
2447 if (pipe->pipe_q.curr == xfer) {
2448 usb2_command_wrapper(&pipe->pipe_q, NULL);
2449
2450 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) {
2451 /* there is another USB transfer waiting */
2452 } else {
2453 /* this is the last USB transfer */
2454 /* clear isochronous sync flag */
2455 xfer->pipe->is_synced = 0;
2456 }
2457 }
2463 mtx_unlock(xfer->usb2_mtx);
2458 USB_BUS_UNLOCK(xfer->udev->bus);
2464done:
2465 return (0);
2466}
2467
2468/*------------------------------------------------------------------------*
2469 * usb2_command_wrapper
2470 *
2471 * This function is used to execute commands non-recursivly on an USB

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

2539 struct usb2_xfer *xfer;
2540 uint8_t no_resetup;
2541 uint8_t iface_index;
2542
2543repeat:
2544
2545 xfer = udev->default_xfer[0];
2546 if (xfer) {
2459done:
2460 return (0);
2461}
2462
2463/*------------------------------------------------------------------------*
2464 * usb2_command_wrapper
2465 *
2466 * This function is used to execute commands non-recursivly on an USB

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

2534 struct usb2_xfer *xfer;
2535 uint8_t no_resetup;
2536 uint8_t iface_index;
2537
2538repeat:
2539
2540 xfer = udev->default_xfer[0];
2541 if (xfer) {
2547 mtx_lock(xfer->priv_mtx);
2542 USB_XFER_LOCK(xfer);
2548 no_resetup =
2549 ((xfer->address == udev->address) &&
2550 (udev->default_ep_desc.wMaxPacketSize[0] ==
2551 udev->ddesc.bMaxPacketSize));
2552 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2553 if (no_resetup) {
2554 /*
2555 * NOTE: checking "xfer->address" and
2556 * starting the USB transfer must be
2557 * atomic!
2558 */
2559 usb2_transfer_start(xfer);
2560 }
2561 }
2543 no_resetup =
2544 ((xfer->address == udev->address) &&
2545 (udev->default_ep_desc.wMaxPacketSize[0] ==
2546 udev->ddesc.bMaxPacketSize));
2547 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
2548 if (no_resetup) {
2549 /*
2550 * NOTE: checking "xfer->address" and
2551 * starting the USB transfer must be
2552 * atomic!
2553 */
2554 usb2_transfer_start(xfer);
2555 }
2556 }
2562 mtx_unlock(xfer->priv_mtx);
2557 USB_XFER_UNLOCK(xfer);
2563 } else {
2564 no_resetup = 0;
2565 }
2566
2567 if (no_resetup) {
2568 /*
2569 * All parameters are exactly the same like before.
2570 * Just return.

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

2604 * NOTE: the intention of this function is not to reset the hardware
2605 * data toggle.
2606 *------------------------------------------------------------------------*/
2607void
2608usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe)
2609{
2610 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe);
2611
2558 } else {
2559 no_resetup = 0;
2560 }
2561
2562 if (no_resetup) {
2563 /*
2564 * All parameters are exactly the same like before.
2565 * Just return.

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

2599 * NOTE: the intention of this function is not to reset the hardware
2600 * data toggle.
2601 *------------------------------------------------------------------------*/
2602void
2603usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe)
2604{
2605 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe);
2606
2612 mtx_lock(&udev->bus->mtx);
2607 USB_BUS_LOCK(udev->bus);
2613 pipe->toggle_next = 0;
2608 pipe->toggle_next = 0;
2614 mtx_unlock(&udev->bus->mtx);
2609 USB_BUS_UNLOCK(udev->bus);
2615 return;
2616}
2617
2618/*------------------------------------------------------------------------*
2619 * usb2_clear_stall_callback - factored out clear stall callback
2620 *
2621 * Input parameters:
2622 * xfer1: Clear Stall Control Transfer

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

2650{
2651 struct usb2_device_request req;
2652
2653 if (xfer2 == NULL) {
2654 /* looks like we are tearing down */
2655 DPRINTF("NULL input parameter\n");
2656 return (0);
2657 }
2610 return;
2611}
2612
2613/*------------------------------------------------------------------------*
2614 * usb2_clear_stall_callback - factored out clear stall callback
2615 *
2616 * Input parameters:
2617 * xfer1: Clear Stall Control Transfer

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

2645{
2646 struct usb2_device_request req;
2647
2648 if (xfer2 == NULL) {
2649 /* looks like we are tearing down */
2650 DPRINTF("NULL input parameter\n");
2651 return (0);
2652 }
2658 mtx_assert(xfer1->priv_mtx, MA_OWNED);
2659 mtx_assert(xfer2->priv_mtx, MA_OWNED);
2653 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
2654 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
2660
2661 switch (USB_GET_STATE(xfer1)) {
2662 case USB_ST_SETUP:
2663
2664 /*
2665 * pre-clear the data toggle to DATA0 ("umass.c" and
2666 * "ata-usb.c" depends on this)
2667 */

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

2781 if (xfer) {
2782 usb2_root = xfer->usb2_root;
2783 udev = xfer->udev;
2784
2785 /*
2786 * Poll hardware - signal that we are polling by
2787 * locking the private mutex:
2788 */
2655
2656 switch (USB_GET_STATE(xfer1)) {
2657 case USB_ST_SETUP:
2658
2659 /*
2660 * pre-clear the data toggle to DATA0 ("umass.c" and
2661 * "ata-usb.c" depends on this)
2662 */

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

2776 if (xfer) {
2777 usb2_root = xfer->usb2_root;
2778 udev = xfer->udev;
2779
2780 /*
2781 * Poll hardware - signal that we are polling by
2782 * locking the private mutex:
2783 */
2789 mtx_lock(xfer->priv_mtx);
2784 USB_XFER_LOCK(xfer);
2790 (udev->bus->methods->do_poll) (udev->bus);
2785 (udev->bus->methods->do_poll) (udev->bus);
2791 mtx_unlock(xfer->priv_mtx);
2786 USB_XFER_UNLOCK(xfer);
2792
2793 /* poll clear stall start */
2787
2788 /* poll clear stall start */
2794 mtx_lock(xfer->usb2_mtx);
2789 USB_BUS_LOCK(xfer->udev->bus);
2795 pm = &udev->cs_msg[0].hdr;
2796 (pm->pm_callback) (pm);
2790 pm = &udev->cs_msg[0].hdr;
2791 (pm->pm_callback) (pm);
2797 mtx_unlock(xfer->usb2_mtx);
2792 USB_BUS_UNLOCK(xfer->udev->bus);
2798
2799 if (udev->default_xfer[1]) {
2800
2801 /* poll timeout */
2802 usb2_callout_poll(udev->default_xfer[1]);
2803
2804 /* poll clear stall done thread */
2793
2794 if (udev->default_xfer[1]) {
2795
2796 /* poll timeout */
2797 usb2_callout_poll(udev->default_xfer[1]);
2798
2799 /* poll clear stall done thread */
2805 mtx_lock(xfer->usb2_mtx);
2800 USB_BUS_LOCK(xfer->udev->bus);
2806 pm = &udev->default_xfer[1]->
2807 usb2_root->done_m[0].hdr;
2808 (pm->pm_callback) (pm);
2801 pm = &udev->default_xfer[1]->
2802 usb2_root->done_m[0].hdr;
2803 (pm->pm_callback) (pm);
2809 mtx_unlock(xfer->usb2_mtx);
2804 USB_BUS_UNLOCK(xfer->udev->bus);
2810 }
2811 /* poll timeout */
2812 usb2_callout_poll(xfer);
2813
2814 /* poll done thread */
2805 }
2806 /* poll timeout */
2807 usb2_callout_poll(xfer);
2808
2809 /* poll done thread */
2815 mtx_lock(xfer->usb2_mtx);
2810 USB_BUS_LOCK(xfer->udev->bus);
2816 pm = &usb2_root->done_m[0].hdr;
2817 (pm->pm_callback) (pm);
2811 pm = &usb2_root->done_m[0].hdr;
2812 (pm->pm_callback) (pm);
2818 mtx_unlock(xfer->usb2_mtx);
2813 USB_BUS_UNLOCK(xfer->udev->bus);
2819 }
2820 }
2821 return;
2822}
2823
2824#else
2825
2826void
2827usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2828{
2829 /* polling not supported */
2830 return;
2831}
2832
2833#endif
2814 }
2815 }
2816 return;
2817}
2818
2819#else
2820
2821void
2822usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max)
2823{
2824 /* polling not supported */
2825 return;
2826}
2827
2828#endif