core: Remove use of gettimeofday()
[platform/upstream/libusb.git] / libusb / libusbi.h
index 2d229da..9fd634f 100644 (file)
 extern "C" {
 #endif
 
-#define DEVICE_DESC_LENGTH             18
+#define DEVICE_DESC_LENGTH     18
 
 #define USB_MAXENDPOINTS       32
 #define USB_MAXINTERFACES      32
 #define USB_MAXCONFIG          8
 
 /* Backend specific capabilities */
-#define USBI_CAP_HAS_HID_ACCESS                                        0x00010000
+#define USBI_CAP_HAS_HID_ACCESS                        0x00010000
 #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
 
 /* Maximum number of bytes in a log line */
@@ -68,10 +68,10 @@ extern "C" {
 #define USBI_LOG_LINE_END      "\n"
 
 /* The following is used to silence warnings for unused variables */
-#define UNUSED(var)                    do { (void)(var); } while(0)
+#define UNUSED(var)            do { (void)(var); } while(0)
 
 #if !defined(ARRAYSIZE)
-#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
+#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
 #endif
 
 struct list_head {
@@ -96,14 +96,14 @@ struct list_head {
  *  type - the type of the first parameter
  */
 #define list_for_each_entry(pos, head, member, type)                   \
-       for (pos = list_entry((head)->next, type, member);                      \
-                &pos->member != (head);                                                                \
+       for (pos = list_entry((head)->next, type, member);              \
+                &pos->member != (head);                                \
                 pos = list_entry(pos->member.next, type, member))
 
-#define list_for_each_entry_safe(pos, n, head, member, type)   \
-       for (pos = list_entry((head)->next, type, member),                      \
-                n = list_entry(pos->member.next, type, member);                \
-                &pos->member != (head);                                                                \
+#define list_for_each_entry_safe(pos, n, head, member, type)           \
+       for (pos = list_entry((head)->next, type, member),              \
+                n = list_entry(pos->member.next, type, member);        \
+                &pos->member != (head);                                \
                 pos = n, n = list_entry(n->member.next, type, member))
 
 #define list_empty(entry) ((entry)->next == (entry))
@@ -147,9 +147,9 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
        return ret;
 }
 
-#define container_of(ptr, type, member) ({                      \
-        const typeof( ((type *)0)->member ) *mptr = (ptr);    \
-        (type *)( (char *)mptr - offsetof(type,member) );})
+#define container_of(ptr, type, member) ({                     \
+       const typeof( ((type *)0)->member ) *mptr = (ptr);      \
+       (type *)( (char *)mptr - offsetof(type,member) );})
 
 #ifndef MIN
 #define MIN(a, b)      ((a) < (b) ? (a) : (b))
@@ -168,11 +168,11 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
 
 /* Some platforms don't have this define */
 #ifndef TIMESPEC_TO_TIMEVAL
-#define TIMESPEC_TO_TIMEVAL(tv, ts)                                     \
-        do {                                                            \
-                (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec;      \
-                (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
-        } while (0)
+#define TIMESPEC_TO_TIMEVAL(tv, ts)                                    \
+       do {                                                            \
+               (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec;      \
+               (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
+       } while (0)
 #endif
 
 void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
@@ -198,49 +198,54 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
 #else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
 
 #ifdef ENABLE_LOGGING
-#define LOG_BODY(ctxt, level) \
-{                             \
-       va_list args;             \
-       va_start (args, format);  \
-       usbi_log_v(ctxt, level, "", format, args); \
-       va_end(args);             \
+#define LOG_BODY(ctxt, level)                          \
+{                                                      \
+       va_list args;                                   \
+       va_start(args, format);                         \
+       usbi_log_v(ctxt, level, "", format, args);      \
+       va_end(args);                                   \
 }
 #else
-#define LOG_BODY(ctxt, level) do { (void)(ctxt); } while(0)
+#define LOG_BODY(ctxt, level)                          \
+{                                                      \
+       (void)(ctxt);                                   \
+}
 #endif
 
-static inline void usbi_info(struct libusb_context *ctx, const char *format,
-       ...)
-       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_INFO)
-static inline void usbi_warn(struct libusb_context *ctx, const char *format,
-       ...)
-       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_WARNING)
-static inline void usbi_err( struct libusb_context *ctx, const char *format,
-       ...)
-       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_ERROR)
+static inline void usbi_info(struct libusb_context *ctx, const char *format, ...)
+       LOG_BODY(ctx, LIBUSB_LOG_LEVEL_INFO)
+static inline void usbi_warn(struct libusb_context *ctx, const char *format, ...)
+       LOG_BODY(ctx, LIBUSB_LOG_LEVEL_WARNING)
+static inline void usbi_err(struct libusb_context *ctx, const char *format, ...)
+       LOG_BODY(ctx, LIBUSB_LOG_LEVEL_ERROR)
 
 static inline void usbi_dbg(const char *format, ...)
-       LOG_BODY(NULL,LIBUSB_LOG_LEVEL_DEBUG)
+       LOG_BODY(NULL, LIBUSB_LOG_LEVEL_DEBUG)
 
 #endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
 
-#define USBI_GET_CONTEXT(ctx) if (!(ctx)) (ctx) = usbi_default_context
-#define DEVICE_CTX(dev) ((dev)->ctx)
-#define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
-#define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
+#define USBI_GET_CONTEXT(ctx)                          \
+       do {                                            \
+               if (!(ctx))                             \
+                       (ctx) = usbi_default_context;   \
+       } while(0)
+
+#define DEVICE_CTX(dev)                ((dev)->ctx)
+#define HANDLE_CTX(handle)     (DEVICE_CTX((handle)->dev))
+#define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
 #define ITRANSFER_CTX(transfer) \
        (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
 
-#define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
-#define IS_EPOUT(ep) (!IS_EPIN(ep))
-#define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
-#define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
+#define IS_EPIN(ep)            (0 != ((ep) & LIBUSB_ENDPOINT_IN))
+#define IS_EPOUT(ep)           (!IS_EPIN(ep))
+#define IS_XFERIN(xfer)                (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
+#define IS_XFEROUT(xfer)       (!IS_XFERIN(xfer))
 
 /* Internal abstraction for thread synchronization */
 #if defined(THREADS_POSIX)
 #include "os/threads_posix.h"
 #elif defined(OS_WINDOWS) || defined(OS_WINCE)
-#include <os/threads_windows.h>
+#include "os/threads_windows.h"
 #endif
 
 extern struct libusb_context *usbi_default_context;
@@ -272,6 +277,8 @@ struct libusb_context {
         * the list, URBs that will time out later are placed after, and urbs with
         * infinite timeout are always placed at the very end. */
        struct list_head flying_transfers;
+       /* Note paths taking both this and usbi_transfer->lock must always
+        * take this lock first */
        usbi_mutex_t flying_transfers_lock;
 
        /* user callbacks for pollfd changes */
@@ -382,7 +389,11 @@ struct libusb_device {
 #else
        [0] /* non-standard, but usually working code */
 #endif
+#if defined(OS_SUNOS)
+       __attribute__ ((aligned (8)));
+#else
        ;
+#endif
 };
 
 struct libusb_device_handle {
@@ -399,12 +410,16 @@ struct libusb_device_handle {
 #else
        [0] /* non-standard, but usually working code */
 #endif
+#if defined(OS_SUNOS)
+       __attribute__ ((aligned (8)));
+#else
        ;
+#endif
 };
 
 enum {
-  USBI_CLOCK_MONOTONIC,
-  USBI_CLOCK_REALTIME
+       USBI_CLOCK_MONOTONIC,
+       USBI_CLOCK_REALTIME
 };
 
 /* in-memory transfer layout:
@@ -427,7 +442,8 @@ struct usbi_transfer {
        struct timeval timeout;
        int transferred;
        uint32_t stream_id;
-       uint8_t flags;
+       uint8_t state_flags;   /* Protected by usbi_transfer->lock */
+       uint8_t timeout_flags; /* Protected by the flying_stransfers_lock */
 
        /* this lock is held during libusb_submit_transfer() and
         * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
@@ -435,45 +451,39 @@ struct usbi_transfer {
         * should also take this lock in the handle_events path, to prevent the user
         * cancelling the transfer from another thread while you are processing
         * its completion (presumably there would be races within your OS backend
-        * if this were possible). */
+        * if this were possible).
+        * Note paths taking both this and the flying_transfers_lock must
+        * always take the flying_transfers_lock first */
        usbi_mutex_t lock;
-
-       /* this lock should be held whenever viewing or modifying flags
-        * relating to the transfer state */
-       usbi_mutex_t flags_lock;
 };
 
-enum usbi_transfer_flags {
-       /* The transfer has timed out */
-       USBI_TRANSFER_TIMED_OUT = 1 << 0,
-
-       /* Set by backend submit_transfer() if the OS handles timeout */
-       USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 1,
+enum usbi_transfer_state_flags {
+       /* Transfer successfully submitted by backend */
+       USBI_TRANSFER_IN_FLIGHT = 1 << 0,
 
        /* Cancellation was requested via libusb_cancel_transfer() */
-       USBI_TRANSFER_CANCELLING = 1 << 2,
+       USBI_TRANSFER_CANCELLING = 1 << 1,
 
        /* Operation on the transfer failed because the device disappeared */
-       USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
-
-       /* Transfer is currently being submitted */
-       USBI_TRANSFER_SUBMITTING = 1 << 4,
-
-       /* Transfer successfully submitted by backend */
-       USBI_TRANSFER_IN_FLIGHT = 1 << 5,
+       USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 2,
+};
 
-       /* Completion handler has run */
-       USBI_TRANSFER_COMPLETED = 1 << 6,
+enum usbi_transfer_timeout_flags {
+       /* Set by backend submit_transfer() if the OS handles timeout */
+       USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 0,
 
        /* The transfer timeout has been handled */
-       USBI_TRANSFER_TIMEOUT_HANDLED = 1 << 7,
+       USBI_TRANSFER_TIMEOUT_HANDLED = 1 << 1,
+
+       /* The transfer timeout was successfully processed */
+       USBI_TRANSFER_TIMED_OUT = 1 << 2,
 };
 
-#define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
-       ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
+#define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)                     \
+       ((struct libusb_transfer *)(((unsigned char *)(transfer))       \
                + sizeof(struct usbi_transfer)))
-#define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
-       ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
+#define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)                     \
+       ((struct usbi_transfer *)(((unsigned char *)(transfer))         \
                - sizeof(struct usbi_transfer)))
 
 static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
@@ -488,8 +498,8 @@ static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
 
 /* All standard descriptors have these 2 fields in common */
 struct usb_descriptor_header {
-       uint8_t  bLength;
-       uint8_t  bDescriptorType;
+       uint8_t bLength;
+       uint8_t bDescriptorType;
 };
 
 /* shared data and functions */
@@ -502,7 +512,7 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
 struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
        unsigned long session_id);
 int usbi_sanitize_device(struct libusb_device *dev);
-void usbi_handle_disconnect(struct libusb_device_handle *handle);
+void usbi_handle_disconnect(struct libusb_device_handle *dev_handle);
 
 int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
        enum libusb_transfer_status status);
@@ -522,7 +532,8 @@ int usbi_signal_event(struct libusb_context *ctx);
 int usbi_clear_event(struct libusb_context *ctx);
 
 /* Internal abstraction for poll (needs struct usbi_transfer on Windows) */
-#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_HAIKU)
+#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
+       defined(OS_HAIKU) || defined(OS_SUNOS)
 #include <unistd.h>
 #include "os/poll_posix.h"
 #elif defined(OS_WINDOWS) || defined(OS_WINCE)
@@ -532,14 +543,6 @@ int usbi_clear_event(struct libusb_context *ctx);
 #if (defined(OS_WINDOWS) || defined(OS_WINCE)) && !defined(__GNUC__)
 #define snprintf _snprintf
 #define vsnprintf _vsnprintf
-int usbi_gettimeofday(struct timeval *tp, void *tzp);
-#define LIBUSB_GETTIMEOFDAY_WIN32
-#define HAVE_USBI_GETTIMEOFDAY
-#else
-#ifdef HAVE_GETTIMEOFDAY
-#define usbi_gettimeofday(tv, tz) gettimeofday((tv), (tz))
-#define HAVE_USBI_GETTIMEOFDAY
-#endif
 #endif
 
 struct usbi_pollfd {
@@ -699,7 +702,7 @@ struct usbi_os_backend {
         * Do not worry about freeing the handle on failed open, the upper layers
         * do this for you.
         */
-       int (*open)(struct libusb_device_handle *handle);
+       int (*open)(struct libusb_device_handle *dev_handle);
 
        /* Close a device such that the handle cannot be used again. Your backend
         * should destroy any resources that were allocated in the open path.
@@ -709,7 +712,7 @@ struct usbi_os_backend {
         *
         * This function is called when the user closes a device handle.
         */
-       void (*close)(struct libusb_device_handle *handle);
+       void (*close)(struct libusb_device_handle *dev_handle);
 
        /* Retrieve the device descriptor from a device.
         *
@@ -816,7 +819,7 @@ struct usbi_os_backend {
         *   blocking
         * - another LIBUSB_ERROR code on other failure.
         */
-       int (*get_configuration)(struct libusb_device_handle *handle, int *config);
+       int (*get_configuration)(struct libusb_device_handle *dev_handle, int *config);
 
        /* Set the active configuration for a device.
         *
@@ -833,7 +836,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure.
         */
-       int (*set_configuration)(struct libusb_device_handle *handle, int config);
+       int (*set_configuration)(struct libusb_device_handle *dev_handle, int config);
 
        /* Claim an interface. When claimed, the application can then perform
         * I/O to an interface's endpoints.
@@ -852,7 +855,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
+       int (*claim_interface)(struct libusb_device_handle *dev_handle, int interface_number);
 
        /* Release a previously claimed interface.
         *
@@ -869,7 +872,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
+       int (*release_interface)(struct libusb_device_handle *dev_handle, int interface_number);
 
        /* Set the alternate setting for an interface.
         *
@@ -885,7 +888,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*set_interface_altsetting)(struct libusb_device_handle *handle,
+       int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
                int interface_number, int altsetting);
 
        /* Clear a halt/stall condition on an endpoint.
@@ -899,12 +902,12 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*clear_halt)(struct libusb_device_handle *handle,
+       int (*clear_halt)(struct libusb_device_handle *dev_handle,
                unsigned char endpoint);
 
        /* Perform a USB port reset to reinitialize a device.
         *
-        * If possible, the handle should still be usable after the reset
+        * If possible, the device handle should still be usable after the reset
         * completes, assuming that the device descriptors did not change during
         * reset and all previous interface state can be restored.
         *
@@ -918,16 +921,26 @@ struct usbi_os_backend {
         *   has been disconnected since it was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*reset_device)(struct libusb_device_handle *handle);
+       int (*reset_device)(struct libusb_device_handle *dev_handle);
 
        /* Alloc num_streams usb3 bulk streams on the passed in endpoints */
-       int (*alloc_streams)(struct libusb_device_handle *handle,
+       int (*alloc_streams)(struct libusb_device_handle *dev_handle,
                uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
 
        /* Free usb3 bulk streams allocated with alloc_streams */
-       int (*free_streams)(struct libusb_device_handle *handle,
+       int (*free_streams)(struct libusb_device_handle *dev_handle,
                unsigned char *endpoints, int num_endpoints);
 
+       /* Allocate persistent DMA memory for the given device, suitable for
+        * zerocopy. May return NULL on failure. Optional to implement.
+        */
+       unsigned char *(*dev_mem_alloc)(struct libusb_device_handle *handle,
+               size_t len);
+
+       /* Free memory allocated by dev_mem_alloc. */
+       int (*dev_mem_free)(struct libusb_device_handle *handle,
+               unsigned char *buffer, size_t len);
+
        /* Determine if a kernel driver is active on an interface. Optional.
         *
         * The presence of a kernel driver on an interface indicates that any
@@ -940,7 +953,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*kernel_driver_active)(struct libusb_device_handle *handle,
+       int (*kernel_driver_active)(struct libusb_device_handle *dev_handle,
                int interface_number);
 
        /* Detach a kernel driver from an interface. Optional.
@@ -956,7 +969,7 @@ struct usbi_os_backend {
         *   was opened
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*detach_kernel_driver)(struct libusb_device_handle *handle,
+       int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle,
                int interface_number);
 
        /* Attach a kernel driver to an interface. Optional.
@@ -973,7 +986,7 @@ struct usbi_os_backend {
         *   preventing reattachment
         * - another LIBUSB_ERROR code on other failure
         */
-       int (*attach_kernel_driver)(struct libusb_device_handle *handle,
+       int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle,
                int interface_number);
 
        /* Destroy a device. Optional.
@@ -1121,6 +1134,7 @@ extern const struct usbi_os_backend windows_backend;
 extern const struct usbi_os_backend usbdk_backend;
 extern const struct usbi_os_backend wince_backend;
 extern const struct usbi_os_backend haiku_usb_raw_backend;
+extern const struct usbi_os_backend sunos_backend;
 
 extern struct list_head active_contexts_list;
 extern usbi_mutex_static_t active_contexts_lock;