Move on to libusb-1.0
authorAndrew Andrianov <andrew@ncrmnt.org>
Mon, 5 Oct 2015 15:27:19 +0000 (18:27 +0300)
committerAndrew Andrianov <andrew@ncrmnt.org>
Mon, 5 Oct 2015 15:27:19 +0000 (18:27 +0300)
Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
Makefile
cp2103.c
main.c
pl2303.c
usb.c

index 814514f..075fe8f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
-CFLAGS  = $(shell pkg-config --cflags libusb)
-LDFLAGS = $(shell pkg-config --libs libusb)
+CFLAGS  = $(shell pkg-config --cflags libusb-1.0)
+LDFLAGS = $(shell pkg-config --libs libusb-1.0)
 
 PREFIX?=/usr/local
 
index 5e4f61e..72ef5be 100644 (file)
--- a/cp2103.c
+++ b/cp2103.c
@@ -9,9 +9,9 @@
 #include <termios.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <usb.h>
 #include <getopt.h>
 #include <stdint.h>
+#include <libusb.h>
 
 #define _GNU_SOURCE
 #include <getopt.h>
 
 
 
+
+int get_device_vid()
+{
+       return I_VENDOR_NUM;
+}
+
+int get_device_pid()
+{
+       return I_PRODUCT_NUM;
+}
+
 //GPIO_READ
 //ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x00c2, 0, gpio, 1);
 //type c0
 
 
 /* Get current GPIO register from PL2303 */
