~ubuntu-branches/ubuntu/saucy/libusbx/saucy-proposed

« back to all changes in this revision

Viewing changes to libusb/os/linux_usbfs.c

  • Committer: Package Import Robot
  • Author(s): Aurelien Jarno
  • Date: 2013-05-06 17:31:43 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130506173143-wfbiltvcnn7pvdpa
Tags: 2:1.0.15-1
* New upstream version.
* Fix cross-build (closes: #694912, #705658).
* Add a -dbg package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
116
116
 
117
117
struct linux_device_handle_priv {
118
118
        int fd;
 
119
        uint32_t caps;
119
120
};
120
121
 
121
122
enum reap_action {
689
690
                        r = LIBUSB_ERROR_NOT_FOUND;
690
691
                } else if (r < len - sizeof(tmp)) {
691
692
                        usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, len);
692
 
                        r = LIBUSB_ERROR_IO;
 
693
                        r = 0;
693
694
                }
694
695
        } else {
695
696
                r = 0;
739
740
                return LIBUSB_ERROR_IO;
740
741
        } else if (r < len) {
741
742
                usbi_err(ctx, "short output read %d/%d", r, len);
742
 
                return LIBUSB_ERROR_IO;
743
743
        }
744
744
 
745
745
        return 0;
1284
1284
{
1285
1285
        struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
1286
1286
        char filename[PATH_MAX];
 
1287
        int r;
1287
1288
 
1288
1289
        _get_usbfs_path(handle->dev, filename);
1289
1290
        usbi_dbg("opening %s", filename);
1306
1307
                }
1307
1308
        }
1308
1309
 
 
1310
        r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
 
1311
        if (r < 0) {
 
1312
                if (errno == ENOTTY)
 
1313
                        usbi_dbg("%s: getcap not available", filename);
 
1314
                else
 
1315
                        usbi_err(HANDLE_CTX(handle),
 
1316
                                 "%s: getcap failed (%d)", filename, errno);
 
1317
                hpriv->caps = 0;
 
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;
 
1322
        }
 
1323
 
1309
1324
        return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1310
1325
}
1311
1326
 
1522
1537
{
1523
1538
        int fd = _device_handle_priv(handle)->fd;
1524
1539
        struct usbfs_ioctl command;
 
1540
        struct usbfs_getdriver getdrv;
1525
1541
        int r;
1526
1542
 
1527
1543
        command.ifno = interface;
1528
1544
        command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1529
1545
        command.data = NULL;
1530
1546
 
 
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;
 
1551
 
1531
1552
        r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1532
1553
        if (r) {
1533
1554
                if (errno == ENODATA)
1652
1673
        struct usbfs_urb *urbs;
1653
1674
        int is_out = (transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK)
1654
1675
                == LIBUSB_ENDPOINT_OUT;
 
1676
        int bulk_buffer_len, use_bulk_continuation;
1655
1677
        int r;
1656
1678
        int i;
1657
1679
        size_t alloc_size;
1659
1681
        if (tpriv->urbs)
1660
1682
                return LIBUSB_ERROR_BUSY;
1661
1683
 
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;
1665
1687
 
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;
 
1688
        /*
 
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.
 
1693
         *
 
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.
 
1698
         *
 
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.
 
1702
         *
 
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.
 
1706
         */
 
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;
 
1721
        } else {
 
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;
 
1728
        }
 
1729
 
 
1730
        int num_urbs = transfer->length / bulk_buffer_len;
1671
1731
        int last_urb_partial = 0;
1672
1732
 
1673
1733
        if (transfer->length == 0) {
1674
1734
                num_urbs = 1;
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;
1677
1737
                num_urbs++;
1678
1738
        }
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);
1683
1743
        if (!urbs)
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;
1704
1763
                else
1705
 
                        urb->buffer_length = MAX_BULK_BUFFER_LENGTH;
 
1764
                        urb->buffer_length = bulk_buffer_len;
1706
1765
 
1707
 
                if (i > 0 && supports_flag_bulk_continuation)
 
1766
                if (i > 0 && use_bulk_continuation)
1708
1767
                        urb->flags |= USBFS_URB_BULK_CONTINUATION;
1709
1768
 
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.
 
1854
         *
 
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.
 
1859
         */
1795
1860
 
1796
1861
        /* calculate how many URBs we need */
1797
1862
        for (i = 0; i < num_packets; i++) {
1808
1873
        usbi_dbg("need %d 32k URBs for transfer", num_urbs);
1809
1874
 
1810
1875
        alloc_size = num_urbs * sizeof(*urbs);
1811
 
        urbs = malloc(alloc_size);
 
1876
        urbs = calloc(1, alloc_size);
1812
1877
        if (!urbs)
1813
1878
                return LIBUSB_ERROR_NO_MEM;
1814
 
        memset(urbs, 0, alloc_size);
1815
1879
 
1816
1880
        tpriv->iso_urbs = urbs;
1817
1881
        tpriv->num_urbs = num_urbs;
1845
1909
 
1846
1910
                alloc_size = sizeof(*urb)
1847
1911
                        + (urb_packet_offset * sizeof(struct usbfs_iso_packet_desc));
1848
 
                urb = malloc(alloc_size);
 
1912
                urb = calloc(1, alloc_size);
1849
1913
                if (!urb) {
1850
1914
                        free_iso_urbs(tpriv);
1851
1915
                        return LIBUSB_ERROR_NO_MEM;
1852
1916
                }
1853
 
                memset(urb, 0, alloc_size);
1854
1917
                urbs[i] = urb;
1855
1918
 
1856
1919
                /* populate packet lengths */
1934
1997
        if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
1935
1998
                return LIBUSB_ERROR_INVALID_PARAM;
1936
1999
 
1937
 
        urb = malloc(sizeof(struct usbfs_urb));
 
2000
        urb = calloc(1, sizeof(struct usbfs_urb));
1938
2001
        if (!urb)
1939
2002
                return LIBUSB_ERROR_NO_MEM;
1940
 
        memset(urb, 0, sizeof(struct usbfs_urb));
1941
2003
        tpriv->urbs = urb;
1942
2004
        tpriv->num_urbs = 1;
1943
2005
        tpriv->reap_action = NORMAL;
2428
2490
                        continue;
2429
2491
                }
2430
2492
 
2431
 
                r = reap_for_handle(handle);
 
2493
                do {
 
2494
                        r = reap_for_handle(handle);
 
2495
                } while (r == 0);
2432
2496
                if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2433
2497
                        continue;
2434
2498
                else if (r < 0)
2463
2527
 
2464
2528
const struct usbi_os_backend linux_usbfs_backend = {
2465
2529
        .name = "Linux usbfs",
 
2530
        .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2466
2531
        .init = op_init,
2467
2532
        .exit = NULL,
2468
2533
        .get_device_list = op_get_device_list,