28 #include "sigrok-internal.h"
38 { 0x08a9, 0x0014,
"CWAV",
"USBee AX", NULL,
39 FIRMWARE_DIR
"/fx2lafw-cwav-usbeeax.fw", 8 },
44 { 0x08a9, 0x0009,
"CWAV",
"USBee SX", NULL,
45 FIRMWARE_DIR
"/fx2lafw-cwav-usbeesx.fw", 8 },
53 { 0x0925, 0x3881,
"Saleae",
"Logic", NULL,
54 FIRMWARE_DIR
"/fx2lafw-saleae-logic.fw", 8 },
61 { 0x04B4, 0x8613,
"Cypress",
"FX2", NULL,
62 FIRMWARE_DIR
"/fx2lafw-cypress-fx2.fw", 8 },
67 { 0x16d0, 0x0498,
"Braintechnology",
"USB-LPS", NULL,
68 FIRMWARE_DIR
"/fx2lafw-braintechnology-usb-lps.fw", 8 },
70 { 0, 0, 0, 0, 0, 0, 0 }
73 static int hwcaps[] = {
86 static const char *probe_names[] = {
98 static uint64_t supported_samplerates[] = {
122 supported_samplerates,
125 static GSList *dev_insts = NULL;
126 static libusb_context *usb_context = NULL;
128 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value);
129 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data);
137 static gboolean check_conf_profile(libusb_device *dev)
139 struct libusb_device_descriptor des;
140 struct libusb_device_handle *hdl;
142 unsigned char strdesc[64];
148 if (libusb_get_device_descriptor(dev, &des) != 0)
151 if (libusb_open(dev, &hdl) != 0)
154 if (libusb_get_string_descriptor_ascii(hdl,
155 des.iManufacturer, strdesc,
sizeof(strdesc)) < 0)
157 if (strncmp((
const char *)strdesc,
"sigrok", 6))
160 if (libusb_get_string_descriptor_ascii(hdl,
161 des.iProduct, strdesc,
sizeof(strdesc)) < 0)
163 if (strncmp((
const char *)strdesc,
"fx2lafw", 7))
175 static int fx2lafw_dev_open(
int dev_index)
177 libusb_device **devlist;
178 struct libusb_device_descriptor des;
194 const int device_count = libusb_get_device_list(usb_context, &devlist);
195 if (device_count < 0) {
196 sr_err(
"fx2lafw: Failed to retrieve device list (%d)",
201 for (i = 0; i < device_count; i++) {
202 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
203 sr_err(
"fx2lafw: Failed to get device descriptor: %d.",
213 if (skip != dev_index) {
223 if (libusb_get_bus_number(devlist[i]) != ctx->
usb->bus
224 || libusb_get_device_address(devlist[i]) != ctx->
usb->address)
229 if (!(ret = libusb_open(devlist[i], &ctx->
usb->devhdl))) {
230 if (ctx->
usb->address == 0xff)
235 ctx->
usb->address = libusb_get_device_address(devlist[i]);
237 sr_err(
"fx2lafw: Failed to open device: %d.", ret);
243 sr_err(
"fx2lafw: Failed to retrieve "
244 "firmware version information.");
250 sr_err(
"fx2lafw: Failed to retrieve REVID.");
260 sr_err(
"fx2lafw: Expected firmware version %d.x, "
267 sr_info(
"fx2lafw: Opened device %d on %d.%d "
268 "interface %d, firmware %d.%d, REVID %d.",
274 libusb_free_device_list(devlist, 1);
288 if (ctx->
usb->devhdl == NULL)
291 sr_info(
"fx2lafw: Closing device %d on %d.%d interface %d.",
294 libusb_close(ctx->
usb->devhdl);
295 ctx->
usb->devhdl = NULL;
299 static int configure_probes(
struct context *ctx, GSList *probes)
303 int probe_bit, stage, i;
312 for (l = probes; l; l = l->next) {
316 probe_bit = 1 << (probe->
index - 1);
321 for (tc = probe->
trigger; *tc; tc++) {
326 if (stage > NUM_TRIGGER_STAGES)
343 static struct context *fx2lafw_dev_new(
void)
347 if (!(ctx = g_try_malloc0(
sizeof(
struct context)))) {
348 sr_err(
"fx2lafw: %s: ctx malloc failed.", __func__);
361 static int hw_init(
const char *devinfo)
364 struct libusb_device_descriptor des;
367 libusb_device **devlist;
375 if (libusb_init(&usb_context) != 0) {
376 sr_warn(
"fx2lafw: Failed to initialize libusb.");
381 libusb_get_device_list(usb_context, &devlist);
382 for (i = 0; devlist[i]; i++) {
384 if ((ret = libusb_get_device_descriptor(
385 devlist[i], &des)) != 0) {
386 sr_warn(
"fx2lafw: Failed to get device descriptor: %d.", ret);
391 for (j = 0; supported_fx2[j].
vid; j++) {
392 if (des.idVendor == supported_fx2[j].
vid &&
393 des.idProduct == supported_fx2[j].
pid) {
394 prof = &supported_fx2[j];
407 ctx = fx2lafw_dev_new();
410 dev_insts = g_slist_append(dev_insts, sdi);
412 if (check_conf_profile(devlist[i])) {
414 sr_dbg(
"fx2lafw: Found an fx2lafw device.");
417 (libusb_get_bus_number(devlist[i]),
418 libusb_get_device_address(devlist[i]), NULL);
425 sr_err(
"fx2lafw: Firmware upload failed for "
426 "device %d.", devcnt);
428 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
433 libusb_free_device_list(devlist, 1);
438 static int hw_dev_open(
int dev_index)
443 int64_t timediff_us, timediff_ms;
456 sr_info(
"fx2lafw: Waiting for device to reset.");
458 g_usleep(300 * 1000);
461 if ((ret = fx2lafw_dev_open(dev_index)) ==
SR_OK)
463 g_usleep(100 * 1000);
465 timediff_us = g_get_monotonic_time() - ctx->
fw_updated;
466 timediff_ms = timediff_us / G_USEC_PER_SEC;
467 sr_spew(
"fx2lafw: timediff: %" PRIi64
" us.",
470 sr_info(
"fx2lafw: Device came back after %d ms.", timediff_ms);
472 ret = fx2lafw_dev_open(dev_index);
476 sr_err(
"fx2lafw: Unable to open device.");
483 sr_err(
"fx2lafw: Unable to claim interface: %d.", ret);
490 &supported_samplerates[0]) ==
SR_ERR)
497 static int hw_dev_close(
int dev_index)
502 sr_err(
"fx2lafw: %s: sdi was NULL.", __func__);
512 static int hw_cleanup(
void)
519 for (l = dev_insts; l; l = l->next) {
520 if (!(sdi = l->data)) {
522 sr_err(
"fx2lafw: %s: sdi was NULL, continuing.",
527 if (!(ctx = sdi->
priv)) {
529 sr_err(
"fx2lafw: %s: sdi->priv was NULL, continuing",
539 g_slist_free(dev_insts);
543 libusb_exit(usb_context);
549 static void *hw_dev_info_get(
int dev_index,
int dev_info_id)
558 switch (dev_info_id) {
576 static int hw_dev_status_get(
int dev_index)
587 static int *hw_hwcap_get_all(
void)
592 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value)
606 ret = configure_probes(ctx, (GSList *) value);
617 static int receive_data(
int fd,
int revents,
void *cb_data)
626 tv.tv_sec = tv.tv_usec = 0;
627 libusb_handle_events_timeout(usb_context, &tv);
632 static void abort_acquisition(
struct context *ctx)
637 static void finish_acquisition(
struct context *ctx)
647 const struct libusb_pollfd **
const lupfd =
648 libusb_get_pollfds(usb_context);
649 for (i = 0; lupfd[i]; i++)
654 static void receive_transfer(
struct libusb_transfer *transfer)
657 static int empty_transfer_count = 0;
660 struct context *ctx = transfer->user_data;
661 int cur_buflen, trigger_offset, i;
662 unsigned char *cur_buf, *new_buf;
670 libusb_free_transfer(transfer);
674 finish_acquisition(ctx);
679 sr_info(
"fx2lafw: receive_transfer(): status %d received %d bytes.",
680 transfer->status, transfer->actual_length);
683 cur_buf = transfer->buffer;
684 cur_buflen = transfer->actual_length;
687 if (!(new_buf = g_try_malloc(4096))) {
688 sr_err(
"fx2lafw: %s: new_buf malloc failed.", __func__);
692 transfer->buffer = new_buf;
693 transfer->length = 4096;
694 if (libusb_submit_transfer(transfer) != 0) {
697 sr_err(
"fx2lafw: %s: libusb_submit_transfer error.", __func__);
700 if (cur_buflen == 0) {
701 empty_transfer_count++;
707 abort_acquisition(ctx);
711 empty_transfer_count = 0;
716 for (i = 0; i < cur_buflen; i++) {
725 trigger_offset = i + 1;
732 packet.payload = NULL;
740 packet.payload = &logic;
772 packet.payload = &logic;
773 logic.length = cur_buflen - trigger_offset;
775 logic.data = cur_buf + trigger_offset;
782 abort_acquisition(ctx);
792 static int hw_dev_acquisition_start(
int dev_index,
void *cb_data)
798 struct libusb_transfer *transfer;
799 const struct libusb_pollfd **lupfd;
810 sr_err(
"fx2lafw: %s: packet malloc failed.", __func__);
815 sr_err(
"fx2lafw: %s: header malloc failed.", __func__);
822 if (!(buf = g_try_malloc(size))) {
823 sr_err(
"fx2lafw: %s: buf malloc failed.", __func__);
826 transfer = libusb_alloc_transfer(0);
827 libusb_fill_bulk_transfer(transfer, ctx->
usb->devhdl,
828 2 | LIBUSB_ENDPOINT_IN, buf, size,
829 receive_transfer, ctx, 40);
830 if (libusb_submit_transfer(transfer) != 0) {
832 libusb_free_transfer(transfer);
841 lupfd = libusb_get_pollfds(usb_context);
842 for (i = 0; lupfd[i]; i++)
844 40, receive_data, NULL);
866 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data)
876 abort_acquisition(sdi->
priv);
883 .longname =
"fx2lafw (generic driver for FX2 based LAs)",
886 .cleanup = hw_cleanup,
887 .dev_open = hw_dev_open,
888 .dev_close = hw_dev_close,
889 .dev_info_get = hw_dev_info_get,
890 .dev_status_get = hw_dev_status_get,
891 .hwcap_get_all = hw_hwcap_get_all,
892 .dev_config_set = hw_dev_config_set,
893 .dev_acquisition_start = hw_dev_acquisition_start,
894 .dev_acquisition_stop = hw_dev_acquisition_stop,