]> sigrok.org Git - libsigrok.git/blobdiff - hardware/zeroplus-logic-cube/gl_usb.c
build: Portability fixes.
[libsigrok.git] / hardware / zeroplus-logic-cube / gl_usb.c
index 7353f65db0bc252ec4cab0aa1d88808b8ce44208..20993286d275ffde471cebbe9809b4ef850c76f7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This file is part of the sigrok project.
+ * This file is part of the libsigrok project.
  *
  * Copyright (C) 2010 Sven Peter <sven@fail0verflow.com>
  * Copyright (C) 2010 Haxx Enterprises <bushing@gmail.com>
  *  THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <libusb.h>
 #include <stdio.h>
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include <libusb.h>
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 #include "gl_usb.h"
+#include "protocol.h"
 
 #define CTRL_IN                (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
                         LIBUSB_RECIPIENT_INTERFACE)
 #define CTRL_OUT       (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \
                         LIBUSB_RECIPIENT_INTERFACE)
+#define EP1_BULK_IN    (LIBUSB_ENDPOINT_IN | 1)
 
-const int PACKET_CTRL_LEN = 2;
-const int PACKET_INT_LEN = 2;
-const int PACKET_BULK_LEN = 64;
-const int INTERFACE = 0;
-const int ENDPOINT_INT_IN = 0x81;      /* Endpoint 0x81 address for IN */
-const int ENDPOINT_INT_OUT = 0x01;     /* Endpoint 1 address for OUT */
-const int ENDPOINT_BULK_IN = 0x81;     /* Endpoint 0x81 address for IN */
-const int ENDPOINT_BULK_OUT = 0x02;    /* Endpoint 1 address for OUT */
-const int TIMEOUT = 5000;              /* Timeout in ms */
+#define TIMEOUT                5000    /* Timeout in ms */
 
 enum {
        REQ_READBULK = 0x82,
@@ -57,9 +51,7 @@ enum {
        REQ_WRITEDATA,
 };
 
-static struct libusb_device_handle *g_devh = NULL;
-
-int gl_write_address(libusb_device_handle *devh, unsigned int address)
+static int gl_write_address(libusb_device_handle *devh, unsigned int address)
 {
        unsigned char packet[8] = { address & 0xFF };
        int ret;
@@ -67,12 +59,11 @@ int gl_write_address(libusb_device_handle *devh, unsigned int address)
        ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
                                         0, packet, 1, TIMEOUT);
        if (ret != 1)
-               sr_err("%s: libusb_control_transfer returned %d\n",
-                      __func__, ret);
+               sr_err("%s: %s.", __func__, libusb_error_name(ret));
        return ret;
 }
 
-int gl_write_data(libusb_device_handle *devh, unsigned int val)
+static int gl_write_data(libusb_device_handle *devh, unsigned int val)
 {
        unsigned char packet[8] = { val & 0xFF };
        int ret;
@@ -80,12 +71,11 @@ int gl_write_data(libusb_device_handle *devh, unsigned int val)
        ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
                                      0, packet, 1, TIMEOUT);
        if (ret != 1)
-               sr_err("%s: libusb_control_transfer returned %d\n",
-                      __func__, ret);
+               sr_err("%s: %s.", __func__, libusb_error_name(ret));
        return ret;
 }
 
-int gl_read_data(libusb_device_handle *devh)
+static int gl_read_data(libusb_device_handle *devh)
 {
        unsigned char packet[8] = { 0 };
        int ret;
@@ -93,12 +83,13 @@ int gl_read_data(libusb_device_handle *devh)
        ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
                                      0, packet, 1, TIMEOUT);
        if (ret != 1)
-               sr_err("%s: libusb_control_transfer returned %d, val=%hhx\n",
-                      __func__, ret, packet[0]);
+               sr_err("%s: %s, val=%hhx.", __func__,
+                      libusb_error_name(ret), packet[0]);
        return (ret == 1) ? packet[0] : ret;
 }
 
-int gl_read_bulk(libusb_device_handle *devh, void *buffer, unsigned int size)
+SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
+                        unsigned int size)
 {
        unsigned char packet[8] =
            { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
@@ -108,17 +99,18 @@ int gl_read_bulk(libusb_device_handle *devh, void *buffer, unsigned int size)
        ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
                                      0, packet, 8, TIMEOUT);
        if (ret != 8)
-               sr_err("%s: libusb_control_transfer returned %d\n",
-                      __func__, ret);
+               sr_err("%s: libusb_control_transfer: %s.", __func__,
+                      libusb_error_name(ret));
 
-       ret = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, buffer, size,
+       ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
                                   &transferred, TIMEOUT);
        if (ret < 0)
-               sr_err("Bulk read error %d\n", ret);
+               sr_err("%s: libusb_bulk_transfer: %s.", __func__,
+                      libusb_error_name(ret));
        return transferred;
 }
 
-int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
+SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
                 unsigned int val)
 {
        int ret;
@@ -130,7 +122,7 @@ int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
        return ret;
 }
 
-int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
+SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
 {
        int ret;
 
@@ -141,68 +133,20 @@ int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
        return ret;
 }
 
-int gl_open(int vid)
+SR_PRIV int gl_reg_read_buf(libusb_device_handle *devh, unsigned int reg,
+               unsigned char *buf, unsigned int len)
 {
        int ret;
-       struct libusb_device **devs;
-       struct libusb_device *dev;
-       size_t i = 0;
-       struct libusb_device_descriptor desc;
+       unsigned int i;
 
-       ret = libusb_init(NULL);
+       ret = gl_write_address(devh, reg);
        if (ret < 0)
-               return GL_ELIBUSB;
-
-       if (libusb_get_device_list(NULL, &devs) < 0) {
-               ret = GL_EOPEN;
-               goto gl_open_error;
-       }
-
-       while ((dev = devs[i++]) != NULL) {
-               if (libusb_get_device_descriptor(dev, &desc) < 0)
-                       break;
-
-               if (desc.idVendor == vid) {
-                       if (libusb_open(dev, &g_devh) < 0)
-                               g_devh = NULL;
-                       break;
-               }
-       }
-
-       libusb_free_device_list(devs, 1);
-
-       if (!g_devh) {
-               ret = GL_EOPEN;
-               goto gl_open_error;
-       }
-
-       ret = libusb_set_configuration(g_devh, 1);
-       if (ret < 0) {
-               ret = GL_ESETCONFIG;
-               goto gl_open_error;
-       }
-
-       ret = libusb_claim_interface(g_devh, 0);
-       if (ret < 0) {
-               ret = GL_ECLAIM;
-               goto gl_open_error;
-       }
-
-       return GL_OK;
-
-gl_open_error:
-       gl_close();
-       return ret;
-}
-
-int gl_close(void)
-{
-       if (g_devh) {
-               libusb_release_interface(g_devh, 0);
-               libusb_reset_device(g_devh);
-               libusb_close(g_devh);
+               return ret;
+       for (i = 0; i < len; i++) {
+               ret = gl_read_data(devh);
+               if (ret < 0)
+                       return ret;
+               buf[i] = ret;
        }
-       libusb_exit(NULL);
-
        return 0;
 }