-char gpio_read_reg(usb_dev_handle *h)
+char gpio_read_reg(libusb_device_handle *h)
 {
        char buf;
-       int bytes = usb_control_msg(
+       int bytes = libusb_control_transfer(
                h,             // handle obtained with usb_open()
                0xc0, // bRequestType
                0xff,      // bRequest
@@ -70,9 +81,9 @@ char gpio_read_reg(usb_dev_handle *h)
 //index gpioreg
 //data NULL
 //len 0
-void gpio_write_reg(usb_dev_handle *h, uint16_t reg)
+void gpio_write_reg(libusb_device_handle *h, uint16_t reg)
 {
-       int bytes = usb_control_msg(
+       int bytes = libusb_control_transfer(
                h,             // handle obtained with usb_open()
                0x40, // bRequestType
                0xff,      // bRequest
@@ -86,7 +97,7 @@ void gpio_write_reg(usb_dev_handle *h, uint16_t reg)
 }
 
 
-void gpio_out(usb_dev_handle *h, int gnum, int value)
+void gpio_out(libusb_device_handle *h, int gnum, int value)
 {
 
        uint16_t gpio = 0;
@@ -115,27 +126,13 @@ void gpio_out(usb_dev_handle *h, int gnum, int value)
        gpio_write_reg(h, gpio);
 }
 
-void gpio_in(usb_dev_handle *h, int gpio, int pullup)
+void gpio_in(libusb_device_handle *h, int gpio, int pullup)
 {
        printf("FixMe: don't know how to make pins input on cp2103\n");
 }
 
-int gpio_read(usb_dev_handle *h, int gpio)
+int gpio_read(libusb_device_handle *h, int gpio)
 {
        printf("FixMe: don't know how to read pins on cp2103\n");
 }
 
-
-extern usb_dev_handle *nc_usb_open(int vendor, int product, 
-                                  const char *vendor_name, const char *product_name, const char *serial);
-void check_handle(usb_dev_handle **h, const char* manuf, const char* product, const char* serial)
-{
-       if (*h)
-               return;
-
-       *h = nc_usb_open(I_VENDOR_NUM, I_PRODUCT_NUM, manuf, product, serial);
-       if (!(*h)) {
-               fprintf(stderr, "No CP2103 USB device %04x:%04x found ;(\n", I_VENDOR_NUM, I_PRODUCT_NUM);
-               exit(1);
-       }
-}
diff --git a/main.c b/main.c
index 8efa7cb..f5cbd67 100644 (file)
--- a/main.c
+++ b/main.c
@@ -9,18 +9,19 @@
 #include <termios.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <usb.h>
+#include <libusb.h>
 #include <getopt.h>
 
 
 #define _GNU_SOURCE
 #include <getopt.h>
 
-
-void check_handle(usb_dev_handle **h, const char* manuf, const char* product, const char* serial);
-int gpio_read(usb_dev_handle *h, int gpio);
-void gpio_in(usb_dev_handle *h, int gpio, int pullup);
-void gpio_out(usb_dev_handle *h, int gpio, int value);
+int get_device_vid();
+int get_device_pid();
+void check_handle(libusb_device_handle **h, int vid, int pid, const char* manuf, const char* product, const char* serial);
+int gpio_read(libusb_device_handle *h, int gpio);
+void gpio_in(libusb_device_handle *h, int gpio, int pullup);
+void gpio_out(libusb_device_handle *h, int gpio, int value);
 
 
 void handle_error(int ret)
@@ -73,7 +74,7 @@ void usage(const char *self)
 int main(int argc, char* argv[])
 {
        char c;
-       usb_dev_handle *h = NULL;
+       libusb_device_handle *h = NULL;
        int gpio=0; 
        const char *product = NULL; 
        const char *manuf = NULL;
@@ -114,7 +115,7 @@ int main(int argc, char* argv[])
                case 'i':
                {
                        int v=0; 
-                       check_handle(&h, manuf, product, serial);
+                       check_handle(&h, get_device_vid(), get_device_pid(), manuf, product, serial);
                        if (optarg)
                                v = atoi(optarg);
                        gpio_in(h, gpio, v); 
@@ -123,7 +124,7 @@ int main(int argc, char* argv[])
                case 'o':
                {
                        int v=0; 
-                       check_handle(&h, manuf, product, serial);
+                       check_handle(&h, get_device_vid(), get_device_pid(), manuf, product, serial);
                        if (optarg)
                                v = atoi(optarg);
                        gpio_out(h, gpio, v); 
@@ -137,7 +138,7 @@ int main(int argc, char* argv[])
                }
                case 'r': 
                {
-                       check_handle(&h, manuf, product, serial);
+                       check_handle(&h, get_device_vid(), get_device_pid(), manuf, product, serial);
                        printf("%d\n", gpio_read(h, gpio) ? 1 : 0); 
                        break;
                }
index f0111cc..c6c4d69 100644 (file)
--- a/pl2303.c
+++ b/pl2303.c
@@ -9,7 +9,7 @@
 #include <termios.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <usb.h>
+#include <libusb.h>
 #include <getopt.h>
 
 
 #define VENDOR_WRITE_REQUEST           0x01
 
 
+int get_device_vid()
+{
+       return I_VENDOR_NUM;
+}
+
+int get_device_pid()
+{
+       return I_PRODUCT_NUM;
+}
 
 /* Get current GPIO register from PL2303 */
-char gpio_read_reg(usb_dev_handle *h)
+char gpio_read_reg(libusb_device_handle *h)
 {
        char buf;
-       int bytes = usb_control_msg(
+       int bytes = libusb_control_transfer(
                h,             // handle obtained with usb_open()
                VENDOR_READ_REQUEST_TYPE, // bRequestType
                VENDOR_READ_REQUEST,      // bRequest
@@ -47,9 +56,9 @@ char gpio_read_reg(usb_dev_handle *h)
        return buf;
 }
 
-void gpio_write_reg(usb_dev_handle *h, unsigned char reg)
+void gpio_write_reg(libusb_device_handle *h, unsigned char reg)
 {
-       int bytes = usb_control_msg(
+       int bytes = libusb_control_transfer(
                h,             // handle obtained with usb_open()
                VENDOR_WRITE_REQUEST_TYPE, // bRequestType
                VENDOR_WRITE_REQUEST,      // bRequest
@@ -80,7 +89,7 @@ int gpio_val_shift(int gpio) {
 }
 
 
-void gpio_out(usb_dev_handle *h, int gpio, int value)
+void gpio_out(libusb_device_handle *h, int gpio, int value)
 {
        int shift_dir = gpio_dir_shift(gpio);
        int shift_val = gpio_val_shift(gpio);
@@ -91,7 +100,7 @@ void gpio_out(usb_dev_handle *h, int gpio, int value)
        gpio_write_reg(h, reg);
 }
 
-void gpio_in(usb_dev_handle *h, int gpio, int pullup)
+void gpio_in(libusb_device_handle *h, int gpio, int pullup)
 {
        int shift_dir = gpio_dir_shift(gpio);
        int shift_val = gpio_val_shift(gpio);
@@ -103,22 +112,10 @@ void gpio_in(usb_dev_handle *h, int gpio, int pullup)
        gpio_write_reg(h, reg);
 }
 
-int gpio_read(usb_dev_handle *h, int gpio)
+int gpio_read(libusb_device_handle *h, int gpio)
 {
        unsigned char r = gpio_read_reg(h);
        int shift = gpio_val_shift(gpio);
        return (r & (1<<shift));
 }
 
-extern usb_dev_handle *nc_usb_open(int vendor, int product, char *vendor_name, char *product_name, char *serial);
-void check_handle(usb_dev_handle **h, const char* manuf, const char* product, const char* serial)
-{
-       if (*h)
-               return;
-
-       *h = nc_usb_open(I_VENDOR_NUM, I_PRODUCT_NUM, manuf, product, serial);
-       if (!(*h)) {
-               fprintf(stderr, "No PL2303 USB device %04x:%04x found ;(\n", I_VENDOR_NUM, I_PRODUCT_NUM);
-               exit(1);
-       }
-}
diff --git a/usb.c b/usb.c
index d9e6640..64b2fe6 100644 (file)
--- a/usb.c
+++ b/usb.c
@@ -3,94 +3,78 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <usb.h>
+#include <libusb.h>
 
-static int did_usb_init = 0;
 
 
-static int  usb_get_string_ascii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen)
-{
-       char    buffer[256];
-       int     rval, i;
-       
-       if((rval = usb_control_msg(dev, 
-                                  USB_ENDPOINT_IN, 
-                                  USB_REQ_GET_DESCRIPTOR, 
-                                  (USB_DT_STRING << 8) + index, 
-                                  langid, buffer, sizeof(buffer), 
-                                  1000)) < 0)
-               return rval;
-       if(buffer[1] != USB_DT_STRING)
-               return 0;
-       if((unsigned char)buffer[0] < rval)
-               rval = (unsigned char)buffer[0];
-       rval /= 2;
-       /* lossy conversion to ISO Latin1 */
-       for(i=1; i<rval; i++) {
-               if(i > buflen)  /* destination buffer overflow */
-                       break;
-               buf[i-1] = buffer[2 * i];
-               if(buffer[2 * i + 1] != 0)  /* outside of ISO Latin1 range */
-                       buf[i-1] = '?';
-       }
-       buf[i-1] = 0;
-       return i-1;
-}
-
 
-int usb_match_string(usb_dev_handle *handle, int index, char* string)
+static int ncusb_match_string(libusb_device_handle *dev, int index, const char* string)
 {
-       char tmp[256];
+       unsigned char tmp[256];
+       libusb_get_string_descriptor_ascii(dev, index, tmp, 256);
        if (string == NULL)
                return 1; /* NULL matches anything */
-       usb_get_string_ascii(handle, index, 0x409, tmp, 256);
-       return (strcmp(string,tmp)==0);
+       return (strcmp(string, (char*) tmp)==0);
 }
 
-usb_dev_handle *usb_check_device(struct usb_device *dev,
-                                char *vendor_name, 
-                                char *product_name, 
-                                char *serial)
+
+struct libusb_device_handle *ncusb_find_and_open(struct libusb_context *ctx,
+                                         int vendor, int product,
+                                         const char *vendor_name,
+                                         const char *product_name,
+                                         const char *serial)
 {
-       usb_dev_handle      *handle = usb_open(dev);
-       if(!handle) {
-               fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
+       libusb_device_handle *found = NULL;
+       libusb_device **list;
+       ssize_t cnt = libusb_get_device_list(ctx, &list);
+       ssize_t i = 0;
+
+       if (cnt < 0){
                return NULL;
        }
-       if (
-               usb_match_string(handle, dev->descriptor.iManufacturer, vendor_name) &&
-               usb_match_string(handle, dev->descriptor.iProduct,      product_name) &&
-               usb_match_string(handle, dev->descriptor.iSerialNumber, serial)
-               ) {
-               return handle;
+
+       for(i = 0; i < cnt; i++) {
+               int err = 0;
+               libusb_device *device = list[i];
+               struct libusb_device_descriptor desc;
+               libusb_device_handle *handle;
+               err = libusb_open(device, &handle);
+               if (err)
+                       continue;
+
+               int r = libusb_get_device_descriptor( device, &desc );
+               if (r) {
+                       libusb_close(handle);
+                       continue;
+               }
+
+               if ( desc.idVendor == vendor && desc.idProduct == product &&
+                    ncusb_match_string(handle, desc.iManufacturer, vendor_name) &&
+                    ncusb_match_string(handle, desc.iProduct,      product_name) &&
+                    ncusb_match_string(handle, desc.iSerialNumber, serial)
+                       )
+               {
+                       found = handle;
+               }
+
+               if (found)
+                       break;
+
        }
-       usb_close(handle);
-       return NULL;
-       
+       libusb_free_device_list(list, 1);
+
+       return found;
 }
 
-usb_dev_handle *nc_usb_open(int vendor, int product, char *vendor_name, char *product_name, char *serial)
+
+void check_handle(libusb_device_handle **h, int vid, int pid, const char* manuf, const char* product, const char* serial)
 {
-       struct usb_bus      *bus;
-       struct usb_device   *dev;
-       usb_dev_handle      *handle = NULL;
-       
-       if(!did_usb_init++)
-               usb_init();
-
-       usb_find_busses();
-       usb_find_devices();
-
-       for(bus=usb_get_busses(); bus; bus=bus->next) {
-               for(dev=bus->devices; dev; dev=dev->next) {
-                                   if(dev->descriptor.idVendor == vendor && 
-                                      dev->descriptor.idProduct == product) {
-                                           handle = usb_check_device(dev, vendor_name, product_name, serial);
-                                           if (handle)
-                                                   return handle;
-                                   }
-               }
+       if (*h)
+               return;
+       libusb_init(NULL);
+       *h = ncusb_find_and_open(NULL, vid, pid, manuf, product, serial);
+       if (!(*h)) {
+               fprintf(stderr, "No PL2303 USB device %04x:%04x found ;(\n", vid, pid);
+               exit(1);
        }
-       return NULL;
 }
-