From f20ffd938d71b70679c7dbb6b0919f11be505430 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Fri, 21 May 2021 17:11:42 +0100 Subject: [PATCH] usb.c: Scanning doesn't require context Remove functions usb_context_scan_init() and usb_context_scan_free(), as the context structure is not really required; the libusb context can be created then destroyed in usb_context_scan() directly. Signed-off-by: Paul Cercueil --- iio-private.h | 7 +------ scan.c | 12 +++++------- usb.c | 45 +++++++++++---------------------------------- 3 files changed, 17 insertions(+), 47 deletions(-) diff --git a/iio-private.h b/iio-private.h index 063c32426..5ce458688 100644 --- a/iio-private.h +++ b/iio-private.h @@ -124,7 +124,6 @@ static inline __check_ret void * ERR_TO_PTR(intptr_t err) struct iio_context_pdata; struct iio_device_pdata; struct iio_channel_pdata; -struct iio_scan_backend_context; struct iio_channel_attr { char *name; @@ -246,11 +245,7 @@ struct iio_context * serial_create_context_from_uri(const char *uri); int local_context_scan(struct iio_scan_result *scan_result); -struct iio_scan_backend_context * usb_context_scan_init(void); -void usb_context_scan_free(struct iio_scan_backend_context *ctx); - -int usb_context_scan(struct iio_scan_backend_context *ctx, - struct iio_scan_result *scan_result); +int usb_context_scan(struct iio_scan_result *scan_result); int dnssd_context_scan(struct iio_scan_result *scan_result); diff --git a/scan.c b/scan.c index b0ebf416d..925c7f103 100644 --- a/scan.c +++ b/scan.c @@ -14,7 +14,7 @@ #include struct iio_scan_context { - struct iio_scan_backend_context *usb_ctx; + bool scan_usb; bool scan_network; bool scan_local; }; @@ -45,8 +45,8 @@ ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx, } } - if (WITH_USB_BACKEND && ctx->usb_ctx) { - int ret = usb_context_scan(ctx->usb_ctx, &scan_result); + if (WITH_USB_BACKEND && ctx->scan_usb) { + int ret = usb_context_scan(&scan_result); if (ret < 0) { if (scan_result.info) iio_context_info_list_free(scan_result.info); @@ -135,8 +135,8 @@ struct iio_scan_context * iio_create_scan_context( if (!backend || strstr(backend, "local")) ctx->scan_local = true; - if (WITH_USB_BACKEND && (!backend || strstr(backend, "usb"))) - ctx->usb_ctx = usb_context_scan_init(); + if (!backend || strstr(backend, "usb")) + ctx->scan_usb = true; if (!backend || strstr(backend, "ip")) ctx->scan_network = true; @@ -146,8 +146,6 @@ struct iio_scan_context * iio_create_scan_context( void iio_scan_context_destroy(struct iio_scan_context *ctx) { - if (WITH_USB_BACKEND && ctx->usb_ctx) - usb_context_scan_free(ctx->usb_ctx); free(ctx); } diff --git a/usb.c b/usb.c index 4cd6f1485..8a7210d8b 100644 --- a/usb.c +++ b/usb.c @@ -1200,49 +1200,24 @@ static int usb_fill_context_info(struct iio_context_info *info, return 0; } -struct iio_scan_backend_context { - libusb_context *ctx; -}; - -struct iio_scan_backend_context * usb_context_scan_init(void) -{ - struct iio_scan_backend_context *ctx; - int ret; - - ctx = malloc(sizeof(*ctx)); - if (!ctx) { - errno = ENOMEM; - return NULL; - } - - ret = libusb_init(&ctx->ctx); - if (ret) { - free(ctx); - errno = (int) libusb_to_errno(ret); - return NULL; - } - - return ctx; -} - -void usb_context_scan_free(struct iio_scan_backend_context *ctx) -{ - libusb_exit(ctx->ctx); - free(ctx); -} - -int usb_context_scan(struct iio_scan_backend_context *ctx, - struct iio_scan_result *scan_result) +int usb_context_scan(struct iio_scan_result *scan_result) { struct iio_context_info **info; libusb_device **device_list; + libusb_context *ctx; unsigned int i; int ret; - ret = (int) libusb_get_device_list(ctx->ctx, &device_list); + ret = libusb_init(&ctx); if (ret < 0) return -(int) libusb_to_errno(ret); + ret = (int) libusb_get_device_list(ctx, &device_list); + if (ret < 0) { + ret = -(int) libusb_to_errno(ret); + goto cleanup_libusb_exit; + } + for (i = 0; device_list[i]; i++) { struct libusb_device_handle *hdl; struct libusb_device *dev = device_list[i]; @@ -1270,5 +1245,7 @@ int usb_context_scan(struct iio_scan_backend_context *ctx, cleanup_free_device_list: libusb_free_device_list(device_list, true); +cleanup_libusb_exit: + libusb_exit(ctx); return ret; }