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/>.
28 #include "sigrok-internal.h"
32 static const struct fx2lafw_profile supported_fx2[] = {
35 * EE Electronics ESLA201A
37 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
38 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
43 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
44 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
48 * EE Electronics ESLA100
50 * Robomotic BugLogic 3
52 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
53 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
56 * Default Cypress FX2 without EEPROM, e.g.:
58 * Braintechnology USB Interface V2.x
60 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
61 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
64 * Braintechnology USB-LPS
66 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
67 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", 8 },
69 { 0, 0, 0, 0, 0, 0, 0 }
72 static int hwcaps[] = {
73 SR_HWCAP_LOGIC_ANALYZER,
76 /* These are really implemented in the driver, not the hardware. */
77 SR_HWCAP_LIMIT_SAMPLES,
83 * TODO: Different probe_names[] for each supported device.
85 static const char *probe_names[] = {
97 static uint64_t supported_samplerates[] = {
113 static struct sr_samplerates samplerates = {
117 supported_samplerates,
120 static GSList *dev_insts = NULL;
121 static libusb_context *usb_context = NULL;
123 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
124 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
127 * Check the USB configuration to determine if this is an fx2lafw device.
129 * @return TRUE if the device's configuration profile match fx2lafw
130 * configuration, FALSE otherwise.
132 static gboolean check_conf_profile(libusb_device *dev)
134 struct libusb_device_descriptor des;
135 struct libusb_device_handle *hdl;
137 unsigned char strdesc[64];
142 /* Assume the FW has not been loaded, unless proven wrong. */
143 if (libusb_get_device_descriptor(dev, &des) != 0)
146 if (libusb_open(dev, &hdl) != 0)
149 if (libusb_get_string_descriptor_ascii(hdl,
150 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
152 if (strncmp((const char *)strdesc, "sigrok", 6))
155 if (libusb_get_string_descriptor_ascii(hdl,
156 des.iProduct, strdesc, sizeof(strdesc)) < 0)
158 if (strncmp((const char *)strdesc, "fx2lafw", 7))
161 /* If we made it here, it must be an fx2lafw. */
170 static int fx2lafw_dev_open(int dev_index)
172 libusb_device **devlist;
173 struct libusb_device_descriptor des;
174 struct sr_dev_inst *sdi;
176 struct version_info vi;
179 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
183 if (sdi->status == SR_ST_ACTIVE)
188 const int device_count = libusb_get_device_list(usb_context, &devlist);
189 if (device_count < 0) {
190 sr_err("fx2lafw: Failed to retrieve device list (%d)",
195 for (i = 0; i < device_count; i++) {
196 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
197 sr_err("fx2lafw: Failed to get device descriptor: %d.",
202 if (des.idVendor != ctx->profile->vid
203 || des.idProduct != ctx->profile->pid)
206 if (sdi->status == SR_ST_INITIALIZING) {
207 if (skip != dev_index) {
208 /* Skip devices of this type that aren't the one we want. */
212 } else if (sdi->status == SR_ST_INACTIVE) {
214 * This device is fully enumerated, so we need to find
215 * this device by vendor, product, bus and address.
217 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
218 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
219 /* this is not the one */
223 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
224 if (ctx->usb->address == 0xff)
226 * first time we touch this device after firmware upload,
227 * so we don't know the address yet.
229 ctx->usb->address = libusb_get_device_address(devlist[i]);
231 sr_err("fx2lafw: Failed to open device: %d.", ret);
235 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
237 sr_err("fx2lafw: Failed to retrieve "
238 "firmware version information.");
242 if (vi.major != FX2LAFW_VERSION_MAJOR ||
243 vi.minor != FX2LAFW_VERSION_MINOR) {
244 sr_err("fx2lafw: Expected firmware version %d.%02d "
245 "got %d.%02d.", FX2LAFW_VERSION_MAJOR,
246 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
250 sdi->status = SR_ST_ACTIVE;
251 sr_info("fx2lafw: Opened device %d on %d.%d "
252 "interface %d, firmware version %d.%02d",
253 sdi->index, ctx->usb->bus, ctx->usb->address,
254 USB_INTERFACE, vi.major, vi.minor);
258 libusb_free_device_list(devlist, 1);
260 if (sdi->status != SR_ST_ACTIVE)
266 static void close_dev(struct sr_dev_inst *sdi)
272 if (ctx->usb->devhdl == NULL)
275 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
276 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
277 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
278 libusb_close(ctx->usb->devhdl);
279 ctx->usb->devhdl = NULL;
280 sdi->status = SR_ST_INACTIVE;
283 static int configure_probes(struct context *ctx, GSList *probes)
285 struct sr_probe *probe;
287 int probe_bit, stage, i;
290 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
291 ctx->trigger_mask[i] = 0;
292 ctx->trigger_value[i] = 0;
296 for (l = probes; l; l = l->next) {
297 probe = (struct sr_probe *)l->data;
298 if (probe->enabled == FALSE)
300 probe_bit = 1 << (probe->index - 1);
301 if (!(probe->trigger))
305 for (tc = probe->trigger; *tc; tc++) {
306 ctx->trigger_mask[stage] |= probe_bit;
308 ctx->trigger_value[stage] |= probe_bit;
310 if (stage > NUM_TRIGGER_STAGES)
317 * We didn't configure any triggers, make sure acquisition
318 * doesn't wait for any.
320 ctx->trigger_stage = TRIGGER_FIRED;
322 ctx->trigger_stage = 0;
327 static struct context *fx2lafw_dev_new(void)
331 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
332 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
336 ctx->trigger_stage = TRIGGER_FIRED;
345 static int hw_init(const char *devinfo)
347 struct sr_dev_inst *sdi;
348 struct libusb_device_descriptor des;
349 const struct fx2lafw_profile *prof;
351 libusb_device **devlist;
356 /* Avoid compiler warnings. */
359 if (libusb_init(&usb_context) != 0) {
360 sr_warn("fx2lafw: Failed to initialize libusb.");
364 /* Find all fx2lafw compatible devices and upload firware to them. */
365 libusb_get_device_list(usb_context, &devlist);
366 for (i = 0; devlist[i]; i++) {
368 if ((ret = libusb_get_device_descriptor(
369 devlist[i], &des)) != 0) {
370 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
375 for (j = 0; supported_fx2[j].vid; j++) {
376 if (des.idVendor == supported_fx2[j].vid &&
377 des.idProduct == supported_fx2[j].pid) {
378 prof = &supported_fx2[j];
382 /* Skip if the device was not found */
386 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
387 prof->vendor, prof->model, prof->model_version);
391 ctx = fx2lafw_dev_new();
394 dev_insts = g_slist_append(dev_insts, sdi);
396 if (check_conf_profile(devlist[i])) {
397 /* Already has the firmware, so fix the new address. */
398 sr_dbg("fx2lafw: Found an fx2lafw device.");
399 sdi->status = SR_ST_INACTIVE;
400 ctx->usb = sr_usb_dev_inst_new
401 (libusb_get_bus_number(devlist[i]),
402 libusb_get_device_address(devlist[i]), NULL);
404 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
405 prof->firmware) == SR_OK)
406 /* Remember when the firmware on this device was updated */
407 g_get_current_time(&ctx->fw_updated);
409 sr_err("fx2lafw: Firmware upload failed for "
410 "device %d.", devcnt);
411 ctx->usb = sr_usb_dev_inst_new
412 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
417 libusb_free_device_list(devlist, 1);
422 static int hw_dev_open(int dev_index)
425 struct sr_dev_inst *sdi;
429 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
434 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
435 * for the FX2 to renumerate.
438 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
439 sr_info("fx2lafw: Waiting for device to reset.");
440 /* takes at least 300ms for the FX2 to be gone from the USB bus */
441 g_usleep(300 * 1000);
443 while (timediff < MAX_RENUM_DELAY) {
444 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
446 g_usleep(100 * 1000);
447 g_get_current_time(&cur_time);
448 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
450 sr_info("fx2lafw: Device came back after %d ms.", timediff);
452 ret = fx2lafw_dev_open(dev_index);
456 sr_err("fx2lafw: Unable to open device.");
461 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
463 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
467 if (ctx->cur_samplerate == 0) {
468 /* Samplerate hasn't been set; default to the slowest one. */
469 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
470 &supported_samplerates[0]) == SR_ERR)
477 static int hw_dev_close(int dev_index)
479 struct sr_dev_inst *sdi;
481 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
482 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
492 static int hw_cleanup(void)
495 struct sr_dev_inst *sdi;
499 for (l = dev_insts; l; l = l->next) {
500 if (!(sdi = l->data)) {
501 /* Log error, but continue cleaning up the rest. */
502 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
507 if (!(ctx = sdi->priv)) {
508 /* Log error, but continue cleaning up the rest. */
509 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
516 sr_dev_inst_free(sdi);
519 g_slist_free(dev_insts);
523 libusb_exit(usb_context);
529 static void *hw_dev_info_get(int dev_index, int dev_info_id)
531 struct sr_dev_inst *sdi;
534 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
538 switch (dev_info_id) {
541 case SR_DI_NUM_PROBES:
542 return GINT_TO_POINTER(ctx->profile->num_probes);
543 case SR_DI_PROBE_NAMES:
545 case SR_DI_SAMPLERATES:
547 case SR_DI_TRIGGER_TYPES:
548 return TRIGGER_TYPES;
549 case SR_DI_CUR_SAMPLERATE:
550 return &ctx->cur_samplerate;
556 static int hw_dev_status_get(int dev_index)
558 const struct sr_dev_inst *const sdi =
559 sr_dev_inst_get(dev_insts, dev_index);
562 return SR_ST_NOT_FOUND;
567 static int *hw_hwcap_get_all(void)
572 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
574 struct sr_dev_inst *sdi;
578 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
582 if (hwcap == SR_HWCAP_SAMPLERATE) {
583 ctx->cur_samplerate = *(uint64_t *)value;
585 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
586 ret = configure_probes(ctx, (GSList *) value);
587 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
588 ctx->limit_samples = *(uint64_t *)value;
597 static int receive_data(int fd, int revents, void *cb_data)
601 /* Avoid compiler warnings. */
606 tv.tv_sec = tv.tv_usec = 0;
607 libusb_handle_events_timeout(usb_context, &tv);
612 static void abort_acquisition(struct context *ctx)
614 ctx->num_samples = -1;
617 static void finish_acquisition(struct context *ctx)
619 struct sr_datafeed_packet packet;
622 /* Terminate session */
623 packet.type = SR_DF_END;
624 sr_session_send(ctx->session_dev_id, &packet);
626 /* Remove fds from polling */
627 const struct libusb_pollfd **const lupfd =
628 libusb_get_pollfds(usb_context);
629 for (i = 0; lupfd[i]; i++)
630 sr_source_remove(lupfd[i]->fd);
631 free(lupfd); /* NOT g_free()! */
634 static void receive_transfer(struct libusb_transfer *transfer)
636 /* TODO: These statics have to move to the ctx struct. */
637 static int empty_transfer_count = 0;
638 struct sr_datafeed_packet packet;
639 struct sr_datafeed_logic logic;
640 struct context *ctx = transfer->user_data;
641 int cur_buflen, trigger_offset, i;
642 unsigned char *cur_buf, *new_buf;
645 * If acquisition has already ended, just free any queued up
646 * transfer that come in.
648 if (ctx->num_samples == -1) {
650 libusb_free_transfer(transfer);
652 ctx->submitted_transfers--;
653 if (ctx->submitted_transfers == 0)
654 finish_acquisition(ctx);
659 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
660 transfer->status, transfer->actual_length);
662 /* Save incoming transfer before reusing the transfer struct. */
663 cur_buf = transfer->buffer;
664 cur_buflen = transfer->actual_length;
666 /* Fire off a new request. */
667 if (!(new_buf = g_try_malloc(4096))) {
668 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
669 return; /* TODO: SR_ERR_MALLOC */
672 transfer->buffer = new_buf;
673 transfer->length = 4096;
674 if (libusb_submit_transfer(transfer) != 0) {
675 /* TODO: Stop session? */
676 /* TODO: Better error message. */
677 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
680 if (cur_buflen == 0) {
681 empty_transfer_count++;
682 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
684 * The FX2 gave up. End the acquisition, the frontend
685 * will work out that the samplecount is short.
687 abort_acquisition(ctx);
691 empty_transfer_count = 0;
695 if (ctx->trigger_stage >= 0) {
696 for (i = 0; i < cur_buflen; i++) {
698 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
699 /* Match on this trigger stage. */
700 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
701 ctx->trigger_stage++;
703 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
704 /* Match on all trigger stages, we're done. */
705 trigger_offset = i + 1;
708 * TODO: Send pre-trigger buffer to session bus.
709 * Tell the frontend we hit the trigger here.
711 packet.type = SR_DF_TRIGGER;
712 packet.payload = NULL;
713 sr_session_send(ctx->session_dev_id, &packet);
716 * Send the samples that triggered it, since we're
717 * skipping past them.
719 packet.type = SR_DF_LOGIC;
720 packet.payload = &logic;
721 logic.length = ctx->trigger_stage;
723 logic.data = ctx->trigger_buffer;
724 sr_session_send(ctx->session_dev_id, &packet);
726 ctx->trigger_stage = TRIGGER_FIRED;
733 * We had a match before, but not in the next sample. However, we may
734 * have a match on this stage in the next bit -- trigger on 0001 will
735 * fail on seeing 00001, so we need to go back to stage 0 -- but at
736 * the next sample from the one that matched originally, which the
737 * counter increment at the end of the loop takes care of.
739 if (ctx->trigger_stage > 0) {
740 i -= ctx->trigger_stage;
742 i = -1; /* Oops, went back past this buffer. */
743 /* Reset trigger stage. */
744 ctx->trigger_stage = 0;
749 if (ctx->trigger_stage == TRIGGER_FIRED) {
750 /* Send the incoming transfer to the session bus. */
751 packet.type = SR_DF_LOGIC;
752 packet.payload = &logic;
753 logic.length = cur_buflen - trigger_offset;
755 logic.data = cur_buf + trigger_offset;
756 sr_session_send(ctx->session_dev_id, &packet);
759 ctx->num_samples += cur_buflen;
760 if (ctx->limit_samples &&
761 (unsigned int)ctx->num_samples > ctx->limit_samples) {
762 abort_acquisition(ctx);
766 * TODO: Buffer pre-trigger data in capture
767 * ratio-sized buffer.
772 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
774 struct sr_dev_inst *sdi;
775 struct sr_datafeed_packet *packet;
776 struct sr_datafeed_header *header;
778 struct libusb_transfer *transfer;
779 const struct libusb_pollfd **lupfd;
783 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
786 ctx->session_dev_id = cb_data;
787 ctx->num_samples = 0;
789 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
790 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
791 return SR_ERR_MALLOC;
794 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
795 sr_err("fx2lafw: %s: header malloc failed.", __func__);
796 return SR_ERR_MALLOC;
799 /* Start with 2K transfer, subsequently increased to 4K. */
801 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
802 if (!(buf = g_try_malloc(size))) {
803 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
804 return SR_ERR_MALLOC;
806 transfer = libusb_alloc_transfer(0);
807 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
808 2 | LIBUSB_ENDPOINT_IN, buf, size,
809 receive_transfer, ctx, 40);
810 if (libusb_submit_transfer(transfer) != 0) {
811 /* TODO: Free them all. */
812 libusb_free_transfer(transfer);
817 ctx->submitted_transfers++;
821 lupfd = libusb_get_pollfds(usb_context);
822 for (i = 0; lupfd[i]; i++)
823 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
824 40, receive_data, NULL);
825 free(lupfd); /* NOT g_free()! */
827 packet->type = SR_DF_HEADER;
828 packet->payload = header;
829 header->feed_version = 1;
830 gettimeofday(&header->starttime, NULL);
831 header->samplerate = ctx->cur_samplerate;
832 header->num_logic_probes = ctx->profile->num_probes;
833 sr_session_send(cb_data, packet);
837 if ((ret = command_start_acquisition (ctx->usb->devhdl,
838 ctx->cur_samplerate)) != SR_OK) {
845 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
846 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
848 struct sr_dev_inst *sdi;
850 /* Avoid compiler warnings. */
853 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
856 abort_acquisition(sdi->priv);
861 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
863 .longname = "fx2lafw (generic driver for FX2 based LAs)",
866 .cleanup = hw_cleanup,
867 .dev_open = hw_dev_open,
868 .dev_close = hw_dev_close,
869 .dev_info_get = hw_dev_info_get,
870 .dev_status_get = hw_dev_status_get,
871 .hwcap_get_all = hw_hwcap_get_all,
872 .dev_config_set = hw_dev_config_set,
873 .dev_acquisition_start = hw_dev_acquisition_start,
874 .dev_acquisition_stop = hw_dev_acquisition_stop,