From c93f3a1136d585f549d24a8905b93f02de276717 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Wed, 28 Oct 2020 15:56:58 +0100 Subject: [PATCH] libusb: establish/terminate USB session Now, the USB connection is established on backend initialization and terminated on backend exit triggered by high-level libusb code. Thanks to Peter for the patch. --- repos/libports/src/lib/libusb/genode_usb_raw.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/repos/libports/src/lib/libusb/genode_usb_raw.cc b/repos/libports/src/lib/libusb/genode_usb_raw.cc index aff0a51c76..fcaf94f6d1 100644 --- a/repos/libports/src/lib/libusb/genode_usb_raw.cc +++ b/repos/libports/src/lib/libusb/genode_usb_raw.cc @@ -211,14 +211,26 @@ struct Usb_device } }; +static Usb_device *device_instance; static int genode_init(struct libusb_context* ctx) { + if (!device_instance) { + device_instance = new (libc_alloc) Usb_device; + } else { + Genode::error("tried to init genode usb context twice"); + } return LIBUSB_SUCCESS; } -static void genode_exit(void) { } +static void genode_exit(void) +{ + if (device_instance) { + destroy(libc_alloc, device_instance); + device_instance = nullptr; + } +} int genode_get_device_list(struct libusb_context *ctx, @@ -248,8 +260,7 @@ int genode_get_device_list(struct libusb_context *ctx, dev->bus_number = busnum; dev->device_address = devaddr; - /* FIXME: find place to free the allocated memory */ - Usb_device *usb_device = new (libc_alloc) Usb_device; + Usb_device *usb_device = device_instance; *(Usb_device**)dev->os_priv = usb_device; switch (usb_device->device_descriptor.speed) {