Windows: Simplify poll_windows and add provisions for WinCE
[platform/upstream/libusb.git] / libusb / libusbi.h
index 1ea7f26..39e4c79 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Internal header for libusb
- * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
- * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
+ * Internal header for libusbx
+ * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
+ * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef __LIBUSBI_H__
-#define __LIBUSBI_H__
+#ifndef LIBUSBI_H
+#define LIBUSBI_H
 
 #include <config.h>
 
 #include <stddef.h>
 #include <stdint.h>
 #include <time.h>
+#include <stdarg.h>
 #ifdef HAVE_POLL_H
 #include <poll.h>
 #endif
 
 #include <libusb.h>
+#include "version.h"
 
-/* Inside the libusb code, mark all public functions as follows:
+/* Inside the libusbx code, mark all public functions as follows:
  *   return_type API_EXPORTED function_name(params) { ... }
  * But if the function returns a pointer, mark it as follows:
  *   DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
- * In the libusb public header, mark all declarations as:
+ * In the libusbx public header, mark all declarations as:
  *   return_type LIBUSB_CALL function_name(params);
  */
 #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
@@ -47,6 +49,9 @@
 #define USB_MAXINTERFACES      32
 #define USB_MAXCONFIG          8
 
+/* The following is used to silence warnings for unused variables */
+#define UNUSED(var)                    (void)(var)
+
 struct list_head {
        struct list_head *prev, *next;
 };
@@ -57,7 +62,7 @@ struct list_head {
  *     member - the list_head element in "type"
  */
 #define list_entry(ptr, type, member) \
-       ((type *)((uintptr_t)(ptr) - (uintptr_t)(&((type *)0L)->member)))
+       ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
 
 /* Get each entry from a list
  *     pos - A structure pointer has a "member" element
@@ -106,49 +111,47 @@ static inline void list_del(struct list_head *entry)
 {
        entry->next->prev = entry->prev;
        entry->prev->next = entry->next;
+       entry->next = entry->prev = NULL;
+}
+
+static inline void *usbi_reallocf(void *ptr, size_t size)
+{
+       void *ret = realloc(ptr, size);
+       if (!ret)
+               free(ptr);
+       return ret;
 }
 
 #define container_of(ptr, type, member) ({                      \
-        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
-        (type *)( (char *)__mptr - offsetof(type,member) );})
+        const typeof( ((type *)0)->member ) *mptr = (ptr);    \
+        (type *)( (char *)mptr - offsetof(type,member) );})
 
 #define MIN(a, b)      ((a) < (b) ? (a) : (b))
 #define MAX(a, b)      ((a) > (b) ? (a) : (b))
 
 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
 
-enum usbi_log_level {
-       LOG_LEVEL_DEBUG,
-       LOG_LEVEL_INFO,
-       LOG_LEVEL_WARNING,
-       LOG_LEVEL_ERROR,
-};
-
-void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
+void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
        const char *function, const char *format, ...);
 
-#if !defined(_MSC_VER) || _MSC_VER > 1200
+void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
+       const char *function, const char *format, va_list args);
+
+#if !defined(_MSC_VER) || _MSC_VER >= 1400
 
 #ifdef ENABLE_LOGGING
 #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
+#define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
 #else
-#define _usbi_log(ctx, level, ...) do {} while(0)
-#endif
-
-#ifdef ENABLE_DEBUG_LOGGING
-#define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
-#else
+#define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
 #define usbi_dbg(...) do {} while(0)
 #endif
 
-#define usbi_info(ctx, ...) _usbi_log(ctx, LOG_LEVEL_INFO, __VA_ARGS__)
-#define usbi_warn(ctx, ...) _usbi_log(ctx, LOG_LEVEL_WARNING, __VA_ARGS__)
-#define usbi_err(ctx, ...) _usbi_log(ctx, LOG_LEVEL_ERROR, __VA_ARGS__)
+#define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
+#define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
+#define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
 
-#else /* !defined(_MSC_VER) || _MSC_VER > 1200 */
-
-void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
-       const char *function, const char *format, va_list args);
+#else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
 
 #ifdef ENABLE_LOGGING
 #define LOG_BODY(ctxt, level) \
@@ -159,49 +162,43 @@ void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
        va_end(args);             \
 }
 #else
-#define LOG_BODY(ctxt, level) { }
+#define LOG_BODY(ctxt, level) do { (void)(ctxt); } while(0)
 #endif
 
 static inline void usbi_info(struct libusb_context *ctx, const char *format,
        ...)
-       LOG_BODY(ctx,LOG_LEVEL_INFO)
+       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_INFO)
 static inline void usbi_warn(struct libusb_context *ctx, const char *format,
        ...)
-       LOG_BODY(ctx,LOG_LEVEL_WARNING)
+       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_WARNING)
 static inline void usbi_err( struct libusb_context *ctx, const char *format,
        ...)
-       LOG_BODY(ctx,LOG_LEVEL_ERROR)
+       LOG_BODY(ctx,LIBUSB_LOG_LEVEL_ERROR)
 
 static inline void usbi_dbg(const char *format, ...)
