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
diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c
index a6f194a..79a8fc3 100644
--- a/src/usb/osmo_libusb.c
+++ b/src/usb/osmo_libusb.c
@@ -735,6 +735,8 @@
 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 @@
 
 	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;
 }