Revert "io: Fix race condition in handle_timeout()"
[platform/upstream/libusb.git] / libusb / sync.c
index 61a8b9c..a609f65 100644 (file)
 #include "libusbi.h"
 
 /**
- * @defgroup syncio Synchronous device I/O
+ * @defgroup libusb_syncio Synchronous device I/O
  *
  * This page documents libusb's synchronous (blocking) API for USB device I/O.
  * This interface is easy to use but has some limitations. More advanced users
- * may wish to consider using the \ref asyncio "asynchronous I/O API" instead.
+ * may wish to consider using the \ref libusb_asyncio "asynchronous I/O API" instead.
  */
 
 static void LIBUSB_CALL sync_transfer_cb(struct libusb_transfer *transfer)
@@ -60,7 +60,7 @@ static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
        }
 }
 
-/** \ingroup syncio
+/** \ingroup libusb_syncio
  * Perform a USB control transfer.
  *
  * The direction of the transfer is inferred from the bmRequestType field of
@@ -86,17 +86,24 @@ static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
  * \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
  * device
  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
+ * \returns LIBUSB_ERROR_BUSY if called from event handling context
+ * \returns LIBUSB_ERROR_INVALID_PARAM if the transfer size is larger than
+ * the operating system and/or hardware can support
  * \returns another LIBUSB_ERROR code on other failures
  */
 int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
        uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
        unsigned char *data, uint16_t wLength, unsigned int timeout)
 {
-       struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+       struct libusb_transfer *transfer;
        unsigned char *buffer;
        int completed = 0;
        int r;
 
+       if (usbi_handling_events(HANDLE_CTX(dev_handle)))
+               return LIBUSB_ERROR_BUSY;
+
+       transfer = libusb_alloc_transfer(0);
        if (!transfer)
                return LIBUSB_ERROR_NO_MEM;
 
@@ -160,10 +167,14 @@ static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
        unsigned char endpoint, unsigned char *buffer, int length,
        int *transferred, unsigned int timeout, unsigned char type)
 {
-       struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+       struct libusb_transfer *transfer;
        int completed = 0;
        int r;
 
+       if (usbi_handling_events(HANDLE_CTX(dev_handle)))
+               return LIBUSB_ERROR_BUSY;
+
+       transfer = libusb_alloc_transfer(0);
        if (!transfer)
                return LIBUSB_ERROR_NO_MEM;
 
@@ -179,7 +190,9 @@ static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
 
        sync_transfer_wait_for_completion(transfer);
 
-       *transferred = transfer->actual_length;
+       if (transferred)
+               *transferred = transfer->actual_length;
+
        switch (transfer->status) {
        case LIBUSB_TRANSFER_COMPLETED:
                r = 0;
@@ -210,7 +223,7 @@ static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
        return r;
 }
 
-/** \ingroup syncio
+/** \ingroup libusb_syncio
  * Perform a USB bulk transfer. The direction of the transfer is inferred from
  * the direction bits of the endpoint address.
  *
@@ -236,7 +249,9 @@ static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
  * \param length for bulk writes, the number of bytes from data to be sent. for
  * bulk reads, the maximum number of bytes to receive into the data buffer.
  * \param transferred output location for the number of bytes actually
- * transferred.
+ * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
+ * it is legal to pass a NULL pointer if you do not wish to receive this
+ * information.
  * \param timeout timeout (in millseconds) that this function should wait
  * before giving up due to no response being received. For an unlimited
  * timeout, use value 0.
@@ -246,8 +261,9 @@ static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
  * <tt>transferred</tt>)
  * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
- * \ref packetoverflow
+ * \ref libusb_packetoverflow
  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
+ * \returns LIBUSB_ERROR_BUSY if called from event handling context
  * \returns another LIBUSB_ERROR code on other failures
  */
 int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
@@ -258,7 +274,7 @@ int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
                transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
 }
 
-/** \ingroup syncio
+/** \ingroup libusb_syncio
  * Perform a USB interrupt transfer. The direction of the transfer is inferred
  * from the direction bits of the endpoint address.
  *
@@ -286,7 +302,9 @@ int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
  * \param length for bulk writes, the number of bytes from data to be sent. for
  * bulk reads, the maximum number of bytes to receive into the data buffer.
  * \param transferred output location for the number of bytes actually
- * transferred.
+ * transferred. Since version 1.0.21 (\ref LIBUSB_API_VERSION >= 0x01000105),
+ * it is legal to pass a NULL pointer if you do not wish to receive this
+ * information.
  * \param timeout timeout (in millseconds) that this function should wait
  * before giving up due to no response being received. For an unlimited
  * timeout, use value 0.
@@ -295,8 +313,9 @@ int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
  * \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
  * \returns LIBUSB_ERROR_PIPE if the endpoint halted
  * \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
- * \ref packetoverflow
+ * \ref libusb_packetoverflow
  * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
+ * \returns LIBUSB_ERROR_BUSY if called from event handling context
  * \returns another LIBUSB_ERROR code on other error
  */
 int API_EXPORTED libusb_interrupt_transfer(