2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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/>.
28 #include <sigrok-internal.h>
30 #define USB_VENDOR 0x0925
31 #define USB_PRODUCT 0x3881
32 #define USB_VENDOR_NAME "Saleae"
33 #define USB_MODEL_NAME "Logic"
34 #define USB_MODEL_VERSION ""
36 #define USB_INTERFACE 0
37 #define USB_CONFIGURATION 1
39 #define NUM_TRIGGER_STAGES 4
40 #define TRIGGER_TYPES "01"
41 #define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
44 #define FIRMWARE_RENUM_DELAY 2000
45 #define NUM_SIMUL_TRANSFERS 10
46 #define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
48 /* Software trigger implementation: positive values indicate trigger stage. */
49 #define TRIGGER_FIRED -1
51 /* There is only one model Saleae Logic, and this is what it supports: */
52 static int capabilities[] = {
53 SR_HWCAP_LOGIC_ANALYZER,
56 /* These are really implemented in the driver, not the hardware. */
57 SR_HWCAP_LIMIT_SAMPLES,
62 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
63 static GSList *device_instances = NULL;
66 * Since we can't keep track of a Saleae Logic device after upgrading the
67 * firmware -- it re-enumerates into a different device address after the
68 * upgrade -- this is like a global lock. No device will open until a proper
69 * delay after the last device was upgraded.
71 static GTimeVal firmware_updated = { 0, 0 };
73 static libusb_context *usb_context = NULL;
75 static uint64_t supported_samplerates[] = {
89 static struct sr_samplerates samplerates = {
93 supported_samplerates,
96 /* TODO: All of these should go in a device-specific struct. */
97 static uint64_t cur_samplerate = 0;
98 static uint64_t limit_samples = 0;
99 static uint8_t probe_mask = 0;
100 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
101 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
102 static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
104 static int trigger_stage = TRIGGER_FIRED;
106 static int hw_set_configuration(int device_index, int capability, void *value);
107 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
110 * Check the USB configuration to determine if this is a Saleae Logic.
112 * @return 1 if the device's configuration profile match the Logic firmware's
113 * configuration, 0 otherwise.
115 static int check_conf_profile(libusb_device *dev)
117 struct libusb_device_descriptor des;
118 struct libusb_config_descriptor *conf_dsc = NULL;
119 const struct libusb_interface_descriptor *intf_dsc;
123 /* Assume it's not a Saleae Logic unless proven wrong. */
126 if (libusb_get_device_descriptor(dev, &des) != 0)
129 if (des.bNumConfigurations != 1)
130 /* Need exactly 1 configuration. */
133 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
136 if (conf_dsc->bNumInterfaces != 1)
137 /* Need exactly 1 interface. */
140 if (conf_dsc->interface[0].num_altsetting != 1)
141 /* Need just one alternate setting. */
144 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
145 if (intf_dsc->bNumEndpoints != 2)
146 /* Need 2 endpoints. */
149 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
150 (1 | LIBUSB_ENDPOINT_OUT))
151 /* First endpoint should be 1 (outbound). */
154 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
155 (2 | LIBUSB_ENDPOINT_IN))
156 /* First endpoint should be 2 (inbound). */
159 /* If we made it here, it must be a Saleae Logic. */
164 libusb_free_config_descriptor(conf_dsc);
169 static struct sr_device_instance *sl_open_device(int device_index)
171 struct sr_device_instance *sdi;
172 libusb_device **devlist;
173 struct libusb_device_descriptor des;
176 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
179 libusb_get_device_list(usb_context, &devlist);
180 if (sdi->status == SR_ST_INITIALIZING) {
182 * This device was renumerating last time we touched it.
183 * opendev() guarantees we've waited long enough for it to
184 * have booted properly, so now we need to find it on
185 * the bus and record its new address.
188 for (i = 0; devlist[i]; i++) {
189 /* TODO: Error handling. */
190 err = opendev2(device_index, &sdi, devlist[i], &des,
191 &skip, USB_VENDOR, USB_PRODUCT,
194 } else if (sdi->status == SR_ST_INACTIVE) {
196 * This device is fully enumerated, so we need to find this
197 * device by vendor, product, bus and address.
199 libusb_get_device_list(usb_context, &devlist);
200 for (i = 0; devlist[i]; i++) {
201 /* TODO: Error handling. */
202 err = opendev3(&sdi, devlist[i], &des, USB_VENDOR,
203 USB_PRODUCT, USB_INTERFACE);
206 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
209 libusb_free_device_list(devlist, 1);
211 if (sdi && sdi->status != SR_ST_ACTIVE)
217 static int upload_firmware(libusb_device *dev)
221 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
225 /* Remember when the last firmware update was done. */
226 g_get_current_time(&firmware_updated);
231 static void close_device(struct sr_device_instance *sdi)
233 if (sdi->usb->devhdl == NULL)
236 g_message("saleae: closing device %d on %d.%d interface %d", sdi->index,
237 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
238 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
239 libusb_close(sdi->usb->devhdl);
240 sdi->usb->devhdl = NULL;
241 sdi->status = SR_ST_INACTIVE;
244 static int configure_probes(GSList *probes)
246 struct sr_probe *probe;
248 int probe_bit, stage, i;
252 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
254 trigger_value[i] = 0;
258 for (l = probes; l; l = l->next) {
259 probe = (struct sr_probe *)l->data;
260 if (probe->enabled == FALSE)
262 probe_bit = 1 << (probe->index - 1);
263 probe_mask |= probe_bit;
264 if (!(probe->trigger))
268 for (tc = probe->trigger; *tc; tc++) {
269 trigger_mask[stage] |= probe_bit;
271 trigger_value[stage] |= probe_bit;
273 if (stage > NUM_TRIGGER_STAGES)
280 * We didn't configure any triggers, make sure acquisition
281 * doesn't wait for any.
283 trigger_stage = TRIGGER_FIRED;
294 static int hw_init(char *deviceinfo)
296 struct sr_device_instance *sdi;
297 struct libusb_device_descriptor des;
298 libusb_device **devlist;
301 /* Avoid compiler warnings. */
302 deviceinfo = deviceinfo;
304 if (libusb_init(&usb_context) != 0) {
305 g_warning("Failed to initialize USB.");
309 /* Find all Saleae Logic devices and upload firmware to all of them. */
311 libusb_get_device_list(usb_context, &devlist);
312 for (i = 0; devlist[i]; i++) {
313 err = libusb_get_device_descriptor(devlist[i], &des);
315 g_warning("failed to get device descriptor: %d", err);
319 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
320 continue; /* Not a Saleae Logic... */
322 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
323 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
326 device_instances = g_slist_append(device_instances, sdi);
328 if (check_conf_profile(devlist[i]) == 0) {
330 * Continue on the off chance that the device is in a
331 * working state. TODO: Could maybe try a USB reset,
332 * or uploading the firmware again.
334 if (upload_firmware(devlist[i]) > 0)
335 g_warning("firmware upload failed for "
336 "device %d", devcnt);
338 sdi->usb = sr_usb_device_instance_new
339 (libusb_get_bus_number(devlist[i]), 0, NULL);
341 /* Already has the firmware, so fix the new address. */
342 sdi->usb = sr_usb_device_instance_new
343 (libusb_get_bus_number(devlist[i]),
344 libusb_get_device_address(devlist[i]), NULL);
348 libusb_free_device_list(devlist, 1);
353 static int hw_opendev(int device_index)
356 struct sr_device_instance *sdi;
358 unsigned int cur, upd;
360 if (firmware_updated.tv_sec > 0) {
361 /* Firmware was recently uploaded. */
362 g_get_current_time(&cur_time);
363 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
364 upd = firmware_updated.tv_sec * 1000 +
365 firmware_updated.tv_usec / 1000;
366 timediff = cur - upd;
367 if (timediff < FIRMWARE_RENUM_DELAY) {
368 timediff = FIRMWARE_RENUM_DELAY - timediff;
369 g_message("saleae: waiting %d ms for device to reset",
371 g_usleep(timediff * 1000);
372 firmware_updated.tv_sec = 0;
376 if (!(sdi = sl_open_device(device_index))) {
377 g_warning("unable to open device");
381 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
383 g_warning("Unable to claim interface: %d", err);
387 if (cur_samplerate == 0) {
388 /* Samplerate hasn't been set; default to the slowest one. */
389 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
390 &supported_samplerates[0]) == SR_ERR)
397 static void hw_closedev(int device_index)
399 struct sr_device_instance *sdi;
401 if ((sdi = sr_get_device_instance(device_instances, device_index)))
405 static void hw_cleanup(void)
409 /* Properly close all devices... */
410 for (l = device_instances; l; l = l->next)
411 close_device((struct sr_device_instance *)l->data);
413 /* ...and free all their memory. */
414 for (l = device_instances; l; l = l->next)
416 g_slist_free(device_instances);
417 device_instances = NULL;
420 libusb_exit(usb_context);
424 static void *hw_get_device_info(int device_index, int device_info_id)
426 struct sr_device_instance *sdi;
429 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
432 switch (device_info_id) {
436 case SR_DI_NUM_PROBES:
437 info = GINT_TO_POINTER(NUM_PROBES);
439 case SR_DI_SAMPLERATES:
442 case SR_DI_TRIGGER_TYPES:
443 info = TRIGGER_TYPES;
445 case SR_DI_CUR_SAMPLERATE:
446 info = &cur_samplerate;
453 static int hw_get_status(int device_index)
455 struct sr_device_instance *sdi;
457 sdi = sr_get_device_instance(device_instances, device_index);
461 return SR_ST_NOT_FOUND;
464 static int *hw_get_capabilities(void)
469 static int set_configuration_samplerate(struct sr_device_instance *sdi,
474 unsigned char buf[2];
476 for (i = 0; supported_samplerates[i]; i++) {
477 if (supported_samplerates[i] == samplerate)
480 if (supported_samplerates[i] == 0)
481 return SR_ERR_SAMPLERATE;
483 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
485 g_message("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
486 samplerate, divider);
489 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
490 buf, 2, &result, 500);
492 g_warning("failed to set samplerate: %d", ret);
495 cur_samplerate = samplerate;
500 static int hw_set_configuration(int device_index, int capability, void *value)
502 struct sr_device_instance *sdi;
506 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
509 if (capability == SR_HWCAP_SAMPLERATE) {
511 ret = set_configuration_samplerate(sdi, *tmp_u64);
512 } else if (capability == SR_HWCAP_PROBECONFIG) {
513 ret = configure_probes((GSList *) value);
514 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
516 limit_samples = *tmp_u64;
525 static int receive_data(int fd, int revents, void *user_data)
529 /* Avoid compiler warnings. */
532 user_data = user_data;
534 tv.tv_sec = tv.tv_usec = 0;
535 libusb_handle_events_timeout(usb_context, &tv);
540 void receive_transfer(struct libusb_transfer *transfer)
542 static int num_samples = 0;
543 static int empty_transfer_count = 0;
544 struct sr_datafeed_packet packet;
546 int cur_buflen, trigger_offset, i;
547 unsigned char *cur_buf, *new_buf;
549 /* hw_stop_acquisition() is telling us to stop. */
550 if (transfer == NULL)
554 * If acquisition has already ended, just free any queued up
555 * transfer that come in.
557 if (num_samples == -1) {
559 libusb_free_transfer(transfer);
563 g_message("saleae: receive_transfer(): status %d received %d bytes",
564 transfer->status, transfer->actual_length);
566 /* Save incoming transfer before reusing the transfer struct. */
567 cur_buf = transfer->buffer;
568 cur_buflen = transfer->actual_length;
569 user_data = transfer->user_data;
571 /* Fire off a new request. */
572 new_buf = g_malloc(4096);
573 transfer->buffer = new_buf;
574 transfer->length = 4096;
575 if (libusb_submit_transfer(transfer) != 0) {
576 /* TODO: Stop session? */
580 if (cur_buflen == 0) {
581 empty_transfer_count++;
582 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
584 * The FX2 gave up. End the acquisition, the frontend
585 * will work out that the samplecount is short.
587 hw_stop_acquisition(-1, user_data);
591 empty_transfer_count = 0;
595 if (trigger_stage >= 0) {
596 for (i = 0; i < cur_buflen; i++) {
598 if ((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage]) {
599 /* Match on this trigger stage. */
600 trigger_buffer[trigger_stage] = cur_buf[i];
602 if (trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0) {
603 /* Match on all trigger stages, we're done. */
604 trigger_offset = i + 1;
607 * TODO: Send pre-trigger buffer to session bus.
608 * Tell the frontend we hit the trigger here.
610 packet.type = SR_DF_TRIGGER;
612 sr_session_bus(user_data, &packet);
615 * Send the samples that triggered it, since we're
616 * skipping past them.
618 packet.type = SR_DF_LOGIC;
619 packet.length = trigger_stage;
621 packet.payload = trigger_buffer;
622 sr_session_bus(user_data, &packet);
624 trigger_stage = TRIGGER_FIRED;
631 * We had a match before, but not in the next sample. However, we may
632 * have a match on this stage in the next bit -- trigger on 0001 will
633 * fail on seeing 00001, so we need to go back to stage 0 -- but at
634 * the next sample from the one that matched originally, which the
635 * counter increment at the end of the loop takes care of.
637 if (trigger_stage > 0) {
640 i = -1; /* Oops, went back past this buffer. */
641 /* Reset trigger stage. */
649 if (trigger_stage == TRIGGER_FIRED) {
650 /* Send the incoming transfer to the session bus. */
651 packet.type = SR_DF_LOGIC;
652 packet.length = cur_buflen - trigger_offset;
654 packet.payload = cur_buf + trigger_offset;
655 sr_session_bus(user_data, &packet);
658 num_samples += cur_buflen;
659 if (limit_samples && (unsigned int) num_samples > limit_samples) {
660 hw_stop_acquisition(-1, user_data);
664 * TODO: Buffer pre-trigger data in capture
665 * ratio-sized buffer.
670 static int hw_start_acquisition(int device_index, gpointer session_device_id)
672 struct sr_device_instance *sdi;
673 struct sr_datafeed_packet *packet;
674 struct sr_datafeed_header *header;
675 struct libusb_transfer *transfer;
676 const struct libusb_pollfd **lupfd;
680 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
683 packet = g_malloc(sizeof(struct sr_datafeed_packet));
684 header = g_malloc(sizeof(struct sr_datafeed_header));
685 if (!packet || !header)
688 /* Start with 2K transfer, subsequently increased to 4K. */
690 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
691 buf = g_malloc(size);
692 transfer = libusb_alloc_transfer(0);
693 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
694 2 | LIBUSB_ENDPOINT_IN, buf, size,
695 receive_transfer, session_device_id, 40);
696 if (libusb_submit_transfer(transfer) != 0) {
697 /* TODO: Free them all. */
698 libusb_free_transfer(transfer);
705 lupfd = libusb_get_pollfds(usb_context);
706 for (i = 0; lupfd[i]; i++)
707 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
711 packet->type = SR_DF_HEADER;
712 packet->length = sizeof(struct sr_datafeed_header);
713 packet->payload = (unsigned char *)header;
714 header->feed_version = 1;
715 gettimeofday(&header->starttime, NULL);
716 header->samplerate = cur_samplerate;
717 header->protocol_id = SR_PROTO_RAW;
718 header->num_logic_probes = NUM_PROBES;
719 header->num_analog_probes = 0;
720 sr_session_bus(session_device_id, packet);
727 /* This stops acquisition on ALL devices, ignoring device_index. */
728 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
730 struct sr_datafeed_packet packet;
732 /* Avoid compiler warnings. */
733 device_index = device_index;
735 packet.type = SR_DF_END;
736 sr_session_bus(session_device_id, &packet);
738 receive_transfer(NULL);
740 /* TODO: Need to cancel and free any queued up transfers. */
743 struct sr_device_plugin saleae_logic_plugin_info = {
754 hw_set_configuration,
755 hw_start_acquisition,