X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fzeroplus-logic-cube%2Fzeroplus.c;h=07f38366203da11ea2ff772fcee647c03b8db33b;hb=a5b35a167a32ffbaee1ce9c0de8501f781f733d1;hp=a029a3cbcb790d0635138e700c4989cedeb7843b;hpb=6f4b1868e8ec8c132878d8b6d558f4af054cbd91;p=libsigrok.git diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index a029a3cb..07f38366 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -44,8 +44,9 @@ #define PACKET_SIZE 2048 /* ?? */ typedef struct { + unsigned short vid; unsigned short pid; - char model_name[64]; + char *model_name; unsigned int channels; unsigned int sample_depth; /* In Ksamples/channel */ unsigned int max_sampling_freq; @@ -56,13 +57,16 @@ typedef struct { * same 128K sample depth. */ static model_t zeroplus_models[] = { - {0x7009, "LAP-C(16064)", 16, 64, 100}, - {0x700A, "LAP-C(16128)", 16, 128, 200}, - {0x700B, "LAP-C(32128)", 32, 128, 200}, - {0x700C, "LAP-C(321000)", 32, 1024, 200}, - {0x700D, "LAP-C(322000)", 32, 2048, 200}, - {0x700E, "LAP-C(16032)", 16, 32, 100}, - {0x7016, "LAP-C(162000)", 16, 2048, 200}, + {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100}, + {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200}, + /* TODO: we don't know anything about these + {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200}, + {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200}, + {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 } }; static const int hwcaps[] = { @@ -161,6 +165,7 @@ struct context { static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, const void *value); +static int hw_dev_close(struct sr_dev_inst *sdi); static unsigned int get_memory_size(int type) { @@ -176,115 +181,6 @@ static unsigned int get_memory_size(int type) return 0; } -static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev, - struct libusb_device_descriptor *des) -{ - struct context *ctx; - unsigned int i; - int ret; - - /* Note: sdi is non-NULL, the caller already checked this. */ - - if (!(ctx = (*sdi)->priv)) { - sr_err("zp: %s: (*sdi)->priv was NULL", __func__); - return -1; - } - - if ((ret = libusb_get_device_descriptor(dev, des))) { - sr_err("zp: failed to get device descriptor: %d", ret); - return -1; - } - - if (des->idVendor != USB_VENDOR) - return 0; - - if (libusb_get_bus_number(dev) == ctx->usb->bus - && libusb_get_device_address(dev) == ctx->usb->address) { - - for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) { - if (!(des->idProduct == zeroplus_models[i].pid)) - continue; - - sr_info("zp: Found ZEROPLUS device 0x%04x (%s)", - des->idProduct, zeroplus_models[i].model_name); - ctx->num_channels = zeroplus_models[i].channels; - ctx->memory_size = zeroplus_models[i].sample_depth * 1024; - break; - } - - if (ctx->num_channels == 0) { - sr_err("zp: Unknown ZEROPLUS device 0x%04x", - des->idProduct); - return -2; - } - - /* Found it. */ - if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) { - (*sdi)->status = SR_ST_ACTIVE; - sr_info("zp: opened device %d on %d.%d interface %d", - (*sdi)->index, ctx->usb->bus, - ctx->usb->address, USB_INTERFACE); - } else { - sr_err("zp: failed to open device: %d", ret); - *sdi = NULL; - } - } - - return 0; -} - -static struct sr_dev_inst *zp_open_dev(int dev_index) -{ - struct sr_dev_inst *sdi; - libusb_device **devlist; - struct libusb_device_descriptor des; - int i; - - if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) - return NULL; - - libusb_get_device_list(usb_context, &devlist); - if (sdi->status == SR_ST_INACTIVE) { - /* Find the device by vendor, product, bus and address. */ - libusb_get_device_list(usb_context, &devlist); - for (i = 0; devlist[i]; i++) { - /* TODO: Error handling. */ - opendev4(&sdi, devlist[i], &des); - } - } else { - /* Status must be SR_ST_ACTIVE, i.e. already in use... */ - sdi = NULL; - } - libusb_free_device_list(devlist, 1); - - if (sdi && sdi->status != SR_ST_ACTIVE) - sdi = NULL; - - return sdi; -} - -static void close_dev(struct sr_dev_inst *sdi) -{ - struct context *ctx; - - if (!(ctx = sdi->priv)) { - sr_err("zp: %s: sdi->priv was NULL", __func__); - return; /* FIXME */ - } - - if (!ctx->usb->devhdl) - return; - - sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, - ctx->usb->bus, ctx->usb->address, USB_INTERFACE); - libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); - libusb_reset_device(ctx->usb->devhdl); - libusb_close(ctx->usb->devhdl); - ctx->usb->devhdl = NULL; - /* TODO: Call libusb_exit() here or only in hw_cleanup()? */ - sdi->status = SR_ST_INACTIVE; -} - static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes) { struct context *ctx; @@ -334,7 +230,7 @@ static void clear_instances(void) for (l = zdi->instances; l; l = l->next) { sdi = l->data; /* Properly close all devices... */ - close_dev(sdi); + hw_dev_close(sdi); /* ...and free all their memory. */ sr_dev_inst_free(sdi); } @@ -361,34 +257,19 @@ static int hw_init(void) static GSList *hw_scan(GSList *options) { struct sr_dev_inst *sdi; + struct sr_probe *probe; + struct context *ctx; + model_t *prof; struct libusb_device_descriptor des; - GSList *devices; libusb_device **devlist; - int ret, devcnt, i; - struct context *ctx; + GSList *devices; + int ret, devcnt, i, j; (void)options; devices = NULL; clear_instances(); - /* Allocate memory for our private driver context. */ - if (!(ctx = g_try_malloc(sizeof(struct context)))) { - sr_err("zp: %s: ctx malloc failed", __func__); - return 0; - } - - /* Set some sane defaults. */ - ctx->cur_samplerate = 0; - ctx->limit_samples = 0; - /* TODO: num_channels isn't initialized before it's needed :( */ - ctx->num_channels = NUM_PROBES; - ctx->memory_size = 0; - ctx->probe_mask = 0; - memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES); - memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES); - // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES); - /* Find all ZEROPLUS analyzers and add them to device list. */ devcnt = 0; libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */ @@ -400,52 +281,102 @@ static GSList *hw_scan(GSList *options) continue; } - if (des.idVendor == USB_VENDOR) { - /* - * Definitely a ZEROPLUS. - * TODO: Any way to detect specific model/version in - * the ZEROPLUS range? - */ - /* Register the device with libsigrok. */ - if (!(sdi = sr_dev_inst_new(devcnt, - SR_ST_INACTIVE, VENDOR_NAME, - MODEL_NAME, MODEL_VERSION))) { - sr_err("zp: %s: sr_dev_inst_new failed", - __func__); - return 0; + prof = NULL; + for (j = 0; j < zeroplus_models[j].vid; j++) { + if (des.idVendor == zeroplus_models[j].vid && + des.idProduct == zeroplus_models[j].pid) { + prof = &zeroplus_models[j]; } - sdi->driver = zdi; - sdi->priv = ctx; - - devices = g_slist_append(devices, sdi); - zdi->instances = g_slist_append(zdi->instances, sdi); - ctx->usb = sr_usb_dev_inst_new( - libusb_get_bus_number(devlist[i]), - libusb_get_device_address(devlist[i]), NULL); - devcnt++; } + /* Skip if the device was not found */ + if (!prof) + continue; + sr_info("zp: Found ZEROPLUS model %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("zp: %s: sr_dev_inst_new failed", __func__); + return NULL; + } + sdi->driver = zdi; + + /* Allocate memory for our private driver context. */ + if (!(ctx = g_try_malloc0(sizeof(struct context)))) { + sr_err("zp: %s: ctx malloc failed", __func__); + return 0; + } + sdi->priv = ctx; + ctx->num_channels = prof->channels; + ctx->memory_size = prof->sample_depth * 1024; + // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES); + + /* Fill in probelist according to this device's profile. */ + for (j = 0; j < ctx->num_channels; j++) { + if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE, + probe_names[j]))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + } + + devices = g_slist_append(devices, sdi); + zdi->instances = g_slist_append(zdi->instances, sdi); + ctx->usb = 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 int hw_dev_open(int dev_index) +static int hw_dev_open(struct sr_dev_inst *sdi) { - struct sr_dev_inst *sdi; struct context *ctx; - int ret; + libusb_device **devlist, *dev; + struct libusb_device_descriptor des; + int device_count, ret, i; + + if (!(ctx = sdi->priv)) { + sr_err("zp: %s: sdi->priv was NULL", __func__); + return SR_ERR_ARG; + } - if (!(sdi = zp_open_dev(dev_index))) { - sr_err("zp: unable to open device"); + device_count = libusb_get_device_list(usb_context, &devlist); + if (device_count < 0) { + sr_err("zp: Failed to retrieve device list"); return SR_ERR; } - /* TODO: Note: sdi is retrieved in zp_open_dev(). */ + dev = NULL; + for (i = 0; i < device_count; i++) { + if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { + sr_err("fx2lafw: Failed to get device descriptor: %d.", + ret); + continue; + } + if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus + && libusb_get_device_address(devlist[i]) == ctx->usb->address) { + dev = devlist[i]; + break; + } + } + if (!dev) { + sr_err("device on bus %d address %d disappeared!", + ctx->usb->bus, ctx->usb->address); + return SR_ERR; + } - if (!(ctx = sdi->priv)) { - sr_err("zp: %s: sdi->priv was NULL", __func__); - return SR_ERR_ARG; + if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) { + sdi->status = SR_ST_ACTIVE; + sr_info("zp: opened device %d on %d.%d interface %d", + sdi->index, ctx->usb->bus, + ctx->usb->address, USB_INTERFACE); + } else { + sr_err("zp: failed to open device: %d", ret); + return SR_ERR; } ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION); @@ -491,17 +422,25 @@ static int hw_dev_open(int dev_index) return SR_OK; } -static int hw_dev_close(int dev_index) +static int hw_dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_inst *sdi; + struct context *ctx; - if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) { - sr_err("zp: %s: sdi was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + if (!(ctx = sdi->priv)) { + sr_err("zp: %s: sdi->priv was NULL", __func__); + return SR_ERR; } - /* TODO */ - close_dev(sdi); + if (!ctx->usb->devhdl) + return SR_ERR; + + sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, + ctx->usb->bus, ctx->usb->address, USB_INTERFACE); + libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); + libusb_reset_device(ctx->usb->devhdl); + libusb_close(ctx->usb->devhdl); + ctx->usb->devhdl = NULL; + sdi->status = SR_ST_INACTIVE; return SR_OK; } @@ -568,17 +507,6 @@ static int hw_info_get(int info_id, const void **data, return SR_OK; } -static int hw_dev_status_get(int dev_index) -{ - struct sr_dev_inst *sdi; - - sdi = sr_dev_inst_get(zdi->instances, dev_index); - if (sdi) - return sdi->status; - else - return SR_ST_NOT_FOUND; -} - static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate) { struct context *ctx; @@ -630,9 +558,9 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, } } -static int hw_dev_acquisition_start(int dev_index, void *cb_data) +static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, + void *cb_data) { - struct sr_dev_inst *sdi; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; struct sr_datafeed_header header; @@ -643,11 +571,6 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data) unsigned char *buf; struct context *ctx; - if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) { - sr_err("zp: %s: sdi was NULL", __func__); - return SR_ERR; - } - if (!(ctx = sdi->priv)) { sr_err("zp: %s: sdi->priv was NULL", __func__); return SR_ERR_ARG; @@ -712,20 +635,15 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data) } /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ -static int hw_dev_acquisition_stop(int dev_index, void *cb_data) +static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, + void *cb_data) { struct sr_datafeed_packet packet; - struct sr_dev_inst *sdi; struct context *ctx; packet.type = SR_DF_END; sr_session_send(cb_data, &packet); - if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) { - sr_err("zp: %s: sdi was NULL", __func__); - return SR_ERR_BUG; - } - if (!(ctx = sdi->priv)) { sr_err("zp: %s: sdi->priv was NULL", __func__); return SR_ERR_BUG; @@ -747,7 +665,6 @@ SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = { .dev_open = hw_dev_open, .dev_close = hw_dev_close, .info_get = hw_info_get, - .dev_status_get = hw_dev_status_get, .dev_config_set = hw_dev_config_set, .dev_acquisition_start = hw_dev_acquisition_start, .dev_acquisition_stop = hw_dev_acquisition_stop,