1310
r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1312
if (errno == ENOTTY)
1313
usbi_dbg("%s: getcap not available", filename);
1315
usbi_err(HANDLE_CTX(handle),
1316
"%s: getcap failed (%d)", filename, errno);
1318
if (supports_flag_zero_packet)
1319
hpriv->caps |= USBFS_CAP_ZERO_PACKET;
1320
if (supports_flag_bulk_continuation)
1321
hpriv->caps |= USBFS_CAP_BULK_CONTINUATION;
1309
1324
return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1523
1538
int fd = _device_handle_priv(handle)->fd;
1524
1539
struct usbfs_ioctl command;
1540
struct usbfs_getdriver getdrv;
1527
1543
command.ifno = interface;
1528
1544
command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1529
1545
command.data = NULL;
1547
getdrv.interface = interface;
1548
r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1549
if (r == 0 && strcmp(getdrv.driver, "usbfs") == 0)
1550
return LIBUSB_ERROR_NOT_FOUND;
1531
1552
r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1533
1554
if (errno == ENODATA)
1659
1681
if (tpriv->urbs)
1660
1682
return LIBUSB_ERROR_BUSY;
1662
if (is_out && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET &&
1663
!supports_flag_zero_packet)
1684
if (is_out && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) &&
1685
!(dpriv->caps & USBFS_CAP_ZERO_PACKET))
1664
1686
return LIBUSB_ERROR_NOT_SUPPORTED;
1666
/* usbfs places a 16kb limit on bulk URBs. we divide up larger requests
1667
* into smaller units to meet such restriction, then fire off all the
1668
* units at once. it would be simpler if we just fired one unit at a time,
1669
* but there is a big performance gain through doing it this way. */
1670
int num_urbs = transfer->length / MAX_BULK_BUFFER_LENGTH;
1689
* Older versions of usbfs place a 16kb limit on bulk URBs. We work
1690
* around this by splitting large transfers into 16k blocks, and then
1691
* submit all urbs at once. it would be simpler to submit one urb at
1692
* a time, but there is a big performance gain doing it this way.
1694
* Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1695
* using arbritary large transfers can still be a bad idea though, as
1696
* the kernel needs to allocate physical contiguous memory for this,
1697
* which may fail for large buffers.
1699
* The kernel solves this problem by splitting the transfer into
1700
* blocks itself when the host-controller is scatter-gather capable
1701
* (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1703
* Last, there is the issue of short-transfers when splitting, for
1704
* short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1705
* is needed, but this is not always available.
1707
if (dpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1708
/* Good! Just submit everything in one go */
1709
bulk_buffer_len = transfer->length ? transfer->length : 1;
1710
use_bulk_continuation = 0;
1711
} else if (dpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1712
/* Split the transfers and use bulk-continuation to
1713
avoid issues with short-transfers */
1714
bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1715
use_bulk_continuation = 1;
1716
} else if (dpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1717
/* Don't split, assume the kernel can alloc the buffer
1718
(otherwise the submit will fail with -ENOMEM) */
1719
bulk_buffer_len = transfer->length ? transfer->length : 1;
1720
use_bulk_continuation = 0;
1722
/* Bad, splitting without bulk-continuation, short transfers
1723
which end before the last urb will not work reliable! */
1724
/* Note we don't warn here as this is "normal" on kernels <
1725
2.6.32 and not a problem for most applications */
1726
bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1727
use_bulk_continuation = 0;
1730
int num_urbs = transfer->length / bulk_buffer_len;
1671
1731
int last_urb_partial = 0;
1673
1733
if (transfer->length == 0) {
1675
} else if ((transfer->length % MAX_BULK_BUFFER_LENGTH) > 0) {
1735
} else if ((transfer->length % bulk_buffer_len) > 0) {
1676
1736
last_urb_partial = 1;
1679
1739
usbi_dbg("need %d urbs for new transfer with length %d", num_urbs,
1680
1740
transfer->length);
1681
1741
alloc_size = num_urbs * sizeof(struct usbfs_urb);
1682
urbs = malloc(alloc_size);
1742
urbs = calloc(1, alloc_size);
1684
1744
return LIBUSB_ERROR_NO_MEM;
1685
memset(urbs, 0, alloc_size);
1686
1745
tpriv->urbs = urbs;
1687
1746
tpriv->num_urbs = num_urbs;
1688
1747
tpriv->num_retired = 0;
1694
1753
urb->usercontext = itransfer;
1695
1754
urb->type = urb_type;
1696
1755
urb->endpoint = transfer->endpoint;
1697
urb->buffer = transfer->buffer + (i * MAX_BULK_BUFFER_LENGTH);
1698
if (supports_flag_bulk_continuation && !is_out)
1756
urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1757
if (use_bulk_continuation && !is_out && (i != num_urbs - 1))
1699
1758
urb->flags = USBFS_URB_SHORT_NOT_OK;
1700
1759
if (i == num_urbs - 1 && last_urb_partial)
1701
urb->buffer_length = transfer->length % MAX_BULK_BUFFER_LENGTH;
1760
urb->buffer_length = transfer->length % bulk_buffer_len;
1702
1761
else if (transfer->length == 0)
1703
1762
urb->buffer_length = 0;
1705
urb->buffer_length = MAX_BULK_BUFFER_LENGTH;
1764
urb->buffer_length = bulk_buffer_len;
1707
if (i > 0 && supports_flag_bulk_continuation)
1766
if (i > 0 && use_bulk_continuation)
1708
1767
urb->flags |= USBFS_URB_BULK_CONTINUATION;
1710
1769
/* we have already checked that the flag is supported */
1791
1850
/* usbfs places a 32kb limit on iso URBs. we divide up larger requests
1792
1851
* into smaller units to meet such restriction, then fire off all the
1793
1852
* units at once. it would be simpler if we just fired one unit at a time,
1794
* but there is a big performance gain through doing it this way. */
1853
* but there is a big performance gain through doing it this way.
1855
* Newer kernels lift the 32k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1856
* using arbritary large transfers is still be a bad idea though, as
1857
* the kernel needs to allocate physical contiguous memory for this,
1858
* which may fail for large buffers.
1796
1861
/* calculate how many URBs we need */
1797
1862
for (i = 0; i < num_packets; i++) {