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;
169 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
173 if (sdi->status == SR_ST_ACTIVE)
178 const int device_count = libusb_get_device_list(usb_context, &devlist);
179 if (device_count < 0) {
180 sr_err("fx2lafw: Failed to retrieve device list (%d)",
185 for (i = 0; i < device_count; i++) {
186 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
187 sr_err("fx2lafw: Failed to get device descriptor: %d.",
192 if (des.idVendor != ctx->profile->vid
193 || des.idProduct != ctx->profile->pid)
196 if (sdi->status == SR_ST_INITIALIZING) {
197 if (skip != dev_index) {
198 /* Skip devices of this type that aren't the one we want. */
202 } else if (sdi->status == SR_ST_INACTIVE) {
204 * This device is fully enumerated, so we need to find
205 * this device by vendor, product, bus and address.
207 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
208 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
209 /* this is not the one */
213 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
214 if (ctx->usb->address == 0xff)
216 * first time we touch this device after firmware upload,
217 * so we don't know the address yet.
219 ctx->usb->address = libusb_get_device_address(devlist[i]);
221 sdi->status = SR_ST_ACTIVE;
222 sr_info("fx2lafw: Opened device %d on %d.%d "
223 "interface %d.", sdi->index, ctx->usb->bus,
224 ctx->usb->address, USB_INTERFACE);
226 sr_err("fx2lafw: Failed to open device: %d.", ret);
229 /* if we made it here, we handled the device one way or another */
232 libusb_free_device_list(devlist, 1);
234 if (sdi->status != SR_ST_ACTIVE)
240 static void close_dev(struct sr_dev_inst *sdi)
246 if (ctx->usb->devhdl == NULL)
249 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
250 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
251 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
252 libusb_close(ctx->usb->devhdl);
253 ctx->usb->devhdl = NULL;
254 sdi->status = SR_ST_INACTIVE;
257 static int configure_probes(struct context *ctx, GSList *probes)
259 struct sr_probe *probe;
261 int probe_bit, stage, i;
264 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
265 ctx->trigger_mask[i] = 0;
266 ctx->trigger_value[i] = 0;
270 for (l = probes; l; l = l->next) {
271 probe = (struct sr_probe *)l->data;
272 if (probe->enabled == FALSE)
274 probe_bit = 1 << (probe->index - 1);
275 if (!(probe->trigger))
279 for (tc = probe->trigger; *tc; tc++) {
280 ctx->trigger_mask[stage] |= probe_bit;
282 ctx->trigger_value[stage] |= probe_bit;
284 if (stage > NUM_TRIGGER_STAGES)
291 * We didn't configure any triggers, make sure acquisition
292 * doesn't wait for any.
294 ctx->trigger_stage = TRIGGER_FIRED;
296 ctx->trigger_stage = 0;
301 static struct context *fx2lafw_dev_new(void)
305 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
306 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
310 ctx->trigger_stage = TRIGGER_FIRED;
319 static int hw_init(const char *devinfo)
321 struct sr_dev_inst *sdi;
322 struct libusb_device_descriptor des;
323 const struct fx2lafw_profile *prof;
325 libusb_device **devlist;
330 /* Avoid compiler warnings. */
333 if (libusb_init(&usb_context) != 0) {
334 sr_warn("fx2lafw: Failed to initialize libusb.");
338 /* Find all fx2lafw compatible devices and upload firware to them. */
339 libusb_get_device_list(usb_context, &devlist);
340 for (i = 0; devlist[i]; i++) {
342 if ((ret = libusb_get_device_descriptor(
343 devlist[i], &des)) != 0) {
344 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
349 for (j = 0; supported_fx2[j].vid; j++) {
350 if (des.idVendor == supported_fx2[j].vid &&
351 des.idProduct == supported_fx2[j].pid) {
352 prof = &supported_fx2[j];
356 /* Skip if the device was not found */
360 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
361 prof->vendor, prof->model, prof->model_version);
365 ctx = fx2lafw_dev_new();
368 dev_insts = g_slist_append(dev_insts, sdi);
370 if (check_conf_profile(devlist[i])) {
371 /* Already has the firmware, so fix the new address. */
372 sr_dbg("fx2lafw: Found an fx2lafw device.");
373 sdi->status = SR_ST_INACTIVE;
374 ctx->usb = sr_usb_dev_inst_new
375 (libusb_get_bus_number(devlist[i]),
376 libusb_get_device_address(devlist[i]), NULL);
378 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
379 prof->firmware) == SR_OK)
380 /* Remember when the firmware on this device was updated */
381 g_get_current_time(&ctx->fw_updated);
383 sr_err("fx2lafw: Firmware upload failed for "
384 "device %d.", devcnt);
385 ctx->usb = sr_usb_dev_inst_new
386 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
391 libusb_free_device_list(devlist, 1);
396 static int hw_dev_open(int dev_index)
399 struct sr_dev_inst *sdi;
403 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
408 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
409 * for the FX2 to renumerate
412 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
413 sr_info("fx2lafw: Waiting for device to reset.");
414 /* takes at least 300ms for the FX2 to be gone from the USB bus */
415 g_usleep(300 * 1000);
417 while (timediff < MAX_RENUM_DELAY) {
418 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
420 g_usleep(100 * 1000);
421 g_get_current_time(&cur_time);
422 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
424 sr_info("fx2lafw: Device came back after %d ms.", timediff);
426 ret = fx2lafw_dev_open(dev_index);
430 sr_err("fx2lafw: Unable to open device.");
435 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
437 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
441 if (ctx->cur_samplerate == 0) {
442 /* Samplerate hasn't been set; default to the slowest one. */
443 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
444 &supported_samplerates[0]) == SR_ERR)
451 static int hw_dev_close(int dev_index)
453 struct sr_dev_inst *sdi;
455 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
456 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
466 static int hw_cleanup(void)
469 struct sr_dev_inst *sdi;
473 for (l = dev_insts; l; l = l->next) {
474 if (!(sdi = l->data)) {
475 /* Log error, but continue cleaning up the rest. */
476 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
481 if (!(ctx = sdi->priv)) {
482 /* Log error, but continue cleaning up the rest. */
483 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
490 sr_dev_inst_free(sdi);
493 g_slist_free(dev_insts);
497 libusb_exit(usb_context);
503 static void *hw_dev_info_get(int dev_index, int dev_info_id)
505 struct sr_dev_inst *sdi;
508 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
512 switch (dev_info_id) {
515 case SR_DI_NUM_PROBES:
516 return GINT_TO_POINTER(ctx->profile->num_probes);
517 case SR_DI_PROBE_NAMES:
519 case SR_DI_SAMPLERATES:
521 case SR_DI_TRIGGER_TYPES:
522 return TRIGGER_TYPES;
523 case SR_DI_CUR_SAMPLERATE:
524 return &ctx->cur_samplerate;
530 static int hw_dev_status_get(int dev_index)
532 const struct sr_dev_inst *const sdi =
533 sr_dev_inst_get(dev_insts, dev_index);
536 return SR_ST_NOT_FOUND;
541 static int *hw_hwcap_get_all(void)
546 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
548 struct sr_dev_inst *sdi;
552 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
556 if (hwcap == SR_HWCAP_SAMPLERATE) {
557 ctx->cur_samplerate = *(uint64_t *)value;
559 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
560 ret = configure_probes(ctx, (GSList *) value);
561 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
562 ctx->limit_samples = *(uint64_t *)value;
571 static int receive_data(int fd, int revents, void *cb_data)
575 /* Avoid compiler warnings. */
580 tv.tv_sec = tv.tv_usec = 0;
581 libusb_handle_events_timeout(usb_context, &tv);
586 static void abort_acquisition(struct context *ctx)
588 ctx->num_samples = -1;
591 static void finish_acquisition(struct context *ctx)
593 struct sr_datafeed_packet packet;
596 /* Terminate session */
597 packet.type = SR_DF_END;
598 sr_session_send(ctx->session_dev_id, &packet);
600 /* Remove fds from polling */
601 const struct libusb_pollfd **const lupfd =
602 libusb_get_pollfds(usb_context);
603 for (i = 0; lupfd[i]; i++)
604 sr_source_remove(lupfd[i]->fd);
605 free(lupfd); /* NOT g_free()! */
608 static void receive_transfer(struct libusb_transfer *transfer)
610 /* TODO: These statics have to move to the ctx struct. */
611 static int empty_transfer_count = 0;
612 struct sr_datafeed_packet packet;
613 struct sr_datafeed_logic logic;
614 struct context *ctx = transfer->user_data;
615 int cur_buflen, trigger_offset, i;
616 unsigned char *cur_buf, *new_buf;
619 * If acquisition has already ended, just free any queued up
620 * transfer that come in.
622 if (ctx->num_samples == -1) {
624 libusb_free_transfer(transfer);
626 ctx->submitted_transfers--;
627 if (ctx->submitted_transfers == 0)
628 finish_acquisition(ctx);
633 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
634 transfer->status, transfer->actual_length);
636 /* Save incoming transfer before reusing the transfer struct. */
637 cur_buf = transfer->buffer;
638 cur_buflen = transfer->actual_length;
640 /* Fire off a new request. */
641 if (!(new_buf = g_try_malloc(4096))) {
642 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
643 return; /* TODO: SR_ERR_MALLOC */
646 transfer->buffer = new_buf;
647 transfer->length = 4096;
648 if (libusb_submit_transfer(transfer) != 0) {
649 /* TODO: Stop session? */
650 /* TODO: Better error message. */
651 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
654 if (cur_buflen == 0) {
655 empty_transfer_count++;
656 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
658 * The FX2 gave up. End the acquisition, the frontend
659 * will work out that the samplecount is short.
661 abort_acquisition(ctx);
665 empty_transfer_count = 0;
669 if (ctx->trigger_stage >= 0) {
670 for (i = 0; i < cur_buflen; i++) {
672 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
673 /* Match on this trigger stage. */
674 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
675 ctx->trigger_stage++;
677 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
678 /* Match on all trigger stages, we're done. */
679 trigger_offset = i + 1;
682 * TODO: Send pre-trigger buffer to session bus.
683 * Tell the frontend we hit the trigger here.
685 packet.type = SR_DF_TRIGGER;
686 packet.payload = NULL;
687 sr_session_send(ctx->session_dev_id, &packet);
690 * Send the samples that triggered it, since we're
691 * skipping past them.
693 packet.type = SR_DF_LOGIC;
694 packet.payload = &logic;
695 logic.length = ctx->trigger_stage;
697 logic.data = ctx->trigger_buffer;
698 sr_session_send(ctx->session_dev_id, &packet);
700 ctx->trigger_stage = TRIGGER_FIRED;
707 * We had a match before, but not in the next sample. However, we may
708 * have a match on this stage in the next bit -- trigger on 0001 will
709 * fail on seeing 00001, so we need to go back to stage 0 -- but at
710 * the next sample from the one that matched originally, which the
711 * counter increment at the end of the loop takes care of.
713 if (ctx->trigger_stage > 0) {
714 i -= ctx->trigger_stage;
716 i = -1; /* Oops, went back past this buffer. */
717 /* Reset trigger stage. */
718 ctx->trigger_stage = 0;
723 if (ctx->trigger_stage == TRIGGER_FIRED) {
724 /* Send the incoming transfer to the session bus. */
725 packet.type = SR_DF_LOGIC;
726 packet.payload = &logic;
727 logic.length = cur_buflen - trigger_offset;
729 logic.data = cur_buf + trigger_offset;
730 sr_session_send(ctx->session_dev_id, &packet);
733 ctx->num_samples += cur_buflen;
734 if (ctx->limit_samples &&
735 (unsigned int)ctx->num_samples > ctx->limit_samples) {
736 abort_acquisition(ctx);
740 * TODO: Buffer pre-trigger data in capture
741 * ratio-sized buffer.
746 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
748 struct sr_dev_inst *sdi;
749 struct sr_datafeed_packet *packet;
750 struct sr_datafeed_header *header;
752 struct libusb_transfer *transfer;
753 const struct libusb_pollfd **lupfd;
757 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
760 ctx->session_dev_id = cb_data;
761 ctx->num_samples = 0;
763 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
764 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
765 return SR_ERR_MALLOC;
768 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
769 sr_err("fx2lafw: %s: header malloc failed.", __func__);
770 return SR_ERR_MALLOC;
773 /* Start with 2K transfer, subsequently increased to 4K. */
775 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
776 if (!(buf = g_try_malloc(size))) {
777 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
778 return SR_ERR_MALLOC;
780 transfer = libusb_alloc_transfer(0);
781 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
782 2 | LIBUSB_ENDPOINT_IN, buf, size,
783 receive_transfer, ctx, 40);
784 if (libusb_submit_transfer(transfer) != 0) {
785 /* TODO: Free them all. */
786 libusb_free_transfer(transfer);
791 ctx->submitted_transfers++;
795 lupfd = libusb_get_pollfds(usb_context);
796 for (i = 0; lupfd[i]; i++)
797 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
798 40, receive_data, NULL);
799 free(lupfd); /* NOT g_free()! */
801 packet->type = SR_DF_HEADER;
802 packet->payload = header;
803 header->feed_version = 1;
804 gettimeofday(&header->starttime, NULL);
805 header->samplerate = ctx->cur_samplerate;
806 header->num_logic_probes = ctx->profile->num_probes;
807 sr_session_send(cb_data, packet);
811 if ((ret = command_start_acquisition (ctx->usb->devhdl,
812 ctx->cur_samplerate)) != SR_OK) {
819 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
820 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
822 struct sr_dev_inst *sdi;
824 /* Avoid compiler warnings. */
827 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
830 abort_acquisition(sdi->priv);
835 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
837 .longname = "fx2lafw (generic driver for FX2 based LAs)",
840 .cleanup = hw_cleanup,
841 .dev_open = hw_dev_open,
842 .dev_close = hw_dev_close,
843 .dev_info_get = hw_dev_info_get,
844 .dev_status_get = hw_dev_status_get,
845 .hwcap_get_all = hw_hwcap_get_all,
846 .dev_config_set = hw_dev_config_set,
847 .dev_acquisition_start = hw_dev_acquisition_start,
848 .dev_acquisition_stop = hw_dev_acquisition_stop,