]> cvs.zerfleddert.de Git - rigol/blobdiff - usbtmc.c
update to libusb-1.0
[rigol] / usbtmc.c
index 84df109b6778a82dcd15396878bbf40c16466abc..5f15d724d1360ae7ccd28e3ad3be6e3bbef437e9 100644 (file)
--- a/usbtmc.c
+++ b/usbtmc.c
@@ -1,7 +1,9 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdint.h>
-#include <usb.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <libusb-1.0/libusb.h>
 #include <arpa/inet.h>
 
 #include "scope.h"
@@ -10,6 +12,9 @@
 #define USB_TIMEOUT            10000
 #define USBTMC_IN_TRANSFERSIZE 0x800
 
+#define ID_VENDOR      0x400
+#define ID_PRODUCT     0x5dc
+
 #if BYTE_ORDER == LITTLE_ENDIAN
 #define LE32(x) x
 #elif BYTE_ORDER == BIG_ENDIAN
 #endif
 
 /* TODO: fix memory leak here: */
-#define USB_ERROR(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror()); usbtmc_clear(sc); return 0; } } while(0)
+#define USB_ERROR(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror(x)); usbtmc_clear(sc); return 0; } } while(0)
+
+/* Not in all libusb-1.0 versions, so we have to roll our own :-( */
+static char * usb_strerror(int e)
+{
+       static char unknerr[256];
+
+       switch (e) {
+               case LIBUSB_SUCCESS:
+                       return "Success";
+               case LIBUSB_ERROR_IO:
+                       return "Input/output error";
+               case LIBUSB_ERROR_INVALID_PARAM:
+                       return "Invalid parameter";
+               case LIBUSB_ERROR_ACCESS:
+                       return "Access denied (insufficient permissions)";
+               case LIBUSB_ERROR_NO_DEVICE:
+                       return "No such device (it may have been disconnected)";
+               case LIBUSB_ERROR_NOT_FOUND:
+                       return "Entity not found";
+               case LIBUSB_ERROR_BUSY:
+                       return "Resource busy";
+               case LIBUSB_ERROR_TIMEOUT:
+                       return "Operation timed out";
+               case LIBUSB_ERROR_OVERFLOW:
+                       return "Overflow";
+               case LIBUSB_ERROR_PIPE:
+                       return "Pipe error";
+               case LIBUSB_ERROR_INTERRUPTED:
+                       return "System call interrupted (perhaps due to signal)";
+               case LIBUSB_ERROR_NO_MEM:
+                       return "Insufficient memory";
+               case LIBUSB_ERROR_NOT_SUPPORTED:
+                       return "Operation not supported or unimplemented on this platform";
+               case LIBUSB_ERROR_OTHER:
+                       return "Other error";
+       };
+       snprintf(unknerr, sizeof(unknerr), "Unknown error code %d / 0x%02x", e, e);
+       return unknerr;
+}
 
 /* This routine locates a scope by VID/PID and returns a struct scope* for it */
 static struct scope* usbtmc_find_scope() {
-       struct usb_bus *bus;
-       struct usb_device *dev=NULL;
-       struct usb_dev_handle *devh;
-
+       libusb_device_handle *devh = NULL;
+       libusb_device **list;
+       ssize_t cnt;
+       ssize_t i;
+       int err;
        struct scope *sc;
 
-       usb_find_busses();
-       usb_find_devices();
+       cnt = libusb_get_device_list(NULL, &list);
+       if (cnt < 0) {
+               fprintf(stderr, "Can't get USB device list: %d\n", (int)cnt);
+               return NULL;
+       }
 
-       for (bus=usb_busses; bus; bus=bus->next) {
-               for (dev=bus->devices; dev; dev=dev->next) {
-                       if (dev->descriptor.idVendor == 0x400 && dev->descriptor.idProduct == 0x5dc) {
-                               devh = usb_open(dev);
-                               if (devh == NULL)
-                                       return NULL;
+       for (i = 0; i < cnt; i++){
+               struct libusb_device_descriptor desc;
 
-                               sc = calloc(1, sizeof(struct scope));
-                               if (sc == NULL) {
-                                       perror("calloc");
-                                       exit(EXIT_FAILURE);
-                               }
+               err = libusb_get_device_descriptor(list[i], &desc);
+               if (err)
+                       continue;
 
-                               sc->usb.dev = devh;
+               if ((desc.idVendor == ID_VENDOR) && (desc.idProduct == ID_PRODUCT)) {
+                       libusb_device *dev = list[i];
+
+                       err = libusb_open(dev, &devh);
+                       if (err) {
+                               fprintf(stderr, "Can't open device: %d\n", err);
+                               return NULL;
+                       }
 
-                               /* TODO: FIXME */
-                               sc->usb.brokenRigol = 1;
-                               sc->usb.ep_bulk_out = 0x01;
-                               sc->usb.ep_bulk_in = 0x82;
-                               sc->usb.wMaxPacketSize_in = 0x40;
-                               
-                               return sc;
+                       sc = calloc(1, sizeof(struct scope));
+                       if (sc == NULL) {
+                               perror("calloc");
+                               exit(EXIT_FAILURE);
                        }
+
+                       sc->usb.dev = devh;
+
+                       /* TODO: FIXME */
+                       sc->usb.brokenRigol = 1;
+                       sc->usb.ep_bulk_out = 0x01;
+                       sc->usb.ep_bulk_in = 0x82;
+                       sc->usb.wMaxPacketSize_in = 0x40;
+
+                       return sc;
                }
+
        }
 
        return NULL;
@@ -68,9 +125,9 @@ static unsigned char usb488_status(struct scope *sc)
 
        sc->usb.bTag++;
 
-       r = usb_control_msg(sc->usb.dev, 0xA1,
+       r = libusb_control_transfer(sc->usb.dev, 0xA1,
                USB488_CTL_READ_STATUS_BYTE,
-               (sc->usb.bTag & 0x7f), 0, (char*)status, 3,
+               (sc->usb.bTag & 0x7f), 0, status, 3,
                USB_TIMEOUT);
 
        if ((r != 3) || (status[0] != USBTMC_STATUS_SUCCESS) || (status[1] != (sc->usb.bTag & 0x7f))) {
@@ -86,12 +143,12 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
        int r;
        static struct usbtmc_capabilities res;
 
-       r = usb_control_msg(sc->usb.dev, 0xA1, 
+       r = libusb_control_transfer(sc->usb.dev, 0xA1, 
                USBTMC_CTL_GET_CAPABILITIES,
-               0, 0, (char*)&res, sizeof(struct usbtmc_capabilities),
+               0, 0, (unsigned char*)&res, sizeof(struct usbtmc_capabilities),
                USB_TIMEOUT);
        if (r != sizeof(struct usbtmc_capabilities)) {
-               printf("GET_CAPABILITIES failed: %s\n", usb_strerror());
+               printf("GET_CAPABILITIES failed: %s\n", usb_strerror(r));
                return NULL;
        }
 
@@ -134,21 +191,26 @@ static struct usbtmc_capabilities* usbtmc_get_capabilities(struct scope *sc)
        return &res;
 }
 
+void usbtmc_reset(struct scope *sc)
+{
+       libusb_reset_device(sc->usb.dev);
+       usbtmc_claim(sc);
+}
+
 static void usbtmc_clear(struct scope *sc)
 {
        int r;
        unsigned char status[2];
 
        printf("Initiating clear...\n");
-       r = usb_control_msg(sc->usb.dev, 0xA1,
+       r = libusb_control_transfer(sc->usb.dev, 0xA1,
                USBTMC_CTL_INITIATE_CLEAR,
-               0, 0, (char*)status, 1,
+               0, 0, status, 1,
                USB_TIMEOUT);
        
        if ((r != 1) || status[0] != USBTMC_STATUS_SUCCESS) {
-               printf("INITIATE_CLEAR failed (0x%x): %s\n", status[0], usb_strerror());
-               usb_reset(sc->usb.dev);
-               usbtmc_claim(sc);
+               printf("INITIATE_CLEAR failed (0x%x): %s\n", status[0], usb_strerror(r));
+               usbtmc_reset(sc);
                return;
        }
 
@@ -156,13 +218,13 @@ static void usbtmc_clear(struct scope *sc)
                usleep(100000);
                printf("Waiting for clear to complete...\n");
 
-               r = usb_control_msg(sc->usb.dev, 0xA1,
+               r = libusb_control_transfer(sc->usb.dev, 0xA1,
                        USBTMC_CTL_CHECK_CLEAR_STAT,
-                       0, 0, (char*)status, 2,
+                       0, 0, status, 2,
                        USB_TIMEOUT);
 
                if (r != 2) {
-                       printf("CHECK_CLEAR failed: %s\n", usb_strerror());
+                       printf("CHECK_CLEAR failed: %s\n", usb_strerror(r));
                        return;
                }
 
@@ -186,7 +248,9 @@ static void usbtmc_clear(struct scope *sc)
 int usbtmc_sendscpi(struct scope *sc, char* cmd, 
                unsigned char *resp, int resplen) {
        int len,r;
+       int err;
        int cmdlen = strlen(cmd);
+       int transferred;
        struct usbtmc_header *req;
 
        sc->usb.bTag++;
@@ -209,18 +273,20 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
        memcpy(req->msg, cmd, cmdlen);
 
        if (sc->usb.brokenRigol) {
-               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
-                       (char*)req, sizeof(struct usbtmc_header),
+               r=libusb_bulk_transfer(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (unsigned char*)req, sizeof(struct usbtmc_header),
+                       &transferred,
                        USB_TIMEOUT);
                USB_ERROR("USBTMC_DEV_DEP_MSG_OUT1", r);
 
-               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
-                       (char*)&(req->msg), len - sizeof(struct usbtmc_header),
+               r=libusb_bulk_transfer(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (unsigned char*)&(req->msg), len - sizeof(struct usbtmc_header),
+                       &transferred,
                        USB_TIMEOUT);
                USB_ERROR("USBTMC_DEV_DEP_MSG_OUT2", r);
        } else {
-               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
-                       (char*)req, len, USB_TIMEOUT);
+               r=libusb_bulk_transfer(sc->usb.dev, sc->usb.ep_bulk_out,
+                       (unsigned char*)req, len, &transferred, USB_TIMEOUT);
                USB_ERROR("USBTMC_DEV_DEP_MSG_OUT", r);
        }
 
@@ -231,7 +297,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
                unsigned char rxbuff[USBTMC_IN_TRANSFERSIZE];
                struct usbtmc_header *res;
                int bytes_read;
-               unsigned int transfer_size = USBTMC_IN_TRANSFERSIZE;
+               unsigned int read_size = USBTMC_IN_TRANSFERSIZE;
                unsigned int headerlen;
 
                req = calloc(1, sizeof(struct usbtmc_header));
@@ -241,7 +307,7 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
                }
 
                if (sc->usb.brokenRigol == 1) {
-                       transfer_size = sc->usb.wMaxPacketSize_in;
+                       read_size = sc->usb.wMaxPacketSize_in;
                }
 
                bytes_read = 0;
@@ -254,28 +320,27 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
                                req->MsgID = USBTMC_REQUEST_DEV_DEP_MSG_IN;
                                req->bTag = sc->usb.bTag;
                                req->bTagInverse = ~sc->usb.bTag;
-                               req->TransferSize = LE32(transfer_size);
+                               req->TransferSize = LE32(USBTMC_IN_TRANSFERSIZE);
                                req->bmTransferAttributes = 0;
                                req->TermChar = 0;
 
                                /* send read command */
-                               r=usb_bulk_write(sc->usb.dev, sc->usb.ep_bulk_out,
-                                               (char*)req, sizeof(struct usbtmc_header), USB_TIMEOUT);
+                               r=libusb_bulk_transfer(sc->usb.dev, sc->usb.ep_bulk_out,
+                                               (unsigned char*)req, sizeof(struct usbtmc_header), &transferred, USB_TIMEOUT);
                                USB_ERROR("USBTMC_REQUEST_DEV_DEP_MSG_IN", r);
 
                                headerlen = sizeof(struct usbtmc_header);
                        }
 
-                       r=usb_bulk_read(sc->usb.dev, sc->usb.ep_bulk_in,
-                                       (char*)rxbuff, transfer_size, USB_TIMEOUT);
-                       USB_ERROR("USBTMC_DEV_DEP_MSG_IN", r);
+                       err=libusb_bulk_transfer(sc->usb.dev, sc->usb.ep_bulk_in,
+                                       (unsigned char*)rxbuff, read_size, &r, USB_TIMEOUT);
+                       USB_ERROR("USBTMC_DEV_DEP_MSG_IN", err);
 
                        if (r < headerlen) {
                                fprintf(stderr, "Short read!\n");
                                return 0;
                        }
 
-
                        if (headerlen > 0) {
                                res = (struct usbtmc_header*)rxbuff;
 
@@ -300,13 +365,12 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
                                }
                        }
 
-                       if ((sc->usb.brokenRigol == 0) || (transfer_size == USBTMC_IN_TRANSFERSIZE) ||
-                           ((r - sizeof(struct usbtmc_header) >= len))) {
+                       if ((r - headerlen) > 0) {
                                memcpy(buff + bytes_read, rxbuff + headerlen, r - headerlen);
                                bytes_read += r - headerlen;
                        }
 
-                       transfer_size = USBTMC_IN_TRANSFERSIZE;
+                       read_size = USBTMC_IN_TRANSFERSIZE;
                } while(bytes_read < len);
 
                free(req);
@@ -328,12 +392,12 @@ int usbtmc_sendscpi(struct scope *sc, char* cmd,
 
 void usbtmc_claim(struct scope *sc)
 {
-       usb_claim_interface(sc->usb.dev, 0);
+       libusb_claim_interface(sc->usb.dev, 0);
 }
 
 void usbtmc_release(struct scope *sc)
 {
-       usb_release_interface(sc->usb.dev, 0);
+       libusb_release_interface(sc->usb.dev, 0);
 }
 
 /* Initialize the scope. */
@@ -343,7 +407,7 @@ struct scope* usbtmc_initscope(void) {
        struct scope *sc;
 
        /* Init libusb */
-       usb_init();
+       libusb_init(NULL);
        /* Locate and open the scope */
        sc = usbtmc_find_scope();
        if (!sc) {
@@ -355,7 +419,7 @@ struct scope* usbtmc_initscope(void) {
        printf("Device status: 0x%x\n", usb488_status(sc));
        /* The following code isn't really necessary, the program works
           OK without it too. */
-       r=usb_control_msg(sc->usb.dev, 0xC8, 9, 0, 0, (char*)&vidpid, 4, USB_TIMEOUT);
+       r=libusb_control_transfer(sc->usb.dev, 0xC8, 9, 0, 0, (unsigned char*)&vidpid, 4, USB_TIMEOUT);
        usbtmc_release(sc);
        if (r < 0) {
                fprintf (stderr, "Error %d sending init message: %s\n", 
@@ -372,5 +436,5 @@ struct scope* usbtmc_initscope(void) {
 void usbtmc_close(struct scope *sc)
 {
        /* Free up and exit */
-       usb_close(sc->usb.dev);
+       libusb_close(sc->usb.dev);
 }
Impressum, Datenschutz