X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fsaleae-logic%2Fsaleae-logic.c;h=7e8c4b10e3ae0316cbccb674b7beb03c4984a54d;hb=69cfcfc8f0f3f4358714d706268fc05aaa70ca23;hp=295582a963c435ed2bb9466f24030fcdb95b8768;hpb=ea9cfed7a55260ab97f57150b57c42a28c3a85a7;p=libsigrok.git diff --git a/hardware/saleae-logic/saleae-logic.c b/hardware/saleae-logic/saleae-logic.c index 295582a9..7e8c4b10 100644 --- a/hardware/saleae-logic/saleae-logic.c +++ b/hardware/saleae-logic/saleae-logic.c @@ -46,6 +46,12 @@ static int hwcaps[] = { 0, }; +/* + * Probes are numbered 1-8. + * + * TODO: FX2 eval boards with the standard Cypress VID/PID can have 16 pins + * or probes in theory, which is not supported by the Saleae Logic firmware. + */ static const char *probe_names[] = { "0", "1", @@ -81,9 +87,9 @@ static uint64_t supported_samplerates[] = { }; static struct sr_samplerates samplerates = { - SR_KHZ(200), - SR_MHZ(24), - SR_HZ(0), + 0, + 0, + 0, supported_samplerates, }; @@ -94,7 +100,7 @@ static libusb_context *usb_context = NULL; static int new_saleae_logic_firmware = 0; static int hw_dev_config_set(int dev_index, int hwcap, void *value); -static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id); +static int hw_dev_acquisition_stop(int dev_index, void *cb_data); /** * Check the USB configuration to determine if this is a Saleae Logic. @@ -171,7 +177,7 @@ static int sl_open_dev(int dev_index) struct libusb_device_descriptor des; struct sr_dev_inst *sdi; struct context *ctx; - int err, skip, i; + int ret, skip, i; if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return SR_ERR; @@ -184,8 +190,8 @@ static int sl_open_dev(int dev_index) skip = 0; libusb_get_device_list(usb_context, &devlist); for (i = 0; devlist[i]; i++) { - if ((err = libusb_get_device_descriptor(devlist[i], &des))) { - sr_err("logic: failed to get device descriptor: %d", err); + if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { + sr_err("logic: failed to get device descriptor: %d", ret); continue; } @@ -210,7 +216,7 @@ static int sl_open_dev(int dev_index) continue; } - if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) { + if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) { if (ctx->usb->address == 0xff) /* * first time we touch this device after firmware upload, @@ -223,7 +229,7 @@ static int sl_open_dev(int dev_index) sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE); } else { - sr_err("logic: failed to open device: %d", err); + sr_err("logic: failed to open device: %d", ret); } /* if we made it here, we handled the device one way or another */ @@ -326,7 +332,7 @@ static int hw_init(const char *devinfo) struct fx2_profile *fx2_prof; struct context *ctx; libusb_device **devlist; - int err, devcnt, i, j; + int ret, devcnt, i, j; /* Avoid compiler warnings. */ (void)devinfo; @@ -341,10 +347,10 @@ static int hw_init(const char *devinfo) libusb_get_device_list(usb_context, &devlist); for (i = 0; devlist[i]; i++) { fx2_prof = NULL; - err = libusb_get_device_descriptor(devlist[i], &des); - if (err != 0) { + ret = libusb_get_device_descriptor(devlist[i], &des); + if (ret != 0) { sr_err("logic: failed to get device descriptor: %d", - err); + ret); continue; } @@ -398,7 +404,7 @@ static int hw_dev_open(int dev_index) GTimeVal cur_time; struct sr_dev_inst *sdi; struct context *ctx; - int timediff, err; + int timediff, ret; if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return SR_ERR; @@ -408,14 +414,14 @@ static int hw_dev_open(int dev_index) * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms * for the FX2 to renumerate */ - err = 0; + ret = 0; if (GTV_TO_MSEC(ctx->fw_updated) > 0) { sr_info("logic: waiting for device to reset"); /* takes at least 300ms for the FX2 to be gone from the USB bus */ g_usleep(300 * 1000); timediff = 0; while (timediff < MAX_RENUM_DELAY) { - if ((err = sl_open_dev(dev_index)) == SR_OK) + if ((ret = sl_open_dev(dev_index)) == SR_OK) break; g_usleep(100 * 1000); g_get_current_time(&cur_time); @@ -423,18 +429,18 @@ static int hw_dev_open(int dev_index) } sr_info("logic: device came back after %d ms", timediff); } else { - err = sl_open_dev(dev_index); + ret = sl_open_dev(dev_index); } - if (err != SR_OK) { + if (ret != SR_OK) { sr_err("logic: unable to open device"); return SR_ERR; } ctx = sdi->priv; - err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); - if (err != 0) { - sr_err("logic: Unable to claim interface: %d", err); + ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); + if (ret != 0) { + sr_err("logic: Unable to claim interface: %d", ret); return SR_ERR; } @@ -454,7 +460,7 @@ static int hw_dev_close(int dev_index) if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { sr_err("logic: %s: sdi was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + return SR_ERR_BUG; } /* TODO */ @@ -651,14 +657,14 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value) return ret; } -static int receive_data(int fd, int revents, void *user_data) +static int receive_data(int fd, int revents, void *cb_data) { struct timeval tv; /* Avoid compiler warnings. */ (void)fd; (void)revents; - (void)user_data; + (void)cb_data; tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(usb_context, &tv); @@ -666,28 +672,50 @@ static int receive_data(int fd, int revents, void *user_data) return TRUE; } +static void abort_acquisition(struct context *ctx) +{ + ctx->num_samples = -1; +} + +static void finish_acquisition(struct context *ctx) +{ + struct sr_datafeed_packet packet; + int i; + + /* Terminate session */ + packet.type = SR_DF_END; + sr_session_send(ctx->session_dev_id, &packet); + + /* Remove fds from polling */ + const struct libusb_pollfd **const lupfd = + libusb_get_pollfds(usb_context); + for (i = 0; lupfd[i]; i++) + sr_source_remove(lupfd[i]->fd); + free(lupfd); /* NOT g_free()! */ +} + static void receive_transfer(struct libusb_transfer *transfer) { /* TODO: These statics have to move to the ctx struct. */ - static int num_samples = 0; static int empty_transfer_count = 0; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; - struct context *ctx; + struct context *ctx = transfer->user_data; int cur_buflen, trigger_offset, i; unsigned char *cur_buf, *new_buf; - /* hw_dev_acquisition_stop() is telling us to stop. */ - if (transfer == NULL) - num_samples = -1; - /* * If acquisition has already ended, just free any queued up * transfer that come in. */ - if (num_samples == -1) { + if (ctx->num_samples == -1) { if (transfer) libusb_free_transfer(transfer); + + ctx->submitted_transfers--; + if (ctx->submitted_transfers == 0) + finish_acquisition(ctx); + return; } @@ -720,7 +748,7 @@ static void receive_transfer(struct libusb_transfer *transfer) * The FX2 gave up. End the acquisition, the frontend * will work out that the samplecount is short. */ - hw_dev_acquisition_stop(-1, ctx->session_data); + abort_acquisition(ctx); } return; } else { @@ -746,7 +774,7 @@ static void receive_transfer(struct libusb_transfer *transfer) */ packet.type = SR_DF_TRIGGER; packet.payload = NULL; - sr_session_bus(ctx->session_data, &packet); + sr_session_send(ctx->session_dev_id, &packet); /* * Send the samples that triggered it, since we're @@ -757,7 +785,7 @@ static void receive_transfer(struct libusb_transfer *transfer) logic.length = ctx->trigger_stage; logic.unitsize = 1; logic.data = ctx->trigger_buffer; - sr_session_bus(ctx->session_data, &packet); + sr_session_send(ctx->session_dev_id, &packet); ctx->trigger_stage = TRIGGER_FIRED; break; @@ -789,12 +817,12 @@ static void receive_transfer(struct libusb_transfer *transfer) logic.length = cur_buflen - trigger_offset; logic.unitsize = 1; logic.data = cur_buf + trigger_offset; - sr_session_bus(ctx->session_data, &packet); + sr_session_send(ctx->session_dev_id, &packet); g_free(cur_buf); - num_samples += cur_buflen; - if (ctx->limit_samples && (unsigned int) num_samples > ctx->limit_samples) { - hw_dev_acquisition_stop(-1, ctx->session_data); + ctx->num_samples += cur_buflen; + if (ctx->limit_samples && (unsigned int)ctx->num_samples > ctx->limit_samples) { + abort_acquisition(ctx); } } else { /* @@ -804,7 +832,7 @@ static void receive_transfer(struct libusb_transfer *transfer) } } -static int hw_dev_acquisition_start(int dev_index, gpointer session_data) +static int hw_dev_acquisition_start(int dev_index, void *cb_data) { struct sr_dev_inst *sdi; struct sr_datafeed_packet *packet; @@ -818,7 +846,8 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data) if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) return SR_ERR; ctx = sdi->priv; - ctx->session_data = session_data; + ctx->session_dev_id = cb_data; + ctx->num_samples = 0; if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { sr_err("logic: %s: packet malloc failed", __func__); @@ -847,6 +876,8 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data) g_free(buf); return SR_ERR; } + + ctx->submitted_transfers++; size = 4096; } @@ -862,32 +893,30 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data) gettimeofday(&header->starttime, NULL); header->samplerate = ctx->cur_samplerate; header->num_logic_probes = ctx->profile->num_probes; - sr_session_bus(session_data, packet); + sr_session_send(ctx->session_dev_id, packet); g_free(header); g_free(packet); return SR_OK; } -/* This stops acquisition on ALL devices, ignoring dev_index. */ -static int hw_dev_acquisition_stop(int dev_index, gpointer session_data) +/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ +static int hw_dev_acquisition_stop(int dev_index, void *cb_data) { - struct sr_datafeed_packet packet; - - /* Avoid compiler warnings. */ - (void)dev_index; + struct sr_dev_inst *sdi; - packet.type = SR_DF_END; - sr_session_bus(session_data, &packet); + /* unused parameter */ + (void)cb_data; - receive_transfer(NULL); + if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) + return SR_ERR; - /* TODO: Need to cancel and free any queued up transfers. */ + abort_acquisition(sdi->priv); return SR_OK; } -SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info = { +SR_PRIV struct sr_dev_driver saleae_logic_driver_info = { .name = "saleae-logic", .longname = "Saleae Logic", .api_version = 1,