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

« back to all changes in this revision

Viewing changes to libusb/core.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:
18
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
 */
20
20
 
21
 
#include <config.h>
 
21
#include "config.h"
22
22
 
23
23
#include <errno.h>
24
24
#include <stdarg.h>
25
25
#include <stdio.h>
26
26
#include <stdlib.h>
27
27
#include <string.h>
 
28
#ifdef HAVE_SYS_TYPES_H
28
29
#include <sys/types.h>
29
 
 
 
30
#endif
30
31
#ifdef HAVE_SYS_TIME_H
31
32
#include <sys/time.h>
32
33
#endif
41
42
const struct usbi_os_backend * const usbi_backend = &openbsd_backend;
42
43
#elif defined(OS_WINDOWS)
43
44
const struct usbi_os_backend * const usbi_backend = &windows_backend;
 
45
#elif defined(OS_WINCE)
 
46
const struct usbi_os_backend * const usbi_backend = &wince_backend;
44
47
#else
45
48
#error "Unsupported OS"
46
49
#endif
48
51
struct libusb_context *usbi_default_context = NULL;
49
52
const struct libusb_version libusb_version_internal =
50
53
        { LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
51
 
          LIBUSB_RC, "unused - please use the nano" };
 
54
          LIBUSB_RC, "http://libusbx.org" };
52
55
static int default_context_refcnt = 0;
53
56
static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
54
57
static struct timeval timestamp_origin = { 0, 0 };
60
63
 *
61
64
 * libusbx is an open source library that allows you to communicate with USB
62
65
 * devices from userspace. For more info, see the
63
 
 * <a href="http://libusbx.sourceforge.net">libusbx homepage</a>.
 
66
 * <a href="http://libusbx.org">libusbx homepage</a>.
64
67
 *
65
68
 * This documentation is aimed at application developers wishing to
66
69
 * communicate with USB peripherals from their own software. After reviewing
67
70
 * this documentation, feedback and questions can be sent to the
68
 
 * <a href="https://lists.sourceforge.net/lists/listinfo/libusbx-devel">libusbx-devel mailing
69
 
 * list</a>.
 
71
 * <a href="http://mailing-list.libusbx.org">libusbx-devel mailing list</a>.
70
72
 *
71
73
 * This documentation assumes knowledge of how to operate USB devices from
