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/>.
29 #include "sigrok-internal.h"
32 static struct fx2lafw_profile supported_fx2[] = {
34 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
38 static int fx2lafw_capabilities[] = {
39 SR_HWCAP_LOGIC_ANALYZER,
42 /* These are really implemented in the driver, not the hardware. */
43 SR_HWCAP_LIMIT_SAMPLES,
48 static const char *fx2lafw_probe_names[] = {
60 static uint64_t fx2lafw_supported_samplerates[] = {
72 static struct sr_samplerates fx2lafw_samplerates = {
76 fx2lafw_supported_samplerates
79 static GSList *dev_insts = NULL;
80 static libusb_context *usb_context = NULL;
82 static int hw_dev_acquisition_stop(int dev_index, gpointer 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, flase otherwise.
90 static bool 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;
151 struct fx2lafw_device *ctx;
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 != FIRMWARE_VID
171 || des.idProduct != FIRMWARE_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)
220 struct fx2lafw_device *ctx;
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 fx2lafw_device* fx2lafw_device_new(void)
237 struct fx2lafw_device *fx2lafw;
239 if (!(fx2lafw = g_try_malloc0(sizeof(struct fx2lafw_device)))) {
240 sr_err("fx2lafw: %s: fx2lafw_device 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;
256 struct fx2lafw_device *ctx;
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 device_index)
331 struct sr_dev_inst *sdi;
332 struct fx2lafw_device *ctx;
335 if (!(sdi = sr_dev_inst_get(dev_insts, device_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(device_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(device_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);
376 static int hw_dev_close(int dev_index)
378 struct sr_dev_inst *sdi;
380 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
381 sr_err("fx2lafw: %s: sdi was NULL", __func__);
382 return SR_ERR; /* TODO: SR_ERR_ARG? */
391 static int hw_cleanup(void)
394 struct sr_dev_inst *sdi;
395 struct fx2lafw_device *ctx;
398 for(l = dev_insts; l; l = l->next) {
399 if (!(sdi = l->data)) {
400 /* Log error, but continue cleaning up the rest. */
401 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
405 if (!(ctx = sdi->priv)) {
406 /* Log error, but continue cleaning up the rest. */
407 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
414 sr_dev_inst_free(sdi);
417 g_slist_free(dev_insts);
421 libusb_exit(usb_context);
427 static void *hw_dev_info_get(int device_index, int device_info_id)
429 struct sr_dev_inst *sdi;
430 struct fx2lafw_device *ctx;
432 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
436 switch (device_info_id) {
439 case SR_DI_NUM_PROBES:
440 return GINT_TO_POINTER(ctx->profile->num_probes);
441 case SR_DI_PROBE_NAMES:
442 return fx2lafw_probe_names;
443 case SR_DI_SAMPLERATES:
444 return &fx2lafw_samplerates;
445 case SR_DI_TRIGGER_TYPES:
446 return TRIGGER_TYPES;
452 static int hw_dev_status_get(int device_index)
454 const struct sr_dev_inst *const sdi =
455 sr_dev_inst_get(dev_insts, device_index);
458 return SR_ST_NOT_FOUND;
463 static int *hw_hwcap_get_all(void)
465 return fx2lafw_capabilities;
468 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
470 struct sr_dev_inst *sdi;
471 struct fx2lafw_device *ctx;
474 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
478 if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
479 ctx->limit_samples = *(uint64_t *)value;
488 static int receive_data(int fd, int revents, void *user_data)
492 /* Avoid compiler warnings. */
497 tv.tv_sec = tv.tv_usec = 0;
498 libusb_handle_events_timeout(usb_context, &tv);
503 static void receive_transfer(struct libusb_transfer *transfer)
505 /* TODO: These statics have to move to the ctx struct. */
506 static int num_samples = 0;
507 static int empty_transfer_count = 0;
508 struct sr_datafeed_packet packet;
509 struct sr_datafeed_logic logic;
510 struct fx2lafw_device *ctx;
512 unsigned char *cur_buf, *new_buf;
514 /* hw_dev_acquisition_stop() is telling us to stop. */
515 if (transfer == NULL)
519 * If acquisition has already ended, just free any queued up
520 * transfer that come in.
522 if (num_samples == -1) {
524 libusb_free_transfer(transfer);
528 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
529 transfer->status, transfer->actual_length);
531 /* Save incoming transfer before reusing the transfer struct. */
532 cur_buf = transfer->buffer;
533 cur_buflen = transfer->actual_length;
534 ctx = transfer->user_data;
536 /* Fire off a new request. */
537 if (!(new_buf = g_try_malloc(4096))) {
538 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
539 return; /* TODO: SR_ERR_MALLOC */
542 transfer->buffer = new_buf;
543 transfer->length = 4096;
544 if (libusb_submit_transfer(transfer) != 0) {
545 /* TODO: Stop session? */
546 /* TODO: Better error message. */
547 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
550 if (cur_buflen == 0) {
551 empty_transfer_count++;
552 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
554 * The FX2 gave up. End the acquisition, the frontend
555 * will work out that the samplecount is short.
557 hw_dev_acquisition_stop(-1, ctx->session_data);
561 empty_transfer_count = 0;
564 /* Send the incoming transfer to the session bus. */
565 packet.type = SR_DF_LOGIC;
566 packet.payload = &logic;
567 logic.length = cur_buflen;
569 logic.data = cur_buf;
570 sr_session_bus(ctx->session_data, &packet);
573 num_samples += cur_buflen;
574 if (ctx->limit_samples &&
575 (unsigned int) num_samples > ctx->limit_samples) {
576 hw_dev_acquisition_stop(-1, ctx->session_data);
580 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
582 struct sr_dev_inst *sdi;
583 struct sr_datafeed_packet *packet;
584 struct sr_datafeed_header *header;
585 struct fx2lafw_device *ctx;
586 struct libusb_transfer *transfer;
587 const struct libusb_pollfd **lupfd;
591 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
594 ctx->session_data = session_data;
596 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
597 sr_err("fx2lafw: %s: packet malloc failed", __func__);
598 return SR_ERR_MALLOC;
601 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
602 sr_err("fx2lafw: %s: header malloc failed", __func__);
603 return SR_ERR_MALLOC;
606 /* Start with 2K transfer, subsequently increased to 4K. */
608 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
609 if (!(buf = g_try_malloc(size))) {
610 sr_err("fx2lafw: %s: buf malloc failed", __func__);
611 return SR_ERR_MALLOC;
613 transfer = libusb_alloc_transfer(0);
614 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
615 2 | LIBUSB_ENDPOINT_IN, buf, size,
616 receive_transfer, ctx, 40);
617 if (libusb_submit_transfer(transfer) != 0) {
618 /* TODO: Free them all. */
619 libusb_free_transfer(transfer);
626 lupfd = libusb_get_pollfds(usb_context);
627 for (i = 0; lupfd[i]; i++)
628 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
629 40, receive_data, NULL);
630 free(lupfd); /* NOT g_free()! */
632 packet->type = SR_DF_HEADER;
633 packet->payload = header;
634 header->feed_version = 1;
635 gettimeofday(&header->starttime, NULL);
636 header->samplerate = 24000000UL;
637 header->num_logic_probes = ctx->profile->num_probes;
638 sr_session_bus(session_data, packet);
645 /* This stops acquisition on ALL devices, ignoring device_index. */
646 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
648 struct sr_datafeed_packet packet;
650 /* Avoid compiler warnings. */
653 packet.type = SR_DF_END;
654 sr_session_bus(session_data, &packet);
656 receive_transfer(NULL);
658 /* TODO: Need to cancel and free any queued up transfers. */
663 SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
665 .longname = "fx2lafw",
668 .cleanup = hw_cleanup,
669 .dev_open = hw_dev_open,
670 .dev_close = hw_dev_close,
671 .dev_info_get = hw_dev_info_get,
672 .dev_status_get = hw_dev_status_get,
673 .hwcap_get_all = hw_hwcap_get_all,
674 .dev_config_set = hw_dev_config_set,
675 .dev_acquisition_start = hw_dev_acquisition_start,
676 .dev_acquisition_stop = hw_dev_acquisition_stop,