Deleted Added
full compact
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
805 info->usb2_mtx = &udev->bus->mtx;
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)) {
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;
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);
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 !
1022 */
1025 mtx_lock(info->usb2_mtx);
1023 USB_BUS_LOCK(info->bus);
1024
1025 /* something went wrong */
1026 usb2_transfer_unsetup_sub(info, 0);
1027 }
1028 }
1029 if (parm.err) {
1030 usb2_transfer_unsetup(ppxfer, n_setup);
1031 }

--- 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);
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);
1051 }
1054 mtx_unlock(info->usb2_mtx);
1052 USB_BUS_UNLOCK(info->bus);
1053
1054 /* wait for interrupt thread to exit */
1055 usb2_proc_unsetup(&info->done_p);
1056
1057 /* free DMA'able memory, if any */
1058 pc = info->dma_page_cache_start;
1059 while (pc != info->dma_page_cache_end) {
1060 usb2_pc_free_mem(pc);

--- 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);
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);
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);
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);
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);
1360
1361 usb2_dump_pipe(xfer->pipe);
1362
1365 mtx_unlock(xfer->usb2_mtx);
1363 USB_BUS_UNLOCK(xfer->udev->bus);
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);
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);
1377 (xfer->pipe->methods->open) (xfer);
1380 mtx_unlock(xfer->usb2_mtx);
1378 USB_BUS_UNLOCK(xfer->udev->bus);
1379 }
1380 /* set "transferring" flag */
1381 xfer->flags_int.transferring = 1;
1382
1383 /*
1384 * Check if the transfer is waiting on a queue, most
1385 * frequently the "done_q":
1386 */
1387 if (xfer->wait_queue) {
1390 mtx_lock(xfer->usb2_mtx);
1388 USB_BUS_LOCK(xfer->udev->bus);
1389 usb2_transfer_dequeue(xfer);
1392 mtx_unlock(xfer->usb2_mtx);
1390 USB_BUS_UNLOCK(xfer->udev->bus);
1391 }
1392 /* clear "did_dma_delay" flag */
1393 xfer->flags_int.did_dma_delay = 0;
1394
1395 /* clear "did_close" flag */
1396 xfer->flags_int.did_close = 0;
1397
1398 /* clear "bdma_setup" flag */

