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 int32_t hwopts[] = {
47 static const int32_t hwcaps[] = {
48 SR_CONF_LOGIC_ANALYZER,
51 /* These are really implemented in the driver, not the hardware. */
52 SR_CONF_LIMIT_SAMPLES,
56 static const char *probe_names[NUM_PROBES + 1] = {
57 "0", "1", "2", "3", "4", "5", "6", "7", "8",
58 "9", "10", "11", "12", "13", "14", "15",
62 static const uint64_t samplerates[] = {
79 static int init(struct sr_context *sr_ctx)
81 return std_init(sr_ctx, di, LOG_PREFIX);
84 static gboolean check_conf_profile(libusb_device *dev)
86 struct libusb_device_descriptor des;
87 struct libusb_device_handle *hdl;
89 unsigned char strdesc[64];
94 /* Assume the FW has not been loaded, unless proven wrong. */
95 if (libusb_get_device_descriptor(dev, &des) != 0)
98 if (libusb_open(dev, &hdl) != 0)
101 if (libusb_get_string_descriptor_ascii(hdl,
102 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
104 if (strcmp((const char *)strdesc, "Saleae LLC"))
107 if (libusb_get_string_descriptor_ascii(hdl,
108 des.iProduct, strdesc, sizeof(strdesc)) < 0)
110 if (strcmp((const char *)strdesc, "Logic S/16"))
113 /* If we made it here, it must be a configured Logic16. */
122 static GSList *scan(GSList *options)
124 struct drv_context *drvc;
125 struct dev_context *devc;
126 struct sr_dev_inst *sdi;
127 struct sr_usb_dev_inst *usb;
128 struct sr_probe *probe;
129 struct sr_config *src;
130 GSList *l, *devices, *conn_devices;
131 struct libusb_device_descriptor des;
132 libusb_device **devlist;
133 int devcnt, ret, i, j;
139 for (l = options; l; l = l->next) {
143 conn = g_variant_get_string(src->data, NULL);
148 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
152 /* Find all Logic16 devices and upload firmware to them. */
154 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
155 for (i = 0; devlist[i]; i++) {
158 for (l = conn_devices; l; l = l->next) {
160 if (usb->bus == libusb_get_bus_number(devlist[i])
161 && usb->address == libusb_get_device_address(devlist[i]))
165 /* This device matched none of the ones that
166 * matched the conn specification. */
170 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
171 sr_warn("Failed to get device descriptor: %s.",
172 libusb_error_name(ret));
176 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
179 devcnt = g_slist_length(drvc->instances);
180 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
181 "Saleae", "Logic16", NULL);
186 for (j = 0; probe_names[j]; j++) {
187 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
190 sdi->probes = g_slist_append(sdi->probes, probe);
193 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
196 drvc->instances = g_slist_append(drvc->instances, sdi);
197 devices = g_slist_append(devices, sdi);
199 if (check_conf_profile(devlist[i])) {
200 /* Already has the firmware, so fix the new address. */
201 sr_dbg("Found a Logic16 device.");
202 sdi->status = SR_ST_INACTIVE;
203 sdi->inst_type = SR_INST_USB;
204 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
205 libusb_get_device_address(devlist[i]), NULL);
207 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
208 FX2_FIRMWARE) == SR_OK)
209 /* Store when this device's FW was updated. */
210 devc->fw_updated = g_get_monotonic_time();
212 sr_err("Firmware upload failed for "
213 "device %d.", devcnt);
214 sdi->inst_type = SR_INST_USB;
215 sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
219 libusb_free_device_list(devlist, 1);
220 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
225 static GSList *dev_list(void)
227 struct drv_context *drvc;
231 return drvc->instances;
234 static int dev_clear(void)
236 return std_dev_clear(di, NULL);
239 static int logic16_dev_open(struct sr_dev_inst *sdi)
241 libusb_device **devlist;
242 struct sr_usb_dev_inst *usb;
243 struct libusb_device_descriptor des;
244 struct drv_context *drvc;
245 int ret, skip, i, device_count;
250 if (sdi->status == SR_ST_ACTIVE)
251 /* Device is already in use. */
255 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
256 if (device_count < 0) {
257 sr_err("Failed to get device list: %s.",
258 libusb_error_name(device_count));
262 for (i = 0; i < device_count; i++) {
263 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
264 sr_err("Failed to get device descriptor: %s.",
265 libusb_error_name(ret));
269 if (des.idVendor != LOGIC16_VID
270 || des.idProduct != LOGIC16_PID)
273 if (sdi->status == SR_ST_INITIALIZING) {
274 if (skip != sdi->index) {
275 /* Skip devices of this type that aren't the one we want. */
279 } else if (sdi->status == SR_ST_INACTIVE) {
281 * This device is fully enumerated, so we need to find
282 * this device by vendor, product, bus and address.
284 if (libusb_get_bus_number(devlist[i]) != usb->bus
285 || libusb_get_device_address(devlist[i]) != usb->address)
286 /* This is not the one. */
290 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
291 if (usb->address == 0xff)
293 * First time we touch this device after FW
294 * upload, so we don't know the address yet.
296 usb->address = libusb_get_device_address(devlist[i]);
298 sr_err("Failed to open device: %s.",
299 libusb_error_name(ret));
303 if ((ret = saleae_logic16_init_device(sdi)) != SR_OK) {
304 sr_err("Failed to init device.");
308 sdi->status = SR_ST_ACTIVE;
309 sr_info("Opened device %d on %d.%d, "
311 sdi->index, usb->bus, usb->address,
316 libusb_free_device_list(devlist, 1);
318 if (sdi->status != SR_ST_ACTIVE)
324 static int dev_open(struct sr_dev_inst *sdi)
326 struct sr_usb_dev_inst *usb;
327 struct dev_context *devc;
329 int64_t timediff_us, timediff_ms;
335 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
336 * milliseconds for the FX2 to renumerate.
339 if (devc->fw_updated > 0) {
340 sr_info("Waiting for device to reset.");
341 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
342 g_usleep(300 * 1000);
344 while (timediff_ms < MAX_RENUM_DELAY_MS) {
345 if ((ret = logic16_dev_open(sdi)) == SR_OK)
347 g_usleep(100 * 1000);
349 timediff_us = g_get_monotonic_time() - devc->fw_updated;
350 timediff_ms = timediff_us / 1000;
351 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
354 sr_err("Device failed to renumerate.");
357 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
359 sr_info("Firmware upload was not needed.");
360 ret = logic16_dev_open(sdi);
364 sr_err("Unable to open device.");
368 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
371 case LIBUSB_ERROR_BUSY:
372 sr_err("Unable to claim USB interface. Another "
373 "program or driver has already claimed it.");
375 case LIBUSB_ERROR_NO_DEVICE:
376 sr_err("Device has been disconnected.");
379 sr_err("Unable to claim interface: %s.",
380 libusb_error_name(ret));
387 if (devc->cur_samplerate == 0) {
388 /* Samplerate hasn't been set; default to the slowest one. */
389 devc->cur_samplerate = samplerates[0];
395 static int dev_close(struct sr_dev_inst *sdi)
397 struct sr_usb_dev_inst *usb;
400 if (usb->devhdl == NULL)
403 sr_info("Closing device %d on %d.%d interface %d.",
404 sdi->index, usb->bus, usb->address, USB_INTERFACE);
405 libusb_release_interface(usb->devhdl, USB_INTERFACE);
406 libusb_close(usb->devhdl);
408 sdi->status = SR_ST_INACTIVE;
413 static int cleanup(void)
416 struct drv_context *drvc;
418 if (!(drvc = di->priv))
419 /* Can get called on an unused driver, doesn't matter. */
429 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
431 struct dev_context *devc;
432 struct sr_usb_dev_inst *usb;
439 if (!sdi || !sdi->conn)
442 if (usb->address == 255)
443 /* Device still needs to re-enumerate after firmware
444 * upload, so we don't know its (future) address. */
446 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
447 *data = g_variant_new_string(str);
449 case SR_CONF_SAMPLERATE:
453 *data = g_variant_new_uint64(devc->cur_samplerate);
462 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
464 struct dev_context *devc;
467 if (sdi->status != SR_ST_ACTIVE)
468 return SR_ERR_DEV_CLOSED;
474 case SR_CONF_SAMPLERATE:
475 devc->cur_samplerate = g_variant_get_uint64(data);
477 case SR_CONF_LIMIT_SAMPLES:
478 devc->limit_samples = g_variant_get_uint64(data);
487 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
497 case SR_CONF_SCAN_OPTIONS:
498 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
499 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
501 case SR_CONF_DEVICE_OPTIONS:
502 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
503 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
505 case SR_CONF_SAMPLERATE:
506 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
507 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
508 ARRAY_SIZE(samplerates), sizeof(uint64_t));
509 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
510 *data = g_variant_builder_end(&gvb);
519 static void abort_acquisition(struct dev_context *devc)
523 devc->num_samples = -1;
525 for (i = devc->num_transfers - 1; i >= 0; i--) {
526 if (devc->transfers[i])
527 libusb_cancel_transfer(devc->transfers[i]);
531 static unsigned int bytes_per_ms(struct dev_context *devc)
533 return devc->cur_samplerate * devc->num_channels / 8000;
536 static size_t get_buffer_size(struct dev_context *devc)
541 * The buffer should be large enough to hold 10ms of data and
544 s = 10 * bytes_per_ms(devc);
545 return (s + 511) & ~511;
548 static unsigned int get_number_of_transfers(struct dev_context *devc)
552 /* Total buffer size should be able to hold about 500ms of data. */
553 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
555 if (n > NUM_SIMUL_TRANSFERS)
556 return NUM_SIMUL_TRANSFERS;
561 static unsigned int get_timeout(struct dev_context *devc)
564 unsigned int timeout;
566 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
567 timeout = total_size / bytes_per_ms(devc);
568 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
571 static int configure_probes(const struct sr_dev_inst *sdi)
573 struct dev_context *devc;
574 struct sr_probe *probe;
580 devc->cur_channels = 0;
581 devc->num_channels = 0;
582 for (l = sdi->probes; l; l = l->next) {
583 probe = (struct sr_probe *)l->data;
584 if (probe->enabled == FALSE)
587 probe_bit = 1 << (probe->index);
589 devc->cur_channels |= probe_bit;
591 #ifdef WORDS_BIGENDIAN
592 /* Output logic data should be stored in little endian
593 format. To speed things up during conversion, do the
594 switcharoo here instead. */
596 probe_bit = 1 << (probe->index ^ 8);
599 devc->channel_masks[devc->num_channels ++] = probe_bit;
605 static int receive_data(int fd, int revents, void *cb_data)
608 struct dev_context *devc;
609 struct drv_context *drvc;
610 const struct sr_dev_inst *sdi;
619 tv.tv_sec = tv.tv_usec = 0;
620 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
622 if (devc->num_samples == -2) {
623 saleae_logic16_abort_acquisition(sdi);
624 abort_acquisition(devc);
630 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
633 struct dev_context *devc;
634 struct drv_context *drvc;
635 struct sr_usb_dev_inst *usb;
636 struct libusb_transfer *transfer;
637 const struct libusb_pollfd **lupfd;
638 unsigned int i, timeout, num_transfers;
641 size_t size, convsize;
643 if (sdi->status != SR_ST_ACTIVE)
644 return SR_ERR_DEV_CLOSED;
650 /* Configures devc->cur_channels */
651 if (configure_probes(sdi) != SR_OK) {
652 sr_err("Failed to configure probes.");
656 devc->cb_data = cb_data;
657 devc->num_samples = 0;
658 devc->empty_transfer_count = 0;
659 devc->cur_channel = 0;
660 memset(devc->channel_data, 0, sizeof(devc->channel_data));
662 timeout = get_timeout(devc);
663 num_transfers = get_number_of_transfers(devc);
664 size = get_buffer_size(devc);
665 convsize = (size / devc->num_channels + 2) * 16;
666 devc->submitted_transfers = 0;
669 devc->convbuffer_size = convsize;
670 if (!(devc->convbuffer = g_try_malloc(convsize))) {
671 sr_err("Conversion buffer malloc failed.");
672 return SR_ERR_MALLOC;
675 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
676 if (!devc->transfers) {
677 sr_err("USB transfers malloc failed.");
678 g_free(devc->convbuffer);
679 return SR_ERR_MALLOC;
682 if ((ret = saleae_logic16_setup_acquisition(sdi, devc->cur_samplerate,
683 devc->cur_channels)) != SR_OK) {
684 g_free(devc->transfers);
685 g_free(devc->convbuffer);
689 devc->num_transfers = num_transfers;
690 for (i = 0; i < num_transfers; i++) {
691 if (!(buf = g_try_malloc(size))) {
692 sr_err("USB transfer buffer malloc failed.");
693 if (devc->submitted_transfers)
694 abort_acquisition(devc);
696 g_free(devc->transfers);
697 g_free(devc->convbuffer);
699 return SR_ERR_MALLOC;
701 transfer = libusb_alloc_transfer(0);
702 libusb_fill_bulk_transfer(transfer, usb->devhdl,
703 2 | LIBUSB_ENDPOINT_IN, buf, size,
704 saleae_logic16_receive_transfer, devc, timeout);
705 if ((ret = libusb_submit_transfer(transfer)) != 0) {
706 sr_err("Failed to submit transfer: %s.",
707 libusb_error_name(ret));
708 libusb_free_transfer(transfer);
710 abort_acquisition(devc);
713 devc->transfers[i] = transfer;
714 devc->submitted_transfers++;
717 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
718 for (i = 0; lupfd[i]; i++);
719 if (!(devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1)))) {
720 abort_acquisition(devc);
724 for (i = 0; lupfd[i]; i++) {
725 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
726 timeout, receive_data, (void *)sdi);
727 devc->usbfd[i] = lupfd[i]->fd;
732 /* Send header packet to the session bus. */
733 std_session_send_df_header(cb_data, LOG_PREFIX);
735 if ((ret = saleae_logic16_start_acquisition(sdi)) != SR_OK) {
736 abort_acquisition(devc);
743 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
749 if (sdi->status != SR_ST_ACTIVE)
750 return SR_ERR_DEV_CLOSED;
752 ret = saleae_logic16_abort_acquisition(sdi);
754 abort_acquisition(sdi->priv);
759 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
760 .name = "saleae-logic16",
761 .longname = "Saleae Logic16",
766 .dev_list = dev_list,
767 .dev_clear = dev_clear,
768 .config_get = config_get,
769 .config_set = config_set,
770 .config_list = config_list,
771 .dev_open = dev_open,
772 .dev_close = dev_close,
773 .dev_acquisition_start = dev_acquisition_start,
774 .dev_acquisition_stop = dev_acquisition_stop,