#include "libusbi.h"
#include "hotplug.h"
-#if defined(OS_LINUX)
-const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend;
-#elif defined(OS_DARWIN)
-const struct usbi_os_backend * const usbi_backend = &darwin_backend;
-#elif defined(OS_OPENBSD)
-const struct usbi_os_backend * const usbi_backend = &openbsd_backend;
-#elif defined(OS_NETBSD)
-const struct usbi_os_backend * const usbi_backend = &netbsd_backend;
-#elif defined(OS_WINDOWS)
-
-#if defined(USE_USBDK)
-const struct usbi_os_backend * const usbi_backend = &usbdk_backend;
-#else
-const struct usbi_os_backend * const usbi_backend = &windows_backend;
-#endif
-
-#elif defined(OS_WINCE)
-const struct usbi_os_backend * const usbi_backend = &wince_backend;
-#elif defined(OS_HAIKU)
-const struct usbi_os_backend * const usbi_backend = &haiku_usb_raw_backend;
-#elif defined (OS_SUNOS)
-const struct usbi_os_backend * const usbi_backend = &sunos_backend;
-#else
-#error "Unsupported OS"
-#endif
-
struct libusb_context *usbi_default_context = NULL;
static const struct libusb_version libusb_version_internal =
{ LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
unsigned long session_id)
{
- size_t priv_size = usbi_backend->device_priv_size;
+ size_t priv_size = usbi_backend.device_priv_size;
struct libusb_device *dev = calloc(1, sizeof(*dev) + priv_size);
int r;
/* backend provides hotplug support */
struct libusb_device *dev;
- if (usbi_backend->hotplug_poll)
- usbi_backend->hotplug_poll();
+ if (usbi_backend.hotplug_poll)
+ usbi_backend.hotplug_poll();
usbi_mutex_lock(&ctx->usb_devs_lock);
list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) {
usbi_mutex_unlock(&ctx->usb_devs_lock);
} else {
/* backend does not provide hotplug support */
- r = usbi_backend->get_device_list(ctx, &discdevs);
+ r = usbi_backend.get_device_list(ctx, &discdevs);
}
if (r < 0) {
libusb_unref_device(dev->parent_dev);
- if (usbi_backend->destroy_device)
- usbi_backend->destroy_device(dev);
+ if (usbi_backend.destroy_device)
+ usbi_backend.destroy_device(dev);
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
/* backend does not support hotplug */
{
struct libusb_context *ctx = DEVICE_CTX(dev);
struct libusb_device_handle *_dev_handle;
- size_t priv_size = usbi_backend->device_handle_priv_size;
+ size_t priv_size = usbi_backend.device_handle_priv_size;
int r;
usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
_dev_handle->claimed_interfaces = 0;
memset(&_dev_handle->os_priv, 0, priv_size);
- r = usbi_backend->open(_dev_handle);
+ r = usbi_backend.open(_dev_handle);
if (r < 0) {
usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
libusb_unref_device(dev);
list_del(&dev_handle->list);
usbi_mutex_unlock(&ctx->open_devs_lock);
- usbi_backend->close(dev_handle);
+ usbi_backend.close(dev_handle);
libusb_unref_device(dev_handle->dev);
usbi_mutex_destroy(&dev_handle->lock);
free(dev_handle);
int r = LIBUSB_ERROR_NOT_SUPPORTED;
usbi_dbg("");
- if (usbi_backend->get_configuration)
- r = usbi_backend->get_configuration(dev_handle, config);
+ if (usbi_backend.get_configuration)
+ r = usbi_backend.get_configuration(dev_handle, config);
if (r == LIBUSB_ERROR_NOT_SUPPORTED) {
uint8_t tmp = 0;
int configuration)
{
usbi_dbg("configuration %d", configuration);
- return usbi_backend->set_configuration(dev_handle, configuration);
+ return usbi_backend.set_configuration(dev_handle, configuration);
}
/** \ingroup libusb_dev
if (dev_handle->claimed_interfaces & (1 << interface_number))
goto out;
- r = usbi_backend->claim_interface(dev_handle, interface_number);
+ r = usbi_backend.claim_interface(dev_handle, interface_number);
if (r == 0)
dev_handle->claimed_interfaces |= 1 << interface_number;
goto out;
}
- r = usbi_backend->release_interface(dev_handle, interface_number);
+ r = usbi_backend.release_interface(dev_handle, interface_number);
if (r == 0)
dev_handle->claimed_interfaces &= ~(1 << interface_number);
}
usbi_mutex_unlock(&dev_handle->lock);
- return usbi_backend->set_interface_altsetting(dev_handle, interface_number,
+ return usbi_backend.set_interface_altsetting(dev_handle, interface_number,
alternate_setting);
}
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- return usbi_backend->clear_halt(dev_handle, endpoint);
+ return usbi_backend.clear_halt(dev_handle, endpoint);
}
/** \ingroup libusb_dev
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- return usbi_backend->reset_device(dev_handle);
+ return usbi_backend.reset_device(dev_handle);
}
/** \ingroup libusb_asyncio
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend->alloc_streams)
- return usbi_backend->alloc_streams(dev_handle, num_streams, endpoints,
+ if (usbi_backend.alloc_streams)
+ return usbi_backend.alloc_streams(dev_handle, num_streams, endpoints,
num_endpoints);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend->free_streams)
- return usbi_backend->free_streams(dev_handle, endpoints,
+ if (usbi_backend.free_streams)
+ return usbi_backend.free_streams(dev_handle, endpoints,
num_endpoints);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
if (!dev_handle->dev->attached)
return NULL;
- if (usbi_backend->dev_mem_alloc)
- return usbi_backend->dev_mem_alloc(dev_handle, length);
+ if (usbi_backend.dev_mem_alloc)
+ return usbi_backend.dev_mem_alloc(dev_handle, length);
else
return NULL;
}
int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle,
unsigned char *buffer, size_t length)
{
- if (usbi_backend->dev_mem_free)
- return usbi_backend->dev_mem_free(dev_handle, buffer, length);
+ if (usbi_backend.dev_mem_free)
+ return usbi_backend.dev_mem_free(dev_handle, buffer, length);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
}
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend->kernel_driver_active)
- return usbi_backend->kernel_driver_active(dev_handle, interface_number);
+ if (usbi_backend.kernel_driver_active)
+ return usbi_backend.kernel_driver_active(dev_handle, interface_number);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
}
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend->detach_kernel_driver)
- return usbi_backend->detach_kernel_driver(dev_handle, interface_number);
+ if (usbi_backend.detach_kernel_driver)
+ return usbi_backend.detach_kernel_driver(dev_handle, interface_number);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
}
if (!dev_handle->dev->attached)
return LIBUSB_ERROR_NO_DEVICE;
- if (usbi_backend->attach_kernel_driver)
- return usbi_backend->attach_kernel_driver(dev_handle, interface_number);
+ if (usbi_backend.attach_kernel_driver)
+ return usbi_backend.attach_kernel_driver(dev_handle, interface_number);
else
return LIBUSB_ERROR_NOT_SUPPORTED;
}
int API_EXPORTED libusb_set_auto_detach_kernel_driver(
libusb_device_handle *dev_handle, int enable)
{
- if (!(usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
+ if (!(usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
return LIBUSB_ERROR_NOT_SUPPORTED;
dev_handle->auto_detach_kernel_driver = enable;
usbi_mutex_static_lock(&default_context_lock);
if (!timestamp_origin.tv_sec) {
- usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, ×tamp_origin);
+ usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, ×tamp_origin);
}
if (!context && usbi_default_context) {
list_add (&ctx->list, &active_contexts_list);
usbi_mutex_static_unlock(&active_contexts_lock);
- if (usbi_backend->init) {
- r = usbi_backend->init(ctx);
+ if (usbi_backend.init) {
+ r = usbi_backend.init(ctx);
if (r)
goto err_free_ctx;
}
return 0;
err_backend_exit:
- if (usbi_backend->exit)
- usbi_backend->exit();
+ if (usbi_backend.exit)
+ usbi_backend.exit();
err_free_ctx:
if (ctx == usbi_default_context) {
usbi_default_context = NULL;
usbi_warn(ctx, "application left some devices open");
usbi_io_exit(ctx);
- if (usbi_backend->exit)
- usbi_backend->exit();
+ if (usbi_backend.exit)
+ usbi_backend.exit();
usbi_mutex_destroy(&ctx->open_devs_lock);
usbi_mutex_destroy(&ctx->usb_devs_lock);
case LIBUSB_CAP_HAS_CAPABILITY:
return 1;
case LIBUSB_CAP_HAS_HOTPLUG:
- return !(usbi_backend->get_device_list);
+ return !(usbi_backend.get_device_list);
case LIBUSB_CAP_HAS_HID_ACCESS:
- return (usbi_backend->caps & USBI_CAP_HAS_HID_ACCESS);
+ return (usbi_backend.caps & USBI_CAP_HAS_HID_ACCESS);
case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
- return (usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
+ return (usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
}
return 0;
}
return;
#endif
- usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, &now);
+ usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &now);
if ((global_debug) && (!has_debug_header_been_displayed)) {
has_debug_header_been_displayed = 1;
usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
{
int r, host_endian = 0;
- r = usbi_backend->get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
+ r = usbi_backend.get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
&host_endian);
if (r < 0)
return r;
int host_endian = 0;
int r;
- r = usbi_backend->get_active_config_descriptor(dev, tmp,
+ r = usbi_backend.get_active_config_descriptor(dev, tmp,
LIBUSB_DT_CONFIG_SIZE, &host_endian);
if (r < 0)
return r;
if (!buf)
return LIBUSB_ERROR_NO_MEM;
- r = usbi_backend->get_active_config_descriptor(dev, buf,
+ r = usbi_backend.get_active_config_descriptor(dev, buf,
_config.wTotalLength, &host_endian);
if (r >= 0)
r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
if (config_index >= dev->num_configurations)
return LIBUSB_ERROR_NOT_FOUND;
- r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
+ r = usbi_backend.get_config_descriptor(dev, config_index, tmp,
LIBUSB_DT_CONFIG_SIZE, &host_endian);
if (r < 0)
return r;
if (!buf)
return LIBUSB_ERROR_NO_MEM;
- r = usbi_backend->get_config_descriptor(dev, config_index, buf,
+ r = usbi_backend.get_config_descriptor(dev, config_index, buf,
_config.wTotalLength, &host_endian);
if (r >= 0)
r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
for (i = 0; i < dev->num_configurations; i++) {
unsigned char tmp[6];
int host_endian;
- int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
+ int r = usbi_backend.get_config_descriptor(dev, i, tmp, sizeof(tmp),
&host_endian);
if (r < 0) {
*idx = -1;
int r, idx, host_endian;
unsigned char *buf = NULL;
- if (usbi_backend->get_config_descriptor_by_value) {
- r = usbi_backend->get_config_descriptor_by_value(dev,
+ if (usbi_backend.get_config_descriptor_by_value) {
+ r = usbi_backend.get_config_descriptor_by_value(dev,
bConfigurationValue, &buf, &host_endian);
if (r < 0)
return r;
goto err_close_pipe;
#ifdef USBI_TIMERFD_AVAILABLE
- ctx->timerfd = timerfd_create(usbi_backend->get_timerfd_clockid(),
+ ctx->timerfd = timerfd_create(usbi_backend.get_timerfd_clockid(),
TFD_NONBLOCK | TFD_CLOEXEC);
if (ctx->timerfd >= 0) {
usbi_dbg("using timerfd for timeouts");
if (!timeout)
return 0;
- r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, ¤t_time);
+ r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, ¤t_time);
if (r < 0) {
usbi_err(ITRANSFER_CTX(transfer),
"failed to read monotonic clock, errno=%d", errno);
int iso_packets)
{
struct libusb_transfer *transfer;
- size_t os_alloc_size = usbi_backend->transfer_priv_size;
+ size_t os_alloc_size = usbi_backend.transfer_priv_size;
size_t alloc_size = sizeof(struct usbi_transfer)
+ sizeof(struct libusb_transfer)
+ (sizeof(struct libusb_iso_packet_descriptor) * iso_packets)
*/
usbi_mutex_unlock(&ctx->flying_transfers_lock);
- r = usbi_backend->submit_transfer(itransfer);
+ r = usbi_backend.submit_transfer(itransfer);
if (r == LIBUSB_SUCCESS) {
itransfer->state_flags |= USBI_TRANSFER_IN_FLIGHT;
/* keep a reference to this device */
r = LIBUSB_ERROR_NOT_FOUND;
goto out;
}
- r = usbi_backend->cancel_transfer(itransfer);
+ r = usbi_backend.cancel_transfer(itransfer);
if (r < 0) {
if (r != LIBUSB_ERROR_NOT_FOUND &&
r != LIBUSB_ERROR_NO_DEVICE)
return 0;
/* get current time */
- r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &systime_ts);
+ r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, &systime_ts);
if (r < 0)
return r;
itransfer = list_first_entry(&ctx->completed_transfers, struct usbi_transfer, completed_list);
list_del(&itransfer->completed_list);
usbi_mutex_unlock(&ctx->event_data_lock);
- ret = usbi_backend->handle_transfer_completion(itransfer);
+ ret = usbi_backend.handle_transfer_completion(itransfer);
if (ret)
usbi_err(ctx, "backend handle_transfer_completion failed with error %d", ret);
usbi_mutex_lock(&ctx->event_data_lock);
}
#endif
- r = usbi_backend->handle_events(ctx, fds + internal_nfds, nfds - internal_nfds, r);
+ r = usbi_backend.handle_events(ctx, fds + internal_nfds, nfds - internal_nfds, r);
if (r)
usbi_err(ctx, "backend handle_events failed with error %d", r);
return 0;
}
- r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &cur_ts);
+ r = usbi_backend.clock_gettime(USBI_CLOCK_MONOTONIC, &cur_ts);
if (r < 0) {
usbi_err(ctx, "failed to read monotonic clock, errno=%d", errno);
return 0;
USBI_TRANSFER_TO_LIBUSB_TRANSFER(to_cancel));
usbi_mutex_lock(&to_cancel->lock);
- usbi_backend->clear_transfer_priv(to_cancel);
+ usbi_backend.clear_transfer_priv(to_cancel);
usbi_mutex_unlock(&to_cancel->lock);
usbi_handle_transfer_completion(to_cancel, LIBUSB_TRANSFER_NO_DEVICE);
}