From 3a2847493a8f21b6400868a34ce56b1efd2fa690 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Tue, 21 Jun 2011 09:27:21 +0200 Subject: [PATCH] Move libusb_init() call to prevent crash on plain ftdi_init() / ftdi_deinit(). Also check ftdi->usb_ctx in ftdi_deinit(). Code based on patch by Uwe Bonnes. --- src/ftdi.c | 21 ++++++++++++--------- 1 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/ftdi.c b/src/ftdi.c index e3201d1..5cb3ca5 100644 --- a/src/ftdi.c +++ b/src/ftdi.c @@ -74,6 +74,7 @@ static void ftdi_usb_close_internal (struct ftdi_context *ftdi) \retval 0: all fine \retval -1: couldn't allocate read buffer \retval -2: couldn't allocate struct buffer + \retval -3: libusb_init() failed \remark This should be called before all functions */ @@ -94,19 +95,20 @@ int ftdi_init(struct ftdi_context *ftdi) ftdi->readbuffer_remaining = 0; ftdi->writebuffer_chunksize = 4096; ftdi->max_packet_size = 0; + ftdi->error_str = NULL; + ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE; + + if (libusb_init(&ftdi->usb_ctx) < 0) + ftdi_error_return(-3, "libusb_init() failed"); ftdi_set_interface(ftdi, INTERFACE_ANY); ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */ - ftdi->error_str = NULL; - if (eeprom == 0) ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom"); memset(eeprom, 0, sizeof(struct ftdi_eeprom)); ftdi->eeprom = eeprom; - ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE; - /* All fine. Now allocate the readbuffer */ return ftdi_read_data_set_chunksize(ftdi, 4096); } @@ -220,7 +222,12 @@ void ftdi_deinit(struct ftdi_context *ftdi) free(ftdi->eeprom); ftdi->eeprom = NULL; } - libusb_exit(ftdi->usb_ctx); + + if (ftdi->usb_ctx) + { + libusb_exit(ftdi->usb_ctx); + ftdi->usb_ctx = NULL; + } } /** @@ -260,7 +267,6 @@ void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb) \retval >0: number of devices found \retval -3: out of memory - \retval -4: libusb_init() failed \retval -5: libusb_get_device_list() failed \retval -6: libusb_get_device_descriptor() failed */ @@ -272,9 +278,6 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli int count = 0; int i = 0; - if (libusb_init(&ftdi->usb_ctx) < 0) - ftdi_error_return(-4, "libusb_init() failed"); - if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0) ftdi_error_return(-5, "libusb_get_device_list() failed"); -- 1.7.1