-#ifdef ENABLE_DEBUG_LOGGING
-       LOG_BODY(NULL,LOG_LEVEL_DEBUG)
-#else
-{ }
-#endif
+       LOG_BODY(NULL,LIBUSB_LOG_LEVEL_DEBUG)
 
-#endif /* !defined(_MSC_VER) || _MSC_VER > 1200 */
+#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 ITRANSFER_CTX(transfer) \
-       (TRANSFER_CTX(__USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
+       (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
 
-/* Internal abstractions for thread synchronization and poll */
+#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>
+#include "os/threads_posix.h"
 #elif defined(OS_WINDOWS)
 #include <os/threads_windows.h>
 #endif
 
-#if defined(OS_LINUX) || defined(OS_DARWIN)
-#include <unistd.h>
-#include <os/poll_posix.h>
-#elif defined(OS_WINDOWS)
-#include <os/poll_windows.h>
-#endif
-
 extern struct libusb_context *usbi_default_context;
 
 struct libusb_context {
@@ -274,8 +271,11 @@ struct libusb_device {
        struct libusb_context *ctx;
 
        uint8_t bus_number;
+       uint8_t port_number;
+       struct libusb_device* parent_dev;
        uint8_t device_address;
        uint8_t num_configurations;
+       enum libusb_speed speed;
 
        struct list_head list;
        unsigned long session_data;
@@ -339,12 +339,15 @@ enum usbi_transfer_flags {
 
        /* Operation on the transfer failed because the device disappeared */
        USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
+
+       /* Set by backend submit_transfer() if the fds in use have been updated */
+       USBI_TRANSFER_UPDATED_FDS = 1 << 4,
 };
 
-#define __USBI_TRANSFER_TO_LIBUSB_TRANSFER(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) \
+#define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
        ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
                - sizeof(struct usbi_transfer)))
 
@@ -385,7 +388,25 @@ int usbi_parse_descriptor(unsigned char *source, const char *descriptor,
 int usbi_get_config_index_by_value(struct libusb_device *dev,
        uint8_t bConfigurationValue, int *idx);
 
-/* polling */
+/* Internal abstraction for poll (needs struct usbi_transfer on Windows) */
+#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD)
+#include <unistd.h>
+#include "os/poll_posix.h"
+#elif defined(OS_WINDOWS) || defined(OS_WINCE)
+#include <os/poll_windows.h>
+#endif
+
+#if defined(OS_WINDOWS) && !defined(__GCC__)
+#undef HAVE_GETTIMEOFDAY
+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 {
        /* must come first */
@@ -426,7 +447,7 @@ struct usbi_os_backend {
         * to determine specific capabilities of the system, allocate required
         * data structures for later, etc.
         *
-        * This function is called when a libusb user initializes the library
+        * This function is called when a libusbx user initializes the library
         * prior to use.
         *
         * Return 0 on success, or a LIBUSB_ERROR code on failure.
@@ -456,7 +477,7 @@ struct usbi_os_backend {
         * but that is an unlikely case.
         *
         * After computing a session ID for a device, call
-        * usbi_get_device_by_session_id(). This function checks if libusb already
+        * usbi_get_device_by_session_id(). This function checks if libusbx already
         * knows about the device, and if so, it provides you with a libusb_device
         * structure for it.
         *
@@ -498,7 +519,7 @@ struct usbi_os_backend {
         *
         * Your backend should allocate any internal resources required for I/O
         * and other operations so that those operations can happen (hopefully)
-        * without hiccup. This is also a good place to inform libusb that it
+        * without hiccup. This is also a good place to inform libusbx that it
         * should monitor certain file descriptors related to this device -
         * see the usbi_add_pollfd() function.
         *
@@ -522,7 +543,7 @@ struct usbi_os_backend {
        /* Close a device such that the handle cannot be used again. Your backend
         * should destroy any resources that were allocated in the open path.
         * This may also be a good place to call usbi_remove_pollfd() to inform
-        * libusb of any file descriptors associated with this device that should
+        * libusbx of any file descriptors associated with this device that should
         * no longer be monitored.
         *
         * This function is called when the user closes a device handle.
@@ -607,7 +628,7 @@ struct usbi_os_backend {
         *
         * If you cannot retrieve this from cache, either do not implement this
         * function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
-        * libusb to retrieve the information through a standard control transfer.
+        * libusbx to retrieve the information through a standard control transfer.
         *
         * This function must be non-blocking.
         * Return:
@@ -804,7 +825,7 @@ struct usbi_os_backend {
         * all private data from the transfer as if you were just about to report
         * completion or cancellation.
         *
-        * This function might seem a bit out of place. It is used when libusb
+        * This function might seem a bit out of place. It is used when libusbx
         * detects a disconnected device - it calls this function for all pending
         * transfers before reporting completion (with the disconnect code) to
         * the user. Maybe we can improve upon this internal interface in future.
@@ -882,7 +903,7 @@ extern const struct usbi_os_backend * const usbi_backend;
 
 extern const struct usbi_os_backend linux_usbfs_backend;
 extern const struct usbi_os_backend darwin_backend;
+extern const struct usbi_os_backend openbsd_backend;
 extern const struct usbi_os_backend windows_backend;
 
 #endif
-