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"
31 static struct fx2lafw_profile supported_fx2[] = {
33 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
37 static int fx2lafw_capabilities[] = {
38 SR_HWCAP_LOGIC_ANALYZER,
41 /* These are really implemented in the driver, not the hardware. */
42 SR_HWCAP_LIMIT_SAMPLES,
47 static const char *fx2lafw_probe_names[] = {
59 static uint64_t fx2lafw_supported_samplerates[] = {
71 static struct sr_samplerates fx2lafw_samplerates = {
75 fx2lafw_supported_samplerates,
78 static GSList *dev_insts = NULL;
79 static libusb_context *usb_context = NULL;
81 static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id);
84 * Check the USB configuration to determine if this is an fx2lafw device.
86 * @return true if the device's configuration profile match fx2lafw
87 * configuration, false otherwise.
89 static bool check_conf_profile(libusb_device *dev)
91 struct libusb_device_descriptor des;
92 struct libusb_config_descriptor *conf_dsc = NULL;
93 const struct libusb_interface_descriptor *intf_dsc;
97 /* Assume it's not a Saleae Logic unless proven wrong. */
100 if (libusb_get_device_descriptor(dev, &des) != 0)
103 if (des.bNumConfigurations != 1)
104 /* Need exactly 1 configuration. */
107 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
110 if (conf_dsc->bNumInterfaces != 1)
111 /* Need exactly 1 interface. */
114 if (conf_dsc->interface[0].num_altsetting != 1)
115 /* Need just one alternate setting. */
118 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
119 if (intf_dsc->bNumEndpoints != 3)
120 /* Need exactly 3 end points. */
123 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
124 (1 | LIBUSB_ENDPOINT_OUT))
125 /* The first endpoint should be 1 (outbound). */
128 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
129 (2 | LIBUSB_ENDPOINT_IN))
130 /* The second endpoint should be 2 (inbound). */
133 /* TODO: Check the debug channel... */
135 /* If we made it here, it must be an fx2lafw. */
140 libusb_free_config_descriptor(conf_dsc);
145 static int fx2lafw_open_dev(int dev_index)
147 libusb_device **devlist;
148 struct libusb_device_descriptor des;
149 struct sr_dev_inst *sdi;
153 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
157 if (sdi->status == SR_ST_ACTIVE)
162 libusb_get_device_list(usb_context, &devlist);
163 for (i = 0; devlist[i]; i++) {
164 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
165 sr_err("fx2lafw: failed to get device descriptor: %d", err);
169 if (des.idVendor != FIRMWARE_VID
170 || des.idProduct != FIRMWARE_PID)
173 if (sdi->status == SR_ST_INITIALIZING) {
174 if (skip != dev_index) {
175 /* Skip devices of this type that aren't the one we want. */
179 } else if (sdi->status == SR_ST_INACTIVE) {
181 * This device is fully enumerated, so we need to find
182 * this device by vendor, product, bus and address.
184 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
185 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
186 /* this is not the one */
190 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
191 if (ctx->usb->address == 0xff)
193 * first time we touch this device after firmware upload,
194 * so we don't know the address yet.
196 ctx->usb->address = libusb_get_device_address(devlist[i]);
198 sdi->status = SR_ST_ACTIVE;
199 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
200 sdi->index, ctx->usb->bus,
201 ctx->usb->address, USB_INTERFACE);
203 sr_err("fx2lafw: failed to open device: %d", err);
206 /* if we made it here, we handled the device one way or another */
209 libusb_free_device_list(devlist, 1);
211 if (sdi->status != SR_ST_ACTIVE)
217 static void close_dev(struct sr_dev_inst *sdi)
223 if (ctx->usb->devhdl == NULL)
226 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
227 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
228 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
229 libusb_close(ctx->usb->devhdl);
230 ctx->usb->devhdl = NULL;
231 sdi->status = SR_ST_INACTIVE;
234 static struct context *fx2lafw_device_new(void)
238 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
239 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
250 static int hw_init(const char *deviceinfo)
252 struct sr_dev_inst *sdi;
253 struct libusb_device_descriptor des;
254 struct fx2lafw_profile *fx2lafw_prof;
256 libusb_device **devlist;
261 /* Avoid compiler warnings. */
264 if (libusb_init(&usb_context) != 0) {
265 sr_warn("Failed to initialize USB.");
269 /* Find all fx2lafw compatible devices and upload firware to all of them. */
270 libusb_get_device_list(usb_context, &devlist);
271 for (i = 0; devlist[i]; i++) {
273 if ((err = libusb_get_device_descriptor(
274 devlist[i], &des)) != 0) {
275 sr_warn("failed to get device descriptor: %d", err);
280 for (j = 0; supported_fx2[j].vid; j++) {
281 if (des.idVendor == supported_fx2[j].vid &&
282 des.idProduct == supported_fx2[j].pid) {
283 fx2lafw_prof = &supported_fx2[j];
287 /* Skip if the device was not found */
291 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
292 fx2lafw_prof->vendor, fx2lafw_prof->model,
293 fx2lafw_prof->model_version);
297 ctx = fx2lafw_device_new();
298 ctx->profile = fx2lafw_prof;
300 dev_insts = g_slist_append(dev_insts, sdi);
302 if (check_conf_profile(devlist[i])) {
303 /* Already has the firmware, so fix the new address. */
304 sr_dbg("fx2lafw: Found a fx2lafw device.");
305 sdi->status = SR_ST_INACTIVE;
306 ctx->usb = sr_usb_dev_inst_new
307 (libusb_get_bus_number(devlist[i]),
308 libusb_get_device_address(devlist[i]), NULL);
310 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
311 /* Remember when the firmware on this device was updated */
312 g_get_current_time(&ctx->fw_updated);
314 sr_err("fx2lafw: firmware upload failed for "
315 "device %d", devcnt);
316 ctx->usb = sr_usb_dev_inst_new
317 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
322 libusb_free_device_list(devlist, 1);
327 static int hw_dev_open(int dev_index)
330 struct sr_dev_inst *sdi;
334 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
339 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
340 * for the FX2 to renumerate
343 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
344 sr_info("fx2lafw: waiting for device to reset");
345 /* takes at least 300ms for the FX2 to be gone from the USB bus */
346 g_usleep(300 * 1000);
348 while (timediff < MAX_RENUM_DELAY) {
349 if ((err = fx2lafw_open_dev(dev_index)) == SR_OK)
351 g_usleep(100 * 1000);
352 g_get_current_time(&cur_time);
353 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
355 sr_info("fx2lafw: device came back after %d ms", timediff);
357 err = fx2lafw_open_dev(dev_index);
361 sr_err("fx2lafw: unable to open device");
366 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
368 sr_err("fx2lafw: Unable to claim interface: %d", err);
375 static int hw_dev_close(int dev_index)
377 struct sr_dev_inst *sdi;
379 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
380 sr_err("fx2lafw: %s: sdi was NULL", __func__);
381 return SR_ERR; /* TODO: SR_ERR_ARG? */
390 static int hw_cleanup(void)
393 struct sr_dev_inst *sdi;
397 for(l = dev_insts; l; l = l->next) {
398 if (!(sdi = l->data)) {
399 /* Log error, but continue cleaning up the rest. */
400 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
404 if (!(ctx = sdi->priv)) {
405 /* Log error, but continue cleaning up the rest. */
406 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
413 sr_dev_inst_free(sdi);
416 g_slist_free(dev_insts);
420 libusb_exit(usb_context);
426 static void *hw_dev_info_get(int dev_index, int dev_info_id)
428 struct sr_dev_inst *sdi;
431 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
435 switch (dev_info_id) {
438 case SR_DI_NUM_PROBES:
439 return GINT_TO_POINTER(ctx->profile->num_probes);
440 case SR_DI_PROBE_NAMES:
441 return fx2lafw_probe_names;
442 case SR_DI_SAMPLERATES:
443 return &fx2lafw_samplerates;
444 case SR_DI_TRIGGER_TYPES:
445 return TRIGGER_TYPES;
451 static int hw_dev_status_get(int dev_index)
453 const struct sr_dev_inst *const sdi =
454 sr_dev_inst_get(dev_insts, dev_index);
457 return SR_ST_NOT_FOUND;
462 static int *hw_hwcap_get_all(void)
464 return fx2lafw_capabilities;
467 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
469 struct sr_dev_inst *sdi;
473 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
477 if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
478 ctx->limit_samples = *(uint64_t *)value;
487 static int receive_data(int fd, int revents, void *user_data)
491 /* Avoid compiler warnings. */
496 tv.tv_sec = tv.tv_usec = 0;
497 libusb_handle_events_timeout(usb_context, &tv);
502 static void receive_transfer(struct libusb_transfer *transfer)
504 /* TODO: These statics have to move to the ctx struct. */
505 static int num_samples = 0;
506 static int empty_transfer_count = 0;
507 struct sr_datafeed_packet packet;
508 struct sr_datafeed_logic logic;
511 unsigned char *cur_buf, *new_buf;
513 /* hw_dev_acquisition_stop() is telling us to stop. */
514 if (transfer == NULL)
518 * If acquisition has already ended, just free any queued up
519 * transfer that come in.
521 if (num_samples == -1) {
523 libusb_free_transfer(transfer);
527 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
528 transfer->status, transfer->actual_length);
530 /* Save incoming transfer before reusing the transfer struct. */
531 cur_buf = transfer->buffer;
532 cur_buflen = transfer->actual_length;
533 ctx = transfer->user_data;
535 /* Fire off a new request. */
536 if (!(new_buf = g_try_malloc(4096))) {
537 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
538 return; /* TODO: SR_ERR_MALLOC */
541 transfer->buffer = new_buf;
542 transfer->length = 4096;
543 if (libusb_submit_transfer(transfer) != 0) {
544 /* TODO: Stop session? */
545 /* TODO: Better error message. */
546 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
549 if (cur_buflen == 0) {
550 empty_transfer_count++;
551 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
553 * The FX2 gave up. End the acquisition, the frontend
554 * will work out that the samplecount is short.
556 hw_dev_acquisition_stop(-1, ctx->session_data);
560 empty_transfer_count = 0;
563 /* Send the incoming transfer to the session bus. */
564 packet.type = SR_DF_LOGIC;
565 packet.payload = &logic;
566 logic.length = cur_buflen;
568 logic.data = cur_buf;
569 sr_session_bus(ctx->session_data, &packet);
572 num_samples += cur_buflen;
573 if (ctx->limit_samples &&
574 (unsigned int) num_samples > ctx->limit_samples) {
575 hw_dev_acquisition_stop(-1, ctx->session_data);
579 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
581 struct sr_dev_inst *sdi;
582 struct sr_datafeed_packet *packet;
583 struct sr_datafeed_header *header;
585 struct libusb_transfer *transfer;
586 const struct libusb_pollfd **lupfd;
590 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
593 ctx->session_data = session_data;
595 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
596 sr_err("fx2lafw: %s: packet malloc failed", __func__);
597 return SR_ERR_MALLOC;
600 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
601 sr_err("fx2lafw: %s: header malloc failed", __func__);
602 return SR_ERR_MALLOC;
605 /* Start with 2K transfer, subsequently increased to 4K. */
607 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
608 if (!(buf = g_try_malloc(size))) {
609 sr_err("fx2lafw: %s: buf malloc failed", __func__);
610 return SR_ERR_MALLOC;
612 transfer = libusb_alloc_transfer(0);
613 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
614 2 | LIBUSB_ENDPOINT_IN, buf, size,
615 receive_transfer, ctx, 40);
616 if (libusb_submit_transfer(transfer) != 0) {
617 /* TODO: Free them all. */
618 libusb_free_transfer(transfer);
625 lupfd = libusb_get_pollfds(usb_context);
626 for (i = 0; lupfd[i]; i++)
627 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
628 40, receive_data, NULL);
629 free(lupfd); /* NOT g_free()! */
631 packet->type = SR_DF_HEADER;
632 packet->payload = header;
633 header->feed_version = 1;
634 gettimeofday(&header->starttime, NULL);
635 header->samplerate = 24000000UL;
636 header->num_logic_probes = ctx->profile->num_probes;
637 sr_session_bus(session_data, packet);
644 /* This stops acquisition on ALL devices, ignoring dev_index. */
645 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
647 struct sr_datafeed_packet packet;
649 /* Avoid compiler warnings. */
652 packet.type = SR_DF_END;
653 sr_session_bus(session_data, &packet);
655 receive_transfer(NULL);
657 /* TODO: Need to cancel and free any queued up transfers. */
662 SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
664 .longname = "fx2lafw",
667 .cleanup = hw_cleanup,
668 .dev_open = hw_dev_open,
669 .dev_close = hw_dev_close,
670 .dev_info_get = hw_dev_info_get,
671 .dev_status_get = hw_dev_status_get,
672 .hwcap_get_all = hw_hwcap_get_all,
673 .dev_config_set = hw_dev_config_set,
674 .dev_acquisition_start = hw_dev_acquisition_start,
675 .dev_acquisition_stop = hw_dev_acquisition_stop,