Quantcast

PATCH: Add a ftdi_usb_open_bus_port function.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

PATCH: Add a ftdi_usb_open_bus_port function.

Maxwell Dreytser
Hello all,

This patch adds a function that will open using the specified Bus and
Port. I don't know how useful it would be to others, but I decided to
submit it cause why not. I pretty much just copied
"ftdi_usb_open_desc_index" and modified the parameters and loop.

Maxwell.

--- PATCH Below This line ---
 From f49494b97dfeab9ebb5daeeea97ef622f9cc7c9c Mon Sep 17 00:00:00 2001
From: Maxwell Dreytser <[hidden email]>
Date: Sun, 9 Apr 2017 20:02:56 -0400
Subject: [PATCH] Add a ftdi_usb_open_bus_port function.

---
  src/ftdi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  src/ftdi.h |  1 +
  2 files changed, 48 insertions(+)

diff --git a/src/ftdi.c b/src/ftdi.c
index 2fff1ff..d44cbb3 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -824,6 +824,53 @@ int ftdi_usb_open_desc_index(struct ftdi_context
*ftdi, int vendor, int product,
  }

  /**
+    Opens the device at a given USB Bus and Port.
+
+    \param ftdi pointer to ftdi_context
+    \param bus Bus Number
+    \param port Port Number
+
+    \retval  0: all fine
+    \retval -1: usb_find_busses() failed
+    \retval -2: usb_find_devices() failed
+    \retval -3: usb device not found
+    \retval -4: unable to open device
+    \retval -5: unable to claim device
+    \retval -6: reset failed
+    \retval -7: set baudrate failed
+    \retval -8: get product description failed
+    \retval -9: get serial number failed
+    \retval -10: unable to close device
+    \retval -11: ftdi context invalid
+*/
+int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus,
uint8_t port)
+{
+    libusb_device *dev;
+    libusb_device **devs;
+    int i = 0;
+
+    if (ftdi == NULL)
+        ftdi_error_return(-11, "ftdi context invalid");
+
+    if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
+        ftdi_error_return(-12, "libusb_get_device_list() failed");
+
+    while ((dev = devs[i++]) != NULL)
+    {
+        if (libusb_get_bus_number(dev) == bus &&
libusb_get_port_number(dev) == port)
+        {
+            int res;
+            res = ftdi_usb_open_dev(ftdi, dev);
+            libusb_free_device_list(devs,1);
+            return res;
+        }
+    }
+
+    // device not found
+    ftdi_error_return_free_device_list(-3, "device not found", devs);
+}
+
+/**
      Opens the ftdi-device described by a description-string.
      Intended to be used for parsing a device-description given as
commandline argument.

diff --git a/src/ftdi.h b/src/ftdi.h
index ef174bf..5927c48 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -502,6 +502,7 @@ extern "C"
                             const char* description, const char* serial);
      int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int
vendor, int product,
                                   const char* description, const char*
serial, unsigned int index);
+    int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus,
uint8_t port);
      int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct
libusb_device *dev);
      int ftdi_usb_open_string(struct ftdi_context *ftdi, const char*
description);

--
2.9.3


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Fahrzin Hemmati
I have a version of this code internally, so I'd switch to this function if it got merged in. I use USB port paths to uniquely determine ftdi devices, but I can convert them to bus and port before calling into the ftdi library. 

On Apr 9, 2017 5:16 PM, "Maxwell Dreytser" <[hidden email]> wrote:
Hello all,

This patch adds a function that will open using the specified Bus and Port. I don't know how useful it would be to others, but I decided to submit it cause why not. I pretty much just copied "ftdi_usb_open_desc_index" and modified the parameters and loop.

Maxwell.

--- PATCH Below This line ---
From f49494b97dfeab9ebb5daeeea97ef622f9cc7c9c Mon Sep 17 00:00:00 2001
From: Maxwell Dreytser <[hidden email]>
Date: Sun, 9 Apr 2017 20:02:56 -0400
Subject: [PATCH] Add a ftdi_usb_open_bus_port function.

---
 src/ftdi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/ftdi.h |  1 +
 2 files changed, 48 insertions(+)

diff --git a/src/ftdi.c b/src/ftdi.c
index 2fff1ff..d44cbb3 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -824,6 +824,53 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
 }

 /**
+    Opens the device at a given USB Bus and Port.
+
+    \param ftdi pointer to ftdi_context
+    \param bus Bus Number
+    \param port Port Number
+
+    \retval  0: all fine
+    \retval -1: usb_find_busses() failed
+    \retval -2: usb_find_devices() failed
+    \retval -3: usb device not found
+    \retval -4: unable to open device
+    \retval -5: unable to claim device
+    \retval -6: reset failed
+    \retval -7: set baudrate failed
+    \retval -8: get product description failed
+    \retval -9: get serial number failed
+    \retval -10: unable to close device
+    \retval -11: ftdi context invalid
+*/
+int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port)
+{
+    libusb_device *dev;
+    libusb_device **devs;
+    int i = 0;
+
+    if (ftdi == NULL)
+        ftdi_error_return(-11, "ftdi context invalid");
+
+    if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
+        ftdi_error_return(-12, "libusb_get_device_list() failed");
+
+    while ((dev = devs[i++]) != NULL)
+    {
+        if (libusb_get_bus_number(dev) == bus && libusb_get_port_number(dev) == port)
+        {
+            int res;
+            res = ftdi_usb_open_dev(ftdi, dev);
+            libusb_free_device_list(devs,1);
+            return res;
+        }
+    }
+
+    // device not found
+    ftdi_error_return_free_device_list(-3, "device not found", devs);
+}
+
+/**
     Opens the ftdi-device described by a description-string.
     Intended to be used for parsing a device-description given as commandline argument.

diff --git a/src/ftdi.h b/src/ftdi.h
index ef174bf..5927c48 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -502,6 +502,7 @@ extern "C"
                            const char* description, const char* serial);
     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
                                  const char* description, const char* serial, unsigned int index);
+    int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port);
     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev);
     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);

--
2.9.3


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]   


libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Maxwell Dreytser

Nice to know that this is actually useful! I had a similar situation in my work.


On 04/09/2017 09:15 PM, Fahrzin Hemmati wrote:
I have a version of this code internally, so I'd switch to this function if it got merged in. I use USB port paths to uniquely determine ftdi devices, but I can convert them to bus and port before calling into the ftdi library. 

On Apr 9, 2017 5:16 PM, "Maxwell Dreytser" <[hidden email]> wrote:
Hello all,

This patch adds a function that will open using the specified Bus and Port. I don't know how useful it would be to others, but I decided to submit it cause why not. I pretty much just copied "ftdi_usb_open_desc_index" and modified the parameters and loop.

Maxwell.

--- PATCH Below This line ---
From f49494b97dfeab9ebb5daeeea97ef622f9cc7c9c Mon Sep 17 00:00:00 2001
From: Maxwell Dreytser <[hidden email]>
Date: Sun, 9 Apr 2017 20:02:56 -0400
Subject: [PATCH] Add a ftdi_usb_open_bus_port function.

---
 src/ftdi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/ftdi.h |  1 +
 2 files changed, 48 insertions(+)

diff --git a/src/ftdi.c b/src/ftdi.c
index 2fff1ff..d44cbb3 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -824,6 +824,53 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
 }

 /**
+    Opens the device at a given USB Bus and Port.
+
+    \param ftdi pointer to ftdi_context
+    \param bus Bus Number
+    \param port Port Number
+
+    \retval  0: all fine
+    \retval -1: usb_find_busses() failed
+    \retval -2: usb_find_devices() failed
+    \retval -3: usb device not found
+    \retval -4: unable to open device
+    \retval -5: unable to claim device
+    \retval -6: reset failed
+    \retval -7: set baudrate failed
+    \retval -8: get product description failed
+    \retval -9: get serial number failed
+    \retval -10: unable to close device
+    \retval -11: ftdi context invalid
+*/
+int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port)
+{
+    libusb_device *dev;
+    libusb_device **devs;
+    int i = 0;
+
+    if (ftdi == NULL)
+        ftdi_error_return(-11, "ftdi context invalid");
+
+    if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
+        ftdi_error_return(-12, "libusb_get_device_list() failed");
+
+    while ((dev = devs[i++]) != NULL)
+    {
+        if (libusb_get_bus_number(dev) == bus && libusb_get_port_number(dev) == port)
+        {
+            int res;
+            res = ftdi_usb_open_dev(ftdi, dev);
+            libusb_free_device_list(devs,1);
+            return res;
+        }
+    }
+
+    // device not found
+    ftdi_error_return_free_device_list(-3, "device not found", devs);
+}
+
+/**
     Opens the ftdi-device described by a description-string.
     Intended to be used for parsing a device-description given as commandline argument.

diff --git a/src/ftdi.h b/src/ftdi.h
index ef174bf..5927c48 100644
--- a/src/ftdi.h
+++ b/src/ftdi.h
@@ -502,6 +502,7 @@ extern "C"
                            const char* description, const char* serial);
     int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
                                  const char* description, const char* serial, unsigned int index);
+    int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus, uint8_t port);
     int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct libusb_device *dev);
     int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description);

--
2.9.3


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]   


libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]





libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Thomas Jarosch
In reply to this post by Maxwell Dreytser
Hi Maxwell,

On Monday, 10 April 2017 02:16:38 CEST Maxwell Dreytser wrote:
> This patch adds a function that will open using the specified Bus and
> Port. I don't know how useful it would be to others, but I decided to
> submit it cause why not. I pretty much just copied
> "ftdi_usb_open_desc_index" and modified the parameters and loop.
>
> Maxwell.

thanks for the patch. It looks good on a first glimpse,
I'll take a closer look after the short Easter holidays.

Best regards,
Thomas


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Maxwell Dreytser
Great! I'll look forward to comments.


On 04/13/2017 12:07 PM, Thomas Jarosch wrote:

> Hi Maxwell,
>
> On Monday, 10 April 2017 02:16:38 CEST Maxwell Dreytser wrote:
>> This patch adds a function that will open using the specified Bus and
>> Port. I don't know how useful it would be to others, but I decided to
>> submit it cause why not. I pretty much just copied
>> "ftdi_usb_open_desc_index" and modified the parameters and loop.
>>
>> Maxwell.
> thanks for the patch. It looks good on a first glimpse,
> I'll take a closer look after the short Easter holidays.
>
> Best regards,
> Thomas
>
>
> --
> libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
> To unsubscribe send a mail to [hidden email]
>


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Thomas Jarosch
In reply to this post by Maxwell Dreytser
Hi Maxwell,

On Monday, 10 April 2017 04:43:24 CEST Maxwell Dreytser wrote:
> Nice to know that this is actually useful! I had a similar situation in
> my work.

patch has been applied, thanks!

The long code lines were mangled by our mail client,
I had to apply the patch manually.

Please double check everything is still correct in git.

Cheers,
Thomas


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: PATCH: Add a ftdi_usb_open_bus_port function.

Maxwell Dreytser
Hi Thomas,

All is fine. Just noticed a minor problem in the comments. Return code
-12 was not mentioned in "ftdi_usb_open_desc_index" as well as the new
"ftdi_usb_open_bus_port".

Maxwell.

--- Begin PATCH ---

 From 66ae37ad943976401a54d3ff409634d332206d67 Mon Sep 17 00:00:00 2001
From: Maxwell Dreytser <[hidden email]>
Date: Fri, 21 Apr 2017 12:48:42 -0400
Subject: [PATCH] Minor comment fix.

---
  src/ftdi.c | 2 ++
  1 file changed, 2 insertions(+)

diff --git a/src/ftdi.c b/src/ftdi.c
index c26ab72..ac8f18a 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -750,6 +750,7 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi,
int vendor, int product,
      \retval -9: get serial number failed
      \retval -10: unable to close device
      \retval -11: ftdi context invalid
+    \retval -12: libusb_get_device_list() failed
  */
  int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor,
int product,
                               const char* description, const char*
serial, unsigned int index)
@@ -842,6 +843,7 @@ int ftdi_usb_open_desc_index(struct ftdi_context
*ftdi, int vendor, int product,
      \retval -9: get serial number failed
      \retval -10: unable to close device
      \retval -11: ftdi context invalid
+    \retval -12: libusb_get_device_list() failed
  */
  int ftdi_usb_open_bus_port(struct ftdi_context *ftdi, uint8_t bus,
uint8_t port)
  {
--
2.9.3

--- End Patch ---


On 04/21/2017 12:04 PM, Thomas Jarosch wrote:

> Hi Maxwell,
>
> On Monday, 10 April 2017 04:43:24 CEST Maxwell Dreytser wrote:
>> Nice to know that this is actually useful! I had a similar situation in
>> my work.
> patch has been applied, thanks!
>
> The long code lines were mangled by our mail client,
> I had to apply the patch manually.
>
> Please double check everything is still correct in git.
>
> Cheers,
> Thomas
>


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Loading...