From a366a895976ffc17c632781f0c9e33a590ed94a7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 31 Jan 2022 18:37:36 +0100 Subject: [PATCH] osmo_libusb: Use libusb_get_pollfds() to get initial file descriptors It seems it is insufficient to register file-descriptor call-backs with libusb_set_pollfd_notifiers(), but we also need to call libusb_get_pollfds() once to get the initial set of file descriptors which may have been created already during libusb_init(). As we don't have a libusb context before libusb_init() returns, we cannot call libusb_set_pollfd_notifiers() early enough to be active already during libusb_init(). Change-Id: Icf81014d689ffa738719af68120fa2dedbeec689 --- src/usb/osmo_libusb.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c index a6f194adb..79a8fc3b6 100644 --- a/src/usb/osmo_libusb.c +++ b/src/usb/osmo_libusb.c @@ -735,6 +735,8 @@ int osmo_libusb_get_ep_addrs(libusb_device_handle *devh, unsigned int if_num, int osmo_libusb_init(libusb_context **pluctx) { libusb_context *luctx = NULL; + const struct libusb_pollfd **pfds; + int rc; rc = libusb_init(pluctx); @@ -750,6 +752,17 @@ int osmo_libusb_init(libusb_context **pluctx) libusb_set_pollfd_notifiers(luctx, osmo_usb_added_cb, osmo_usb_removed_cb, luctx); + /* get the initial file descriptors which were created even before during libusb_init() */ + pfds = libusb_get_pollfds(luctx); + if (pfds) { + const struct libusb_pollfd **pfds2 = pfds; + const struct libusb_pollfd *pfd; + /* synthesize 'add' call-backs. not sure why libusb doesn't do that by itself? */ + for (pfd = *pfds2; pfd; pfd = *++pfds2) + osmo_usb_added_cb(pfd->fd, pfd->events, luctx); + libusb_free_pollfds(pfds); + } + return 0; }