2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 #define LOGIC16_VID 0x21a9
29 #define LOGIC16_PID 0x1001
32 #define USB_INTERFACE 0
33 #define USB_CONFIGURATION 1
34 #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw"
36 #define MAX_RENUM_DELAY_MS 3000
37 #define NUM_SIMUL_TRANSFERS 32
40 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
41 static struct sr_dev_driver *di = &saleae_logic16_driver_info;
43 static const char *probe_names[NUM_PROBES + 1] = {
44 "0", "1", "2", "3", "4", "5", "6", "7", "8",
45 "9", "10", "11", "12", "13", "14", "15",
49 static const uint64_t samplerates[] = {
66 static int init(struct sr_context *sr_ctx)
68 return std_init(sr_ctx, di, LOG_PREFIX);
71 static gboolean check_conf_profile(libusb_device *dev)
73 struct libusb_device_descriptor des;
74 struct libusb_device_handle *hdl;
76 unsigned char strdesc[64];
81 /* Assume the FW has not been loaded, unless proven wrong. */
82 if (libusb_get_device_descriptor(dev, &des) != 0)
85 if (libusb_open(dev, &hdl) != 0)
88 if (libusb_get_string_descriptor_ascii(hdl,
89 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
91 if (strcmp((const char *)strdesc, "Saleae LLC"))
94 if (libusb_get_string_descriptor_ascii(hdl,
95 des.iProduct, strdesc, sizeof(strdesc)) < 0)
97 if (strcmp((const char *)strdesc, "Logic S/16"))
100 /* If we made it here, it must be a configured Logic16. */
109 static GSList *scan(GSList *options)
111 struct drv_context *drvc;
112 struct dev_context *devc;
113 struct sr_dev_inst *sdi;
114 struct sr_usb_dev_inst *usb;
115 struct sr_probe *probe;
116 struct sr_config *src;
117 GSList *l, *devices, *conn_devices;
118 struct libusb_device_descriptor des;
119 libusb_device **devlist;
120 int devcnt, ret, i, j;
126 for (l = options; l; l = l->next) {
130 conn = g_variant_get_string(src->data, NULL);
135 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
139 /* Find all Logic16 devices and upload firmware to them. */
141 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
142 for (i = 0; devlist[i]; i++) {
145 for (l = conn_devices; l; l = l->next) {
147 if (usb->bus == libusb_get_bus_number(devlist[i])
148 && usb->address == libusb_get_device_address(devlist[i]))
152 /* This device matched none of the ones that
153 * matched the conn specification. */
157 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
158 sr_warn("Failed to get device descriptor: %s.",
159 libusb_error_name(ret));
163 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
166 devcnt = g_slist_length(drvc->instances);
167 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
168 "Saleae", "Logic16", NULL);
173 for (j = 0; probe_names[j]; j++) {
174 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
177 sdi->probes = g_slist_append(sdi->probes, probe);
180 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
183 drvc->instances = g_slist_append(drvc->instances, sdi);
184 devices = g_slist_append(devices, sdi);
186 if (check_conf_profile(devlist[i])) {
187 /* Already has the firmware, so fix the new address. */
188 sr_dbg("Found a Logic16 device.");
189 sdi->status = SR_ST_INACTIVE;
190 sdi->inst_type = SR_INST_USB;
191 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
192 libusb_get_device_address(devlist[i]), NULL);
194 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
195 FX2_FIRMWARE) == SR_OK)
196 /* Store when this device's FW was updated. */
197 devc->fw_updated = g_get_monotonic_time();
199 sr_err("Firmware upload failed for "
200 "device %d.", devcnt);
201 sdi->inst_type = SR_INST_USB;
202 sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
206 libusb_free_device_list(devlist, 1);
207 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
212 static GSList *dev_list(void)
214 struct drv_context *drvc;
218 return drvc->instances;
221 static int dev_clear(void)
223 return std_dev_clear(di, NULL);
226 static int logic16_dev_open(struct sr_dev_inst *sdi)
228 libusb_device **devlist;
229 struct sr_usb_dev_inst *usb;
230 struct libusb_device_descriptor des;
231 struct dev_context *devc;
232 struct drv_context *drvc;
233 int ret, skip, i, device_count;
239 if (sdi->status == SR_ST_ACTIVE)
240 /* Device is already in use. */
244 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
245 if (device_count < 0) {
246 sr_err("Failed to get device list: %s.",
247 libusb_error_name(device_count));
251 for (i = 0; i < device_count; i++) {
252 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
253 sr_err("Failed to get device descriptor: %s.",
254 libusb_error_name(ret));
258 if (des.idVendor != LOGIC16_VID
259 || des.idProduct != LOGIC16_PID)
262 if (sdi->status == SR_ST_INITIALIZING) {
263 if (skip != sdi->index) {
264 /* Skip devices of this type that aren't the one we want. */
268 } else if (sdi->status == SR_ST_INACTIVE) {
270 * This device is fully enumerated, so we need to find
271 * this device by vendor, product, bus and address.
273 if (libusb_get_bus_number(devlist[i]) != usb->bus
274 || libusb_get_device_address(devlist[i]) != usb->address)
275 /* This is not the one. */
279 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
280 if (usb->address == 0xff)
282 * First time we touch this device after FW
283 * upload, so we don't know the address yet.
285 usb->address = libusb_get_device_address(devlist[i]);
287 sr_err("Failed to open device: %s.",
288 libusb_error_name(ret));
292 if ((ret = saleae_logic16_init_device(sdi)) != SR_OK) {
293 sr_err("Failed to init device.");
297 sdi->status = SR_ST_ACTIVE;
298 sr_info("Opened device %d on %d.%d, "
300 sdi->index, usb->bus, usb->address,
305 libusb_free_device_list(devlist, 1);
307 if (sdi->status != SR_ST_ACTIVE)
313 static int dev_open(struct sr_dev_inst *sdi)
315 struct sr_usb_dev_inst *usb;
316 struct dev_context *devc;
318 int64_t timediff_us, timediff_ms;
324 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
325 * milliseconds for the FX2 to renumerate.
328 if (devc->fw_updated > 0) {
329 sr_info("Waiting for device to reset.");
330 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
331 g_usleep(300 * 1000);
333 while (timediff_ms < MAX_RENUM_DELAY_MS) {
334 if ((ret = logic16_dev_open(sdi)) == SR_OK)
336 g_usleep(100 * 1000);
338 timediff_us = g_get_monotonic_time() - devc->fw_updated;
339 timediff_ms = timediff_us / 1000;
340 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
343 sr_err("Device failed to renumerate.");
346 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
348 sr_info("Firmware upload was not needed.");
349 ret = logic16_dev_open(sdi);
353 sr_err("Unable to open device.");
357 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
360 case LIBUSB_ERROR_BUSY:
361 sr_err("Unable to claim USB interface. Another "
362 "program or driver has already claimed it.");
364 case LIBUSB_ERROR_NO_DEVICE:
365 sr_err("Device has been disconnected.");
368 sr_err("Unable to claim interface: %s.",
369 libusb_error_name(ret));
376 if (devc->cur_samplerate == 0) {
377 /* Samplerate hasn't been set; default to the slowest one. */
378 devc->cur_samplerate = samplerates[0];
384 static int dev_close(struct sr_dev_inst *sdi)
386 struct sr_usb_dev_inst *usb;
389 if (usb->devhdl == NULL)
392 sr_info("Closing device %d on %d.%d interface %d.",
393 sdi->index, usb->bus, usb->address, USB_INTERFACE);
394 libusb_release_interface(usb->devhdl, USB_INTERFACE);
395 libusb_close(usb->devhdl);
397 sdi->status = SR_ST_INACTIVE;
402 static int cleanup(void)
405 struct drv_context *drvc;
407 if (!(drvc = di->priv))
408 /* Can get called on an unused driver, doesn't matter. */
418 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
420 struct dev_context *devc;
428 case SR_CONF_SAMPLERATE:
432 *data = g_variant_new_uint64(devc->cur_samplerate);
441 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
443 struct dev_context *devc;
446 if (sdi->status != SR_ST_ACTIVE)
447 return SR_ERR_DEV_CLOSED;
453 case SR_CONF_SAMPLERATE:
454 devc->cur_samplerate = g_variant_get_uint64(data);
456 case SR_CONF_LIMIT_SAMPLES:
457 devc->limit_samples = g_variant_get_uint64(data);
466 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
476 case SR_CONF_SAMPLERATE:
477 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
478 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
479 ARRAY_SIZE(samplerates), sizeof(uint64_t));
480 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
481 *data = g_variant_builder_end(&gvb);
490 static void abort_acquisition(struct dev_context *devc)
494 devc->num_samples = -1;
496 for (i = devc->num_transfers - 1; i >= 0; i--) {
497 if (devc->transfers[i])
498 libusb_cancel_transfer(devc->transfers[i]);
502 static unsigned int bytes_per_ms(struct dev_context *devc)
504 return devc->cur_samplerate * devc->num_channels / 8000;
507 static size_t get_buffer_size(struct dev_context *devc)
512 * The buffer should be large enough to hold 10ms of data and
515 s = 10 * bytes_per_ms(devc);
516 return (s + 511) & ~511;
519 static unsigned int get_number_of_transfers(struct dev_context *devc)
523 /* Total buffer size should be able to hold about 500ms of data. */
524 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
526 if (n > NUM_SIMUL_TRANSFERS)
527 return NUM_SIMUL_TRANSFERS;
532 static unsigned int get_timeout(struct dev_context *devc)
535 unsigned int timeout;
537 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
538 timeout = total_size / bytes_per_ms(devc);
539 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
542 static int configure_probes(const struct sr_dev_inst *sdi)
544 struct dev_context *devc;
545 struct sr_probe *probe;
551 devc->cur_channels = 0;
552 devc->num_channels = 0;
553 for (l = sdi->probes; l; l = l->next) {
554 probe = (struct sr_probe *)l->data;
555 if (probe->enabled == FALSE)
558 probe_bit = 1 << (probe->index);
560 devc->cur_channels |= probe_bit;
562 #ifdef WORDS_BIGENDIAN
563 /* Output logic data should be stored in little endian
564 format. To speed things up during conversion, do the
565 switcharoo here instead. */
567 probe_bit = 1 << (probe->index ^ 8);
570 devc->channel_masks[devc->num_channels ++] = probe_bit;
576 static int receive_data(int fd, int revents, void *cb_data)
579 struct dev_context *devc;
580 struct drv_context *drvc;
581 const struct sr_dev_inst *sdi;
590 tv.tv_sec = tv.tv_usec = 0;
591 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
593 if (devc->num_samples == -2) {
594 saleae_logic16_abort_acquisition(sdi);
595 abort_acquisition(devc);
601 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
604 struct dev_context *devc;
605 struct drv_context *drvc;
606 struct sr_usb_dev_inst *usb;
607 struct libusb_transfer *transfer;
608 const struct libusb_pollfd **lupfd;
609 unsigned int i, timeout, num_transfers;
612 size_t size, convsize;
614 if (sdi->status != SR_ST_ACTIVE)
615 return SR_ERR_DEV_CLOSED;
621 /* Configures devc->cur_channels */
622 if (configure_probes(sdi) != SR_OK) {
623 sr_err("Failed to configure probes.");
627 devc->cb_data = cb_data;
628 devc->num_samples = 0;
629 devc->empty_transfer_count = 0;
630 devc->cur_channel = 0;
631 memset(devc->channel_data, 0, sizeof(devc->channel_data));
633 timeout = get_timeout(devc);
634 num_transfers = get_number_of_transfers(devc);
635 size = get_buffer_size(devc);
636 convsize = (size / devc->num_channels + 2) * 16;
637 devc->submitted_transfers = 0;
640 devc->convbuffer_size = convsize;
641 if (!(devc->convbuffer = g_try_malloc(convsize))) {
642 sr_err("Conversion buffer malloc failed.");
643 return SR_ERR_MALLOC;
646 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
647 if (!devc->transfers) {
648 sr_err("USB transfers malloc failed.");
649 g_free(devc->convbuffer);
650 return SR_ERR_MALLOC;
653 if ((ret = saleae_logic16_setup_acquisition(sdi, devc->cur_samplerate,
654 devc->cur_channels)) != SR_OK) {
655 g_free(devc->transfers);
656 g_free(devc->convbuffer);
660 devc->num_transfers = num_transfers;
661 for (i = 0; i < num_transfers; i++) {
662 if (!(buf = g_try_malloc(size))) {
663 sr_err("USB transfer buffer malloc failed.");
664 if (devc->submitted_transfers)
665 abort_acquisition(devc);
667 g_free(devc->transfers);
668 g_free(devc->convbuffer);
670 return SR_ERR_MALLOC;
672 transfer = libusb_alloc_transfer(0);
673 libusb_fill_bulk_transfer(transfer, usb->devhdl,
674 2 | LIBUSB_ENDPOINT_IN, buf, size,
675 saleae_logic16_receive_transfer, devc, timeout);
676 if ((ret = libusb_submit_transfer(transfer)) != 0) {
677 sr_err("Failed to submit transfer: %s.",
678 libusb_error_name(ret));
679 libusb_free_transfer(transfer);
681 abort_acquisition(devc);
684 devc->transfers[i] = transfer;
685 devc->submitted_transfers++;
688 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
689 for (i = 0; lupfd[i]; i++);
690 if (!(devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1)))) {
691 abort_acquisition(devc);
695 for (i = 0; lupfd[i]; i++) {
696 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
697 timeout, receive_data, (void *)sdi);
698 devc->usbfd[i] = lupfd[i]->fd;
703 /* Send header packet to the session bus. */
704 std_session_send_df_header(cb_data, LOG_PREFIX);
706 if ((ret = saleae_logic16_start_acquisition(sdi)) != SR_OK) {
707 abort_acquisition(devc);
714 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
720 if (sdi->status != SR_ST_ACTIVE)
721 return SR_ERR_DEV_CLOSED;
723 ret = saleae_logic16_abort_acquisition(sdi);
725 abort_acquisition(sdi->priv);
730 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
731 .name = "saleae-logic16",
732 .longname = "Saleae Logic16",
737 .dev_list = dev_list,
738 .dev_clear = dev_clear,
739 .config_get = config_get,
740 .config_set = config_set,
741 .config_list = config_list,
742 .dev_open = dev_open,
743 .dev_close = dev_close,
744 .dev_acquisition_start = dev_acquisition_start,
745 .dev_acquisition_stop = dev_acquisition_stop,