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.firmware"
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);
107 * Returns 1 if the device's configuration profile match the Logic firmware's
108 * configuration, 0 otherwise.
110 int check_conf_profile(libusb_device *dev)
112 struct libusb_device_descriptor des;
113 struct libusb_config_descriptor *conf_dsc = NULL;
114 const struct libusb_interface_descriptor *intf_dsc;
118 /* Assume it's not a Saleae Logic unless proven wrong. */
121 if (libusb_get_device_descriptor(dev, &des) != 0)
124 if (des.bNumConfigurations != 1)
125 /* Need exactly 1 configuration. */
128 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
131 if (conf_dsc->bNumInterfaces != 1)
132 /* Need exactly 1 interface. */
135 if (conf_dsc->interface[0].num_altsetting != 1)
136 /* Need just one alternate setting. */
139 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
140 if (intf_dsc->bNumEndpoints != 2)
141 /* Need 2 endpoints. */
144 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
145 (1 | LIBUSB_ENDPOINT_OUT))
146 /* First endpoint should be 1 (outbound). */
149 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
150 (2 | LIBUSB_ENDPOINT_IN))
151 /* First endpoint should be 2 (inbound). */
154 /* If we made it here, it must be a Saleae Logic. */
159 libusb_free_config_descriptor(conf_dsc);
164 static int opendev2(int device_index, struct sigrok_device_instance **sdi,
165 libusb_device *dev, struct libusb_device_descriptor *des,
170 if ((err = libusb_get_device_descriptor(dev, des))) {
171 g_warning("failed to get device descriptor: %d", err);
175 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
178 if (*skip != device_index) {
179 /* Skip devices of this type that aren't the one we want. */
185 * Should check the bus here, since we know that already. But what are
186 * we going to do if it doesn't match after the right number of skips?
188 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
189 (*sdi)->usb->address = libusb_get_device_address(dev);
190 (*sdi)->status = ST_ACTIVE;
191 g_message("opened device %d on %d.%d interface %d",
192 (*sdi)->index, (*sdi)->usb->bus,
193 (*sdi)->usb->address, USB_INTERFACE);
195 g_warning("failed to open device: %d", err);
202 static int opendev3(struct sigrok_device_instance **sdi, libusb_device *dev,
203 struct libusb_device_descriptor *des)
207 if ((err = libusb_get_device_descriptor(dev, des))) {
208 g_warning("failed to get device descriptor: %d", err);
212 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
215 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
216 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
218 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
219 (*sdi)->status = ST_ACTIVE;
220 g_message("opened device %d on %d.%d interface %d",
221 (*sdi)->index, (*sdi)->usb->bus,
222 (*sdi)->usb->address, USB_INTERFACE);
224 g_warning("failed to open device: %d", err);
232 struct sigrok_device_instance *sl_open_device(int device_index)
234 struct sigrok_device_instance *sdi;
235 libusb_device **devlist;
236 struct libusb_device_descriptor des;
239 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
242 libusb_get_device_list(usb_context, &devlist);
243 if (sdi->status == ST_INITIALIZING) {
245 * This device was renumerating last time we touched it.
246 * opendev() guarantees we've waited long enough for it to
247 * have booted properly, so now we need to find it on
248 * the bus and record its new address.
251 for (i = 0; devlist[i]; i++) {
252 /* TODO: Error handling. */
253 err = opendev2(device_index, &sdi, devlist[i], &des,
256 } else if (sdi->status == ST_INACTIVE) {
258 * This device is fully enumerated, so we need to find this
259 * device by vendor, product, bus and address.
261 libusb_get_device_list(usb_context, &devlist);
262 for (i = 0; devlist[i]; i++) {
263 /* TODO: Error handling. */
264 err = opendev3(&sdi, devlist[i], &des);
267 /* Status must be ST_ACTIVE, i.e. already in use... */
270 libusb_free_device_list(devlist, 1);
272 if (sdi && sdi->status != ST_ACTIVE)
278 int upload_firmware(libusb_device *dev)
282 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
286 /* Remember when the last firmware update was done. */
287 g_get_current_time(&firmware_updated);
292 static void close_device(struct sigrok_device_instance *sdi)
294 if (sdi->usb->devhdl == NULL)
297 g_message("closing device %d on %d.%d interface %d", sdi->index,
298 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
299 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
300 libusb_close(sdi->usb->devhdl);
301 sdi->usb->devhdl = NULL;
302 sdi->status = ST_INACTIVE;
305 static int configure_probes(GSList *probes)
309 int probe_bit, stage, i;
313 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
315 trigger_value[i] = 0;
319 for (l = probes; l; l = l->next) {
320 probe = (struct probe *)l->data;
321 if (probe->enabled == FALSE)
323 probe_bit = 1 << (probe->index - 1);
324 probe_mask |= probe_bit;
325 if (probe->trigger) {
327 for (tc = probe->trigger; *tc; tc++) {
328 trigger_mask[stage] |= probe_bit;
330 trigger_value[stage] |= probe_bit;
332 if (stage > NUM_TRIGGER_STAGES)
340 * We didn't configure any triggers, make sure acquisition
341 * doesn't wait for any.
343 trigger_stage = TRIGGER_FIRED;
354 static int hw_init(char *deviceinfo)
356 struct sigrok_device_instance *sdi;
357 struct libusb_device_descriptor des;
358 libusb_device **devlist;
362 deviceinfo = deviceinfo;
364 if (libusb_init(&usb_context) != 0) {
365 g_warning("Failed to initialize USB.");
368 libusb_set_debug(usb_context, 3);
370 /* Find all Saleae Logic devices and upload firmware to all of them. */
372 libusb_get_device_list(usb_context, &devlist);
373 for (i = 0; devlist[i]; i++) {
374 err = libusb_get_device_descriptor(devlist[i], &des);
376 g_warning("failed to get device descriptor: %d", err);
380 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
381 continue; /* Not a Saleae Logic... */
383 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
384 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
387 device_instances = g_slist_append(device_instances, sdi);
389 if (check_conf_profile(devlist[i]) == 0) {
391 * Continue on the off chance that the device is in a
392 * working state. TODO: Could maybe try a USB reset,
393 * or uploading the firmware again.
395 if (upload_firmware(devlist[i]) > 0)
396 g_warning("firmware upload failed for "
397 "device %d", devcnt);
399 sdi->usb = usb_device_instance_new
400 (libusb_get_bus_number(devlist[i]), 0, NULL);
402 /* Already has the firmware, so fix the new address. */
403 sdi->usb = usb_device_instance_new
404 (libusb_get_bus_number(devlist[i]),
405 libusb_get_device_address(devlist[i]), NULL);
409 libusb_free_device_list(devlist, 1);
414 static int hw_opendev(int device_index)
417 struct sigrok_device_instance *sdi;
419 unsigned int cur, upd;
421 if (firmware_updated.tv_sec > 0) {
422 /* Firmware was recently uploaded. */
423 g_get_current_time(&cur_time);
424 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
425 upd = firmware_updated.tv_sec * 1000 +
426 firmware_updated.tv_usec / 1000;
427 timediff = cur - upd;
428 if (timediff < FIRMWARE_RENUM_DELAY) {
429 timediff = FIRMWARE_RENUM_DELAY - timediff;
430 g_message("waiting %d ms for device to reset",
432 g_usleep(timediff * 1000);
433 firmware_updated.tv_sec = 0;
437 if (!(sdi = sl_open_device(device_index))) {
438 g_warning("unable to open device");
442 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
444 g_warning("Unable to claim interface: %d", err);
448 if (cur_samplerate == 0) {
449 /* Samplerate hasn't been set; default to the slowest one. */
450 if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
451 &supported_samplerates[0]) == SIGROK_ERR)
458 static void hw_closedev(int device_index)
460 struct sigrok_device_instance *sdi;
462 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
466 static void hw_cleanup(void)
470 /* Properly close all devices... */
471 for (l = device_instances; l; l = l->next)
472 close_device((struct sigrok_device_instance *)l->data);
474 /* ...and free all their memory. */
475 for (l = device_instances; l; l = l->next)
477 g_slist_free(device_instances);
478 device_instances = NULL;
481 libusb_exit(usb_context);
485 static void *hw_get_device_info(int device_index, int device_info_id)
487 struct sigrok_device_instance *sdi;
490 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
493 switch (device_info_id) {
498 info = GINT_TO_POINTER(NUM_PROBES);
503 case DI_TRIGGER_TYPES:
504 info = TRIGGER_TYPES;
506 case DI_CUR_SAMPLERATE:
507 info = &cur_samplerate;
514 static int hw_get_status(int device_index)
516 struct sigrok_device_instance *sdi;
518 sdi = get_sigrok_device_instance(device_instances, device_index);
525 static int *hw_get_capabilities(void)
530 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
535 unsigned char buf[2];
537 for (i = 0; supported_samplerates[i]; i++) {
538 if (supported_samplerates[i] == samplerate)
541 if (supported_samplerates[i] == 0)
542 return SIGROK_ERR_SAMPLERATE;
544 divider = (uint8_t) (48 / (float)(samplerate / 1000000)) - 1;
546 g_message("setting samplerate to %" PRIu64 " Hz (divider %d)",
547 samplerate, divider);
550 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
551 buf, 2, &result, 500);
553 g_warning("failed to set samplerate: %d", ret);
556 cur_samplerate = samplerate;
561 static int hw_set_configuration(int device_index, int capability, void *value)
563 struct sigrok_device_instance *sdi;
567 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
570 if (capability == HWCAP_SAMPLERATE) {
572 ret = set_configuration_samplerate(sdi, *tmp_u64);
573 } else if (capability == HWCAP_PROBECONFIG) {
574 ret = configure_probes((GSList *) value);
575 } else if (capability == HWCAP_LIMIT_SAMPLES) {
576 limit_samples = strtoull(value, NULL, 10);
585 static int receive_data(int fd, int revents, void *user_data)
592 user_data = user_data;
594 tv.tv_sec = tv.tv_usec = 0;
595 libusb_handle_events_timeout(usb_context, &tv);
600 static void trigger_helper(int i, unsigned char *cur_buf,
601 struct datafeed_packet *packet, void *user_data,
604 if ((cur_buf[i] & trigger_mask[trigger_stage])
605 == trigger_value[trigger_stage]) {
606 /* Match on this trigger stage. */
607 trigger_buffer[trigger_stage] = cur_buf[i];
609 if (trigger_stage == NUM_TRIGGER_STAGES
610 || trigger_mask[trigger_stage] == 0) {
611 /* Match on all trigger stages, we're done. */
612 *trigger_offset = i + 1;
615 * TODO: Send pre-trigger buffer to session bus.
616 * Tell the frontend we hit the trigger here.
618 packet->type = DF_TRIGGER;
620 session_bus(user_data, packet);
623 * Send the samples that triggered it, since we're
624 * skipping past them.
626 packet->type = DF_LOGIC8;
627 packet->length = trigger_stage;
628 packet->payload = trigger_buffer;
629 session_bus(user_data, packet);
630 // break; // FIXME???
632 trigger_stage = TRIGGER_FIRED;
638 * We had a match before, but not in the next sample. However, we may
639 * have a match on this stage in the next bit -- trigger on 0001 will
640 * fail on seeing 00001, so we need to go back to stage 0 -- but at
641 * the next sample from the one that matched originally, which the
642 * counter increment at the end of the loop takes care of.
644 if (trigger_stage > 0) {
647 i = -1; /* Oops, went back past this buffer. */
648 /* Reset trigger stage. */
653 void receive_transfer(struct libusb_transfer *transfer)
655 static int num_samples = 0;
656 static int empty_transfer_count = 0;
657 struct datafeed_packet packet;
659 int cur_buflen, trigger_offset, i;
660 unsigned char *cur_buf, *new_buf;
662 /* hw_stop_acquisition() is telling us to stop. */
663 if (transfer == NULL)
667 * If acquisition has already ended, just free any queued up
668 * transfer that come in.
670 if (num_samples == -1) {
671 libusb_free_transfer(transfer);
675 g_message("receive_transfer(): status %d received %d bytes",
676 transfer->status, transfer->actual_length);
678 /* Save incoming transfer before reusing the transfer struct. */
679 cur_buf = transfer->buffer;
680 cur_buflen = transfer->actual_length;
681 user_data = transfer->user_data;
683 /* Fire off a new request. */
684 new_buf = g_malloc(4096);
685 transfer->buffer = new_buf;
686 transfer->length = 4096;
687 if (libusb_submit_transfer(transfer) != 0) {
688 /* TODO: Stop session? */
692 if (cur_buflen == 0) {
693 empty_transfer_count++;
694 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
696 * The FX2 gave up. End the acquisition, the frontend
697 * will work out that the samplecount is short.
699 packet.type = DF_END;
700 session_bus(user_data, &packet);
705 empty_transfer_count = 0;
709 if (trigger_stage >= 0) {
710 for (i = 0; i < cur_buflen; i++) {
711 trigger_helper(i, cur_buf, &packet, user_data,
716 if (trigger_stage == TRIGGER_FIRED) {
717 /* Send the incoming transfer to the session bus. */
718 packet.type = DF_LOGIC8;
719 packet.length = cur_buflen - trigger_offset;
720 packet.payload = cur_buf + trigger_offset;
721 session_bus(user_data, &packet);
724 num_samples += cur_buflen;
725 if ((unsigned int)num_samples > limit_samples) {
726 /* End the acquisition. */
727 packet.type = DF_END;
728 session_bus(user_data, &packet);
733 * TODO: Buffer pre-trigger data in capture
734 * ratio-sized buffer.
739 static int hw_start_acquisition(int device_index, gpointer session_device_id)
741 struct sigrok_device_instance *sdi;
742 struct datafeed_packet *packet;
743 struct datafeed_header *header;
744 struct libusb_transfer *transfer;
745 const struct libusb_pollfd **lupfd;
749 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
752 packet = g_malloc(sizeof(struct datafeed_packet));
753 header = g_malloc(sizeof(struct datafeed_header));
754 if (!packet || !header)
757 /* Start with 2K transfer, subsequently increased to 4K. */
759 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
760 buf = g_malloc(size);
761 transfer = libusb_alloc_transfer(0);
762 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
763 2 | LIBUSB_ENDPOINT_IN, buf, size,
764 receive_transfer, session_device_id, 40);
765 if (libusb_submit_transfer(transfer) != 0) {
766 /* TODO: Free them all. */
767 libusb_free_transfer(transfer);
774 lupfd = libusb_get_pollfds(usb_context);
775 for (i = 0; lupfd[i]; i++)
776 source_add(lupfd[i]->fd, lupfd[i]->events, -1, receive_data,
780 packet->type = DF_HEADER;
781 packet->length = sizeof(struct datafeed_header);
782 packet->payload = (unsigned char *)header;
783 header->feed_version = 1;
784 gettimeofday(&header->starttime, NULL);
785 header->samplerate = cur_samplerate;
786 header->protocol_id = PROTO_RAW;
787 header->num_probes = NUM_PROBES;
788 session_bus(session_device_id, packet);
795 /* This stops acquisition on ALL devices, ignoring device_index. */
796 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
798 struct datafeed_packet packet;
801 device_index = device_index;
803 packet.type = DF_END;
804 session_bus(session_device_id, &packet);
806 receive_transfer(NULL);
808 /* TODO: Need to cancel and free any queued up transfers. */
811 struct device_plugin saleae_logic_plugin_info = {
822 hw_set_configuration,
823 hw_start_acquisition,