libftdi Archives

Subject: Re: libusb_init and libusb_exit in libftdi-1.0

From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Sun, 19 Jun 2011 21:59:11 +0200
>>>>> "Xiaofan" == Xiaofan Chen <xiaofanc@xxxxxxxxx> writes:

    Xiaofan> On Thu, Jun 16, 2011 at 4:34 AM, David Wiberg
    Xiaofan> <dwiberg@xxxxxxxxx> wrote:
    >> I'm not sure if you want the code posted on the list or some pastebin
    >> but since it's quite small I'll post it here this time. It's the
    >> simple.c example modified to only perform init and deinit of the
    >> library.
    >> 
    >> #include <stdio.h> #include <stdlib.h> #include <ftdi.h>
    >> 
    >> int main(void) {    int ret;    struct ftdi_context ftdic;    if
    >> (ftdi_init(&ftdic) < 0)    {        fprintf(stderr, "ftdi_init
    >> failed\n");        return EXIT_FAILURE;    }
    >> 
    >>    ftdi_deinit(&ftdic);
    >> 
    >>    return EXIT_SUCCESS; }
    >> 

    Xiaofan> This is indeed true.

Appended patch moves the the init.
-- 
Uwe Bonnes                bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
>From 306fb8d9ea4a361d42882873fc66e82a31b13170 Mon Sep 17 00:00:00 2001
From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Jun 2011 18:29:39 +0200
Subject: Move libusb_init into libftdi_init to avoid crash as shown by the 
sample program by David Wiberg

---
 src/ftdi.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/ftdi.c b/src/ftdi.c
index fd659e2..42a42f0 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -74,13 +74,16 @@ 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
 */
 int ftdi_init(struct ftdi_context *ftdi)
 {
     struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct 
ftdi_eeprom));
-    ftdi->usb_ctx = NULL;
+    if (libusb_init(&ftdi->usb_ctx) < 0)
+        ftdi_error_return(-3, "libusb_init() failed");
+
     ftdi->usb_dev = NULL;
     ftdi->usb_read_timeout = 5000;
     ftdi->usb_write_timeout = 5000;
@@ -260,7 +263,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 +274,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.3.4


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx

Current Thread