]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/zeroplus-logic-cube/api.c
Drop unneeded std_session_send_df_header() comments.
[libsigrok.git] / src / hardware / zeroplus-logic-cube / api.c
index 3a0e4d8c714ffa7dacfc4252233d9bd5a6950d79..ab4c3cc4eb99525f4d9155895fe6e9beb9cd9d20 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include "protocol.h"
 
 #define VENDOR_NAME                    "ZEROPLUS"
@@ -30,7 +31,7 @@
 struct zp_model {
        uint16_t vid;
        uint16_t pid;
-       char *model_name;
+       const char *model_name;
        unsigned int channels;
        unsigned int sample_depth;      /* In Ksamples/channel */
        unsigned int max_sampling_freq;
@@ -49,16 +50,17 @@ static const struct zp_model zeroplus_models[] = {
        {0x0c12, 0x700d, "LAP-C(322000)", 32, 2048, 200},
        {0x0c12, 0x700e, "LAP-C(16032)",  16, 32,   100},
        {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
-       { 0, 0, 0, 0, 0, 0 }
+       {0x0c12, 0x7100, "AKIP-9101", 16, 256, 200},
+       ALL_ZERO
 };
 
-static const uint32_t hwcaps[] = {
+static const uint32_t devopts[] = {
        SR_CONF_LOGIC_ANALYZER,
-       SR_CONF_SAMPLERATE,
-       SR_CONF_TRIGGER_MATCH,
-       SR_CONF_CAPTURE_RATIO,
-       SR_CONF_VOLTAGE_THRESHOLD,
-       SR_CONF_LIMIT_SAMPLES,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+       SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
+       SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
 static const int32_t trigger_matches[] = {
@@ -75,11 +77,9 @@ static const char *channel_names[] = {
        "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
        "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
        "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
-       NULL,
 };
 
 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
-static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info;
 
 /*
  * The hardware supports more samplerates than these, but these are the
@@ -155,41 +155,53 @@ SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
        return SR_OK;
 }
 
-static int init(struct sr_context *sr_ctx)
+static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct sr_dev_inst *sdi;
-       struct sr_channel *ch;
        struct drv_context *drvc;
        struct dev_context *devc;
        const struct zp_model *prof;
        struct libusb_device_descriptor des;
+       struct libusb_device_handle *hdl;
        libusb_device **devlist;
        GSList *devices;
-       int ret, devcnt, i, j;
+       int ret, i, j;
+       char serial_num[64], connection_id[64];
 
        (void)options;
 
-       drvc = di->priv;
+       drvc = di->context;
 
        devices = NULL;
 
        /* Find all ZEROPLUS analyzers and add them to device list. */
-       devcnt = 0;
        libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
 
        for (i = 0; devlist[i]; i++) {
-               ret = libusb_get_device_descriptor(devlist[i], &des);
-               if (ret != 0) {
-                       sr_err("Failed to get device descriptor: %s.",
-                              libusb_error_name(ret));
+               libusb_get_device_descriptor(devlist[i], &des);
+
+               if ((ret = libusb_open(devlist[i], &hdl)) < 0)
+                       continue;
+
+               if (des.iSerialNumber == 0) {
+                       serial_num[0] = '\0';
+               } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+                               des.iSerialNumber, (unsigned char *) serial_num,
+                               sizeof(serial_num))) < 0) {
+                       sr_warn("Failed to get serial number string descriptor: %s.",
+                               libusb_error_name(ret));
                        continue;
                }
 
+               libusb_close(hdl);
+
+               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+
                prof = NULL;
                for (j = 0; j < zeroplus_models[j].vid; j++) {
                        if (des.idVendor == zeroplus_models[j].vid &&
@@ -203,19 +215,16 @@ static GSList *scan(GSList *options)
                sr_info("Found ZEROPLUS %s.", prof->model_name);
 
                /* Register the device with libsigrok. */
-               if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
-                               VENDOR_NAME, prof->model_name, NULL))) {
-                       sr_err("%s: sr_dev_inst_new failed", __func__);
-                       return NULL;
-               }
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sdi->status = SR_ST_INACTIVE;
+               sdi->vendor = g_strdup(VENDOR_NAME);
+               sdi->model = g_strdup(prof->model_name);
                sdi->driver = di;
+               sdi->serial_num = g_strdup(serial_num);
+               sdi->connection_id = g_strdup(connection_id);
 
                /* Allocate memory for our private driver context. */
-               if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-                       sr_err("Device context malloc failed.");
-                       return NULL;
-               }
-
+               devc = g_malloc0(sizeof(struct dev_context));
                sdi->priv = devc;
                devc->prof = prof;
                devc->num_channels = prof->channels;
@@ -231,12 +240,9 @@ static GSList *scan(GSList *options)
                // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
 
                /* Fill in channellist according to this device's profile. */
-               for (j = 0; j < devc->num_channels; j++) {
-                       if (!(ch = sr_channel_new(j, SR_CHANNEL_LOGIC, TRUE,
-                                       channel_names[j])))
-                               return NULL;
-                       sdi->channels = g_slist_append(sdi->channels, ch);
-               }
+               for (j = 0; j < devc->num_channels; j++)
+                       sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
+                                       channel_names[j]);
 
                devices = g_slist_append(devices, sdi);
                drvc->instances = g_slist_append(drvc->instances, sdi);
@@ -244,29 +250,28 @@ static GSList *scan(GSList *options)
                sdi->conn = sr_usb_dev_inst_new(
                        libusb_get_bus_number(devlist[i]),
                        libusb_get_device_address(devlist[i]), NULL);
-               devcnt++;
-
        }
        libusb_free_device_list(devlist, 1);
 
        return devices;
 }
 