--- 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);
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);
1427 return;
1428 }
1431 mtx_lock(xfer->usb2_mtx);
1429 USB_BUS_LOCK(xfer->udev->bus);
1430 usb2_transfer_done(xfer, USB_ERR_INVAL);
1433 mtx_unlock(xfer->usb2_mtx);
1431 USB_BUS_UNLOCK(xfer->udev->bus);
1432 return;
1433 }
1434 /* compute total transfer length */
1435
1436 for (x = 0; x != xfer->nframes; x++) {
1437 xfer->sumlen += xfer->frlengths[x];
1438 if (xfer->sumlen < xfer->frlengths[x]) {
1439 /* length wrapped around */
1442 mtx_lock(xfer->usb2_mtx);
1440 USB_BUS_LOCK(xfer->udev->bus);
1441 usb2_transfer_done(xfer, USB_ERR_INVAL);
1444 mtx_unlock(xfer->usb2_mtx);
1442 USB_BUS_UNLOCK(xfer->udev->bus);
1443 return;
1444 }
1445 }
1446
1447 /* clear some internal flags */
1448
1449 xfer->flags_int.short_xfer_ok = 0;
1450 xfer->flags_int.short_frames_ok = 0;
1451
1452 /* check if this is a control transfer */
1453
1454 if (xfer->flags_int.control_xfr) {
1455
1456 if (usb2_start_hardware_sub(xfer)) {
1459 mtx_lock(xfer->usb2_mtx);
1457 USB_BUS_LOCK(xfer->udev->bus);
1458 usb2_transfer_done(xfer, USB_ERR_STALLED);
1461 mtx_unlock(xfer->usb2_mtx);
1459 USB_BUS_UNLOCK(xfer->udev->bus);
1460 return;
1461 }
1462 }
1463 /*
1464 * Setup filtered version of some transfer flags,
1465 * in case of data read direction
1466 */
1467 if (USB_GET_DATA_ISREAD(xfer)) {

--- 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);
1515
1518 mtx_lock(xfer->usb2_mtx);
1516 USB_BUS_LOCK(xfer->udev->bus);
1517
1518 pipe = xfer->pipe;
1519
1520 DPRINTF("enter\n");
1521
1522 /* enter the transfer */
1523 (pipe->methods->enter) (xfer);
1524
1525 /* check cancelability */
1526 if (pipe->methods->enter_is_cancelable) {
1527 xfer->flags_int.can_cancel_immed = 1;
1528 /* check for transfer error */
1529 if (xfer->error) {
1530 /* some error has happened */
1531 usb2_transfer_done(xfer, 0);
1534 mtx_unlock(xfer->usb2_mtx);
1532 USB_BUS_UNLOCK(xfer->udev->bus);
1533 return;
1534 }
1535 } else {
1536 xfer->flags_int.can_cancel_immed = 0;
1537 }
1538
1539 /* start the transfer */
1540 usb2_command_wrapper(&pipe->pipe_q, xfer);
1543 mtx_unlock(xfer->usb2_mtx);
1541 USB_BUS_UNLOCK(xfer->udev->bus);
1542 return;
1543}
1544
1545/*------------------------------------------------------------------------*
1546 * usb2_transfer_start - start an USB transfer
1547 *
1548 * NOTE: Calling this function more than one time will only
1549 * result in a single transfer start, until the USB transfer
1550 * completes.
1551 *------------------------------------------------------------------------*/
1552void
1553usb2_transfer_start(struct usb2_xfer *xfer)
1554{
1555 if (xfer == NULL) {
1556 /* transfer is gone */
1557 return;
1558 }
1561 mtx_assert(xfer->priv_mtx, MA_OWNED);
1559 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1560
1561 /* mark the USB transfer started */
1562
1563 if (!xfer->flags_int.started) {
1564 xfer->flags_int.started = 1;
1565 }
1566 /* check if the USB transfer callback is already transferring */
1567
1568 if (xfer->flags_int.transferring) {
1569 return;
1570 }
1573 mtx_lock(xfer->usb2_mtx);
1571 USB_BUS_LOCK(xfer->udev->bus);
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);
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);
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);
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);
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);
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);
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);
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);
1695 return (1);
1696 }
1697 /* we are not pending */
1700 mtx_unlock(xfer->usb2_mtx);
1698 USB_BUS_UNLOCK(xfer->udev->bus);
1699 return (0);
1700}
1701
1702/*------------------------------------------------------------------------*
1703 * usb2_transfer_drain
1704 *
1705 * This function will stop the USB transfer and wait for any
1706 * additional BUS-DMA and HW-DMA operations to complete. Buffers that

--- 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);
1722 }
1725 mtx_lock(xfer->priv_mtx);
1723 USB_XFER_LOCK(xfer);
1724
1725 usb2_transfer_stop(xfer);
1726
1727 while (usb2_transfer_pending(xfer)) {
1728 xfer->flags_int.draining = 1;
1729 /*
1730 * Wait until the current outstanding USB
1731 * transfer is complete !
1732 */
1735 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->priv_mtx);
1733 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->xfer_mtx);
1734 }
1737 mtx_unlock(xfer->priv_mtx);
1735 USB_XFER_UNLOCK(xfer);
1736
1737 return;
1738}
1739
1740/*------------------------------------------------------------------------*
1741 * usb2_set_frame_data
1742 *
1743 * This function sets the pointer of the buffer that should

--- 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);
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);
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
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)) {
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);
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);
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);
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);
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);
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);
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);
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)) {
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);
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);
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);
2134
2135 /* avoid any races by locking the USB mutex */
2141 mtx_lock(xfer->usb2_mtx);
2136 USB_BUS_LOCK(xfer->udev->bus);
2137
2138 xfer->flags.stall_pipe = 1;
2139
2145 mtx_unlock(xfer->usb2_mtx);
2140 USB_BUS_UNLOCK(xfer->udev->bus);
2141
2142 return;
2143}
2144
2145/*------------------------------------------------------------------------*
2146 * usb2_transfer_clear_stall
2147 *
2148 * This function is used to clear the stall flag outside the
2149 * callback. This function is NULL safe.
2150 *------------------------------------------------------------------------*/
2151void
2152usb2_transfer_clear_stall(struct usb2_xfer *xfer)
2153{
2154 if (xfer == NULL) {
2155 /* tearing down */
2156 return;
2157 }
2163 mtx_assert(xfer->priv_mtx, MA_OWNED);
2158 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2159
2160 /* avoid any races by locking the USB mutex */
2166 mtx_lock(xfer->usb2_mtx);
2161 USB_BUS_LOCK(xfer->udev->bus);
2162
2163 xfer->flags.stall_pipe = 0;
2164
2170 mtx_unlock(xfer->usb2_mtx);
2165 USB_BUS_UNLOCK(xfer->udev->bus);
2166
2167 return;
2168}
2169
2170/*------------------------------------------------------------------------*
2171 * usb2_pipe_start
2172 *
2173 * This function is used to add an USB transfer to the pipe transfer list.
2174 *------------------------------------------------------------------------*/
2175void
2176usb2_pipe_start(struct usb2_xfer_queue *pq)
2177{
2178 struct usb2_pipe *pipe;
2179 struct usb2_xfer *xfer;
2180 uint8_t type;
2181
2182 xfer = pq->curr;
2183 pipe = xfer->pipe;
2184
2190 mtx_assert(xfer->usb2_mtx, MA_OWNED);
2185 USB_BUS_LOCK_ASSERT(xfer->udev->bus, MA_OWNED);
2186
2187 /*
2188 * If the pipe is already stalled we do nothing !
2189 */
2190 if (pipe->is_stalled) {
2191 return;
2192 }
2193 /*

--- 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);
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);
2323 (xfer->pipe->methods->close) (xfer);
2329 mtx_unlock(xfer->usb2_mtx);
2324 USB_BUS_UNLOCK(xfer->udev->bus);
2325 /* only close once */
2326 xfer->flags_int.did_close = 1;
2327 return (1); /* wait for new callback */
2328 }
2329 /*
2330 * If we have a non-hardware induced error we
2331 * need to do the DMA delay!
2332 */

--- 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);
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);
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);
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);
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);
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);
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);
2608 pipe->toggle_next = 0;
2614 mtx_unlock(&udev->bus->mtx);
2609 USB_BUS_UNLOCK(udev->bus);
2610 return;
2611}
2612
2613/*------------------------------------------------------------------------*
2614 * usb2_clear_stall_callback - factored out clear stall callback
2615 *
2616 * Input parameters:
2617 * xfer1: Clear Stall Control Transfer

--- 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);
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);
2785 (udev->bus->methods->do_poll) (udev->bus);
2791 mtx_unlock(xfer->priv_mtx);
2786 USB_XFER_UNLOCK(xfer);
2787
2788 /* poll clear stall start */
2794 mtx_lock(xfer->usb2_mtx);
2789 USB_BUS_LOCK(xfer->udev->bus);
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);
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);
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);
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);
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);
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