Deleted Added
sdiff udiff text old ( 184610 ) new ( 184824 )
full compact
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_transfer.c 184610 2008-11-04 02:31:03Z alfred $ */
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,
828 &udev->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;
881 xfer->priv_mtx = priv_mtx;
882 xfer->usb2_mtx = &udev->bus->mtx;
883 xfer->usb2_root = info;
884 info->setup_refcount++;
885
886 usb2_callout_init_mtx(&xfer->timeout_handle, xfer->usb2_mtx,
887 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
1023 * "usb2_mtx" before returning !
1024 */
1025 mtx_lock(info->usb2_mtx);
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
1046 mtx_assert(info->usb2_mtx, MA_OWNED);
1047
1048 /* wait for any outstanding DMA operations */
1049
1050 if (needs_delay) {
1051 temp = usb2_get_dma_delay(info->bus);
1052 usb2_pause_mtx(info->usb2_mtx, temp);
1053 }
1054 mtx_unlock(info->usb2_mtx);
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) {
1108 mtx_lock(xfer->priv_mtx);
1109 mtx_lock(xfer->usb2_mtx);
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
1133 mtx_unlock(xfer->usb2_mtx);
1134 mtx_unlock(xfer->priv_mtx);
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
1157 mtx_lock(info->usb2_mtx);
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 {
1169 mtx_unlock(info->usb2_mtx);
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) {
1361 mtx_lock(xfer->usb2_mtx);
1362
1363 usb2_dump_pipe(xfer->pipe);
1364
1365 mtx_unlock(xfer->usb2_mtx);
1366 }
1367#endif
1368
1369 mtx_assert(xfer->priv_mtx, MA_OWNED);
1370 mtx_assert(xfer->usb2_mtx, 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
1378 mtx_lock(xfer->usb2_mtx);
1379 (xfer->pipe->methods->open) (xfer);
1380 mtx_unlock(xfer->usb2_mtx);
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) {
1390 mtx_lock(xfer->usb2_mtx);
1391 usb2_transfer_dequeue(xfer);
1392 mtx_unlock(xfer->usb2_mtx);
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);
1424 mtx_lock(xfer->usb2_mtx);
1425 xfer->flags_int.can_cancel_immed = 1;
1426 /* start the transfer */
1427 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer);
1428 mtx_unlock(xfer->usb2_mtx);
1429 return;
1430 }
1431 mtx_lock(xfer->usb2_mtx);
1432 usb2_transfer_done(xfer, USB_ERR_INVAL);
1433 mtx_unlock(xfer->usb2_mtx);
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 */
1442 mtx_lock(xfer->usb2_mtx);
1443 usb2_transfer_done(xfer, USB_ERR_INVAL);
1444 mtx_unlock(xfer->usb2_mtx);
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)) {
1459 mtx_lock(xfer->usb2_mtx);
1460 usb2_transfer_done(xfer, USB_ERR_STALLED);
1461 mtx_unlock(xfer->usb2_mtx);
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
1516 mtx_assert(xfer->priv_mtx, MA_OWNED);
1517
1518 mtx_lock(xfer->usb2_mtx);
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);
1534 mtx_unlock(xfer->usb2_mtx);
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);
1543 mtx_unlock(xfer->usb2_mtx);
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 }
1561 mtx_assert(xfer->priv_mtx, 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 }
1573 mtx_lock(xfer->usb2_mtx);
1574 /* call the USB transfer callback */
1575 usb2_callback_ss_done_defer(xfer);
1576 mtx_unlock(xfer->usb2_mtx);
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 }
1597 mtx_assert(xfer->priv_mtx, 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
1608 mtx_lock(xfer->usb2_mtx);
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
1660 mtx_unlock(xfer->usb2_mtx);
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
1679 mtx_assert(xfer->priv_mtx, MA_OWNED);
1680
1681 if (xfer->flags_int.transferring) {
1682 /* trivial case */
1683 return (1);
1684 }
1685 mtx_lock(xfer->usb2_mtx);
1686 if (xfer->wait_queue) {
1687 /* we are waiting on a queue somewhere */
1688 mtx_unlock(xfer->usb2_mtx);
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 */
1696 mtx_unlock(xfer->usb2_mtx);
1697 return (1);
1698 }
1699 /* we are not pending */
1700 mtx_unlock(xfer->usb2_mtx);
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 }
1722 if (xfer->priv_mtx != &Giant) {
1723 mtx_assert(xfer->priv_mtx, MA_NOTOWNED);
1724 }
1725 mtx_lock(xfer->priv_mtx);
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 */
1735 usb2_cv_wait(&xfer->usb2_root->cv_drain, xfer->priv_mtx);
1736 }
1737 mtx_unlock(xfer->priv_mtx);
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 */
1789 mtx_unlock(info->usb2_mtx);
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);
1796 mtx_lock(info->usb2_mtx);
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
1818 if (!mtx_owned(xfer->usb2_mtx)) {
1819 panic("%s: called unlocked!\n", __FUNCTION__);
1820 }
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
1857 if (!mtx_owned(xfer->usb2_mtx)) {
1858 panic("%s: called unlocked!\n", __FUNCTION__);
1859 }
1860 if (!mtx_owned(xfer->priv_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
1891 mtx_unlock(xfer->usb2_mtx);
1892 mtx_assert(xfer->usb2_mtx, 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 */
1899 mtx_lock(xfer->usb2_mtx);
1900 goto done;
1901 }
1902 } else {
1903
1904 if (usb2_callback_wrapper_sub(xfer)) {
1905 /* the callback has been deferred */
1906 mtx_lock(xfer->usb2_mtx);
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 */
1929 mtx_lock(xfer->usb2_mtx);
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
1968 mtx_assert(xfer->usb2_mtx, MA_OWNED);
1969
1970 DPRINTFN(3, "Completed %p\n", xfer);
1971
1972 /* queue callback for execution, again */
1973 usb2_transfer_done(xfer, 0);
1974
1975 mtx_unlock(xfer->usb2_mtx);
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
2038 mtx_assert(xfer->usb2_mtx, 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
2064 if (mtx_owned(xfer->priv_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
2103 mtx_assert(xfer->usb2_mtx, 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 }
2120 mtx_unlock(xfer->usb2_mtx);
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 }
2138 mtx_assert(xfer->priv_mtx, MA_OWNED);
2139
2140 /* avoid any races by locking the USB mutex */
2141 mtx_lock(xfer->usb2_mtx);
2142
2143 xfer->flags.stall_pipe = 1;
2144
2145 mtx_unlock(xfer->usb2_mtx);
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 }
2163 mtx_assert(xfer->priv_mtx, MA_OWNED);
2164
2165 /* avoid any races by locking the USB mutex */
2166 mtx_lock(xfer->usb2_mtx);
2167
2168 xfer->flags.stall_pipe = 0;
2169
2170 mtx_unlock(xfer->usb2_mtx);
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
2190 mtx_assert(xfer->usb2_mtx, 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{
2293 mtx_assert(xfer->usb2_mtx, 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");
2327 mtx_lock(xfer->usb2_mtx);
2328 (xfer->pipe->methods->close) (xfer);
2329 mtx_unlock(xfer->usb2_mtx);
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) {
2356 mtx_lock(xfer->usb2_mtx);
2357 usb2_transfer_timeout_ms(xfer,
2358 &usb2_dma_delay_done_cb, temp);
2359 mtx_unlock(xfer->usb2_mtx);
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 */
2451 mtx_lock(xfer->usb2_mtx);
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 }
2463 mtx_unlock(xfer->usb2_mtx);
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) {
2547 mtx_lock(xfer->priv_mtx);
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 }
2562 mtx_unlock(xfer->priv_mtx);
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
2612 mtx_lock(&udev->bus->mtx);
2613 pipe->toggle_next = 0;
2614 mtx_unlock(&udev->bus->mtx);
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 }
2658 mtx_assert(xfer1->priv_mtx, MA_OWNED);
2659 mtx_assert(xfer2->priv_mtx, 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 */
2789 mtx_lock(xfer->priv_mtx);
2790 (udev->bus->methods->do_poll) (udev->bus);
2791 mtx_unlock(xfer->priv_mtx);
2792
2793 /* poll clear stall start */
2794 mtx_lock(xfer->usb2_mtx);
2795 pm = &udev->cs_msg[0].hdr;
2796 (pm->pm_callback) (pm);
2797 mtx_unlock(xfer->usb2_mtx);
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 */
2805 mtx_lock(xfer->usb2_mtx);
2806 pm = &udev->default_xfer[1]->
2807 usb2_root->done_m[0].hdr;
2808 (pm->pm_callback) (pm);
2809 mtx_unlock(xfer->usb2_mtx);
2810 }
2811 /* poll timeout */
2812 usb2_callout_poll(xfer);
2813
2814 /* poll done thread */
2815 mtx_lock(xfer->usb2_mtx);
2816 pm = &usb2_root->done_m[0].hdr;
2817 (pm->pm_callback) (pm);
2818 mtx_unlock(xfer->usb2_mtx);
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