/* config.h. Manually generated for Xcode. */
+/* On 10.12 and later, use newly available clock_*() functions */
+#include <AvailabilityMacros.h>
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
+/* Define to 1 if you have the `clock_gettime' function. */
+#define HAVE_CLOCK_GETTIME 1
+#endif
+
/* Default visibility */
#define DEFAULT_VISIBILITY /**/
dnl headers not available on all platforms but required on others
AC_CHECK_HEADERS([sys/time.h])
+dnl the clock_gettime() function needs certain clock IDs defined
+AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [have_clockgettime=no])
+if test "x$have_clock_gettime" = xyes; then
+ AC_CHECK_DECL([CLOCK_REALTIME], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_REALTIME])], [[#include <time.h>]])
+ AC_CHECK_DECL([CLOCK_MONOTONIC], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_MONOTONIC])], [[#include <time.h>]])
+elif test "x$backend" != xdarwin && test "x$backend" != xwindows; then
+ AC_MSG_ERROR([clock_gettime() is required on this platform])
+fi
+
dnl timerfd support
if test "x$backend" = xlinux || test "x$backend" = xsunos; then
AC_ARG_ENABLE([timerfd],
usbi_mutex_static_lock(&default_context_lock);
if (!timestamp_origin.tv_sec) {
- usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, ×tamp_origin);
+ usbi_clock_gettime(USBI_CLOCK_REALTIME, ×tamp_origin);
}
if (!context && usbi_default_context) {
global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
#endif
- usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &now);
+ usbi_clock_gettime(USBI_CLOCK_REALTIME, &now);
if ((global_debug) && (!has_debug_header_been_displayed)) {
has_debug_header_been_displayed = 1;
usbi_log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
return 0;
}
- r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, &itransfer->timeout);
+ r = usbi_clock_gettime(USBI_CLOCK_MONOTONIC, &itransfer->timeout);
if (r < 0) {
usbi_err(ITRANSFER_CTX(itransfer),
"failed to read monotonic clock, errno=%d", errno);
return 0;
/* get current time */
- r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, &systime);
+ r = usbi_clock_gettime(USBI_CLOCK_MONOTONIC, &systime);
if (r < 0)
return r;
return 0;
}
- r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, &systime);
+ r = usbi_clock_gettime(USBI_CLOCK_MONOTONIC, &systime);
if (r < 0) {
usbi_err(ctx, "failed to read monotonic clock, errno=%d", errno);
return 0;
int auto_detach_kernel_driver;
};
-enum {
- USBI_CLOCK_MONOTONIC,
- USBI_CLOCK_REALTIME
-};
+#ifdef HAVE_CLOCK_GETTIME
+#define USBI_CLOCK_REALTIME CLOCK_REALTIME
+#define USBI_CLOCK_MONOTONIC CLOCK_MONOTONIC
+#define usbi_clock_gettime clock_gettime
+#else
+/* If the platform doesn't provide the clock_gettime() function, the backend
+ * must provide its own implementation. Two clocks must be supported by the
+ * backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
+ *
+ * Description of clocks:
+ * USBI_CLOCK_REALTIME: clock returns time since system epoch.
+ * USBI_CLOCK_MONOTONIC: clock returns time since unspecified start time
+ * (usually boot).
+ */
+#define USBI_CLOCK_REALTIME 0
+#define USBI_CLOCK_MONOTONIC 1
+int usbi_clock_gettime(int clk_id, struct timespec *tp);
+#endif
/* in-memory transfer layout:
*
*/
int (*handle_transfer_completion)(struct usbi_transfer *itransfer);
- /* Get time from specified clock. At least two clocks must be implemented
- by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
-
- Description of clocks:
- USBI_CLOCK_REALTIME : clock returns time since system epoch.
- USBI_CLOCK_MONOTONIC: clock returns time since unspecified start
- time (usually boot).
- */
- int (*clock_gettime)(int clkid, struct timespec *tp);
-
/* Number of bytes to reserve for per-context private backend data.
* This private data area is accessible by calling
* usbi_get_context_priv() on the libusb_context instance.
#include <objc/objc-auto.h>
#endif
-/* On 10.12 and later, use newly available clock_*() functions */
-#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-#define OSX_USE_CLOCK_GETTIME 1
-#else
-#define OSX_USE_CLOCK_GETTIME 0
-#endif
-
#include "darwin_usb.h"
static pthread_mutex_t libusb_darwin_init_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t libusb_darwin_at_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t libusb_darwin_at_cond = PTHREAD_COND_INITIALIZER;
-#if !OSX_USE_CLOCK_GETTIME
+#if !defined(HAVE_CLOCK_GETTIME)
static clock_serv_t clock_realtime;
static clock_serv_t clock_monotonic;
#endif
first_init = (1 == ++init_count);
do {
-#if !OSX_USE_CLOCK_GETTIME
+#if !defined(HAVE_CLOCK_GETTIME)
if (first_init) {
/* create the clocks that will be used if clock_gettime() is not available */
host_name_port_t host_self;
} while (0);
if (LIBUSB_SUCCESS != rc) {
-#if !OSX_USE_CLOCK_GETTIME
+#if !defined(HAVE_CLOCK_GETTIME)
if (first_init) {
mach_port_deallocate(mach_task_self(), clock_realtime);
mach_port_deallocate(mach_task_self(), clock_monotonic);
pthread_mutex_unlock (&libusb_darwin_at_mutex);
pthread_join (libusb_darwin_at, NULL);
-#if !OSX_USE_CLOCK_GETTIME
+#if !defined(HAVE_CLOCK_GETTIME)
mach_port_deallocate(mach_task_self(), clock_realtime);
mach_port_deallocate(mach_task_self(), clock_monotonic);
#endif
return usbi_handle_transfer_completion (itransfer, darwin_transfer_status (itransfer, tpriv->result));
}
-static int darwin_clock_gettime(int clk_id, struct timespec *tp) {
-#if !OSX_USE_CLOCK_GETTIME
+#if !defined(HAVE_CLOCK_GETTIME)
+int usbi_clock_gettime(int clk_id, struct timespec *tp) {
mach_timespec_t sys_time;
clock_serv_t clock_ref;
tp->tv_nsec = sys_time.tv_nsec;
return LIBUSB_SUCCESS;
-#else
- switch (clk_id) {
- case USBI_CLOCK_MONOTONIC:
- return clock_gettime(CLOCK_MONOTONIC, tp);
- case USBI_CLOCK_REALTIME:
- return clock_gettime(CLOCK_REALTIME, tp);
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
-#endif
}
+#endif
#if InterfaceVersion >= 550
static int darwin_alloc_streams (struct libusb_device_handle *dev_handle, uint32_t num_streams, unsigned char *endpoints,
.handle_transfer_completion = darwin_handle_transfer_completion,
- .clock_gettime = darwin_clock_gettime,
-
.device_priv_size = sizeof(struct darwin_device_priv),
.device_handle_priv_size = sizeof(struct darwin_device_handle_priv),
.transfer_priv_size = sizeof(struct darwin_transfer_priv),
return usbi_handle_transfer_completion(itransfer, status);
}
-static int
-haiku_clock_gettime(int clkid, struct timespec *tp)
-{
- if (clkid == USBI_CLOCK_REALTIME)
- return clock_gettime(CLOCK_REALTIME, tp);
- if (clkid == USBI_CLOCK_MONOTONIC)
- return clock_gettime(CLOCK_MONOTONIC, tp);
- return LIBUSB_ERROR_INVALID_PARAM;
-}
-
const struct usbi_os_backend usbi_backend = {
.name = "Haiku usbfs",
.caps = 0,
.handle_transfer_completion = haiku_handle_transfer_completion,
- .clock_gettime = haiku_clock_gettime,
-
.device_priv_size = sizeof(USBDevice *),
.device_handle_priv_size = sizeof(USBDeviceHandle *),
.transfer_priv_size = sizeof(USBTransfer *),
return r;
}
-static int op_clock_gettime(int clk_id, struct timespec *tp)
-{
- switch (clk_id) {
- case USBI_CLOCK_MONOTONIC:
- return clock_gettime(CLOCK_MONOTONIC, tp);
- case USBI_CLOCK_REALTIME:
- return clock_gettime(CLOCK_REALTIME, tp);
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
-}
-
const struct usbi_os_backend usbi_backend = {
.name = "Linux usbfs",
.caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
.handle_events = op_handle_events,
- .clock_gettime = op_clock_gettime,
-
.device_priv_size = sizeof(struct linux_device_priv),
.device_handle_priv_size = sizeof(struct linux_device_handle_priv),
.transfer_priv_size = sizeof(struct linux_transfer_priv),
static int netbsd_submit_transfer(struct usbi_transfer *);
static int netbsd_cancel_transfer(struct usbi_transfer *);
static int netbsd_handle_transfer_completion(struct usbi_transfer *);
-static int netbsd_clock_gettime(int, struct timespec *);
/*
* Private functions
.handle_transfer_completion = netbsd_handle_transfer_completion,
- .clock_gettime = netbsd_clock_gettime,
-
.device_priv_size = sizeof(struct device_priv),
.device_handle_priv_size = sizeof(struct handle_priv),
};
}
int
-netbsd_clock_gettime(int clkid, struct timespec *tp)
-{
- if (clkid == USBI_CLOCK_REALTIME)
- return clock_gettime(CLOCK_REALTIME, tp);
-
- if (clkid == USBI_CLOCK_MONOTONIC)
- return clock_gettime(CLOCK_MONOTONIC, tp);
-
- return (LIBUSB_ERROR_INVALID_PARAM);
-}
-
-int
_errno_to_libusb(int err)
{
switch (err) {
return LIBUSB_ERROR_NOT_SUPPORTED;
}
-static int
-null_clock_gettime(int clkid, struct timespec *tp)
-{
- switch (clkid) {
- case USBI_CLOCK_MONOTONIC:
- return clock_gettime(CLOCK_REALTIME, tp);
- case USBI_CLOCK_REALTIME:
- return clock_gettime(CLOCK_REALTIME, tp);
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
-}
-
const struct usbi_os_backend usbi_backend = {
.name = "Null backend",
.caps = 0,
.reset_device = null_reset_device,
.submit_transfer = null_submit_transfer,
.cancel_transfer = null_cancel_transfer,
- .clock_gettime = null_clock_gettime,
};
static int obsd_submit_transfer(struct usbi_transfer *);
static int obsd_cancel_transfer(struct usbi_transfer *);
static int obsd_handle_transfer_completion(struct usbi_transfer *);
-static int obsd_clock_gettime(int, struct timespec *);
/*
* Private functions
.handle_transfer_completion = obsd_handle_transfer_completion,
- .clock_gettime = obsd_clock_gettime,
-
.device_priv_size = sizeof(struct device_priv),
.device_handle_priv_size = sizeof(struct handle_priv),
};
}
int
-obsd_clock_gettime(int clkid, struct timespec *tp)
-{
- if (clkid == USBI_CLOCK_REALTIME)
- return clock_gettime(CLOCK_REALTIME, tp);
-
- if (clkid == USBI_CLOCK_MONOTONIC)
- return clock_gettime(CLOCK_MONOTONIC, tp);
-
- return (LIBUSB_ERROR_INVALID_PARAM);
-}
-
-int
_errno_to_libusb(int err)
{
usbi_dbg("error: %s (%d)", strerror(err), err);
static int sunos_submit_transfer(struct usbi_transfer *);
static int sunos_cancel_transfer(struct usbi_transfer *);
static int sunos_handle_transfer_completion(struct usbi_transfer *);
-static int sunos_clock_gettime(int, struct timespec *);
static int sunos_kernel_driver_active(struct libusb_device_handle *, int interface);
static int sunos_detach_kernel_driver (struct libusb_device_handle *dev, int interface_number);
static int sunos_attach_kernel_driver (struct libusb_device_handle *dev, int interface_number);
}
int
-sunos_clock_gettime(int clkid, struct timespec *tp)
-{
- if (clkid == USBI_CLOCK_REALTIME)
- return clock_gettime(CLOCK_REALTIME, tp);
-
- if (clkid == USBI_CLOCK_MONOTONIC)
- return clock_gettime(CLOCK_MONOTONIC, tp);
-
- return (LIBUSB_ERROR_INVALID_PARAM);
-}
-
-int
_errno_to_libusb(int err)
{
usbi_dbg("error: %s (%d)", strerror(err), err);
.submit_transfer = sunos_submit_transfer,
.cancel_transfer = sunos_cancel_transfer,
.handle_transfer_completion = sunos_handle_transfer_completion,
- .clock_gettime = sunos_clock_gettime,
.device_priv_size = sizeof(sunos_dev_priv_t),
.device_handle_priv_size = sizeof(sunos_dev_handle_priv_t),
.transfer_priv_size = sizeof(sunos_xfer_priv_t),
struct timespec timeout;
int r;
- r = usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &timeout);
+ r = usbi_clock_gettime(USBI_CLOCK_REALTIME, &timeout);
if (r < 0)
return r;
static unsigned int init_count;
static bool usbdk_available;
+#if !defined(HAVE_CLOCK_GETTIME)
// Global variables for clock_gettime mechanism
static uint64_t hires_ticks_to_ps;
static uint64_t hires_frequency;
+#endif
/*
* Converts a windows error to human readable string
static void windows_init_clock(void)
{
+#if !defined(HAVE_CLOCK_GETTIME)
LARGE_INTEGER li_frequency;
// Microsoft says that the QueryPerformanceFrequency() and
hires_frequency = li_frequency.QuadPart;
hires_ticks_to_ps = UINT64_C(1000000000000) / hires_frequency;
usbi_dbg("hires timer frequency: %"PRIu64" Hz", hires_frequency);
+#endif
}
/* Windows version detection */
return r;
}
-static int windows_clock_gettime(int clk_id, struct timespec *tp)
+#if !defined(HAVE_CLOCK_GETTIME)
+int usbi_clock_gettime(int clk_id, struct timespec *tp)
{
LARGE_INTEGER hires_counter;
#if !defined(_MSC_VER) || (_MSC_VER < 1900)
return LIBUSB_ERROR_INVALID_PARAM;
}
}
+#endif
// NB: MSVC6 does not support named initializers.
const struct usbi_os_backend usbi_backend = {
NULL, /* clear_transfer_priv */
windows_handle_events,
NULL, /* handle_transfer_completion */
- windows_clock_gettime,
sizeof(struct windows_context_priv),
sizeof(union windows_device_priv),
sizeof(union windows_device_handle_priv),
-#define LIBUSB_NANO 11463
+#define LIBUSB_NANO 11464