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[] = {
33 * EE Electronics ESLA201A
35 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
36 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
40 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
41 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
44 * EE Electronics ESLA100
47 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
48 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
50 { 0, 0, 0, 0, 0, 0, 0 }
53 static int fx2lafw_capabilities[] = {
54 SR_HWCAP_LOGIC_ANALYZER,
57 /* These are really implemented in the driver, not the hardware. */
58 SR_HWCAP_LIMIT_SAMPLES,
63 static const char *fx2lafw_probe_names[] = {
75 static uint64_t fx2lafw_supported_samplerates[] = {
87 static struct sr_samplerates fx2lafw_samplerates = {
91 fx2lafw_supported_samplerates,
94 static GSList *dev_insts = NULL;
95 static libusb_context *usb_context = NULL;
97 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
98 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
101 * Check the USB configuration to determine if this is an fx2lafw device.
103 * @return TRUE if the device's configuration profile match fx2lafw
104 * configuration, FALSE otherwise.
106 static gboolean check_conf_profile(libusb_device *dev)
108 struct libusb_device_descriptor des;
109 struct libusb_config_descriptor *conf_dsc = NULL;
110 const struct libusb_interface_descriptor *intf_dsc;
111 gboolean ret = FALSE;
114 /* Assume the firmware has not been loaded, unless proven wrong. */
117 if (libusb_get_device_descriptor(dev, &des) != 0)
120 if (des.bNumConfigurations != 1)
121 /* Need exactly 1 configuration. */
124 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
127 if (conf_dsc->bNumInterfaces != 1)
128 /* Need exactly 1 interface. */
131 if (conf_dsc->interface[0].num_altsetting != 1)
132 /* Need just one alternate setting. */
135 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
136 if (intf_dsc->bNumEndpoints != 2)
137 /* Need exactly 2 end points. */
140 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
141 (2 | LIBUSB_ENDPOINT_IN)) // 0x82
142 /* The first endpoint should be 2 (inbound). */
145 /* TODO: Check the debug channel... */
147 /* If we made it here, it must be an fx2lafw. */
152 libusb_free_config_descriptor(conf_dsc);
157 static int fx2lafw_open_dev(int dev_index)
159 libusb_device **devlist;
160 struct libusb_device_descriptor des;
161 struct sr_dev_inst *sdi;
165 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
169 if (sdi->status == SR_ST_ACTIVE)
174 libusb_get_device_list(usb_context, &devlist);
175 for (i = 0; devlist[i]; i++) {
176 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
177 sr_err("fx2lafw: failed to get device descriptor: %d", ret);
181 if (des.idVendor != ctx->profile->vid
182 || des.idProduct != ctx->profile->pid)
185 if (sdi->status == SR_ST_INITIALIZING) {
186 if (skip != dev_index) {
187 /* Skip devices of this type that aren't the one we want. */
191 } else if (sdi->status == SR_ST_INACTIVE) {
193 * This device is fully enumerated, so we need to find
194 * this device by vendor, product, bus and address.
196 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
197 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
198 /* this is not the one */
202 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
203 if (ctx->usb->address == 0xff)
205 * first time we touch this device after firmware upload,
206 * so we don't know the address yet.
208 ctx->usb->address = libusb_get_device_address(devlist[i]);
210 sdi->status = SR_ST_ACTIVE;
211 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
212 sdi->index, ctx->usb->bus,
213 ctx->usb->address, USB_INTERFACE);
215 sr_err("fx2lafw: failed to open device: %d", ret);
218 /* if we made it here, we handled the device one way or another */
221 libusb_free_device_list(devlist, 1);
223 if (sdi->status != SR_ST_ACTIVE)
229 static void close_dev(struct sr_dev_inst *sdi)
235 if (ctx->usb->devhdl == NULL)
238 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
239 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
240 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
241 libusb_close(ctx->usb->devhdl);
242 ctx->usb->devhdl = NULL;
243 sdi->status = SR_ST_INACTIVE;
246 static struct context *fx2lafw_device_new(void)
250 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
251 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
262 static int hw_init(const char *deviceinfo)
264 struct sr_dev_inst *sdi;
265 struct libusb_device_descriptor des;
266 const struct fx2lafw_profile *fx2lafw_prof;
268 libusb_device **devlist;
273 /* Avoid compiler warnings. */
276 if (libusb_init(&usb_context) != 0) {
277 sr_warn("Failed to initialize USB.");
281 /* Find all fx2lafw compatible devices and upload firware to all of them. */
282 libusb_get_device_list(usb_context, &devlist);
283 for (i = 0; devlist[i]; i++) {
285 if ((ret = libusb_get_device_descriptor(
286 devlist[i], &des)) != 0) {
287 sr_warn("failed to get device descriptor: %d", ret);
292 for (j = 0; supported_fx2[j].vid; j++) {
293 if (des.idVendor == supported_fx2[j].vid &&
294 des.idProduct == supported_fx2[j].pid) {
295 fx2lafw_prof = &supported_fx2[j];
299 /* Skip if the device was not found */
303 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
304 fx2lafw_prof->vendor, fx2lafw_prof->model,
305 fx2lafw_prof->model_version);
309 ctx = fx2lafw_device_new();
310 ctx->profile = fx2lafw_prof;
312 dev_insts = g_slist_append(dev_insts, sdi);
314 if (check_conf_profile(devlist[i])) {
315 /* Already has the firmware, so fix the new address. */
316 sr_dbg("fx2lafw: Found a fx2lafw device.");
317 sdi->status = SR_ST_INACTIVE;
318 ctx->usb = sr_usb_dev_inst_new
319 (libusb_get_bus_number(devlist[i]),
320 libusb_get_device_address(devlist[i]), NULL);
322 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
323 fx2lafw_prof->firmware) == SR_OK)
324 /* Remember when the firmware on this device was updated */
325 g_get_current_time(&ctx->fw_updated);
327 sr_err("fx2lafw: firmware upload failed for "
328 "device %d", devcnt);
329 ctx->usb = sr_usb_dev_inst_new
330 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
335 libusb_free_device_list(devlist, 1);
340 static int hw_dev_open(int dev_index)
343 struct sr_dev_inst *sdi;
347 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
352 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
353 * for the FX2 to renumerate
356 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
357 sr_info("fx2lafw: waiting for device to reset");
358 /* takes at least 300ms for the FX2 to be gone from the USB bus */
359 g_usleep(300 * 1000);
361 while (timediff < MAX_RENUM_DELAY) {
362 if ((ret = fx2lafw_open_dev(dev_index)) == SR_OK)
364 g_usleep(100 * 1000);
365 g_get_current_time(&cur_time);
366 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
368 sr_info("fx2lafw: device came back after %d ms", timediff);
370 ret = fx2lafw_open_dev(dev_index);
374 sr_err("fx2lafw: unable to open device");
379 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
381 sr_err("fx2lafw: Unable to claim interface: %d", ret);
385 if (ctx->cur_samplerate == 0) {
386 /* Samplerate hasn't been set; default to the slowest one. */
387 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
388 &fx2lafw_supported_samplerates[0]) == SR_ERR)
395 static int hw_dev_close(int dev_index)
397 struct sr_dev_inst *sdi;
399 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
400 sr_err("fx2lafw: %s: sdi was NULL", __func__);
410 static int hw_cleanup(void)
413 struct sr_dev_inst *sdi;
417 for(l = dev_insts; l; l = l->next) {
418 if (!(sdi = l->data)) {
419 /* Log error, but continue cleaning up the rest. */
420 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
424 if (!(ctx = sdi->priv)) {
425 /* Log error, but continue cleaning up the rest. */
426 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
433 sr_dev_inst_free(sdi);
436 g_slist_free(dev_insts);
440 libusb_exit(usb_context);
446 static void *hw_dev_info_get(int dev_index, int dev_info_id)
448 struct sr_dev_inst *sdi;
451 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
455 switch (dev_info_id) {
458 case SR_DI_NUM_PROBES:
459 return GINT_TO_POINTER(ctx->profile->num_probes);
460 case SR_DI_PROBE_NAMES:
461 return fx2lafw_probe_names;
462 case SR_DI_SAMPLERATES:
463 return &fx2lafw_samplerates;
464 case SR_DI_TRIGGER_TYPES:
465 return TRIGGER_TYPES;
466 case SR_DI_CUR_SAMPLERATE:
467 return &ctx->cur_samplerate;
473 static int hw_dev_status_get(int dev_index)
475 const struct sr_dev_inst *const sdi =
476 sr_dev_inst_get(dev_insts, dev_index);
479 return SR_ST_NOT_FOUND;
484 static int *hw_hwcap_get_all(void)
486 return fx2lafw_capabilities;
489 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
491 struct sr_dev_inst *sdi;
495 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
499 if (hwcap == SR_HWCAP_SAMPLERATE) {
500 ctx->cur_samplerate = *(uint64_t *)value;
502 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
504 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
505 ctx->limit_samples = *(uint64_t *)value;
514 static int receive_data(int fd, int revents, void *cb_data)
518 /* Avoid compiler warnings. */
523 tv.tv_sec = tv.tv_usec = 0;
524 libusb_handle_events_timeout(usb_context, &tv);
529 static void abort_acquisition(struct context *ctx)
531 struct sr_datafeed_packet packet;
533 packet.type = SR_DF_END;
534 sr_session_send(ctx->session_dev_id, &packet);
536 ctx->num_samples = -1;
538 /* TODO: Need to cancel and free any queued up transfers. */
541 static void receive_transfer(struct libusb_transfer *transfer)
543 /* TODO: These statics have to move to the ctx struct. */
544 static int empty_transfer_count = 0;
545 struct sr_datafeed_packet packet;
546 struct sr_datafeed_logic logic;
547 struct context *ctx = transfer->user_data;
549 unsigned char *cur_buf, *new_buf;
552 * If acquisition has already ended, just free any queued up
553 * transfer that come in.
555 if (ctx->num_samples == -1) {
557 libusb_free_transfer(transfer);
561 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
562 transfer->status, transfer->actual_length);
564 /* Save incoming transfer before reusing the transfer struct. */
565 cur_buf = transfer->buffer;
566 cur_buflen = transfer->actual_length;
567 ctx = transfer->user_data;
569 /* Fire off a new request. */
570 if (!(new_buf = g_try_malloc(4096))) {
571 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
572 return; /* TODO: SR_ERR_MALLOC */
575 transfer->buffer = new_buf;
576 transfer->length = 4096;
577 if (libusb_submit_transfer(transfer) != 0) {
578 /* TODO: Stop session? */
579 /* TODO: Better error message. */
580 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
583 if (cur_buflen == 0) {
584 empty_transfer_count++;
585 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
587 * The FX2 gave up. End the acquisition, the frontend
588 * will work out that the samplecount is short.
590 abort_acquisition(ctx);
594 empty_transfer_count = 0;
597 /* Send the incoming transfer to the session bus. */
598 packet.type = SR_DF_LOGIC;
599 packet.payload = &logic;
600 logic.length = cur_buflen;
602 logic.data = cur_buf;
603 sr_session_send(ctx->session_dev_id, &packet);
606 ctx->num_samples += cur_buflen;
607 if (ctx->limit_samples &&
608 (unsigned int) ctx->num_samples > ctx->limit_samples) {
609 abort_acquisition(ctx);
613 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
615 struct sr_dev_inst *sdi;
616 struct sr_datafeed_packet *packet;
617 struct sr_datafeed_header *header;
619 struct libusb_transfer *transfer;
620 const struct libusb_pollfd **lupfd;
624 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
627 ctx->session_dev_id = cb_data;
628 ctx->num_samples = 0;
630 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
631 sr_err("fx2lafw: %s: packet malloc failed", __func__);
632 return SR_ERR_MALLOC;
635 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
636 sr_err("fx2lafw: %s: header malloc failed", __func__);
637 return SR_ERR_MALLOC;
640 /* Start with 2K transfer, subsequently increased to 4K. */
642 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
643 if (!(buf = g_try_malloc(size))) {
644 sr_err("fx2lafw: %s: buf malloc failed", __func__);
645 return SR_ERR_MALLOC;
647 transfer = libusb_alloc_transfer(0);
648 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
649 2 | LIBUSB_ENDPOINT_IN, buf, size,
650 receive_transfer, ctx, 40);
651 if (libusb_submit_transfer(transfer) != 0) {
652 /* TODO: Free them all. */
653 libusb_free_transfer(transfer);
660 lupfd = libusb_get_pollfds(usb_context);
661 for (i = 0; lupfd[i]; i++)
662 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
663 40, receive_data, NULL);
664 free(lupfd); /* NOT g_free()! */
666 packet->type = SR_DF_HEADER;
667 packet->payload = header;
668 header->feed_version = 1;
669 gettimeofday(&header->starttime, NULL);
670 header->samplerate = ctx->cur_samplerate;
671 header->num_logic_probes = ctx->profile->num_probes;
672 sr_session_send(cb_data, packet);
676 if ((ret = command_start_acquisition (ctx->usb->devhdl,
677 ctx->cur_samplerate)) != SR_OK) {
684 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
685 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
687 struct sr_dev_inst *sdi;
689 /* unused parameter */
692 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
695 abort_acquisition(sdi->priv);
700 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
702 .longname = "fx2lafw",
705 .cleanup = hw_cleanup,
706 .dev_open = hw_dev_open,
707 .dev_close = hw_dev_close,
708 .dev_info_get = hw_dev_info_get,
709 .dev_status_get = hw_dev_status_get,
710 .hwcap_get_all = hw_hwcap_get_all,
711 .dev_config_set = hw_dev_config_set,
712 .dev_acquisition_start = hw_dev_acquisition_start,
713 .dev_acquisition_stop = hw_dev_acquisition_stop,