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
50 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
51 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
53 { 0, 0, 0, 0, 0, 0, 0 }
56 static int hwcaps[] = {
57 SR_HWCAP_LOGIC_ANALYZER,
60 /* These are really implemented in the driver, not the hardware. */
61 SR_HWCAP_LIMIT_SAMPLES,
67 * TODO: Different probe_names[] for each supported device.
69 static const char *probe_names[] = {
81 static uint64_t supported_samplerates[] = {
93 static struct sr_samplerates samplerates = {
97 supported_samplerates,
100 static GSList *dev_insts = NULL;
101 static libusb_context *usb_context = NULL;
103 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
104 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
107 * Check the USB configuration to determine if this is an fx2lafw device.
109 * @return TRUE if the device's configuration profile match fx2lafw
110 * configuration, FALSE otherwise.
112 static gboolean check_conf_profile(libusb_device *dev)
114 struct libusb_device_descriptor des;
115 struct libusb_config_descriptor *conf_dsc = NULL;
116 const struct libusb_interface_descriptor *intf_dsc;
117 gboolean ret = FALSE;
120 /* Assume the FW has not been loaded, unless proven wrong. */
123 if (libusb_get_device_descriptor(dev, &des) != 0)
126 /* Need exactly 1 configuration. */
127 if (des.bNumConfigurations != 1)
130 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
133 /* Need exactly 1 interface. */
134 if (conf_dsc->bNumInterfaces != 1)
137 /* Need just one alternate setting. */
138 if (conf_dsc->interface[0].num_altsetting != 1)
141 /* Need exactly 2 endpoints. */
142 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
143 if (intf_dsc->bNumEndpoints != 2)
146 /* The first endpoint should be 2 (inbound). */
147 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
148 (2 | LIBUSB_ENDPOINT_IN))
151 /* If we made it here, it must be an fx2lafw. */
156 libusb_free_config_descriptor(conf_dsc);
161 static int fx2lafw_dev_open(int dev_index)
163 libusb_device **devlist;
164 struct libusb_device_descriptor des;
165 struct sr_dev_inst *sdi;
167 struct version_info vi;
170 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
174 if (sdi->status == SR_ST_ACTIVE)
179 const int device_count = libusb_get_device_list(usb_context, &devlist);
180 if (device_count < 0) {
181 sr_err("fx2lafw: Failed to retrieve device list (%d)",
186 for (i = 0; i < device_count; i++) {
187 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
188 sr_err("fx2lafw: Failed to get device descriptor: %d.",
193 if (des.idVendor != ctx->profile->vid
194 || des.idProduct != ctx->profile->pid)
197 if (sdi->status == SR_ST_INITIALIZING) {
198 if (skip != dev_index) {
199 /* Skip devices of this type that aren't the one we want. */
203 } else if (sdi->status == SR_ST_INACTIVE) {
205 * This device is fully enumerated, so we need to find
206 * this device by vendor, product, bus and address.
208 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
209 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
210 /* this is not the one */
214 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
215 if (ctx->usb->address == 0xff)
217 * first time we touch this device after firmware upload,
218 * so we don't know the address yet.
220 ctx->usb->address = libusb_get_device_address(devlist[i]);
222 sr_err("fx2lafw: Failed to open device: %d.", ret);
226 if((ret = command_get_fw_version(ctx->usb->devhdl, &vi)) != SR_OK) {
227 sr_err("fx2lafw: Failed to retrieve "
228 "firmware version information");
232 if(vi.major != FX2LAFW_VERSION_MAJOR ||
233 vi.minor != FX2LAFW_VERSION_MINOR) {
234 sr_err("fx2lafw: Expected firmware version %d.%02d "
235 "got %d.%02d", FX2LAFW_VERSION_MAJOR,
236 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
240 sdi->status = SR_ST_ACTIVE;
241 sr_info("fx2lafw: Opened device %d on %d.%d "
242 "interface %d, firmware version %d.%02d",
243 sdi->index, ctx->usb->bus, ctx->usb->address,
244 USB_INTERFACE, vi.major, vi.minor);
248 libusb_free_device_list(devlist, 1);
250 if (sdi->status != SR_ST_ACTIVE)
256 static void close_dev(struct sr_dev_inst *sdi)
262 if (ctx->usb->devhdl == NULL)
265 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
266 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
267 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
268 libusb_close(ctx->usb->devhdl);
269 ctx->usb->devhdl = NULL;
270 sdi->status = SR_ST_INACTIVE;
273 static int configure_probes(struct context *ctx, GSList *probes)
275 struct sr_probe *probe;
277 int probe_bit, stage, i;
280 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
281 ctx->trigger_mask[i] = 0;
282 ctx->trigger_value[i] = 0;
286 for (l = probes; l; l = l->next) {
287 probe = (struct sr_probe *)l->data;
288 if (probe->enabled == FALSE)
290 probe_bit = 1 << (probe->index - 1);
291 if (!(probe->trigger))
295 for (tc = probe->trigger; *tc; tc++) {
296 ctx->trigger_mask[stage] |= probe_bit;
298 ctx->trigger_value[stage] |= probe_bit;
300 if (stage > NUM_TRIGGER_STAGES)
307 * We didn't configure any triggers, make sure acquisition
308 * doesn't wait for any.
310 ctx->trigger_stage = TRIGGER_FIRED;
312 ctx->trigger_stage = 0;
317 static struct context *fx2lafw_dev_new(void)
321 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
322 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
326 ctx->trigger_stage = TRIGGER_FIRED;
335 static int hw_init(const char *devinfo)
337 struct sr_dev_inst *sdi;
338 struct libusb_device_descriptor des;
339 const struct fx2lafw_profile *prof;
341 libusb_device **devlist;
346 /* Avoid compiler warnings. */
349 if (libusb_init(&usb_context) != 0) {
350 sr_warn("fx2lafw: Failed to initialize libusb.");
354 /* Find all fx2lafw compatible devices and upload firware to them. */
355 libusb_get_device_list(usb_context, &devlist);
356 for (i = 0; devlist[i]; i++) {
358 if ((ret = libusb_get_device_descriptor(
359 devlist[i], &des)) != 0) {
360 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
365 for (j = 0; supported_fx2[j].vid; j++) {
366 if (des.idVendor == supported_fx2[j].vid &&
367 des.idProduct == supported_fx2[j].pid) {
368 prof = &supported_fx2[j];
372 /* Skip if the device was not found */
376 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
377 prof->vendor, prof->model, prof->model_version);
381 ctx = fx2lafw_dev_new();
384 dev_insts = g_slist_append(dev_insts, sdi);
386 if (check_conf_profile(devlist[i])) {
387 /* Already has the firmware, so fix the new address. */
388 sr_dbg("fx2lafw: Found an fx2lafw device.");
389 sdi->status = SR_ST_INACTIVE;
390 ctx->usb = sr_usb_dev_inst_new
391 (libusb_get_bus_number(devlist[i]),
392 libusb_get_device_address(devlist[i]), NULL);
394 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
395 prof->firmware) == SR_OK)
396 /* Remember when the firmware on this device was updated */
397 g_get_current_time(&ctx->fw_updated);
399 sr_err("fx2lafw: Firmware upload failed for "
400 "device %d.", devcnt);
401 ctx->usb = sr_usb_dev_inst_new
402 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
407 libusb_free_device_list(devlist, 1);
412 static int hw_dev_open(int dev_index)
415 struct sr_dev_inst *sdi;
419 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
424 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
425 * for the FX2 to renumerate
428 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
429 sr_info("fx2lafw: Waiting for device to reset.");
430 /* takes at least 300ms for the FX2 to be gone from the USB bus */
431 g_usleep(300 * 1000);
433 while (timediff < MAX_RENUM_DELAY) {
434 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
436 g_usleep(100 * 1000);
437 g_get_current_time(&cur_time);
438 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
440 sr_info("fx2lafw: Device came back after %d ms.", timediff);
442 ret = fx2lafw_dev_open(dev_index);
446 sr_err("fx2lafw: Unable to open device.");
451 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
453 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
457 if (ctx->cur_samplerate == 0) {
458 /* Samplerate hasn't been set; default to the slowest one. */
459 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
460 &supported_samplerates[0]) == SR_ERR)
467 static int hw_dev_close(int dev_index)
469 struct sr_dev_inst *sdi;
471 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
472 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
482 static int hw_cleanup(void)
485 struct sr_dev_inst *sdi;
489 for (l = dev_insts; l; l = l->next) {
490 if (!(sdi = l->data)) {
491 /* Log error, but continue cleaning up the rest. */
492 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
497 if (!(ctx = sdi->priv)) {
498 /* Log error, but continue cleaning up the rest. */
499 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
506 sr_dev_inst_free(sdi);
509 g_slist_free(dev_insts);
513 libusb_exit(usb_context);
519 static void *hw_dev_info_get(int dev_index, int dev_info_id)
521 struct sr_dev_inst *sdi;
524 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
528 switch (dev_info_id) {
531 case SR_DI_NUM_PROBES:
532 return GINT_TO_POINTER(ctx->profile->num_probes);
533 case SR_DI_PROBE_NAMES:
535 case SR_DI_SAMPLERATES:
537 case SR_DI_TRIGGER_TYPES:
538 return TRIGGER_TYPES;
539 case SR_DI_CUR_SAMPLERATE:
540 return &ctx->cur_samplerate;
546 static int hw_dev_status_get(int dev_index)
548 const struct sr_dev_inst *const sdi =
549 sr_dev_inst_get(dev_insts, dev_index);
552 return SR_ST_NOT_FOUND;
557 static int *hw_hwcap_get_all(void)
562 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
564 struct sr_dev_inst *sdi;
568 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
572 if (hwcap == SR_HWCAP_SAMPLERATE) {
573 ctx->cur_samplerate = *(uint64_t *)value;
575 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
576 ret = configure_probes(ctx, (GSList *) value);
577 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
578 ctx->limit_samples = *(uint64_t *)value;
587 static int receive_data(int fd, int revents, void *cb_data)
591 /* Avoid compiler warnings. */
596 tv.tv_sec = tv.tv_usec = 0;
597 libusb_handle_events_timeout(usb_context, &tv);
602 static void abort_acquisition(struct context *ctx)
604 ctx->num_samples = -1;
607 static void finish_acquisition(struct context *ctx)
609 struct sr_datafeed_packet packet;
612 /* Terminate session */
613 packet.type = SR_DF_END;
614 sr_session_send(ctx->session_dev_id, &packet);
616 /* Remove fds from polling */
617 const struct libusb_pollfd **const lupfd =
618 libusb_get_pollfds(usb_context);
619 for (i = 0; lupfd[i]; i++)
620 sr_source_remove(lupfd[i]->fd);
621 free(lupfd); /* NOT g_free()! */
624 static void receive_transfer(struct libusb_transfer *transfer)
626 /* TODO: These statics have to move to the ctx struct. */
627 static int empty_transfer_count = 0;
628 struct sr_datafeed_packet packet;
629 struct sr_datafeed_logic logic;
630 struct context *ctx = transfer->user_data;
631 int cur_buflen, trigger_offset, i;
632 unsigned char *cur_buf, *new_buf;
635 * If acquisition has already ended, just free any queued up
636 * transfer that come in.
638 if (ctx->num_samples == -1) {
640 libusb_free_transfer(transfer);
642 ctx->submitted_transfers--;
643 if (ctx->submitted_transfers == 0)
644 finish_acquisition(ctx);
649 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
650 transfer->status, transfer->actual_length);
652 /* Save incoming transfer before reusing the transfer struct. */
653 cur_buf = transfer->buffer;
654 cur_buflen = transfer->actual_length;
656 /* Fire off a new request. */
657 if (!(new_buf = g_try_malloc(4096))) {
658 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
659 return; /* TODO: SR_ERR_MALLOC */
662 transfer->buffer = new_buf;
663 transfer->length = 4096;
664 if (libusb_submit_transfer(transfer) != 0) {
665 /* TODO: Stop session? */
666 /* TODO: Better error message. */
667 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
670 if (cur_buflen == 0) {
671 empty_transfer_count++;
672 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
674 * The FX2 gave up. End the acquisition, the frontend
675 * will work out that the samplecount is short.
677 abort_acquisition(ctx);
681 empty_transfer_count = 0;
685 if (ctx->trigger_stage >= 0) {
686 for (i = 0; i < cur_buflen; i++) {
688 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
689 /* Match on this trigger stage. */
690 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
691 ctx->trigger_stage++;
693 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
694 /* Match on all trigger stages, we're done. */
695 trigger_offset = i + 1;
698 * TODO: Send pre-trigger buffer to session bus.
699 * Tell the frontend we hit the trigger here.
701 packet.type = SR_DF_TRIGGER;
702 packet.payload = NULL;
703 sr_session_send(ctx->session_dev_id, &packet);
706 * Send the samples that triggered it, since we're
707 * skipping past them.
709 packet.type = SR_DF_LOGIC;
710 packet.payload = &logic;
711 logic.length = ctx->trigger_stage;
713 logic.data = ctx->trigger_buffer;
714 sr_session_send(ctx->session_dev_id, &packet);
716 ctx->trigger_stage = TRIGGER_FIRED;
723 * We had a match before, but not in the next sample. However, we may
724 * have a match on this stage in the next bit -- trigger on 0001 will
725 * fail on seeing 00001, so we need to go back to stage 0 -- but at
726 * the next sample from the one that matched originally, which the
727 * counter increment at the end of the loop takes care of.
729 if (ctx->trigger_stage > 0) {
730 i -= ctx->trigger_stage;
732 i = -1; /* Oops, went back past this buffer. */
733 /* Reset trigger stage. */
734 ctx->trigger_stage = 0;
739 if (ctx->trigger_stage == TRIGGER_FIRED) {
740 /* Send the incoming transfer to the session bus. */
741 packet.type = SR_DF_LOGIC;
742 packet.payload = &logic;
743 logic.length = cur_buflen - trigger_offset;
745 logic.data = cur_buf + trigger_offset;
746 sr_session_send(ctx->session_dev_id, &packet);
749 ctx->num_samples += cur_buflen;
750 if (ctx->limit_samples &&
751 (unsigned int)ctx->num_samples > ctx->limit_samples) {
752 abort_acquisition(ctx);
756 * TODO: Buffer pre-trigger data in capture
757 * ratio-sized buffer.
762 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
764 struct sr_dev_inst *sdi;
765 struct sr_datafeed_packet *packet;
766 struct sr_datafeed_header *header;
768 struct libusb_transfer *transfer;
769 const struct libusb_pollfd **lupfd;
773 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
776 ctx->session_dev_id = cb_data;
777 ctx->num_samples = 0;
779 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
780 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
781 return SR_ERR_MALLOC;
784 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
785 sr_err("fx2lafw: %s: header malloc failed.", __func__);
786 return SR_ERR_MALLOC;
789 /* Start with 2K transfer, subsequently increased to 4K. */
791 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
792 if (!(buf = g_try_malloc(size))) {
793 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
794 return SR_ERR_MALLOC;
796 transfer = libusb_alloc_transfer(0);
797 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
798 2 | LIBUSB_ENDPOINT_IN, buf, size,
799 receive_transfer, ctx, 40);
800 if (libusb_submit_transfer(transfer) != 0) {
801 /* TODO: Free them all. */
802 libusb_free_transfer(transfer);
807 ctx->submitted_transfers++;
811 lupfd = libusb_get_pollfds(usb_context);
812 for (i = 0; lupfd[i]; i++)
813 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
814 40, receive_data, NULL);
815 free(lupfd); /* NOT g_free()! */
817 packet->type = SR_DF_HEADER;
818 packet->payload = header;
819 header->feed_version = 1;
820 gettimeofday(&header->starttime, NULL);
821 header->samplerate = ctx->cur_samplerate;
822 header->num_logic_probes = ctx->profile->num_probes;
823 sr_session_send(cb_data, packet);
827 if ((ret = command_start_acquisition (ctx->usb->devhdl,
828 ctx->cur_samplerate)) != SR_OK) {
835 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
836 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
838 struct sr_dev_inst *sdi;
840 /* Avoid compiler warnings. */
843 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
846 abort_acquisition(sdi->priv);
851 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
853 .longname = "fx2lafw (generic driver for FX2 based LAs)",
856 .cleanup = hw_cleanup,
857 .dev_open = hw_dev_open,
858 .dev_close = hw_dev_close,
859 .dev_info_get = hw_dev_info_get,
860 .dev_status_get = hw_dev_status_get,
861 .hwcap_get_all = hw_hwcap_get_all,
862 .dev_config_set = hw_dev_config_set,
863 .dev_acquisition_start = hw_dev_acquisition_start,
864 .dev_acquisition_stop = hw_dev_acquisition_stop,