-static GSList *dev_list(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
-       return ((struct drv_context *)(di->priv))->instances;
+       return ((struct drv_context *)(di->context))->instances;
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
+       struct sr_dev_driver *di = sdi->driver;
        struct dev_context *devc;
        struct drv_context *drvc;
        struct sr_usb_dev_inst *usb;
        libusb_device **devlist, *dev;
-       struct libusb_device_descriptor des;
        int device_count, ret, i;
+       char connection_id[64];
 
-       drvc = di->priv;
+       drvc = di->context;
        usb = sdi->conn;
 
        if (!(devc = sdi->priv)) {
@@ -283,27 +288,22 @@ static int dev_open(struct sr_dev_inst *sdi)
 
        dev = NULL;
        for (i = 0; i < device_count; i++) {
-               if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
-                       sr_err("Failed to get device descriptor: %s.",
-                              libusb_error_name(ret));
-                       continue;
-               }
-               if (libusb_get_bus_number(devlist[i]) == usb->bus
-                   && libusb_get_device_address(devlist[i]) == usb->address) {
+               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+               if (!strcmp(sdi->connection_id, connection_id)) {
                        dev = devlist[i];
                        break;
                }
        }
        if (!dev) {
-               sr_err("Device on bus %d address %d disappeared!",
-                      usb->bus, usb->address);
+               sr_err("Device on %d.%d (logical) / %s (physical) disappeared!",
+                      usb->bus, usb->address, sdi->connection_id);
                return SR_ERR;
        }
 
        if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
                sdi->status = SR_ST_ACTIVE;
-               sr_info("Opened device %d on %d.%d interface %d.",
-                       sdi->index, usb->bus, usb->address, USB_INTERFACE);
+               sr_info("Opened device on %d.%d (logical) / %s (physical) interface %d.",
+                       usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
        } else {
                sr_err("Failed to open device: %s.", libusb_error_name(ret));
                return SR_ERR;
@@ -366,8 +366,8 @@ static int dev_close(struct sr_dev_inst *sdi)
        if (!usb->devhdl)
                return SR_ERR;
 
-       sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
-               usb->bus, usb->address, USB_INTERFACE);
+       sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
+               usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
        libusb_release_interface(usb->devhdl, USB_INTERFACE);
        libusb_reset_device(usb->devhdl);
        libusb_close(usb->devhdl);
@@ -377,7 +377,7 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
+static int cleanup(const struct sr_dev_driver *di)
 {
        return std_dev_clear(di, NULL);
 }
@@ -386,35 +386,26 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
+       GVariant *range[2];
 
        (void)cg;
 
+       if (!sdi)
+               return SR_ERR_ARG;
+
+       devc = sdi->priv;
+
        switch (key) {
        case SR_CONF_SAMPLERATE:
-               if (sdi) {
-                       devc = sdi->priv;
-                       *data = g_variant_new_uint64(devc->cur_samplerate);
-                       sr_spew("Returning samplerate: %" PRIu64 "Hz.",
-                               devc->cur_samplerate);
-               } else
-                       return SR_ERR_ARG;
+               *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
        case SR_CONF_CAPTURE_RATIO:
-               if (sdi) {
-                       devc = sdi->priv;
-                       *data = g_variant_new_uint64(devc->capture_ratio);
-               } else
-                       return SR_ERR_ARG;
+               *data = g_variant_new_uint64(devc->capture_ratio);
                break;
        case SR_CONF_VOLTAGE_THRESHOLD:
-               if (sdi) {
-                       GVariant *range[2];
-                       devc = sdi->priv;
-                       range[0] = g_variant_new_double(devc->cur_threshold);
-                       range[1] = g_variant_new_double(devc->cur_threshold);
-                       *data = g_variant_new_tuple(range, 2);
-               } else
-                       return SR_ERR_ARG;
+               range[0] = g_variant_new_double(devc->cur_threshold);
+               range[1] = g_variant_new_double(devc->cur_threshold);
+               *data = g_variant_new_tuple(range, 2);
                break;
        default:
                return SR_ERR_NA;
@@ -470,7 +461,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        switch (key) {
        case SR_CONF_DEVICE_OPTIONS:
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(uint32_t));
+                               devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        case SR_CONF_SAMPLERATE:
                devc = sdi->priv;
@@ -587,20 +578,15 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_info("Ramsize trigger    = 0x%x.", ramsize_trigger);
        sr_info("Memory size        = 0x%x.", memory_size);
 
-       /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
 
        /* Check for empty capture */
        if ((status & STATUS_READY) && !stop_address) {
-               packet.type = SR_DF_END;
-               sr_session_send(cb_data, &packet);
+               std_session_send_df_end(cb_data, LOG_PREFIX);
                return SR_OK;
        }
 
-       if (!(buf = g_try_malloc(PACKET_SIZE))) {
-               sr_err("Packet buffer malloc failed.");
-               return SR_ERR_MALLOC;
-       }
+       buf = g_malloc(PACKET_SIZE);
 
        /* Check if the trigger is in the samples we are throwing away */
        trigger_now = now_address == trigger_address ||
@@ -704,8 +690,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        analyzer_read_stop(usb->devhdl);
        g_free(buf);
 
-       packet.type = SR_DF_END;
-       sr_session_send(cb_data, &packet);
+       std_session_send_df_end(cb_data, LOG_PREFIX);
 
        return SR_OK;
 }
@@ -715,10 +700,8 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
-       struct sr_datafeed_packet packet;
 
-       packet.type = SR_DF_END;
-       sr_session_send(cb_data, &packet);
+       std_session_send_df_end(cb_data, LOG_PREFIX);
 
        if (!(devc = sdi->priv)) {
                sr_err("%s: sdi->priv was NULL", __func__);
@@ -748,5 +731,5 @@ SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };