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 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_config_set(int dev_index, int hwcap, void *value);
82 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
85 * Check the USB configuration to determine if this is an fx2lafw device.
87 * @return TRUE if the device's configuration profile match fx2lafw
88 * configuration, FALSE otherwise.
90 static gboolean check_conf_profile(libusb_device *dev)
92 struct libusb_device_descriptor des;
93 struct libusb_config_descriptor *conf_dsc = NULL;
94 const struct libusb_interface_descriptor *intf_dsc;
98 /* Assume it's not a Saleae Logic unless proven wrong. */
101 if (libusb_get_device_descriptor(dev, &des) != 0)
104 if (des.bNumConfigurations != 1)
105 /* Need exactly 1 configuration. */
108 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
111 if (conf_dsc->bNumInterfaces != 1)
112 /* Need exactly 1 interface. */
115 if (conf_dsc->interface[0].num_altsetting != 1)
116 /* Need just one alternate setting. */
119 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
120 if (intf_dsc->bNumEndpoints != 3)
121 /* Need exactly 3 end points. */
124 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
125 (1 | LIBUSB_ENDPOINT_OUT))
126 /* The first endpoint should be 1 (outbound). */
129 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
130 (2 | LIBUSB_ENDPOINT_IN))
131 /* The second endpoint should be 2 (inbound). */
134 /* TODO: Check the debug channel... */
136 /* If we made it here, it must be an fx2lafw. */
141 libusb_free_config_descriptor(conf_dsc);
146 static int fx2lafw_open_dev(int dev_index)
148 libusb_device **devlist;
149 struct libusb_device_descriptor des;
150 struct sr_dev_inst *sdi;
154 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
158 if (sdi->status == SR_ST_ACTIVE)
163 libusb_get_device_list(usb_context, &devlist);
164 for (i = 0; devlist[i]; i++) {
165 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
166 sr_err("fx2lafw: failed to get device descriptor: %d", err);
170 if (des.idVendor != ctx->profile->vid
171 || des.idProduct != ctx->profile->pid)
174 if (sdi->status == SR_ST_INITIALIZING) {
175 if (skip != dev_index) {
176 /* Skip devices of this type that aren't the one we want. */
180 } else if (sdi->status == SR_ST_INACTIVE) {
182 * This device is fully enumerated, so we need to find
183 * this device by vendor, product, bus and address.
185 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
186 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
187 /* this is not the one */
191 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
192 if (ctx->usb->address == 0xff)
194 * first time we touch this device after firmware upload,
195 * so we don't know the address yet.
197 ctx->usb->address = libusb_get_device_address(devlist[i]);
199 sdi->status = SR_ST_ACTIVE;
200 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
201 sdi->index, ctx->usb->bus,
202 ctx->usb->address, USB_INTERFACE);
204 sr_err("fx2lafw: failed to open device: %d", err);
207 /* if we made it here, we handled the device one way or another */
210 libusb_free_device_list(devlist, 1);
212 if (sdi->status != SR_ST_ACTIVE)
218 static void close_dev(struct sr_dev_inst *sdi)
224 if (ctx->usb->devhdl == NULL)
227 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
228 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
229 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
230 libusb_close(ctx->usb->devhdl);
231 ctx->usb->devhdl = NULL;
232 sdi->status = SR_ST_INACTIVE;
235 static struct context *fx2lafw_device_new(void)
239 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
240 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
251 static int hw_init(const char *deviceinfo)
253 struct sr_dev_inst *sdi;
254 struct libusb_device_descriptor des;
255 struct fx2lafw_profile *fx2lafw_prof;
257 libusb_device **devlist;
262 /* Avoid compiler warnings. */
265 if (libusb_init(&usb_context) != 0) {
266 sr_warn("Failed to initialize USB.");
270 /* Find all fx2lafw compatible devices and upload firware to all of them. */
271 libusb_get_device_list(usb_context, &devlist);
272 for (i = 0; devlist[i]; i++) {
274 if ((err = libusb_get_device_descriptor(
275 devlist[i], &des)) != 0) {
276 sr_warn("failed to get device descriptor: %d", err);
281 for (j = 0; supported_fx2[j].vid; j++) {
282 if (des.idVendor == supported_fx2[j].vid &&
283 des.idProduct == supported_fx2[j].pid) {
284 fx2lafw_prof = &supported_fx2[j];
288 /* Skip if the device was not found */
292 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
293 fx2lafw_prof->vendor, fx2lafw_prof->model,
294 fx2lafw_prof->model_version);
298 ctx = fx2lafw_device_new();
299 ctx->profile = fx2lafw_prof;
301 dev_insts = g_slist_append(dev_insts, sdi);
303 if (check_conf_profile(devlist[i])) {
304 /* Already has the firmware, so fix the new address. */
305 sr_dbg("fx2lafw: Found a fx2lafw device.");
306 sdi->status = SR_ST_INACTIVE;
307 ctx->usb = sr_usb_dev_inst_new
308 (libusb_get_bus_number(devlist[i]),
309 libusb_get_device_address(devlist[i]), NULL);
311 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
312 /* Remember when the firmware on this device was updated */
313 g_get_current_time(&ctx->fw_updated);
315 sr_err("fx2lafw: firmware upload failed for "
316 "device %d", devcnt);
317 ctx->usb = sr_usb_dev_inst_new
318 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
323 libusb_free_device_list(devlist, 1);
328 static int hw_dev_open(int dev_index)
331 struct sr_dev_inst *sdi;
335 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
340 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
341 * for the FX2 to renumerate
344 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
345 sr_info("fx2lafw: waiting for device to reset");
346 /* takes at least 300ms for the FX2 to be gone from the USB bus */
347 g_usleep(300 * 1000);
349 while (timediff < MAX_RENUM_DELAY) {
350 if ((err = fx2lafw_open_dev(dev_index)) == SR_OK)
352 g_usleep(100 * 1000);
353 g_get_current_time(&cur_time);
354 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
356 sr_info("fx2lafw: device came back after %d ms", timediff);
358 err = fx2lafw_open_dev(dev_index);
362 sr_err("fx2lafw: unable to open device");
367 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
369 sr_err("fx2lafw: Unable to claim interface: %d", err);
373 if (ctx->cur_samplerate == 0) {
374 /* Samplerate hasn't been set; default to the slowest one. */
375 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
376 &fx2lafw_supported_samplerates[0]) == SR_ERR)
383 static int hw_dev_close(int dev_index)
385 struct sr_dev_inst *sdi;
387 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
388 sr_err("fx2lafw: %s: sdi was NULL", __func__);
398 static int hw_cleanup(void)
401 struct sr_dev_inst *sdi;
405 for(l = dev_insts; l; l = l->next) {
406 if (!(sdi = l->data)) {
407 /* Log error, but continue cleaning up the rest. */
408 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
412 if (!(ctx = sdi->priv)) {
413 /* Log error, but continue cleaning up the rest. */
414 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
421 sr_dev_inst_free(sdi);
424 g_slist_free(dev_insts);
428 libusb_exit(usb_context);
434 static void *hw_dev_info_get(int dev_index, int dev_info_id)
436 struct sr_dev_inst *sdi;
439 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
443 switch (dev_info_id) {
446 case SR_DI_NUM_PROBES:
447 return GINT_TO_POINTER(ctx->profile->num_probes);
448 case SR_DI_PROBE_NAMES:
449 return fx2lafw_probe_names;
450 case SR_DI_SAMPLERATES:
451 return &fx2lafw_samplerates;
452 case SR_DI_TRIGGER_TYPES:
453 return TRIGGER_TYPES;
454 case SR_DI_CUR_SAMPLERATE:
455 return &ctx->cur_samplerate;
461 static int hw_dev_status_get(int dev_index)
463 const struct sr_dev_inst *const sdi =
464 sr_dev_inst_get(dev_insts, dev_index);
467 return SR_ST_NOT_FOUND;
472 static int *hw_hwcap_get_all(void)
474 return fx2lafw_capabilities;
477 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
479 struct sr_dev_inst *sdi;
483 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
487 if (hwcap == SR_HWCAP_SAMPLERATE) {
488 ctx->cur_samplerate = *(uint64_t *)value;
490 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
492 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
493 ctx->limit_samples = *(uint64_t *)value;
502 static int receive_data(int fd, int revents, void *cb_data)
506 /* Avoid compiler warnings. */
511 tv.tv_sec = tv.tv_usec = 0;
512 libusb_handle_events_timeout(usb_context, &tv);
517 static void abort_acquisition(struct context *ctx)
519 struct sr_datafeed_packet packet;
521 packet.type = SR_DF_END;
522 sr_session_send(ctx->session_dev_id, &packet);
524 ctx->num_samples = -1;
526 /* TODO: Need to cancel and free any queued up transfers. */
529 static void receive_transfer(struct libusb_transfer *transfer)
531 /* TODO: These statics have to move to the ctx struct. */
532 static int empty_transfer_count = 0;
533 struct sr_datafeed_packet packet;
534 struct sr_datafeed_logic logic;
535 struct context *ctx = transfer->user_data;
537 unsigned char *cur_buf, *new_buf;
540 * If acquisition has already ended, just free any queued up
541 * transfer that come in.
543 if (ctx->num_samples == -1) {
545 libusb_free_transfer(transfer);
549 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
550 transfer->status, transfer->actual_length);
552 /* Save incoming transfer before reusing the transfer struct. */
553 cur_buf = transfer->buffer;
554 cur_buflen = transfer->actual_length;
555 ctx = transfer->user_data;
557 /* Fire off a new request. */
558 if (!(new_buf = g_try_malloc(4096))) {
559 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
560 return; /* TODO: SR_ERR_MALLOC */
563 transfer->buffer = new_buf;
564 transfer->length = 4096;
565 if (libusb_submit_transfer(transfer) != 0) {
566 /* TODO: Stop session? */
567 /* TODO: Better error message. */
568 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
571 if (cur_buflen == 0) {
572 empty_transfer_count++;
573 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
575 * The FX2 gave up. End the acquisition, the frontend
576 * will work out that the samplecount is short.
578 abort_acquisition(ctx);
582 empty_transfer_count = 0;
585 /* Send the incoming transfer to the session bus. */
586 packet.type = SR_DF_LOGIC;
587 packet.payload = &logic;
588 logic.length = cur_buflen;
590 logic.data = cur_buf;
591 sr_session_send(ctx->session_dev_id, &packet);
594 ctx->num_samples += cur_buflen;
595 if (ctx->limit_samples &&
596 (unsigned int) ctx->num_samples > ctx->limit_samples) {
597 abort_acquisition(ctx);
601 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
603 struct sr_dev_inst *sdi;
604 struct sr_datafeed_packet *packet;
605 struct sr_datafeed_header *header;
607 struct libusb_transfer *transfer;
608 const struct libusb_pollfd **lupfd;
612 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
615 ctx->session_dev_id = cb_data;
616 ctx->num_samples = 0;
618 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
619 sr_err("fx2lafw: %s: packet malloc failed", __func__);
620 return SR_ERR_MALLOC;
623 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
624 sr_err("fx2lafw: %s: header malloc failed", __func__);
625 return SR_ERR_MALLOC;
628 if ((err = command_start_acquisition (ctx->usb->devhdl,
629 ctx->cur_samplerate)) != SR_OK) {
633 /* Start with 2K transfer, subsequently increased to 4K. */
635 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
636 if (!(buf = g_try_malloc(size))) {
637 sr_err("fx2lafw: %s: buf malloc failed", __func__);
638 return SR_ERR_MALLOC;
640 transfer = libusb_alloc_transfer(0);
641 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
642 2 | LIBUSB_ENDPOINT_IN, buf, size,
643 receive_transfer, ctx, 40);
644 if (libusb_submit_transfer(transfer) != 0) {
645 /* TODO: Free them all. */
646 libusb_free_transfer(transfer);
653 lupfd = libusb_get_pollfds(usb_context);
654 for (i = 0; lupfd[i]; i++)
655 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
656 40, receive_data, NULL);
657 free(lupfd); /* NOT g_free()! */
659 packet->type = SR_DF_HEADER;
660 packet->payload = header;
661 header->feed_version = 1;
662 gettimeofday(&header->starttime, NULL);
663 header->samplerate = ctx->cur_samplerate;
664 header->num_logic_probes = ctx->profile->num_probes;
665 sr_session_send(cb_data, packet);
672 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
673 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
675 struct sr_dev_inst *sdi;
677 /* unused parameter */
680 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
683 abort_acquisition(sdi->priv);
688 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
690 .longname = "fx2lafw",
693 .cleanup = hw_cleanup,
694 .dev_open = hw_dev_open,
695 .dev_close = hw_dev_close,
696 .dev_info_get = hw_dev_info_get,
697 .dev_status_get = hw_dev_status_get,
698 .hwcap_get_all = hw_hwcap_get_all,
699 .dev_config_set = hw_dev_config_set,
700 .dev_acquisition_start = hw_dev_acquisition_start,
701 .dev_acquisition_stop = hw_dev_acquisition_stop,