SHARE
TWEET

Untitled

a guest Feb 19th, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module libusb;
  2.  
  3. alias ushort uint16_t;
  4. alias ubyte uint8_t;
  5.  
  6. extern (C)
  7. {
  8. /*
  9.  * Public libusbx header file
  10.  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
  11.  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
  12.  *
  13.  * This library is free software; you can redistribute it and/or
  14.  * modify it under the terms of the GNU Lesser General Public
  15.  * License as published by the Free Software Foundation; either
  16.  * version 2.1 of the License, or (at your option) any later version.
  17.  *
  18.  * This library is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21.  * Lesser General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU Lesser General Public
  24.  * License along with this library; if not, write to the Free Software
  25.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26.  */
  27.  
  28. /** \def libusb_cpu_to_le16
  29.  * \ingroup misc
  30.  * Convert a 16-bit value from host-endian to little-endian format. On
  31.  * little endian systems, this function does nothing. On big endian systems,
  32.  * the bytes are swapped.
  33.  * \param x the host-endian value to convert
  34.  * \returns the value in little-endian byte order
  35.  */
  36. uint16_t libusb_cpu_to_le16(const uint16_t x)
  37. {
  38.     union {
  39.         uint8_t  b8[2];
  40.         uint16_t b16;
  41.     } _tmp;
  42.     _tmp.b8[1] = x >> 8;
  43.     _tmp.b8[0] = x & 0xff;
  44.     return _tmp.b16;
  45. }
  46.  
  47. /** \def libusb_le16_to_cpu
  48.  * \ingroup misc
  49.  * Convert a 16-bit value from little-endian to host-endian format. On
  50.  * little endian systems, this function does nothing. On big endian systems,
  51.  * the bytes are swapped.
  52.  * \param x the little-endian value to convert
  53.  * \returns the value in host-endian byte order
  54.  */
  55. alias libusb_le16_to_cpu libusb_cpu_to_le16;
  56.  
  57. /* standard USB stuff */
  58.  
  59. /** \ingroup desc
  60.  * Device and/or Interface Class codes */
  61. enum libusb_class_code {
  62.     /** In the context of a \ref libusb_device_descriptor "device descriptor",
  63.      * this bDeviceClass value indicates that each interface specifies its
  64.      * own class information and all interfaces operate independently.
  65.      */
  66.     LIBUSB_CLASS_PER_INTERFACE = 0,
  67.  
  68.     /** Audio class */
  69.     LIBUSB_CLASS_AUDIO = 1,
  70.  
  71.     /** Communications class */
  72.     LIBUSB_CLASS_COMM = 2,
  73.  
  74.     /** Human Interface Device class */
  75.     LIBUSB_CLASS_HID = 3,
  76.  
  77.     /** Physical */
  78.     LIBUSB_CLASS_PHYSICAL = 5,
  79.  
  80.     /** Printer class */
  81.     LIBUSB_CLASS_PRINTER = 7,
  82.  
  83.     /** Image class */
  84.     LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
  85.     LIBUSB_CLASS_IMAGE = 6,
  86.  
  87.     /** Mass storage class */
  88.     LIBUSB_CLASS_MASS_STORAGE = 8,
  89.  
  90.     /** Hub class */
  91.     LIBUSB_CLASS_HUB = 9,
  92.  
  93.     /** Data class */
  94.     LIBUSB_CLASS_DATA = 10,
  95.  
  96.     /** Smart Card */
  97.     LIBUSB_CLASS_SMART_CARD = 0x0b,
  98.  
  99.     /** Content Security */
  100.     LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
  101.  
  102.     /** Video */
  103.     LIBUSB_CLASS_VIDEO = 0x0e,
  104.  
  105.     /** Personal Healthcare */
  106.     LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
  107.  
  108.     /** Diagnostic Device */
  109.     LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
  110.  
  111.     /** Wireless class */
  112.     LIBUSB_CLASS_WIRELESS = 0xe0,
  113.  
  114.     /** Application class */
  115.     LIBUSB_CLASS_APPLICATION = 0xfe,
  116.  
  117.     /** Class is vendor-specific */
  118.     LIBUSB_CLASS_VENDOR_SPEC = 0xff
  119. };
  120.  
  121. /** \ingroup desc
  122.  * Descriptor types as defined by the USB specification. */
  123. enum libusb_descriptor_type {
  124.     /** Device descriptor. See libusb_device_descriptor. */
  125.     LIBUSB_DT_DEVICE = 0x01,
  126.  
  127.     /** Configuration descriptor. See libusb_config_descriptor. */
  128.     LIBUSB_DT_CONFIG = 0x02,
  129.  
  130.     /** String descriptor */
  131.     LIBUSB_DT_STRING = 0x03,
  132.  
  133.     /** Interface descriptor. See libusb_interface_descriptor. */
  134.     LIBUSB_DT_INTERFACE = 0x04,
  135.  
  136.     /** Endpoint descriptor. See libusb_endpoint_descriptor. */
  137.     LIBUSB_DT_ENDPOINT = 0x05,
  138.  
  139.     /** HID descriptor */
  140.     LIBUSB_DT_HID = 0x21,
  141.  
  142.     /** HID report descriptor */
  143.     LIBUSB_DT_REPORT = 0x22,
  144.  
  145.     /** Physical descriptor */
  146.     LIBUSB_DT_PHYSICAL = 0x23,
  147.  
  148.     /** Hub descriptor */
  149.     LIBUSB_DT_HUB = 0x29,
  150. };
  151.  
  152. /* Descriptor sizes per descriptor type */
  153. enum LIBUSB_DT_DEVICE_SIZE = 18;
  154. enum LIBUSB_DT_CONFIG_SIZE = 9;
  155. enum LIBUSB_DT_INTERFACE_SIZE = 9;
  156. enum LIBUSB_DT_ENDPOINT_SIZE = 7;
  157. enum LIBUSB_DT_ENDPOINT_AUDIO_SIZE = 9; /* Audio extension */
  158. enum LIBUSB_DT_HUB_NONVAR_SIZE = 7;
  159.  
  160. enum LIBUSB_ENDPOINT_ADDRESS_MASK = 0x0f;    /* in bEndpointAddress */
  161. enum LIBUSB_ENDPOINT_DIR_MASK = 0x80;
  162.  
  163. /** \ingroup desc
  164.  * Endpoint direction. Values for bit 7 of the
  165.  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
  166.  */
  167. enum libusb_endpoint_direction {
  168.     /** In: device-to-host */
  169.     LIBUSB_ENDPOINT_IN = 0x80,
  170.  
  171.     /** Out: host-to-device */
  172.     LIBUSB_ENDPOINT_OUT = 0x00
  173. };
  174.  
  175. enum LIBUSB_TRANSFER_TYPE_MASK = 0x03;    /* in bmAttributes */
  176.  
  177. /** \ingroup desc
  178.  * Endpoint transfer type. Values for bits 0:1 of the
  179.  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
  180.  */
  181. enum libusb_transfer_type {
  182.     /** Control endpoint */
  183.     LIBUSB_TRANSFER_TYPE_CONTROL = 0,
  184.  
  185.     /** Isochronous endpoint */
  186.     LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
  187.  
  188.     /** Bulk endpoint */
  189.     LIBUSB_TRANSFER_TYPE_BULK = 2,
  190.  
  191.     /** Interrupt endpoint */
  192.     LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
  193. };
  194.  
  195. /** \ingroup misc
  196.  * Standard requests, as defined in table 9-3 of the USB2 specifications */
  197. enum libusb_standard_request {
  198.     /** Request status of the specific recipient */
  199.     LIBUSB_REQUEST_GET_STATUS = 0x00,
  200.  
  201.     /** Clear or disable a specific feature */
  202.     LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
  203.  
  204.     /* 0x02 is reserved */
  205.  
  206.     /** Set or enable a specific feature */
  207.     LIBUSB_REQUEST_SET_FEATURE = 0x03,
  208.  
  209.     /* 0x04 is reserved */
  210.  
  211.     /** Set device address for all future accesses */
  212.     LIBUSB_REQUEST_SET_ADDRESS = 0x05,
  213.  
  214.     /** Get the specified descriptor */
  215.     LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
  216.  
  217.     /** Used to update existing descriptors or add new descriptors */
  218.     LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
  219.  
  220.     /** Get the current device configuration value */
  221.     LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
  222.  
  223.     /** Set device configuration */
  224.     LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
  225.  
  226.     /** Return the selected alternate setting for the specified interface */
  227.     LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
  228.  
  229.     /** Select an alternate interface for the specified interface */
  230.     LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
  231.  
  232.     /** Set then report an endpoint's synchronization frame */
  233.     LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
  234. };
  235.  
  236. /** \ingroup misc
  237.  * Request type bits of the
  238.  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
  239.  * transfers. */
  240. enum libusb_request_type {
  241.     /** Standard */
  242.     LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
  243.  
  244.     /** Class */
  245.     LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
  246.  
  247.     /** Vendor */
  248.     LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
  249.  
  250.     /** Reserved */
  251.     LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
  252. };
  253.  
  254. /** \ingroup misc
  255.  * Recipient bits of the
  256.  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
  257.  * transfers. Values 4 through 31 are reserved. */
  258. enum libusb_request_recipient {
  259.     /** Device */
  260.     LIBUSB_RECIPIENT_DEVICE = 0x00,
  261.  
  262.     /** Interface */
  263.     LIBUSB_RECIPIENT_INTERFACE = 0x01,
  264.  
  265.     /** Endpoint */
  266.     LIBUSB_RECIPIENT_ENDPOINT = 0x02,
  267.  
  268.     /** Other */
  269.     LIBUSB_RECIPIENT_OTHER = 0x03,
  270. };
  271.  
  272. enum LIBUSB_ISO_SYNC_TYPE_MASK = 0x0C;
  273.  
  274. /** \ingroup desc
  275.  * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
  276.  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
  277.  * libusb_endpoint_descriptor.
  278.  */
  279. enum libusb_iso_sync_type {
  280.     /** No synchronization */
  281.     LIBUSB_ISO_SYNC_TYPE_NONE = 0,
  282.  
  283.     /** Asynchronous */
  284.     LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
  285.  
  286.     /** Adaptive */
  287.     LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
  288.  
  289.     /** Synchronous */
  290.     LIBUSB_ISO_SYNC_TYPE_SYNC = 3
  291. };
  292.  
  293. enum LIBUSB_ISO_USAGE_TYPE_MASK = 0x30;
  294.  
  295. /** \ingroup desc
  296.  * Usage type for isochronous endpoints. Values for bits 4:5 of the
  297.  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
  298.  * libusb_endpoint_descriptor.
  299.  */
  300. enum libusb_iso_usage_type {
  301.     /** Data endpoint */
  302.     LIBUSB_ISO_USAGE_TYPE_DATA = 0,
  303.  
  304.     /** Feedback endpoint */
  305.     LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
  306.  
  307.     /** Implicit feedback Data endpoint */
  308.     LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
  309. };
  310.  
  311. /** \ingroup desc
  312.  * A structure representing the standard USB device descriptor. This
  313.  * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
  314.  * All multiple-byte fields are represented in host-endian format.
  315.  */
  316. struct libusb_device_descriptor {
  317.     /** Size of this descriptor (in bytes) */
  318.     uint8_t  bLength;
  319.  
  320.     /** Descriptor type. Will have value
  321.      * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
  322.      * context. */
  323.     uint8_t  bDescriptorType;
  324.  
  325.     /** USB specification release number in binary-coded decimal. A value of
  326.      * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
  327.     uint16_t bcdUSB;
  328.  
  329.     /** USB-IF class code for the device. See \ref libusb_class_code. */
  330.     uint8_t  bDeviceClass;
  331.  
  332.     /** USB-IF subclass code for the device, qualified by the bDeviceClass
  333.      * value */
  334.     uint8_t  bDeviceSubClass;
  335.  
  336.     /** USB-IF protocol code for the device, qualified by the bDeviceClass and
  337.      * bDeviceSubClass values */
  338.     uint8_t  bDeviceProtocol;
  339.  
  340.     /** Maximum packet size for endpoint 0 */
  341.     uint8_t  bMaxPacketSize0;
  342.  
  343.     /** USB-IF vendor ID */
  344.     uint16_t idVendor;
  345.  
  346.     /** USB-IF product ID */
  347.     uint16_t idProduct;
  348.  
  349.     /** Device release number in binary-coded decimal */
  350.     uint16_t bcdDevice;
  351.  
  352.     /** Index of string descriptor describing manufacturer */
  353.     uint8_t  iManufacturer;
  354.  
  355.     /** Index of string descriptor describing product */
  356.     uint8_t  iProduct;
  357.  
  358.     /** Index of string descriptor containing device serial number */
  359.     uint8_t  iSerialNumber;
  360.  
  361.     /** Number of possible configurations */
  362.     uint8_t  bNumConfigurations;
  363. };
  364.  
  365. /** \ingroup desc
  366.  * A structure representing the standard USB endpoint descriptor. This
  367.  * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
  368.  * All multiple-byte fields are represented in host-endian format.
  369.  */
  370. struct libusb_endpoint_descriptor {
  371.     /** Size of this descriptor (in bytes) */
  372.     uint8_t  bLength;
  373.  
  374.     /** Descriptor type. Will have value
  375.      * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
  376.      * this context. */
  377.     uint8_t  bDescriptorType;
  378.  
  379.     /** The address of the endpoint described by this descriptor. Bits 0:3 are
  380.      * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
  381.      * see \ref libusb_endpoint_direction.
  382.      */
  383.     uint8_t  bEndpointAddress;
  384.  
  385.     /** Attributes which apply to the endpoint when it is configured using
  386.      * the bConfigurationValue. Bits 0:1 determine the transfer type and
  387.      * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
  388.      * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
  389.      * Bits 4:5 are also only used for isochronous endpoints and correspond to
  390.      * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
  391.      */
  392.     uint8_t  bmAttributes;
  393.  
  394.     /** Maximum packet size this endpoint is capable of sending/receiving. */
  395.     uint16_t wMaxPacketSize;
  396.  
  397.     /** Interval for polling endpoint for data transfers. */
  398.     uint8_t  bInterval;
  399.  
  400.     /** For audio devices only: the rate at which synchronization feedback
  401.      * is provided. */
  402.     uint8_t  bRefresh;
  403.  
  404.     /** For audio devices only: the address if the synch endpoint */
  405.     uint8_t  bSynchAddress;
  406.  
  407.     /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
  408.      * it will store them here, should you wish to parse them. */
  409.     const(ubyte) *extra;
  410.  
  411.     /** Length of the extra descriptors, in bytes. */
  412.     int extra_length;
  413. };
  414.  
  415. /** \ingroup desc
  416.  * A structure representing the standard USB interface descriptor. This
  417.  * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
  418.  * All multiple-byte fields are represented in host-endian format.
  419.  */
  420. struct libusb_interface_descriptor {
  421.     /** Size of this descriptor (in bytes) */
  422.     uint8_t  bLength;
  423.  
  424.     /** Descriptor type. Will have value
  425.      * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
  426.      * in this context. */
  427.     uint8_t  bDescriptorType;
  428.  
  429.     /** Number of this interface */
  430.     uint8_t  bInterfaceNumber;
  431.  
  432.     /** Value used to select this alternate setting for this interface */
  433.     uint8_t  bAlternateSetting;
  434.  
  435.     /** Number of endpoints used by this interface (excluding the control
  436.      * endpoint). */
  437.     uint8_t  bNumEndpoints;
  438.  
  439.     /** USB-IF class code for this interface. See \ref libusb_class_code. */
  440.     uint8_t  bInterfaceClass;
  441.  
  442.     /** USB-IF subclass code for this interface, qualified by the
  443.      * bInterfaceClass value */
  444.     uint8_t  bInterfaceSubClass;
  445.  
  446.     /** USB-IF protocol code for this interface, qualified by the
  447.      * bInterfaceClass and bInterfaceSubClass values */
  448.     uint8_t  bInterfaceProtocol;
  449.  
  450.     /** Index of string descriptor describing this interface */
  451.     uint8_t  iInterface;
  452.  
  453.     /** Array of endpoint descriptors. This length of this array is determined
  454.      * by the bNumEndpoints field. */
  455.     const(libusb_endpoint_descriptor) *endpoint;
  456.  
  457.     /** Extra descriptors. If libusbx encounters unknown interface descriptors,
  458.      * it will store them here, should you wish to parse them. */
  459.     const(ubyte) *extra;
  460.  
  461.     /** Length of the extra descriptors, in bytes. */
  462.     int extra_length;
  463. };
  464.  
  465. /** \ingroup desc
  466.  * A collection of alternate settings for a particular USB interface.
  467.  */
  468. struct libusb_interface {
  469.     /** Array of interface descriptors. The length of this array is determined
  470.      * by the num_altsetting field. */
  471.     const(libusb_interface_descriptor) *altsetting;
  472.  
  473.     /** The number of alternate settings that belong to this interface */
  474.     int num_altsetting;
  475. };
  476.  
  477. /** \ingroup desc
  478.  * A structure representing the standard USB configuration descriptor. This
  479.  * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
  480.  * All multiple-byte fields are represented in host-endian format.
  481.  */
  482. struct libusb_config_descriptor {
  483.     /** Size of this descriptor (in bytes) */
  484.     uint8_t  bLength;
  485.  
  486.     /** Descriptor type. Will have value
  487.      * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
  488.      * in this context. */
  489.     uint8_t  bDescriptorType;
  490.  
  491.     /** Total length of data returned for this configuration */
  492.     uint16_t wTotalLength;
  493.  
  494.     /** Number of interfaces supported by this configuration */
  495.     uint8_t  bNumInterfaces;
  496.  
  497.     /** Identifier value for this configuration */
  498.     uint8_t  bConfigurationValue;
  499.  
  500.     /** Index of string descriptor describing this configuration */
  501.     uint8_t  iConfiguration;
  502.  
  503.     /** Configuration characteristics */
  504.     uint8_t  bmAttributes;
  505.  
  506.     /** Maximum power consumption of the USB device from this bus in this
  507.      * configuration when the device is fully opreation. Expressed in units
  508.      * of 2 mA. */
  509.     uint8_t  MaxPower;
  510.  
  511.     /** Array of interfaces supported by this configuration. The length of
  512.      * this array is determined by the bNumInterfaces field. */
  513.     const(libusb_interface) *_interface;
  514.  
  515.     /** Extra descriptors. If libusbx encounters unknown configuration
  516.      * descriptors, it will store them here, should you wish to parse them. */
  517.     const(ubyte) *extra;
  518.  
  519.     /** Length of the extra descriptors, in bytes. */
  520.     int extra_length;
  521. };
  522.  
  523. /** \ingroup asyncio
  524.  * Setup packet for control transfers. */
  525. struct libusb_control_setup {
  526.     /** Request type. Bits 0:4 determine recipient, see
  527.      * \ref libusb_request_recipient. Bits 5:6 determine type, see
  528.      * \ref libusb_request_type. Bit 7 determines data transfer direction, see
  529.      * \ref libusb_endpoint_direction.
  530.      */
  531.     uint8_t  bmRequestType;
  532.  
  533.     /** Request. If the type bits of bmRequestType are equal to
  534.      * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
  535.      * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
  536.      * \ref libusb_standard_request. For other cases, use of this field is
  537.      * application-specific. */
  538.     uint8_t  bRequest;
  539.  
  540.     /** Value. Varies according to request */
  541.     uint16_t wValue;
  542.  
  543.     /** Index. Varies according to request, typically used to pass an index
  544.      * or offset */
  545.     uint16_t wIndex;
  546.  
  547.     /** Number of bytes to transfer */
  548.     uint16_t wLength;
  549. };
  550.  
  551. enum LIBUSB_CONTROL_SETUP_SIZE = ((libusb_control_setup).sizeof);
  552.  
  553. /* libusbx */
  554.  
  555. struct libusb_context;
  556. struct libusb_device;
  557. struct libusb_device_handle;
  558.  
  559. /** \ingroup lib
  560.  * Structure providing the version of the libusbx runtime
  561.  */
  562. struct libusb_version {
  563.     /** Library major version. */
  564.     const uint16_t major;
  565.  
  566.     /** Library minor version. */
  567.     const uint16_t minor;
  568.  
  569.     /** Library micro version. */
  570.     const uint16_t micro;
  571.  
  572.     /** Library nano version. */
  573.     const uint16_t nano;
  574.  
  575.     /** Library release candidate suffix string, e.g. "-rc4". */
  576.     const char *rc;
  577.  
  578.     /** For ABI compatibility only. */
  579.     const char* describe;
  580. };
  581.  
  582. /** \ingroup lib
  583.  * Structure representing a libusbx session. The concept of individual libusbx
  584.  * sessions allows for your program to use two libraries (or dynamically
  585.  * load two modules) which both independently use libusb. This will prevent
  586.  * interference between the individual libusbx users - for example
  587.  * libusb_set_debug() will not affect the other user of the library, and
  588.  * libusb_exit() will not destroy resources that the other user is still
  589.  * using.
  590.  *
  591.  * Sessions are created by libusb_init() and destroyed through libusb_exit().
  592.  * If your application is guaranteed to only ever include a single libusbx
  593.  * user (i.e. you), you do not have to worry about contexts: pass NULL in
  594.  * every function call where a context is required. The default context
  595.  * will be used.
  596.  *
  597.  * For more information, see \ref contexts.
  598.  */
  599. struct libusb_context;
  600.  
  601. /** \ingroup dev
  602.  * Structure representing a USB device detected on the system. This is an
  603.  * opaque type for which you are only ever provided with a pointer, usually
  604.  * originating from libusb_get_device_list().
  605.  *
  606.  * Certain operations can be performed on a device, but in order to do any
  607.  * I/O you will have to first obtain a device handle using libusb_open().
  608.  *
  609.  * Devices are reference counted with libusb_device_ref() and
  610.  * libusb_device_unref(), and are freed when the reference count reaches 0.
  611.  * New devices presented by libusb_get_device_list() have a reference count of
  612.  * 1, and libusb_free_device_list() can optionally decrease the reference count
  613.  * on all devices in the list. libusb_open() adds another reference which is
  614.  * later destroyed by libusb_close().
  615.  */
  616. struct libusb_device;
  617.  
  618.  
  619. /** \ingroup dev
  620.  * Structure representing a handle on a USB device. This is an opaque type for
  621.  * which you are only ever provided with a pointer, usually originating from
  622.  * libusb_open().
  623.  *
  624.  * A device handle is used to perform I/O and other operations. When finished
  625.  * with a device handle, you should call libusb_close().
  626.  */
  627. struct libusb_device_handle;
  628.  
  629. /** \ingroup dev
  630.  * Speed codes. Indicates the speed at which the device is operating.
  631.  */
  632. enum libusb_speed {
  633.     /** The OS doesn't report or know the device speed. */
  634.     LIBUSB_SPEED_UNKNOWN = 0,
  635.  
  636.     /** The device is operating at low speed (1.5MBit/s). */
  637.     LIBUSB_SPEED_LOW = 1,
  638.  
  639.     /** The device is operating at full speed (12MBit/s). */
  640.     LIBUSB_SPEED_FULL = 2,
  641.  
  642.     /** The device is operating at high speed (480MBit/s). */
  643.     LIBUSB_SPEED_HIGH = 3,
  644.  
  645.     /** The device is operating at super speed (5000MBit/s). */
  646.     LIBUSB_SPEED_SUPER = 4,
  647. };
  648.  
  649. /** \ingroup misc
  650.  * Error codes. Most libusbx functions return 0 on success or one of these
  651.  * codes on failure.
  652.  * You can call \ref libusb_error_name() to retrieve a string representation
  653.  * of an error code.
  654.  */
  655. enum libusb_error {
  656.     /** Success (no error) */
  657.     LIBUSB_SUCCESS = 0,
  658.  
  659.     /** Input/output error */
  660.     LIBUSB_ERROR_IO = -1,
  661.  
  662.     /** Invalid parameter */
  663.     LIBUSB_ERROR_INVALID_PARAM = -2,
  664.  
  665.     /** Access denied (insufficient permissions) */
  666.     LIBUSB_ERROR_ACCESS = -3,
  667.  
  668.     /** No such device (it may have been disconnected) */
  669.     LIBUSB_ERROR_NO_DEVICE = -4,
  670.  
  671.     /** Entity not found */
  672.     LIBUSB_ERROR_NOT_FOUND = -5,
  673.  
  674.     /** Resource busy */
  675.     LIBUSB_ERROR_BUSY = -6,
  676.  
  677.     /** Operation timed out */
  678.     LIBUSB_ERROR_TIMEOUT = -7,
  679.  
  680.     /** Overflow */
  681.     LIBUSB_ERROR_OVERFLOW = -8,
  682.  
  683.     /** Pipe error */
  684.     LIBUSB_ERROR_PIPE = -9,
  685.  
  686.     /** System call interrupted (perhaps due to signal) */
  687.     LIBUSB_ERROR_INTERRUPTED = -10,
  688.  
  689.     /** Insufficient memory */
  690.     LIBUSB_ERROR_NO_MEM = -11,
  691.  
  692.     /** Operation not supported or unimplemented on this platform */
  693.     LIBUSB_ERROR_NOT_SUPPORTED = -12,
  694.  
  695.     /* NB! Remember to update libusb_error_name()
  696.        when adding new error codes here. */
  697.  
  698.     /** Other error */
  699.     LIBUSB_ERROR_OTHER = -99,
  700. };
  701.  
  702. /** \ingroup asyncio
  703.  * Transfer status codes */
  704. enum libusb_transfer_status {
  705.     /** Transfer completed without error. Note that this does not indicate
  706.      * that the entire amount of requested data was transferred. */
  707.     LIBUSB_TRANSFER_COMPLETED,
  708.  
  709.     /** Transfer failed */
  710.     LIBUSB_TRANSFER_ERROR,
  711.  
  712.     /** Transfer timed out */
  713.     LIBUSB_TRANSFER_TIMED_OUT,
  714.  
  715.     /** Transfer was cancelled */
  716.     LIBUSB_TRANSFER_CANCELLED,
  717.  
  718.     /** For bulk/interrupt endpoints: halt condition detected (endpoint
  719.      * stalled). For control endpoints: control request not supported. */
  720.     LIBUSB_TRANSFER_STALL,
  721.  
  722.     /** Device was disconnected */
  723.     LIBUSB_TRANSFER_NO_DEVICE,
  724.  
  725.     /** Device sent more data than requested */
  726.     LIBUSB_TRANSFER_OVERFLOW,
  727. };
  728.  
  729. /** \ingroup asyncio
  730.  * libusb_transfer.flags values */
  731. enum libusb_transfer_flags {
  732.     /** Report short frames as errors */
  733.     LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
  734.  
  735.     /** Automatically free() transfer buffer during libusb_free_transfer() */
  736.     LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
  737.  
  738.     /** Automatically call libusb_free_transfer() after callback returns.
  739.      * If this flag is set, it is illegal to call libusb_free_transfer()
  740.      * from your transfer callback, as this will result in a double-free
  741.      * when this flag is acted upon. */
  742.     LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
  743.  
  744.     /** Terminate transfers that are a multiple of the endpoint's
  745.      * wMaxPacketSize with an extra zero length packet. This is useful
  746.      * when a device protocol mandates that each logical request is
  747.      * terminated by an incomplete packet (i.e. the logical requests are
  748.      * not separated by other means).
  749.      *
  750.      * This flag only affects host-to-device transfers to bulk and interrupt
  751.      * endpoints. In other situations, it is ignored.
  752.      *
  753.      * This flag only affects transfers with a length that is a multiple of
  754.      * the endpoint's wMaxPacketSize. On transfers of other lengths, this
  755.      * flag has no effect. Therefore, if you are working with a device that
  756.      * needs a ZLP whenever the end of the logical request falls on a packet
  757.      * boundary, then it is sensible to set this flag on <em>every</em>
  758.      * transfer (you do not have to worry about only setting it on transfers
  759.      * that end on the boundary).
  760.      *
  761.      * This flag is currently only supported on Linux.
  762.      * On other systems, libusb_submit_transfer() will return
  763.      * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
  764.      *
  765.      * Available since libusb-1.0.9.
  766.      */
  767.     LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
  768. };
  769.  
  770. /** \ingroup asyncio
  771.  * Isochronous packet descriptor. */
  772. struct libusb_iso_packet_descriptor {
  773.     /** Length of data to request in this packet */
  774.     uint length;
  775.  
  776.     /** Amount of data that was actually transferred */
  777.     uint actual_length;
  778.  
  779.     /** Status code for this packet */
  780.     enum libusb_transfer_status status;
  781. };
  782.  
  783. struct libusb_transfer;
  784.  
  785. /** \ingroup asyncio
  786.  * Asynchronous transfer callback function type. When submitting asynchronous
  787.  * transfers, you pass a pointer to a callback function of this type via the
  788.  * \ref libusb_transfer::callback "callback" member of the libusb_transfer
  789.  * structure. libusbx will call this function later, when the transfer has
  790.  * completed or failed. See \ref asyncio for more information.
  791.  * \param transfer The libusb_transfer struct the callback function is being
  792.  * notified about.
  793.  */
  794. void function (libusb_transfer *transfer) libusb_transfer_cb_fn;
  795.  
  796. /** \ingroup asyncio
  797.  * The generic USB transfer structure. The user populates this structure and
  798.  * then submits it in order to request a transfer. After the transfer has
  799.  * completed, the library populates the transfer with the results and passes
  800.  * it back to the user.
  801.  */
  802. struct libusb_transfer {
  803.     /** Handle of the device that this transfer will be submitted to */
  804.     libusb_device_handle *dev_handle;
  805.  
  806.     /** A bitwise OR combination of \ref libusb_transfer_flags. */
  807.     uint8_t flags;
  808.  
  809.     /** Address of the endpoint where this transfer will be sent. */
  810.     ubyte endpoint;
  811.  
  812.     /** Type of the endpoint from \ref libusb_transfer_type */
  813.     ubyte type;
  814.  
  815.     /** Timeout for this transfer in millseconds. A value of 0 indicates no
  816.      * timeout. */
  817.     uint timeout;
  818.  
  819.     /** The status of the transfer. Read-only, and only for use within
  820.      * transfer callback function.
  821.      *
  822.      * If this is an isochronous transfer, this field may read COMPLETED even
  823.      * if there were errors in the frames. Use the
  824.      * \ref libusb_iso_packet_descriptor::status "status" field in each packet
  825.      * to determine if errors occurred. */
  826.     enum libusb_transfer_status status;
  827.  
  828.     /** Length of the data buffer */
  829.     int length;
  830.  
  831.     /** Actual length of data that was transferred. Read-only, and only for
  832.      * use within transfer callback function. Not valid for isochronous
  833.      * endpoint transfers. */
  834.     int actual_length;
  835.  
  836.     /** Callback function. This will be invoked when the transfer completes,
  837.      * fails, or is cancelled. */
  838.     libusb_transfer_cb_fn callback;
  839.  
  840.     /** User context data to pass to the callback function. */
  841.     void *user_data;
  842.  
  843.     /** Data buffer */
  844.     ubyte *buffer;
  845.  
  846.     /** Number of isochronous packets. Only used for I/O with isochronous
  847.      * endpoints. */
  848.     int num_iso_packets;
  849.  
  850.     /** Isochronous packet descriptors, for isochronous transfers only. */
  851.     //libusb_iso_packet_descriptor[0] iso_packet_desc;
  852. };
  853.  
  854. /** \ingroup misc
  855.  * Capabilities supported by this instance of libusb. Test if the loaded
  856.  * library supports a given capability by calling
  857.  * \ref libusb_has_capability().
  858.  */
  859. enum libusb_capability {
  860.     /** The libusb_has_capability() API is available. */
  861.     LIBUSB_CAP_HAS_CAPABILITY = 0,
  862. };
  863.  
  864. /** \ingroup lib
  865.  *  Log message levels.
  866.  *  - LIBUSB_LOG_LEVEL_NONE (0)    : no messages ever printed by the library (default)
  867.  *  - LIBUSB_LOG_LEVEL_ERROR (1)   : error messages are printed to stderr
  868.  *  - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
  869.  *  - LIBUSB_LOG_LEVEL_INFO (3)    : informational messages are printed to stdout, warning
  870.  *    and error messages are printed to stderr
  871.  *  - LIBUSB_LOG_LEVEL_DEBUG (4)   : debug and informational messages are printed to stdout,
  872.  *    warnings and errors to stderr
  873.  */
  874. enum libusb_log_level {
  875.     LIBUSB_LOG_LEVEL_NONE = 0,
  876.     LIBUSB_LOG_LEVEL_ERROR,
  877.     LIBUSB_LOG_LEVEL_WARNING,
  878.     LIBUSB_LOG_LEVEL_INFO,
  879.     LIBUSB_LOG_LEVEL_DEBUG,
  880. };
  881.  
  882. int LIBUSB_CALL libusb_init(libusb_context **ctx);
  883. void LIBUSB_CALL libusb_exit(libusb_context *ctx);
  884. void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
  885. const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
  886. int LIBUSB_CALL libusb_has_capability(uint32_t capability);
  887. const char * LIBUSB_CALL libusb_error_name(int errcode);
  888.  
  889. ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
  890.     libusb_device ***list);
  891. void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
  892.     int unref_devices);
  893. libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
  894. void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
  895.  
  896. int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
  897.     int *config);
  898. int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
  899.     struct libusb_device_descriptor *desc);
  900. int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
  901.     struct libusb_config_descriptor **config);
  902. int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
  903.     uint8_t config_index, struct libusb_config_descriptor **config);
  904. int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
  905.     uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
  906. void LIBUSB_CALL libusb_free_config_descriptor(
  907.     struct libusb_config_descriptor *config);
  908. uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
  909. uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
  910. libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
  911. int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
  912. uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
  913. int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
  914. int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
  915.     unsigned char endpoint);
  916. int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
  917.     unsigned char endpoint);
  918.  
  919. int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
  920. void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
  921. libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
  922.  
  923. int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
  924.     int configuration);
  925. int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
  926.     int interface_number);
  927. int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
  928.     int interface_number);
  929.  
  930. libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
  931.     libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
  932.  
  933. int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
  934.     int interface_number, int alternate_setting);
  935. int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
  936.     unsigned char endpoint);
  937. int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
  938.  
  939. int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
  940.     int interface_number);
  941. int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
  942.     int interface_number);
  943. int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
  944.     int interface_number);
  945.  
  946. /* async I/O */
  947.  
  948. /** \ingroup asyncio
  949.  * Get the data section of a control transfer. This convenience function is here
  950.  * to remind you that the data does not start until 8 bytes into the actual
  951.  * buffer, as the setup packet comes first.
  952.  *
  953.  * Calling this function only makes sense from a transfer callback function,
  954.  * or situations where you have already allocated a suitably sized buffer at
  955.  * transfer->buffer.
  956.  *
  957.  * \param transfer a transfer
  958.  * \returns pointer to the first byte of the data section
  959.  */
  960. static inline unsigned char *libusb_control_transfer_get_data(
  961.     struct libusb_transfer *transfer)
  962. {
  963.     return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
  964. }
  965.  
  966. /** \ingroup asyncio
  967.  * Get the control setup packet of a control transfer. This convenience
  968.  * function is here to remind you that the control setup occupies the first
  969.  * 8 bytes of the transfer data buffer.
  970.  *
  971.  * Calling this function only makes sense from a transfer callback function,
  972.  * or situations where you have already allocated a suitably sized buffer at
  973.  * transfer->buffer.
  974.  *
  975.  * \param transfer a transfer
  976.  * \returns a casted pointer to the start of the transfer data buffer
  977.  */
  978. static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
  979.     struct libusb_transfer *transfer)
  980. {
  981.     return (struct libusb_control_setup *) transfer->buffer;
  982. }
  983.  
  984. /** \ingroup asyncio
  985.  * Helper function to populate the setup packet (first 8 bytes of the data
  986.  * buffer) for a control transfer. The wIndex, wValue and wLength values should
  987.  * be given in host-endian byte order.
  988.  *
  989.  * \param buffer buffer to output the setup packet into
  990.  * \param bmRequestType see the
  991.  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
  992.  * \ref libusb_control_setup
  993.  * \param bRequest see the
  994.  * \ref libusb_control_setup::bRequest "bRequest" field of
  995.  * \ref libusb_control_setup
  996.  * \param wValue see the
  997.  * \ref libusb_control_setup::wValue "wValue" field of
  998.  * \ref libusb_control_setup
  999.  * \param wIndex see the
  1000.  * \ref libusb_control_setup::wIndex "wIndex" field of
  1001.  * \ref libusb_control_setup
  1002.  * \param wLength see the
  1003.  * \ref libusb_control_setup::wLength "wLength" field of
  1004.  * \ref libusb_control_setup
  1005.  */
  1006. static inline void libusb_fill_control_setup(unsigned char *buffer,
  1007.     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  1008.     uint16_t wLength)
  1009. {
  1010.     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
  1011.     setup->bmRequestType = bmRequestType;
  1012.     setup->bRequest = bRequest;
  1013.     setup->wValue = libusb_cpu_to_le16(wValue);
  1014.     setup->wIndex = libusb_cpu_to_le16(wIndex);
  1015.     setup->wLength = libusb_cpu_to_le16(wLength);
  1016. }
  1017.  
  1018. struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
  1019. int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
  1020. int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
  1021. void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
  1022.  
  1023. /** \ingroup asyncio
  1024.  * Helper function to populate the required \ref libusb_transfer fields
  1025.  * for a control transfer.
  1026.  *
  1027.  * If you pass a transfer buffer to this function, the first 8 bytes will
  1028.  * be interpreted as a control setup packet, and the wLength field will be
  1029.  * used to automatically populate the \ref libusb_transfer::length "length"
  1030.  * field of the transfer. Therefore the recommended approach is:
  1031.  * -# Allocate a suitably sized data buffer (including space for control setup)
  1032.  * -# Call libusb_fill_control_setup()
  1033.  * -# If this is a host-to-device transfer with a data stage, put the data
  1034.  *    in place after the setup packet
  1035.  * -# Call this function
  1036.  * -# Call libusb_submit_transfer()
  1037.  *
  1038.  * It is also legal to pass a NULL buffer to this function, in which case this
  1039.  * function will not attempt to populate the length field. Remember that you
  1040.  * must then populate the buffer and length fields later.
  1041.  *
  1042.  * \param transfer the transfer to populate
  1043.  * \param dev_handle handle of the device that will handle the transfer
  1044.  * \param buffer data buffer. If provided, this function will interpret the
  1045.  * first 8 bytes as a setup packet and infer the transfer length from that.
  1046.  * \param callback callback function to be invoked on transfer completion
  1047.  * \param user_data user data to pass to callback function
  1048.  * \param timeout timeout for the transfer in milliseconds
  1049.  */
  1050. static inline void libusb_fill_control_transfer(
  1051.     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  1052.     unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
  1053.     unsigned int timeout)
  1054. {
  1055.     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
  1056.     transfer->dev_handle = dev_handle;
  1057.     transfer->endpoint = 0;
  1058.     transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
  1059.     transfer->timeout = timeout;
  1060.     transfer->buffer = buffer;
  1061.     if (setup)
  1062.         transfer->length = LIBUSB_CONTROL_SETUP_SIZE
  1063.             + libusb_le16_to_cpu(setup->wLength);
  1064.     transfer->user_data = user_data;
  1065.     transfer->callback = callback;
  1066. }
  1067.  
  1068. /** \ingroup asyncio
  1069.  * Helper function to populate the required \ref libusb_transfer fields
  1070.  * for a bulk transfer.
  1071.  *
  1072.  * \param transfer the transfer to populate
  1073.  * \param dev_handle handle of the device that will handle the transfer
  1074.  * \param endpoint address of the endpoint where this transfer will be sent
  1075.  * \param buffer data buffer
  1076.  * \param length length of data buffer
  1077.  * \param callback callback function to be invoked on transfer completion
  1078.  * \param user_data user data to pass to callback function
  1079.  * \param timeout timeout for the transfer in milliseconds
  1080.  */
  1081. static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
  1082.     libusb_device_handle *dev_handle, unsigned char endpoint,
  1083.     unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
  1084.     void *user_data, unsigned int timeout)
  1085. {
  1086.     transfer->dev_handle = dev_handle;
  1087.     transfer->endpoint = endpoint;
  1088.     transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
  1089.     transfer->timeout = timeout;
  1090.     transfer->buffer = buffer;
  1091.     transfer->length = length;
  1092.     transfer->user_data = user_data;
  1093.     transfer->callback = callback;
  1094. }
  1095.  
  1096. /** \ingroup asyncio
  1097.  * Helper function to populate the required \ref libusb_transfer fields
  1098.  * for an interrupt transfer.
  1099.  *
  1100.  * \param transfer the transfer to populate
  1101.  * \param dev_handle handle of the device that will handle the transfer
  1102.  * \param endpoint address of the endpoint where this transfer will be sent
  1103.  * \param buffer data buffer
  1104.  * \param length length of data buffer
  1105.  * \param callback callback function to be invoked on transfer completion
  1106.  * \param user_data user data to pass to callback function
  1107.  * \param timeout timeout for the transfer in milliseconds
  1108.  */
  1109. static inline void libusb_fill_interrupt_transfer(
  1110.     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  1111.     unsigned char endpoint, unsigned char *buffer, int length,
  1112.     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
  1113. {
  1114.     transfer->dev_handle = dev_handle;
  1115.     transfer->endpoint = endpoint;
  1116.     transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
  1117.     transfer->timeout = timeout;
  1118.     transfer->buffer = buffer;
  1119.     transfer->length = length;
  1120.     transfer->user_data = user_data;
  1121.     transfer->callback = callback;
  1122. }
  1123.  
  1124. /** \ingroup asyncio
  1125.  * Helper function to populate the required \ref libusb_transfer fields
  1126.  * for an isochronous transfer.
  1127.  *
  1128.  * \param transfer the transfer to populate
  1129.  * \param dev_handle handle of the device that will handle the transfer
  1130.  * \param endpoint address of the endpoint where this transfer will be sent
  1131.  * \param buffer data buffer
  1132.  * \param length length of data buffer
  1133.  * \param num_iso_packets the number of isochronous packets
  1134.  * \param callback callback function to be invoked on transfer completion
  1135.  * \param user_data user data to pass to callback function
  1136.  * \param timeout timeout for the transfer in milliseconds
  1137.  */
  1138. static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
  1139.     libusb_device_handle *dev_handle, unsigned char endpoint,
  1140.     unsigned char *buffer, int length, int num_iso_packets,
  1141.     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
  1142. {
  1143.     transfer->dev_handle = dev_handle;
  1144.     transfer->endpoint = endpoint;
  1145.     transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
  1146.     transfer->timeout = timeout;
  1147.     transfer->buffer = buffer;
  1148.     transfer->length = length;
  1149.     transfer->num_iso_packets = num_iso_packets;
  1150.     transfer->user_data = user_data;
  1151.     transfer->callback = callback;
  1152. }
  1153.  
  1154. /** \ingroup asyncio
  1155.  * Convenience function to set the length of all packets in an isochronous
  1156.  * transfer, based on the num_iso_packets field in the transfer structure.
  1157.  *
  1158.  * \param transfer a transfer
  1159.  * \param length the length to set in each isochronous packet descriptor
  1160.  * \see libusb_get_max_packet_size()
  1161.  */
  1162. static inline void libusb_set_iso_packet_lengths(
  1163.     struct libusb_transfer *transfer, unsigned int length)
  1164. {
  1165.     int i;
  1166.     for (i = 0; i < transfer->num_iso_packets; i++)
  1167.         transfer->iso_packet_desc[i].length = length;
  1168. }
  1169.  
  1170. /** \ingroup asyncio
  1171.  * Convenience function to locate the position of an isochronous packet
  1172.  * within the buffer of an isochronous transfer.
  1173.  *
  1174.  * This is a thorough function which loops through all preceding packets,
  1175.  * accumulating their lengths to find the position of the specified packet.
  1176.  * Typically you will assign equal lengths to each packet in the transfer,
  1177.  * and hence the above method is sub-optimal. You may wish to use
  1178.  * libusb_get_iso_packet_buffer_simple() instead.
  1179.  *
  1180.  * \param transfer a transfer
  1181.  * \param packet the packet to return the address of
  1182.  * \returns the base address of the packet buffer inside the transfer buffer,
  1183.  * or NULL if the packet does not exist.
  1184.  * \see libusb_get_iso_packet_buffer_simple()
  1185.  */
  1186. static inline unsigned char *libusb_get_iso_packet_buffer(
  1187.     struct libusb_transfer *transfer, unsigned int packet)
  1188. {
  1189.     int i;
  1190.     size_t offset = 0;
  1191.     int _packet;
  1192.  
  1193.     /* oops..slight bug in the API. packet is an unsigned int, but we use
  1194.      * signed integers almost everywhere else. range-check and convert to
  1195.      * signed to avoid compiler warnings. FIXME for libusb-2. */
  1196.     if (packet > INT_MAX)
  1197.         return NULL;
  1198.     _packet = packet;
  1199.  
  1200.     if (_packet >= transfer->num_iso_packets)
  1201.         return NULL;
  1202.  
  1203.     for (i = 0; i < _packet; i++)
  1204.         offset += transfer->iso_packet_desc[i].length;
  1205.  
  1206.     return transfer->buffer + offset;
  1207. }
  1208.  
  1209. /** \ingroup asyncio
  1210.  * Convenience function to locate the position of an isochronous packet
  1211.  * within the buffer of an isochronous transfer, for transfers where each
  1212.  * packet is of identical size.
  1213.  *
  1214.  * This function relies on the assumption that every packet within the transfer
  1215.  * is of identical size to the first packet. Calculating the location of
  1216.  * the packet buffer is then just a simple calculation:
  1217.  * <tt>buffer + (packet_size * packet)</tt>
  1218.  *
  1219.  * Do not use this function on transfers other than those that have identical
  1220.  * packet lengths for each packet.
  1221.  *
  1222.  * \param transfer a transfer
  1223.  * \param packet the packet to return the address of
  1224.  * \returns the base address of the packet buffer inside the transfer buffer,
  1225.  * or NULL if the packet does not exist.
  1226.  * \see libusb_get_iso_packet_buffer()
  1227.  */
  1228. static inline unsigned char *libusb_get_iso_packet_buffer_simple(
  1229.     struct libusb_transfer *transfer, unsigned int packet)
  1230. {
  1231.     int _packet;
  1232.  
  1233.     /* oops..slight bug in the API. packet is an unsigned int, but we use
  1234.      * signed integers almost everywhere else. range-check and convert to
  1235.      * signed to avoid compiler warnings. FIXME for libusb-2. */
  1236.     if (packet > INT_MAX)
  1237.         return NULL;
  1238.     _packet = packet;
  1239.  
  1240.     if (_packet >= transfer->num_iso_packets)
  1241.         return NULL;
  1242.  
  1243.     return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
  1244. }
  1245.  
  1246. /* sync I/O */
  1247.  
  1248. int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
  1249.     uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  1250.     unsigned char *data, uint16_t wLength, unsigned int timeout);
  1251.  
  1252. int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
  1253.     unsigned char endpoint, unsigned char *data, int length,
  1254.     int *actual_length, unsigned int timeout);
  1255.  
  1256. int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
  1257.     unsigned char endpoint, unsigned char *data, int length,
  1258.     int *actual_length, unsigned int timeout);
  1259.  
  1260. /** \ingroup desc
  1261.  * Retrieve a descriptor from the default control pipe.
  1262.  * This is a convenience function which formulates the appropriate control
  1263.  * message to retrieve the descriptor.
  1264.  *
  1265.  * \param dev a device handle
  1266.  * \param desc_type the descriptor type, see \ref libusb_descriptor_type
  1267.  * \param desc_index the index of the descriptor to retrieve
  1268.  * \param data output buffer for descriptor
  1269.  * \param length size of data buffer
  1270.  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
  1271.  */
  1272. static inline int libusb_get_descriptor(libusb_device_handle *dev,
  1273.     uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
  1274. {
  1275.     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  1276.         LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
  1277.         (uint16_t) length, 1000);
  1278. }
  1279.  
  1280. /** \ingroup desc
  1281.  * Retrieve a descriptor from a device.
  1282.  * This is a convenience function which formulates the appropriate control
  1283.  * message to retrieve the descriptor. The string returned is Unicode, as
  1284.  * detailed in the USB specifications.
  1285.  *
  1286.  * \param dev a device handle
  1287.  * \param desc_index the index of the descriptor to retrieve
  1288.  * \param langid the language ID for the string descriptor
  1289.  * \param data output buffer for descriptor
  1290.  * \param length size of data buffer
  1291.  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
  1292.  * \see libusb_get_string_descriptor_ascii()
  1293.  */
  1294. static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
  1295.     uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
  1296. {
  1297.     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  1298.         LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
  1299.         langid, data, (uint16_t) length, 1000);
  1300. }
  1301.  
  1302. int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
  1303.     uint8_t desc_index, unsigned char *data, int length);
  1304.  
  1305. /* polling and timeouts */
  1306.  
  1307. int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
  1308. void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
  1309. void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
  1310. int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
  1311. int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
  1312. void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
  1313. void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
  1314. int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
  1315.  
  1316. int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
  1317.     struct timeval *tv);
  1318. int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
  1319.     struct timeval *tv, int *completed);
  1320. int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
  1321. int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
  1322. int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
  1323.     struct timeval *tv);
  1324. int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
  1325. int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
  1326.     struct timeval *tv);
  1327.  
  1328. /** \ingroup poll
  1329.  * File descriptor for polling
  1330.  */
  1331. struct libusb_pollfd {
  1332.     /** Numeric file descriptor */
  1333.     int fd;
  1334.  
  1335.     /** Event flags to poll for from <poll.h>. POLLIN indicates that you
  1336.      * should monitor this file descriptor for becoming ready to read from,
  1337.      * and POLLOUT indicates that you should monitor this file descriptor for
  1338.      * nonblocking write readiness. */
  1339.     short events;
  1340. };
  1341.  
  1342. /** \ingroup poll
  1343.  * Callback function, invoked when a new file descriptor should be added
  1344.  * to the set of file descriptors monitored for events.
  1345.  * \param fd the new file descriptor
  1346.  * \param events events to monitor for, see \ref libusb_pollfd for a
  1347.  * description
  1348.  * \param user_data User data pointer specified in
  1349.  * libusb_set_pollfd_notifiers() call
  1350.  * \see libusb_set_pollfd_notifiers()
  1351.  */
  1352. typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
  1353.     void *user_data);
  1354.  
  1355. /** \ingroup poll
  1356.  * Callback function, invoked when a file descriptor should be removed from
  1357.  * the set of file descriptors being monitored for events. After returning
  1358.  * from this callback, do not use that file descriptor again.
  1359.  * \param fd the file descriptor to stop monitoring
  1360.  * \param user_data User data pointer specified in
  1361.  * libusb_set_pollfd_notifiers() call
  1362.  * \see libusb_set_pollfd_notifiers()
  1363.  */
  1364. typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
  1365.  
  1366. const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
  1367.     libusb_context *ctx);
  1368. void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
  1369.     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
  1370.     void *user_data);
  1371. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top