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/>.
29 #define USB_VENDOR 0x0925
30 #define USB_PRODUCT 0x3881
31 #define USB_VENDOR_NAME "Saleae"
32 #define USB_MODEL_NAME "Logic"
33 #define USB_MODEL_VERSION ""
35 #define USB_INTERFACE 0
36 #define USB_CONFIGURATION 1
38 #define NUM_TRIGGER_STAGES 4
39 #define TRIGGER_TYPES "01"
40 #define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
43 #define FIRMWARE_RENUM_DELAY 2000
44 #define NUM_SIMUL_TRANSFERS 10
45 #define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
47 /* Software trigger implementation: positive values indicate trigger stage. */
48 #define TRIGGER_FIRED -1
50 /* There is only one model Saleae Logic, and this is what it supports: */
51 static int capabilities[] = {
55 /* These are really implemented in the driver, not the hardware. */
60 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
61 static GSList *device_instances = NULL;
64 * Since we can't keep track of a Saleae Logic device after upgrading the
65 * firmware -- it re-enumerates into a different device address after the
66 * upgrade -- this is like a global lock. No device will open until a proper
67 * delay after the last device was upgraded.
69 GTimeVal firmware_updated = { 0, 0 };
71 static libusb_context *usb_context = NULL;
73 static uint64_t supported_samplerates[] = {
87 static struct samplerates samplerates = {
91 supported_samplerates,
94 /* TODO: All of these should go in a device-specific struct. */
95 static uint64_t cur_samplerate = 0;
96 static uint64_t limit_samples = 0;
97 static uint8_t probe_mask = 0;
98 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
99 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
100 static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
102 int trigger_stage = TRIGGER_FIRED;
104 static int hw_set_configuration(int device_index, int capability, void *value);
105 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
108 * Check the USB configuration to determine if this is a Saleae Logic.
110 * @return 1 if the device's configuration profile match the Logic firmware's
111 * configuration, 0 otherwise.
113 int check_conf_profile(libusb_device *dev)
115 struct libusb_device_descriptor des;
116 struct libusb_config_descriptor *conf_dsc = NULL;
117 const struct libusb_interface_descriptor *intf_dsc;
121 /* Assume it's not a Saleae Logic unless proven wrong. */
124 if (libusb_get_device_descriptor(dev, &des) != 0)
127 if (des.bNumConfigurations != 1)
128 /* Need exactly 1 configuration. */
131 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
134 if (conf_dsc->bNumInterfaces != 1)
135 /* Need exactly 1 interface. */
138 if (conf_dsc->interface[0].num_altsetting != 1)
139 /* Need just one alternate setting. */
142 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
143 if (intf_dsc->bNumEndpoints != 2)
144 /* Need 2 endpoints. */
147 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
148 (1 | LIBUSB_ENDPOINT_OUT))
149 /* First endpoint should be 1 (outbound). */
152 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
153 (2 | LIBUSB_ENDPOINT_IN))
154 /* First endpoint should be 2 (inbound). */
157 /* If we made it here, it must be a Saleae Logic. */
162 libusb_free_config_descriptor(conf_dsc);
167 static int opendev2(int device_index, struct sigrok_device_instance **sdi,
168 libusb_device *dev, struct libusb_device_descriptor *des,
173 if ((err = libusb_get_device_descriptor(dev, des))) {
174 g_warning("failed to get device descriptor: %d", err);
178 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
181 if (*skip != device_index) {
182 /* Skip devices of this type that aren't the one we want. */
188 * Should check the bus here, since we know that already. But what are
189 * we going to do if it doesn't match after the right number of skips?
191 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
192 (*sdi)->usb->address = libusb_get_device_address(dev);
193 (*sdi)->status = ST_ACTIVE;
194 g_message("opened device %d on %d.%d interface %d",
195 (*sdi)->index, (*sdi)->usb->bus,
196 (*sdi)->usb->address, USB_INTERFACE);
198 g_warning("failed to open device: %d", err);
205 static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
206 struct libusb_device_descriptor *des)
210 if ((err = libusb_get_device_descriptor(dev, des))) {
211 g_warning("failed to get device descriptor: %d", err);
215 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
218 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
219 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
221 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
222 (*sdi)->status = ST_ACTIVE;
223 g_message("opened device %d on %d.%d interface %d",
224 (*sdi)->index, (*sdi)->usb->bus,
225 (*sdi)->usb->address, USB_INTERFACE);
227 g_warning("failed to open device: %d", err);
235 struct sigrok_device_instance *sl_open_device(int device_index)
237 struct sigrok_device_instance *sdi;
238 libusb_device **devlist;
239 struct libusb_device_descriptor des;
242 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
245 libusb_get_device_list(usb_context, &devlist);
246 if (sdi->status == ST_INITIALIZING) {
248 * This device was renumerating last time we touched it.
249 * opendev() guarantees we've waited long enough for it to
250 * have booted properly, so now we need to find it on
251 * the bus and record its new address.
254 for (i = 0; devlist[i]; i++) {
255 /* TODO: Error handling. */
256 err = opendev2(device_index, &sdi, devlist[i], &des,
259 } else if (sdi->status == ST_INACTIVE) {
261 * This device is fully enumerated, so we need to find this
262 * device by vendor, product, bus and address.
264 libusb_get_device_list(usb_context, &devlist);
265 for (i = 0; devlist[i]; i++) {
266 /* TODO: Error handling. */
267 err = opendev3(&sdi, devlist[i], &des);
270 /* Status must be ST_ACTIVE, i.e. already in use... */
273 libusb_free_device_list(devlist, 1);
275 if (sdi && sdi->status != ST_ACTIVE)
281 int upload_firmware(libusb_device *dev)
285 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
289 /* Remember when the last firmware update was done. */
290 g_get_current_time(&firmware_updated);
295 static void close_device(struct sigrok_device_instance *sdi)
297 if (sdi->usb->devhdl == NULL)
300 g_message("closing device %d on %d.%d interface %d", sdi->index,
301 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
302 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
303 libusb_close(sdi->usb->devhdl);
304 sdi->usb->devhdl = NULL;
305 sdi->status = ST_INACTIVE;
308 static int configure_probes(GSList *probes)
312 int probe_bit, stage, i;
316 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
318 trigger_value[i] = 0;
322 for (l = probes; l; l = l->next) {
323 probe = (struct probe *)l->data;
324 if (probe->enabled == FALSE)
326 probe_bit = 1 << (probe->index - 1);
327 probe_mask |= probe_bit;
328 if (probe->trigger) {
330 for (tc = probe->trigger; *tc; tc++) {
331 trigger_mask[stage] |= probe_bit;
333 trigger_value[stage] |= probe_bit;
335 if (stage > NUM_TRIGGER_STAGES)
343 * We didn't configure any triggers, make sure acquisition
344 * doesn't wait for any.
346 trigger_stage = TRIGGER_FIRED;
357 static int hw_init(char *deviceinfo)
359 struct sigrok_device_instance *sdi;
360 struct libusb_device_descriptor des;
361 libusb_device **devlist;
365 deviceinfo = deviceinfo;
367 if (libusb_init(&usb_context) != 0) {
368 g_warning("Failed to initialize USB.");
371 libusb_set_debug(usb_context, 3);
373 /* Find all Saleae Logic devices and upload firmware to all of them. */
375 libusb_get_device_list(usb_context, &devlist);
376 for (i = 0; devlist[i]; i++) {
377 err = libusb_get_device_descriptor(devlist[i], &des);
379 g_warning("failed to get device descriptor: %d", err);
383 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
384 continue; /* Not a Saleae Logic... */
386 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
387 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
390 device_instances = g_slist_append(device_instances, sdi);
392 if (check_conf_profile(devlist[i]) == 0) {
394 * Continue on the off chance that the device is in a
395 * working state. TODO: Could maybe try a USB reset,
396 * or uploading the firmware again.
398 if (upload_firmware(devlist[i]) > 0)
399 g_warning("firmware upload failed for "
400 "device %d", devcnt);
402 sdi->usb = usb_device_instance_new
403 (libusb_get_bus_number(devlist[i]), 0, NULL);
405 /* Already has the firmware, so fix the new address. */
406 sdi->usb = usb_device_instance_new
407 (libusb_get_bus_number(devlist[i]),
408 libusb_get_device_address(devlist[i]), NULL);
412 libusb_free_device_list(devlist, 1);
417 static int hw_opendev(int device_index)
420 struct sigrok_device_instance *sdi;
422 unsigned int cur, upd;
424 if (firmware_updated.tv_sec > 0) {
425 /* Firmware was recently uploaded. */
426 g_get_current_time(&cur_time);
427 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
428 upd = firmware_updated.tv_sec * 1000 +
429 firmware_updated.tv_usec / 1000;
430 timediff = cur - upd;
431 if (timediff < FIRMWARE_RENUM_DELAY) {
432 timediff = FIRMWARE_RENUM_DELAY - timediff;
433 g_message("waiting %d ms for device to reset",
435 g_usleep(timediff * 1000);
436 firmware_updated.tv_sec = 0;
440 if (!(sdi = sl_open_device(device_index))) {
441 g_warning("unable to open device");
445 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
447 g_warning("Unable to claim interface: %d", err);
451 if (cur_samplerate == 0) {
452 /* Samplerate hasn't been set; default to the slowest one. */
453 if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
454 &supported_samplerates[0]) == SIGROK_ERR)
461 static void hw_closedev(int device_index)
463 struct sigrok_device_instance *sdi;
465 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
469 static void hw_cleanup(void)
473 /* Properly close all devices... */
474 for (l = device_instances; l; l = l->next)
475 close_device((struct sigrok_device_instance *)l->data);
477 /* ...and free all their memory. */
478 for (l = device_instances; l; l = l->next)
480 g_slist_free(device_instances);
481 device_instances = NULL;
484 libusb_exit(usb_context);
488 static void *hw_get_device_info(int device_index, int device_info_id)
490 struct sigrok_device_instance *sdi;
493 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
496 switch (device_info_id) {
501 info = GINT_TO_POINTER(NUM_PROBES);
506 case DI_TRIGGER_TYPES:
507 info = TRIGGER_TYPES;
509 case DI_CUR_SAMPLERATE:
510 info = &cur_samplerate;
517 static int hw_get_status(int device_index)
519 struct sigrok_device_instance *sdi;
521 sdi = get_sigrok_device_instance(device_instances, device_index);
528 static int *hw_get_capabilities(void)
533 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
538 unsigned char buf[2];
540 for (i = 0; supported_samplerates[i]; i++) {
541 if (supported_samplerates[i] == samplerate)
544 if (supported_samplerates[i] == 0)
545 return SIGROK_ERR_SAMPLERATE;
547 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
549 g_message("setting samplerate to %" PRIu64 " Hz (divider %d)",
550 samplerate, divider);
553 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
554 buf, 2, &result, 500);
556 g_warning("failed to set samplerate: %d", ret);
559 cur_samplerate = samplerate;
564 static int hw_set_configuration(int device_index, int capability, void *value)
566 struct sigrok_device_instance *sdi;
570 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
573 if (capability == HWCAP_SAMPLERATE) {
575 ret = set_configuration_samplerate(sdi, *tmp_u64);
576 } else if (capability == HWCAP_PROBECONFIG) {
577 ret = configure_probes((GSList *) value);
578 } else if (capability == HWCAP_LIMIT_SAMPLES) {
579 limit_samples = strtoull(value, NULL, 10);
588 static int receive_data(int fd, int revents, void *user_data)
595 user_data = user_data;
597 tv.tv_sec = tv.tv_usec = 0;
598 libusb_handle_events_timeout(usb_context, &tv);
603 static void trigger_helper(int i, unsigned char *cur_buf,
604 struct datafeed_packet *packet, void *user_data,
607 if ((cur_buf[i] & trigger_mask[trigger_stage])
608 == trigger_value[trigger_stage]) {
609 /* Match on this trigger stage. */
610 trigger_buffer[trigger_stage] = cur_buf[i];
612 if (trigger_stage == NUM_TRIGGER_STAGES
613 || trigger_mask[trigger_stage] == 0) {
614 /* Match on all trigger stages, we're done. */
615 *trigger_offset = i + 1;
618 * TODO: Send pre-trigger buffer to session bus.
619 * Tell the frontend we hit the trigger here.
621 packet->type = DF_TRIGGER;
623 session_bus(user_data, packet);
626 * Send the samples that triggered it, since we're
627 * skipping past them.
629 packet->type = DF_LOGIC8;
630 packet->length = trigger_stage;
631 packet->payload = trigger_buffer;
632 session_bus(user_data, packet);
633 // break; // FIXME???
635 trigger_stage = TRIGGER_FIRED;
641 * We had a match before, but not in the next sample. However, we may
642 * have a match on this stage in the next bit -- trigger on 0001 will
643 * fail on seeing 00001, so we need to go back to stage 0 -- but at
644 * the next sample from the one that matched originally, which the
645 * counter increment at the end of the loop takes care of.
647 if (trigger_stage > 0) {
650 i = -1; /* Oops, went back past this buffer. */
651 /* Reset trigger stage. */
656 void receive_transfer(struct libusb_transfer *transfer)
658 static int num_samples = 0;
659 static int empty_transfer_count = 0;
660 struct datafeed_packet packet;
662 int cur_buflen, trigger_offset, i;
663 unsigned char *cur_buf, *new_buf;
665 /* hw_stop_acquisition() is telling us to stop. */
666 if (transfer == NULL)
670 * If acquisition has already ended, just free any queued up
671 * transfer that come in.
673 if (num_samples == -1) {
675 libusb_free_transfer(transfer);
679 g_message("receive_transfer(): status %d received %d bytes",
680 transfer->status, transfer->actual_length);
682 /* Save incoming transfer before reusing the transfer struct. */
683 cur_buf = transfer->buffer;
684 cur_buflen = transfer->actual_length;
685 user_data = transfer->user_data;
687 /* Fire off a new request. */
688 new_buf = g_malloc(4096);
689 transfer->buffer = new_buf;
690 transfer->length = 4096;
691 if (libusb_submit_transfer(transfer) != 0) {
692 /* TODO: Stop session? */
696 if (cur_buflen == 0) {
697 empty_transfer_count++;
698 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
700 * The FX2 gave up. End the acquisition, the frontend
701 * will work out that the samplecount is short.
703 hw_stop_acquisition(-1, user_data);
707 empty_transfer_count = 0;
711 if (trigger_stage >= 0) {
712 for (i = 0; i < cur_buflen; i++) {
713 trigger_helper(i, cur_buf, &packet, user_data,
718 if (trigger_stage == TRIGGER_FIRED) {
719 /* Send the incoming transfer to the session bus. */
720 packet.type = DF_LOGIC8;
721 packet.length = cur_buflen - trigger_offset;
722 packet.payload = cur_buf + trigger_offset;
723 session_bus(user_data, &packet);
726 num_samples += cur_buflen;
727 if ((unsigned int)num_samples > limit_samples) {
728 hw_stop_acquisition(-1, user_data);
732 * TODO: Buffer pre-trigger data in capture
733 * ratio-sized buffer.
738 static int hw_start_acquisition(int device_index, gpointer session_device_id)
740 struct sigrok_device_instance *sdi;
741 struct datafeed_packet *packet;
742 struct datafeed_header *header;
743 struct libusb_transfer *transfer;
744 const struct libusb_pollfd **lupfd;
748 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
751 packet = g_malloc(sizeof(struct datafeed_packet));
752 header = g_malloc(sizeof(struct datafeed_header));
753 if (!packet || !header)
756 /* Start with 2K transfer, subsequently increased to 4K. */
758 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
759 buf = g_malloc(size);
760 transfer = libusb_alloc_transfer(0);
761 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
762 2 | LIBUSB_ENDPOINT_IN, buf, size,
763 receive_transfer, session_device_id, 40);
764 if (libusb_submit_transfer(transfer) != 0) {
765 /* TODO: Free them all. */
766 libusb_free_transfer(transfer);
773 lupfd = libusb_get_pollfds(usb_context);
774 for (i = 0; lupfd[i]; i++)
775 source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
779 packet->type = DF_HEADER;
780 packet->length = sizeof(struct datafeed_header);
781 packet->payload = (unsigned char *)header;
782 header->feed_version = 1;
783 gettimeofday(&header->starttime, NULL);
784 header->samplerate = cur_samplerate;
785 header->protocol_id = PROTO_RAW;
786 header->num_probes = NUM_PROBES;
787 session_bus(session_device_id, packet);
794 /* This stops acquisition on ALL devices, ignoring device_index. */
795 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
797 struct datafeed_packet packet;
800 device_index = device_index;
802 packet.type = DF_END;
803 session_bus(session_device_id, &packet);
805 receive_transfer(NULL);
807 /* TODO: Need to cancel and free any queued up transfers. */
810 struct device_plugin saleae_logic_plugin_info = {
821 hw_set_configuration,
822 hw_start_acquisition,