72
74
 * a software standpoint (descriptors, configurations, interfaces, endpoints,
107
109
 *
108
110
 * \section msglog Debug message logging
109
111
 *
110
 
 * libusbx does not log any messages by default. Your application is therefore
111
 
 * free to close stdout/stderr and those descriptors may be reused without
112
 
 * worry.
 
112
 * libusbx uses stderr for all logging. By default, logging is set to NONE,
 
113
 * which means that no output will be produced. However, unless the library
 
114
 * has been compiled with logging disabled, then any application calls to
 
115
 * libusb_set_debug(), or the setting of the environmental variable
 
116
 * LIBUSB_DEBUG outside of the application, can result in logging being
 
117
 * produced. Your application should therefore not close stderr, but instead
 
118
 * direct it to the null device if its output is undesireable.
113
119
 *
114
 
 * The libusb_set_debug() function can be used to enable stderr logging of
115
 
 * certain messages. Under standard configuration, libusbx doesn't really
116
 
 * log much at all, so you are advised to use this function to enable all
117
 
 * error/warning/informational messages. It will help you debug problems with
118
 
 * your software.
 
120
 * The libusb_set_debug() function can be used to enable logging of certain
 
121
 * messages. Under standard configuration, libusbx doesn't really log much
 
122
 * so you are advised to use this function to enable all error/warning/
 
123
 * informational messages. It will help debug problems with your software.
119
124
 *
120
125
 * The logged messages are unstructured. There is no one-to-one correspondence
121
126
 * between messages being logged and success or failure return codes from
138
143
 * systems. In this case, libusb_set_debug() and the LIBUSB_DEBUG environment
139
144
 * variable have no effects.
140
145
 *
141
 
 * libusbx can also be compiled with verbose debugging messages. When the
142
 
 * library is compiled in this way, all messages of all verbosities are always
143
 
 * logged.  libusb_set_debug() and the LIBUSB_DEBUG environment variable have
144
 
 * no effects.
 
146
 * libusbx can also be compiled with verbose debugging messages always. When
 
147
 * the library is compiled in this way, all messages of all verbosities are
 
148
 * always logged. libusb_set_debug() and the LIBUSB_DEBUG environment variable
 
149
 * have no effects.
145
150
 *
146
151
 * \section remarks Other remarks
147
152
 *
672
677
 
673
678
/** \ingroup dev
674
679
 * Get the list of all port numbers from root for the specified device
 
680
 * \param ctx the context to operate on, or NULL for the default context
675
681
 * \param dev a device
676
682
 * \param path the array that should contain the port numbers
677
683
 * \param path_len the maximum length of the array. As per the USB 3.0
683
689
{
684
690
        int i = path_len;
685
691
        ssize_t r;
686
 
        struct libusb_device **devs;
 
692
        struct libusb_device **devs = NULL;
687
693
 
688
694
        /* The device needs to be open, else the parents may have been destroyed */
689
695
        r = libusb_get_device_list(ctx, &devs);
857
863
                return LIBUSB_ERROR_NOT_FOUND;
858
864
 
859
865
        val = ep->wMaxPacketSize;
860
 
        ep_type = ep->bmAttributes & 0x3;
 
866
        ep_type = (enum libusb_transfer_type) (ep->bmAttributes & 0x3);
861
867
        libusb_free_config_descriptor(config);
862
868
 
863
869
        r = val & 0x07ff;
1000
1006
 
1001
1007
        r = usbi_backend->open(_handle);
1002
1008
        if (r < 0) {
1003
 
                usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
 
1009
                usbi_dbg("could not open device: %s", libusb_error_name(r));
1004
1010
                libusb_unref_device(dev);
1005
1011
                usbi_mutex_destroy(&_handle->lock);
1006
1012
                free(_handle);
1513
1519
 *
1514
1520
 * This functionality is not available on Darwin or Windows.
1515
1521
 *
 
1522
 * Note that libusbx itself also talks to the device through a special kernel
 
1523
 * driver, if this driver is already attached to the device, this call will
 
1524
 * not detach it and return LIBUSB_ERROR_NOT_FOUND.
 
1525
 *
1516
1526
 * \param dev a device handle
1517
1527
 * \param interface_number the interface to detach the driver from
1518
1528
 * \returns 0 on success
1611
1621
 */
1612
1622
int API_EXPORTED libusb_init(libusb_context **context)
1613
1623
{
1614
 
        char *dbg = getenv("LIBUSB_DEBUG");
 
1624
        char *dbg;
1615
1625
        struct libusb_context *ctx;
1616
1626
        int r = 0;
1617
1627
 
1628
1638
                return 0;
1629
1639
        }
1630
1640
 
1631
 
        ctx = malloc(sizeof(*ctx));
 
1641
        ctx = calloc(1, sizeof(*ctx));
1632
1642
        if (!ctx) {
1633
1643
                r = LIBUSB_ERROR_NO_MEM;
1634
1644
                goto err_unlock;
1635
1645
        }
1636
 
        memset(ctx, 0, sizeof(*ctx));
1637
1646
 
1638
1647
#ifdef ENABLE_DEBUG_LOGGING
1639
1648
        ctx->debug = LIBUSB_LOG_LEVEL_DEBUG;
1640
1649
#endif
1641
1650
 
 
1651
        dbg = getenv("LIBUSB_DEBUG");
1642
1652
        if (dbg) {
1643
1653
                ctx->debug = atoi(dbg);
1644
1654
                if (ctx->debug)
1648
1658
        /* default context should be initialized before calling usbi_dbg */
1649
1659
        if (!usbi_default_context) {
1650
1660
                usbi_default_context = ctx;
 
1661
                default_context_refcnt++;
1651
1662
                usbi_dbg("created default context");
1652
1663
        }
1653
1664
 
1674
1685
 
1675
1686
        if (context) {
1676
1687
                *context = ctx;
1677
 
        } else if (!usbi_default_context) {
1678
 
                usbi_dbg("created default context");
1679
 
                usbi_default_context = ctx;
1680
 
                default_context_refcnt++;
1681
1688
        }
1682
1689
        usbi_mutex_static_unlock(&default_context_lock);
1683
1690
 
1733
1740
 
1734
1741
/** \ingroup misc
1735
1742
 * Check at runtime if the loaded library has a given capability.
 
1743
 * This call should be performed after \ref libusb_init(), to ensure the
 
1744
 * backend has updated its capability set.
1736
1745
 *
1737
1746
 * \param capability the \ref libusb_capability to check for
1738
 
 * \returns 1 if the running library has the capability, 0 otherwise
 
1747
 * \returns nonzero if the running library has the capability, 0 otherwise
1739
1748
 */
1740
1749
int API_EXPORTED libusb_has_capability(uint32_t capability)
1741
1750
{
1742
 
        enum libusb_capability cap = capability;
1743
 
        switch (cap) {
 
1751
        switch (capability) {
1744
1752
        case LIBUSB_CAP_HAS_CAPABILITY:
1745
1753
                return 1;
 
1754
        case LIBUSB_CAP_HAS_HID_ACCESS:
 
1755
                return (usbi_backend->caps & USBI_CAP_HAS_HID_ACCESS);
 
1756
        case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
 
1757
                return (usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
1746
1758
        }
1747
1759
        return 0;
1748
1760
}
1775
1787
#define _W32_FT_OFFSET (116444736000000000)
1776
1788
 
1777
1789
int usbi_gettimeofday(struct timeval *tp, void *tzp)
1778
 
 {
1779
 
  union {
1780
 
    unsigned __int64 ns100; /*time since 1 Jan 1601 in 100ns units */
1781
 
    FILETIME ft;
1782
 
  }  _now;
 
1790
{
 
1791
        union {
 
1792
                unsigned __int64 ns100; /* Time since 1 Jan 1601, in 100ns units */
 
1793
                FILETIME ft;
 
1794
        } _now;
 
1795
        UNUSED(tzp);
1783
1796
 
1784
 
  if(tp)
1785
 
    {
1786
 
      GetSystemTimeAsFileTime (&_now.ft);
1787
 
      tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
1788
 
      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
1789
 
    }
1790
 
  /* Always return 0 as per Open Group Base Specifications Issue 6.
1791
 
     Do not set errno on error.  */
1792
 
  return 0;
 
1797
        if(tp) {
 
1798
#if defined(OS_WINCE)
 
1799
                SYSTEMTIME st;
 
1800
                GetSystemTime(&st);
 
1801
                SystemTimeToFileTime(&st, &_now.ft);
 
1802
#else
 
1803
                GetSystemTimeAsFileTime (&_now.ft);
 
1804
#endif
 
1805
                tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
 
1806
                tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
 
1807
        }
 
1808
        /* Always return 0 as per Open Group Base Specifications Issue 6.
 
1809
           Do not set errno on error.  */
 
1810
        return 0;
1793
1811
}
1794
1812
#endif
1795
1813
 
1803
1821
 
1804
1822
#ifdef ENABLE_DEBUG_LOGGING
1805
1823
        global_debug = 1;
 
1824
        UNUSED(ctx);
1806
1825
#else
1807
1826
        USBI_GET_CONTEXT(ctx);
1808
1827
        if (ctx == NULL)
1875
1894
 
1876
1895
/** \ingroup misc
1877
1896
 * Returns a constant NULL-terminated string with the ASCII name of a libusb
1878
 
 * error code. The caller must not free() the returned string.
 
1897
 * error or transfer status code. The caller must not free() the returned
 
1898
 * string.
1879
1899
 *
1880
 
 * \param error_code The \ref libusb_error code to return the name of.
 
1900
 * \param error_code The \ref libusb_error or libusb_transfer_status code to
 
1901
 * return the name of.
1881
1902
 * \returns The error name, or the string **UNKNOWN** if the value of
1882
 
 * error_code is not a known error code.
 
1903
 * error_code is not a known error / status code.
1883
1904
 */
1884
1905
DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
1885
1906
{
1886
 
        enum libusb_error error = error_code;
1887
 
        switch (error) {
1888
 
        case LIBUSB_SUCCESS:
1889
 
                return "LIBUSB_SUCCESS";
 
1907
        switch (error_code) {
1890
1908
        case LIBUSB_ERROR_IO:
1891
1909
                return "LIBUSB_ERROR_IO";
1892
1910
        case LIBUSB_ERROR_INVALID_PARAM:
1913
1931
                return "LIBUSB_ERROR_NOT_SUPPORTED";
1914
1932
        case LIBUSB_ERROR_OTHER:
1915
1933
                return "LIBUSB_ERROR_OTHER";
 
1934
 
 
1935
        case LIBUSB_TRANSFER_ERROR:
 
1936
                return "LIBUSB_TRANSFER_ERROR";
 
1937
        case LIBUSB_TRANSFER_TIMED_OUT:
 
1938
                return "LIBUSB_TRANSFER_TIMED_OUT";
 
1939
        case LIBUSB_TRANSFER_CANCELLED:
 
1940
                return "LIBUSB_TRANSFER_CANCELLED";
 
1941
        case LIBUSB_TRANSFER_STALL:
 
1942
                return "LIBUSB_TRANSFER_STALL";
 
1943
        case LIBUSB_TRANSFER_NO_DEVICE:
 
1944
                return "LIBUSB_TRANSFER_NO_DEVICE";
 
1945
        case LIBUSB_TRANSFER_OVERFLOW:
 
1946
                return "LIBUSB_TRANSFER_OVERFLOW";
 
1947
 
 
1948
        case 0:
 
1949
                return "LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLETED";
 
1950
        default:
 
1951
                return "**UNKNOWN**";
1916
1952
        }
1917
 
        return "**UNKNOWN**";
1918
1953
}
1919
1954
 
1920
1955
/** \ingroup misc