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"
30 static struct fx2lafw_profile supported_fx2[] = {
32 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
36 static int fx2lafw_capabilities[] = {
37 SR_HWCAP_LOGIC_ANALYZER,
40 /* These are really implemented in the driver, not the hardware. */
41 SR_HWCAP_LIMIT_SAMPLES,
46 static const char *fx2lafw_probe_names[] = {
58 static uint64_t fx2lafw_supported_samplerates[] = {
70 static struct sr_samplerates fx2lafw_samplerates = {
74 fx2lafw_supported_samplerates,
77 static GSList *dev_insts = NULL;
78 static libusb_context *usb_context = NULL;
80 static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id);
83 * Check the USB configuration to determine if this is an fx2lafw device.
85 * @return TRUE if the device's configuration profile match fx2lafw
86 * configuration, FALSE otherwise.
88 static gboolean check_conf_profile(libusb_device *dev)
90 struct libusb_device_descriptor des;
91 struct libusb_config_descriptor *conf_dsc = NULL;
92 const struct libusb_interface_descriptor *intf_dsc;
96 /* Assume it's not a Saleae Logic unless proven wrong. */
99 if (libusb_get_device_descriptor(dev, &des) != 0)
102 if (des.bNumConfigurations != 1)
103 /* Need exactly 1 configuration. */
106 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
109 if (conf_dsc->bNumInterfaces != 1)
110 /* Need exactly 1 interface. */
113 if (conf_dsc->interface[0].num_altsetting != 1)
114 /* Need just one alternate setting. */
117 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
118 if (intf_dsc->bNumEndpoints != 3)
119 /* Need exactly 3 end points. */
122 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
123 (1 | LIBUSB_ENDPOINT_OUT))
124 /* The first endpoint should be 1 (outbound). */
127 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
128 (2 | LIBUSB_ENDPOINT_IN))
129 /* The second endpoint should be 2 (inbound). */
132 /* TODO: Check the debug channel... */
134 /* If we made it here, it must be an fx2lafw. */
139 libusb_free_config_descriptor(conf_dsc);
144 static int fx2lafw_open_dev(int dev_index)
146 libusb_device **devlist;
147 struct libusb_device_descriptor des;
148 struct sr_dev_inst *sdi;
152 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
156 if (sdi->status == SR_ST_ACTIVE)
161 libusb_get_device_list(usb_context, &devlist);
162 for (i = 0; devlist[i]; i++) {
163 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
164 sr_err("fx2lafw: failed to get device descriptor: %d", err);
168 if (des.idVendor != FIRMWARE_VID
169 || des.idProduct != FIRMWARE_PID)
172 if (sdi->status == SR_ST_INITIALIZING) {
173 if (skip != dev_index) {
174 /* Skip devices of this type that aren't the one we want. */
178 } else if (sdi->status == SR_ST_INACTIVE) {
180 * This device is fully enumerated, so we need to find
181 * this device by vendor, product, bus and address.
183 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
184 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
185 /* this is not the one */
189 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
190 if (ctx->usb->address == 0xff)
192 * first time we touch this device after firmware upload,
193 * so we don't know the address yet.
195 ctx->usb->address = libusb_get_device_address(devlist[i]);
197 sdi->status = SR_ST_ACTIVE;
198 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
199 sdi->index, ctx->usb->bus,
200 ctx->usb->address, USB_INTERFACE);
202 sr_err("fx2lafw: failed to open device: %d", err);
205 /* if we made it here, we handled the device one way or another */
208 libusb_free_device_list(devlist, 1);
210 if (sdi->status != SR_ST_ACTIVE)
216 static void close_dev(struct sr_dev_inst *sdi)
222 if (ctx->usb->devhdl == NULL)
225 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
226 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
227 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
228 libusb_close(ctx->usb->devhdl);
229 ctx->usb->devhdl = NULL;
230 sdi->status = SR_ST_INACTIVE;
233 static struct context *fx2lafw_device_new(void)
237 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
238 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
249 static int hw_init(const char *deviceinfo)
251 struct sr_dev_inst *sdi;
252 struct libusb_device_descriptor des;
253 struct fx2lafw_profile *fx2lafw_prof;
255 libusb_device **devlist;
260 /* Avoid compiler warnings. */
263 if (libusb_init(&usb_context) != 0) {
264 sr_warn("Failed to initialize USB.");
268 /* Find all fx2lafw compatible devices and upload firware to all of them. */
269 libusb_get_device_list(usb_context, &devlist);
270 for (i = 0; devlist[i]; i++) {
272 if ((err = libusb_get_device_descriptor(
273 devlist[i], &des)) != 0) {
274 sr_warn("failed to get device descriptor: %d", err);
279 for (j = 0; supported_fx2[j].vid; j++) {
280 if (des.idVendor == supported_fx2[j].vid &&
281 des.idProduct == supported_fx2[j].pid) {
282 fx2lafw_prof = &supported_fx2[j];
286 /* Skip if the device was not found */
290 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
291 fx2lafw_prof->vendor, fx2lafw_prof->model,
292 fx2lafw_prof->model_version);
296 ctx = fx2lafw_device_new();
297 ctx->profile = fx2lafw_prof;
299 dev_insts = g_slist_append(dev_insts, sdi);
301 if (check_conf_profile(devlist[i])) {
302 /* Already has the firmware, so fix the new address. */
303 sr_dbg("fx2lafw: Found a fx2lafw device.");
304 sdi->status = SR_ST_INACTIVE;
305 ctx->usb = sr_usb_dev_inst_new
306 (libusb_get_bus_number(devlist[i]),
307 libusb_get_device_address(devlist[i]), NULL);
309 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
310 /* Remember when the firmware on this device was updated */
311 g_get_current_time(&ctx->fw_updated);
313 sr_err("fx2lafw: firmware upload failed for "
314 "device %d", devcnt);
315 ctx->usb = sr_usb_dev_inst_new
316 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
321 libusb_free_device_list(devlist, 1);
326 static int hw_dev_open(int dev_index)
329 struct sr_dev_inst *sdi;
333 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
338 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
339 * for the FX2 to renumerate
342 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
343 sr_info("fx2lafw: waiting for device to reset");
344 /* takes at least 300ms for the FX2 to be gone from the USB bus */
345 g_usleep(300 * 1000);
347 while (timediff < MAX_RENUM_DELAY) {
348 if ((err = fx2lafw_open_dev(dev_index)) == SR_OK)
350 g_usleep(100 * 1000);
351 g_get_current_time(&cur_time);
352 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
354 sr_info("fx2lafw: device came back after %d ms", timediff);
356 err = fx2lafw_open_dev(dev_index);
360 sr_err("fx2lafw: unable to open device");
365 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
367 sr_err("fx2lafw: Unable to claim interface: %d", err);
374 static int hw_dev_close(int dev_index)
376 struct sr_dev_inst *sdi;
378 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
379 sr_err("fx2lafw: %s: sdi was NULL", __func__);
380 return SR_ERR; /* TODO: SR_ERR_ARG? */
389 static int hw_cleanup(void)
392 struct sr_dev_inst *sdi;
396 for(l = dev_insts; l; l = l->next) {
397 if (!(sdi = l->data)) {
398 /* Log error, but continue cleaning up the rest. */
399 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
403 if (!(ctx = sdi->priv)) {
404 /* Log error, but continue cleaning up the rest. */
405 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
412 sr_dev_inst_free(sdi);
415 g_slist_free(dev_insts);
419 libusb_exit(usb_context);
425 static void *hw_dev_info_get(int dev_index, int dev_info_id)
427 struct sr_dev_inst *sdi;
430 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
434 switch (dev_info_id) {
437 case SR_DI_NUM_PROBES:
438 return GINT_TO_POINTER(ctx->profile->num_probes);
439 case SR_DI_PROBE_NAMES:
440 return fx2lafw_probe_names;
441 case SR_DI_SAMPLERATES:
442 return &fx2lafw_samplerates;
443 case SR_DI_TRIGGER_TYPES:
444 return TRIGGER_TYPES;
450 static int hw_dev_status_get(int dev_index)
452 const struct sr_dev_inst *const sdi =
453 sr_dev_inst_get(dev_insts, dev_index);
456 return SR_ST_NOT_FOUND;
461 static int *hw_hwcap_get_all(void)
463 return fx2lafw_capabilities;
466 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
468 struct sr_dev_inst *sdi;
472 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
476 if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
477 ctx->limit_samples = *(uint64_t *)value;
486 static int receive_data(int fd, int revents, void *user_data)
490 /* Avoid compiler warnings. */
495 tv.tv_sec = tv.tv_usec = 0;
496 libusb_handle_events_timeout(usb_context, &tv);
501 static void receive_transfer(struct libusb_transfer *transfer)
503 /* TODO: These statics have to move to the ctx struct. */
504 static int num_samples = 0;
505 static int empty_transfer_count = 0;
506 struct sr_datafeed_packet packet;
507 struct sr_datafeed_logic logic;
510 unsigned char *cur_buf, *new_buf;
512 /* hw_dev_acquisition_stop() is telling us to stop. */
513 if (transfer == NULL)
517 * If acquisition has already ended, just free any queued up
518 * transfer that come in.
520 if (num_samples == -1) {
522 libusb_free_transfer(transfer);
526 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
527 transfer->status, transfer->actual_length);
529 /* Save incoming transfer before reusing the transfer struct. */
530 cur_buf = transfer->buffer;
531 cur_buflen = transfer->actual_length;
532 ctx = transfer->user_data;
534 /* Fire off a new request. */
535 if (!(new_buf = g_try_malloc(4096))) {
536 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
537 return; /* TODO: SR_ERR_MALLOC */
540 transfer->buffer = new_buf;
541 transfer->length = 4096;
542 if (libusb_submit_transfer(transfer) != 0) {
543 /* TODO: Stop session? */
544 /* TODO: Better error message. */
545 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
548 if (cur_buflen == 0) {
549 empty_transfer_count++;
550 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
552 * The FX2 gave up. End the acquisition, the frontend
553 * will work out that the samplecount is short.
555 hw_dev_acquisition_stop(-1, ctx->session_data);
559 empty_transfer_count = 0;
562 /* Send the incoming transfer to the session bus. */
563 packet.type = SR_DF_LOGIC;
564 packet.payload = &logic;
565 logic.length = cur_buflen;
567 logic.data = cur_buf;
568 sr_session_bus(ctx->session_data, &packet);
571 num_samples += cur_buflen;
572 if (ctx->limit_samples &&
573 (unsigned int) num_samples > ctx->limit_samples) {
574 hw_dev_acquisition_stop(-1, ctx->session_data);
578 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
580 struct sr_dev_inst *sdi;
581 struct sr_datafeed_packet *packet;
582 struct sr_datafeed_header *header;
584 struct libusb_transfer *transfer;
585 const struct libusb_pollfd **lupfd;
589 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
592 ctx->session_data = session_data;
594 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
595 sr_err("fx2lafw: %s: packet malloc failed", __func__);
596 return SR_ERR_MALLOC;
599 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
600 sr_err("fx2lafw: %s: header malloc failed", __func__);
601 return SR_ERR_MALLOC;
604 /* Start with 2K transfer, subsequently increased to 4K. */
606 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
607 if (!(buf = g_try_malloc(size))) {
608 sr_err("fx2lafw: %s: buf malloc failed", __func__);
609 return SR_ERR_MALLOC;
611 transfer = libusb_alloc_transfer(0);
612 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
613 2 | LIBUSB_ENDPOINT_IN, buf, size,
614 receive_transfer, ctx, 40);
615 if (libusb_submit_transfer(transfer) != 0) {
616 /* TODO: Free them all. */
617 libusb_free_transfer(transfer);
624 lupfd = libusb_get_pollfds(usb_context);
625 for (i = 0; lupfd[i]; i++)
626 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
627 40, receive_data, NULL);
628 free(lupfd); /* NOT g_free()! */
630 packet->type = SR_DF_HEADER;
631 packet->payload = header;
632 header->feed_version = 1;
633 gettimeofday(&header->starttime, NULL);
634 header->samplerate = 24000000UL;
635 header->num_logic_probes = ctx->profile->num_probes;
636 sr_session_bus(session_data, packet);
643 /* This stops acquisition on ALL devices, ignoring dev_index. */
644 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
646 struct sr_datafeed_packet packet;
648 /* Avoid compiler warnings. */
651 packet.type = SR_DF_END;
652 sr_session_bus(session_data, &packet);
654 receive_transfer(NULL);
656 /* TODO: Need to cancel and free any queued up transfers. */
661 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
663 .longname = "fx2lafw",
666 .cleanup = hw_cleanup,
667 .dev_open = hw_dev_open,
668 .dev_close = hw_dev_close,
669 .dev_info_get = hw_dev_info_get,
670 .dev_status_get = hw_dev_status_get,
671 .hwcap_get_all = hw_hwcap_get_all,
672 .dev_config_set = hw_dev_config_set,
673 .dev_acquisition_start = hw_dev_acquisition_start,
674 .dev_acquisition_stop = hw_dev_acquisition_stop,