2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "sigrok-internal.h"
31 static const struct fx2lafw_profile supported_fx2[] = {
34 * EE Electronics ESLA201A
36 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
37 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
42 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
43 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
47 * EE Electronics ESLA100
49 * Robomotic BugLogic 3
51 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
52 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
55 * Default Cypress FX2 without EEPROM, e.g.:
57 * Braintechnology USB Interface V2.x
59 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
60 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
63 * Braintechnology USB-LPS
65 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
66 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", 8 },
68 { 0, 0, 0, 0, 0, 0, 0 }
71 static int hwcaps[] = {
72 SR_HWCAP_LOGIC_ANALYZER,
75 /* These are really implemented in the driver, not the hardware. */
76 SR_HWCAP_LIMIT_SAMPLES,
82 * TODO: Different probe_names[] for each supported device.
84 static const char *probe_names[] = {
96 static uint64_t supported_samplerates[] = {
114 static struct sr_samplerates samplerates = {
118 supported_samplerates,
121 static GSList *dev_insts = NULL;
122 static libusb_context *usb_context = NULL;
124 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
125 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
128 * Check the USB configuration to determine if this is an fx2lafw device.
130 * @return TRUE if the device's configuration profile match fx2lafw
131 * configuration, FALSE otherwise.
133 static gboolean check_conf_profile(libusb_device *dev)
135 struct libusb_device_descriptor des;
136 struct libusb_device_handle *hdl;
138 unsigned char strdesc[64];
143 /* Assume the FW has not been loaded, unless proven wrong. */
144 if (libusb_get_device_descriptor(dev, &des) != 0)
147 if (libusb_open(dev, &hdl) != 0)
150 if (libusb_get_string_descriptor_ascii(hdl,
151 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
153 if (strncmp((const char *)strdesc, "sigrok", 6))
156 if (libusb_get_string_descriptor_ascii(hdl,
157 des.iProduct, strdesc, sizeof(strdesc)) < 0)
159 if (strncmp((const char *)strdesc, "fx2lafw", 7))
162 /* If we made it here, it must be an fx2lafw. */
171 static int fx2lafw_dev_open(int dev_index)
173 libusb_device **devlist;
174 struct libusb_device_descriptor des;
175 struct sr_dev_inst *sdi;
177 struct version_info vi;
181 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
185 if (sdi->status == SR_ST_ACTIVE)
190 const int device_count = libusb_get_device_list(usb_context, &devlist);
191 if (device_count < 0) {
192 sr_err("fx2lafw: Failed to retrieve device list (%d)",
197 for (i = 0; i < device_count; i++) {
198 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
199 sr_err("fx2lafw: Failed to get device descriptor: %d.",
204 if (des.idVendor != ctx->profile->vid
205 || des.idProduct != ctx->profile->pid)
208 if (sdi->status == SR_ST_INITIALIZING) {
209 if (skip != dev_index) {
210 /* Skip devices of this type that aren't the one we want. */
214 } else if (sdi->status == SR_ST_INACTIVE) {
216 * This device is fully enumerated, so we need to find
217 * this device by vendor, product, bus and address.
219 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
220 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
221 /* this is not the one */
225 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
226 if (ctx->usb->address == 0xff)
228 * first time we touch this device after firmware upload,
229 * so we don't know the address yet.
231 ctx->usb->address = libusb_get_device_address(devlist[i]);
233 sr_err("fx2lafw: Failed to open device: %d.", ret);
237 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
239 sr_err("fx2lafw: Failed to retrieve "
240 "firmware version information.");
244 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
246 sr_err("fx2lafw: Failed to retrieve REVID.");
250 if (vi.major != FX2LAFW_VERSION_MAJOR ||
251 vi.minor != FX2LAFW_VERSION_MINOR) {
252 sr_err("fx2lafw: Expected firmware version %d.%d "
253 "got %d.%d.", FX2LAFW_VERSION_MAJOR,
254 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
258 sdi->status = SR_ST_ACTIVE;
259 sr_info("fx2lafw: Opened device %d on %d.%d "
260 "interface %d, firmware %d.%d, REVID %d.",
261 sdi->index, ctx->usb->bus, ctx->usb->address,
262 USB_INTERFACE, vi.major, vi.minor, revid);
266 libusb_free_device_list(devlist, 1);
268 if (sdi->status != SR_ST_ACTIVE)
274 static void close_dev(struct sr_dev_inst *sdi)
280 if (ctx->usb->devhdl == NULL)
283 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
284 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
285 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
286 libusb_close(ctx->usb->devhdl);
287 ctx->usb->devhdl = NULL;
288 sdi->status = SR_ST_INACTIVE;
291 static int configure_probes(struct context *ctx, GSList *probes)
293 struct sr_probe *probe;
295 int probe_bit, stage, i;
298 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
299 ctx->trigger_mask[i] = 0;
300 ctx->trigger_value[i] = 0;
304 for (l = probes; l; l = l->next) {
305 probe = (struct sr_probe *)l->data;
306 if (probe->enabled == FALSE)
308 probe_bit = 1 << (probe->index - 1);
309 if (!(probe->trigger))
313 for (tc = probe->trigger; *tc; tc++) {
314 ctx->trigger_mask[stage] |= probe_bit;
316 ctx->trigger_value[stage] |= probe_bit;
318 if (stage > NUM_TRIGGER_STAGES)
325 * We didn't configure any triggers, make sure acquisition
326 * doesn't wait for any.
328 ctx->trigger_stage = TRIGGER_FIRED;
330 ctx->trigger_stage = 0;
335 static struct context *fx2lafw_dev_new(void)
339 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
340 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
344 ctx->trigger_stage = TRIGGER_FIRED;
353 static int hw_init(const char *devinfo)
355 struct sr_dev_inst *sdi;
356 struct libusb_device_descriptor des;
357 const struct fx2lafw_profile *prof;
359 libusb_device **devlist;
364 /* Avoid compiler warnings. */
367 if (libusb_init(&usb_context) != 0) {
368 sr_warn("fx2lafw: Failed to initialize libusb.");
372 /* Find all fx2lafw compatible devices and upload firware to them. */
373 libusb_get_device_list(usb_context, &devlist);
374 for (i = 0; devlist[i]; i++) {
376 if ((ret = libusb_get_device_descriptor(
377 devlist[i], &des)) != 0) {
378 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
383 for (j = 0; supported_fx2[j].vid; j++) {
384 if (des.idVendor == supported_fx2[j].vid &&
385 des.idProduct == supported_fx2[j].pid) {
386 prof = &supported_fx2[j];
390 /* Skip if the device was not found */
394 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
395 prof->vendor, prof->model, prof->model_version);
399 ctx = fx2lafw_dev_new();
402 dev_insts = g_slist_append(dev_insts, sdi);
404 if (check_conf_profile(devlist[i])) {
405 /* Already has the firmware, so fix the new address. */
406 sr_dbg("fx2lafw: Found an fx2lafw device.");
407 sdi->status = SR_ST_INACTIVE;
408 ctx->usb = sr_usb_dev_inst_new
409 (libusb_get_bus_number(devlist[i]),
410 libusb_get_device_address(devlist[i]), NULL);
412 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
413 prof->firmware) == SR_OK)
414 /* Remember when the firmware on this device was updated */
415 g_get_current_time(&ctx->fw_updated);
417 sr_err("fx2lafw: Firmware upload failed for "
418 "device %d.", devcnt);
419 ctx->usb = sr_usb_dev_inst_new
420 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
425 libusb_free_device_list(devlist, 1);
430 static int hw_dev_open(int dev_index)
433 struct sr_dev_inst *sdi;
437 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
442 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
443 * for the FX2 to renumerate.
446 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
447 sr_info("fx2lafw: Waiting for device to reset.");
448 /* takes at least 300ms for the FX2 to be gone from the USB bus */
449 g_usleep(300 * 1000);
451 while (timediff < MAX_RENUM_DELAY) {
452 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
454 g_usleep(100 * 1000);
455 g_get_current_time(&cur_time);
456 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
458 sr_info("fx2lafw: Device came back after %d ms.", timediff);
460 ret = fx2lafw_dev_open(dev_index);
464 sr_err("fx2lafw: Unable to open device.");
469 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
471 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
475 if (ctx->cur_samplerate == 0) {
476 /* Samplerate hasn't been set; default to the slowest one. */
477 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
478 &supported_samplerates[0]) == SR_ERR)
485 static int hw_dev_close(int dev_index)
487 struct sr_dev_inst *sdi;
489 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
490 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
500 static int hw_cleanup(void)
503 struct sr_dev_inst *sdi;
507 for (l = dev_insts; l; l = l->next) {
508 if (!(sdi = l->data)) {
509 /* Log error, but continue cleaning up the rest. */
510 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
515 if (!(ctx = sdi->priv)) {
516 /* Log error, but continue cleaning up the rest. */
517 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
524 sr_dev_inst_free(sdi);
527 g_slist_free(dev_insts);
531 libusb_exit(usb_context);
537 static void *hw_dev_info_get(int dev_index, int dev_info_id)
539 struct sr_dev_inst *sdi;
542 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
546 switch (dev_info_id) {
549 case SR_DI_NUM_PROBES:
550 return GINT_TO_POINTER(ctx->profile->num_probes);
551 case SR_DI_PROBE_NAMES:
553 case SR_DI_SAMPLERATES:
555 case SR_DI_TRIGGER_TYPES:
556 return TRIGGER_TYPES;
557 case SR_DI_CUR_SAMPLERATE:
558 return &ctx->cur_samplerate;
564 static int hw_dev_status_get(int dev_index)
566 const struct sr_dev_inst *const sdi =
567 sr_dev_inst_get(dev_insts, dev_index);
570 return SR_ST_NOT_FOUND;
575 static int *hw_hwcap_get_all(void)
580 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
582 struct sr_dev_inst *sdi;
586 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
590 if (hwcap == SR_HWCAP_SAMPLERATE) {
591 ctx->cur_samplerate = *(uint64_t *)value;
593 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
594 ret = configure_probes(ctx, (GSList *) value);
595 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
596 ctx->limit_samples = *(uint64_t *)value;
605 static int receive_data(int fd, int revents, void *cb_data)
609 /* Avoid compiler warnings. */
614 tv.tv_sec = tv.tv_usec = 0;
615 libusb_handle_events_timeout(usb_context, &tv);
620 static void abort_acquisition(struct context *ctx)
622 ctx->num_samples = -1;
625 static void finish_acquisition(struct context *ctx)
627 struct sr_datafeed_packet packet;
630 /* Terminate session */
631 packet.type = SR_DF_END;
632 sr_session_send(ctx->session_dev_id, &packet);
634 /* Remove fds from polling */
635 const struct libusb_pollfd **const lupfd =
636 libusb_get_pollfds(usb_context);
637 for (i = 0; lupfd[i]; i++)
638 sr_source_remove(lupfd[i]->fd);
639 free(lupfd); /* NOT g_free()! */
642 static void receive_transfer(struct libusb_transfer *transfer)
644 /* TODO: These statics have to move to the ctx struct. */
645 static int empty_transfer_count = 0;
646 struct sr_datafeed_packet packet;
647 struct sr_datafeed_logic logic;
648 struct context *ctx = transfer->user_data;
649 int cur_buflen, trigger_offset, i;
650 unsigned char *cur_buf, *new_buf;
653 * If acquisition has already ended, just free any queued up
654 * transfer that come in.
656 if (ctx->num_samples == -1) {
658 libusb_free_transfer(transfer);
660 ctx->submitted_transfers--;
661 if (ctx->submitted_transfers == 0)
662 finish_acquisition(ctx);
667 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
668 transfer->status, transfer->actual_length);
670 /* Save incoming transfer before reusing the transfer struct. */
671 cur_buf = transfer->buffer;
672 cur_buflen = transfer->actual_length;
674 /* Fire off a new request. */
675 if (!(new_buf = g_try_malloc(4096))) {
676 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
677 return; /* TODO: SR_ERR_MALLOC */
680 transfer->buffer = new_buf;
681 transfer->length = 4096;
682 if (libusb_submit_transfer(transfer) != 0) {
683 /* TODO: Stop session? */
684 /* TODO: Better error message. */
685 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
688 if (cur_buflen == 0) {
689 empty_transfer_count++;
690 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
692 * The FX2 gave up. End the acquisition, the frontend
693 * will work out that the samplecount is short.
695 abort_acquisition(ctx);
699 empty_transfer_count = 0;
703 if (ctx->trigger_stage >= 0) {
704 for (i = 0; i < cur_buflen; i++) {
706 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
707 /* Match on this trigger stage. */
708 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
709 ctx->trigger_stage++;
711 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
712 /* Match on all trigger stages, we're done. */
713 trigger_offset = i + 1;
716 * TODO: Send pre-trigger buffer to session bus.
717 * Tell the frontend we hit the trigger here.
719 packet.type = SR_DF_TRIGGER;
720 packet.payload = NULL;
721 sr_session_send(ctx->session_dev_id, &packet);
724 * Send the samples that triggered it, since we're
725 * skipping past them.
727 packet.type = SR_DF_LOGIC;
728 packet.payload = &logic;
729 logic.length = ctx->trigger_stage;
731 logic.data = ctx->trigger_buffer;
732 sr_session_send(ctx->session_dev_id, &packet);
734 ctx->trigger_stage = TRIGGER_FIRED;
741 * We had a match before, but not in the next sample. However, we may
742 * have a match on this stage in the next bit -- trigger on 0001 will
743 * fail on seeing 00001, so we need to go back to stage 0 -- but at
744 * the next sample from the one that matched originally, which the
745 * counter increment at the end of the loop takes care of.
747 if (ctx->trigger_stage > 0) {
748 i -= ctx->trigger_stage;
750 i = -1; /* Oops, went back past this buffer. */
751 /* Reset trigger stage. */
752 ctx->trigger_stage = 0;
757 if (ctx->trigger_stage == TRIGGER_FIRED) {
758 /* Send the incoming transfer to the session bus. */
759 packet.type = SR_DF_LOGIC;
760 packet.payload = &logic;
761 logic.length = cur_buflen - trigger_offset;
763 logic.data = cur_buf + trigger_offset;
764 sr_session_send(ctx->session_dev_id, &packet);
767 ctx->num_samples += cur_buflen;
768 if (ctx->limit_samples &&
769 (unsigned int)ctx->num_samples > ctx->limit_samples) {
770 abort_acquisition(ctx);
774 * TODO: Buffer pre-trigger data in capture
775 * ratio-sized buffer.
780 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
782 struct sr_dev_inst *sdi;
783 struct sr_datafeed_packet *packet;
784 struct sr_datafeed_header *header;
786 struct libusb_transfer *transfer;
787 const struct libusb_pollfd **lupfd;
791 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
794 ctx->session_dev_id = cb_data;
795 ctx->num_samples = 0;
797 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
798 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
799 return SR_ERR_MALLOC;
802 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
803 sr_err("fx2lafw: %s: header malloc failed.", __func__);
804 return SR_ERR_MALLOC;
807 /* Start with 2K transfer, subsequently increased to 4K. */
809 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
810 if (!(buf = g_try_malloc(size))) {
811 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
812 return SR_ERR_MALLOC;
814 transfer = libusb_alloc_transfer(0);
815 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
816 2 | LIBUSB_ENDPOINT_IN, buf, size,
817 receive_transfer, ctx, 40);
818 if (libusb_submit_transfer(transfer) != 0) {
819 /* TODO: Free them all. */
820 libusb_free_transfer(transfer);
825 ctx->submitted_transfers++;
829 lupfd = libusb_get_pollfds(usb_context);
830 for (i = 0; lupfd[i]; i++)
831 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
832 40, receive_data, NULL);
833 free(lupfd); /* NOT g_free()! */
835 packet->type = SR_DF_HEADER;
836 packet->payload = header;
837 header->feed_version = 1;
838 gettimeofday(&header->starttime, NULL);
839 header->samplerate = ctx->cur_samplerate;
840 header->num_logic_probes = ctx->profile->num_probes;
841 sr_session_send(cb_data, packet);
845 if ((ret = command_start_acquisition (ctx->usb->devhdl,
846 ctx->cur_samplerate)) != SR_OK) {
853 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
854 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
856 struct sr_dev_inst *sdi;
858 /* Avoid compiler warnings. */
861 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
864 abort_acquisition(sdi->priv);
869 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
871 .longname = "fx2lafw (generic driver for FX2 based LAs)",
874 .cleanup = hw_cleanup,
875 .dev_open = hw_dev_open,
876 .dev_close = hw_dev_close,
877 .dev_info_get = hw_dev_info_get,
878 .dev_status_get = hw_dev_status_get,
879 .hwcap_get_all = hw_hwcap_get_all,
880 .dev_config_set = hw_dev_config_set,
881 .dev_acquisition_start = hw_dev_acquisition_start,
882 .dev_acquisition_stop = hw_dev_acquisition_stop,