libftdi Archives

Subject: Re: Re: patch to libftdi-1.0 git head, fix opening of multiple devices

From: "Rolf Fiedler" <derRolf@xxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 18 Feb 2013 11:45:12 +0100
-------- Original-Nachricht --------
> Datum: Tue, 05 Feb 2013 15:28:03 +0100
> Von: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>
> An: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
> Betreff: Re: Re: patch to libftdi-1.0 git head, fix opening of multiple 
> devices

> Hi Rolf,
> 
> On Thursday, 17. January 2013 22:49:53 derRolf@xxxxxxxxxxxxxx wrote:
> > thanks for your thoughts. Yes, we have our own VID/PID. However, our
> > software needs to operate multiple devices with the same VID/PID
> > (multi-threaded). The only difference between the devices is the serial
> > number, which is unique for each device. We have settings and
> > configuration data for the devices, which applies to the specific device
> > (i.e. serial number). Depending on the serial number (i.e. device type)
> > we boot different FPGA bitstreams.
> > 
> > Actually I don't think any application could be broken by the patch. If
> > the device is not found, the code still returns the error. It's just
> > that right now the search for the device ends if another device that
> > comes first in the list has already been opened. From my point of view
> > this is a bug (but one that only happens if you play with multiple
> > devices from the same process).
> > 
> > I understand that you don't want to apply any changes before the 1.0
> > release. That's fine, I don't use the release code but the git head. But
> > it would be good if the patch could be applied after the release is out.
> > Right now we have patched our git clone and are merging on each pull
> > from upstream.
> 
> I've taken a close look at the code in ftdi_usb_open_desc_index().
> If I apply your patch in the current form, the -8 and -9 error code
> will stop to work.
> 
> This can be a problem if there's a permission issue reading
> the string descriptors, the function will always return
> -3: "device not found" instead of -8 or -9.
> 
> How about using ftdi_usb_find_all() to get a list of all your devices.
> Then you can try to get the strings via ftdi_usb_get_strings()
> and if you f.e. get a serial the device is not in use.
> 
> Would that work?
> 
> Otherwise we need to come up with a different solution.
> 
> Thomas

Thanks again Thomas for this proposal.

I changed 

i=ftdi_usb_open_desc(device->ft_handle, 0x0403, 0xefa0, NULL, device_name);

to

    if ((ret=ftdi_usb_find_all(device->ft_handle,&devlist,0x0403,0xefa0))<0) {
        DBG("ftdi_usb_find_all failed: %d (%s)\n", ret,
            ftdi_get_error_string(device->ft_handle));
        ftdi_free(device->ft_handle);
        FREE(device);
        return NULL;
    }
    for (ret=-1, i=0, curdev = devlist; curdev != NULL; i++, curdev = curdev->n$
        //printf("Checking device: %d\n", i);
        if ((rv = ftdi_usb_get_strings(device->ft_handle, curdev->dev,
                                        manuf, sizeof(manuf),
                                        descr, sizeof(descr),
                                        serial, sizeof(serial))) < 0) {
            //DBG("ftdi_usb_get_strings failed: %d (%s)\n", ret, ftdi_get_error$
            continue; // try next in list, device may already have been opened
        }
        if(strlen(serial) >= len) {
            DBG("%s: buffer overrun while copying serial (%d>%d)\n",
                __FUNCTION__, strlen(serial), len);
            continue; // maybe more luck with next in list?
        }
        if(!strcmp(serial, device_name)) {
            i=ftdi_usb_open_dev(device->ft_handle, curdev->dev);
            if (i<0 && i!=-5) {
                DBG("ftdi_usb_open_desc failed with code %d for %s\n", i, devic$
                ftdi_free(device->ft_handle);
                FREE(device);
                return NULL;
            }
            break;
        }
    }

and can now open devices by serial number if they all have the same PID and 
VID. However, I still believe that ftdi_open_by_desc is broken if it does not 
allow me to do that when I have already other devices with the same PID/VID 
open. Also it is not ideal to require lib users to write that much code just to 
open a device.

For me the issue is solved, since my programme works with an unmodified 
libftdi1 now.

Thanks and kind regards,
Rolf

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

Current Thread