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 };
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 };
101 int trigger_stage = TRIGGER_FIRED;
103 static int hw_set_configuration(int device_index, int capability, void *value);
105 /* returns 1 if the device's configuration profile match the Logic firmware's
106 * configuration, 0 otherwise
108 int check_conf_profile(libusb_device *dev)
110 struct libusb_device_descriptor des;
111 struct libusb_config_descriptor *conf_dsc = NULL;
112 const struct libusb_interface_descriptor *intf_dsc;
116 /* Assume it's not a Saleae Logic unless proven wrong. */
119 if (libusb_get_device_descriptor(dev, &des) != 0)
122 if (des.bNumConfigurations != 1)
123 /* Need exactly 1 configuration. */
126 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
129 if (conf_dsc->bNumInterfaces != 1)
130 /* Need exactly 1 interface. */
133 if (conf_dsc->interface[0].num_altsetting != 1)
134 /* Need just one alternate setting. */
137 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
138 if (intf_dsc->bNumEndpoints != 2)
139 /* Need 2 endpoints. */
142 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
143 (1 | LIBUSB_ENDPOINT_OUT))
144 /* First endpoint should be 1 (outbound). */
147 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
148 (2 | LIBUSB_ENDPOINT_IN))
149 /* First endpoint should be 2 (inbound). */
152 /* If we made it here, it must be a Saleae Logic. */
157 libusb_free_config_descriptor(conf_dsc);
162 static int opendev2(int device_index, struct sigrok_device_instance **sdi,
163 libusb_device *dev, struct libusb_device_descriptor *des,
168 if ((err = libusb_get_device_descriptor(dev, des))) {
169 g_warning("failed to get device descriptor: %d", err);
173 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
176 if (*skip != device_index) {
177 /* Skip devices of this type that aren't the one we want. */
183 * Should check the bus here, since we know that already. But what are
184 * we going to do if it doesn't match after the right number of skips?
186 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
187 (*sdi)->usb->address = libusb_get_device_address(dev);
188 (*sdi)->status = ST_ACTIVE;
189 g_message("opened device %d on %d.%d interface %d",
190 (*sdi)->index, (*sdi)->usb->bus,
191 (*sdi)->usb->address, USB_INTERFACE);
193 g_warning("failed to open device: %d", err);
200 static int opendev3(int device_index, struct sigrok_device_instance **sdi,
201 libusb_device *dev, struct libusb_device_descriptor *des)
205 if ((err = libusb_get_device_descriptor(dev, des))) {
206 g_warning("failed to get device descriptor: %d", err);
210 if (des->idVendor != USB_VENDOR || des->idProduct != USB_PRODUCT)
213 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
214 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
216 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
217 (*sdi)->status = ST_ACTIVE;
218 g_message("opened device %d on %d.%d interface %d",
219 (*sdi)->index, (*sdi)->usb->bus,
220 (*sdi)->usb->address, USB_INTERFACE);
222 g_warning("failed to open device: %d", err);
230 struct sigrok_device_instance *sl_open_device(int device_index)
232 struct sigrok_device_instance *sdi;
233 libusb_device **devlist;
234 struct libusb_device_descriptor des;
237 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
240 libusb_get_device_list(usb_context, &devlist);
241 if (sdi->status == ST_INITIALIZING) {
243 * This device was renumerating last time we touched it.
244 * opendev() guarantees we've waited long enough for it to
245 * have booted properly, so now we need to find it on
246 * the bus and record its new address.
249 for (i = 0; devlist[i]; i++) {
250 /* TODO: Error handling. */
251 err = opendev2(device_index, &sdi, devlist[i], &des,
254 } else if (sdi->status == ST_INACTIVE) {
256 * This device is fully enumerated, so we need to find this
257 * device by vendor, product, bus and address.
259 libusb_get_device_list(usb_context, &devlist);
260 for (i = 0; devlist[i]; i++) {
261 /* TODO: Error handling. */
262 err = opendev3(device_index, &sdi, devlist[i], &des);
265 /* Status must be ST_ACTIVE, i.e. already in use... */
268 libusb_free_device_list(devlist, 1);
270 if (sdi && sdi->status != ST_ACTIVE)
276 int upload_firmware(libusb_device *dev)
280 if (ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE) != 0)
283 /* Remember when the last firmware update was done. */
284 g_get_current_time(&firmware_updated);
289 static void close_device(struct sigrok_device_instance *sdi)
291 if (sdi->usb->devhdl == NULL)
294 g_message("closing device %d on %d.%d interface %d", sdi->index,
295 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
296 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
297 libusb_close(sdi->usb->devhdl);
298 sdi->usb->devhdl = NULL;
299 sdi->status = ST_INACTIVE;
302 static int configure_probes(GSList * probes)
306 int probe_bit, stage, i;
310 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
312 trigger_value[i] = 0;
316 for (l = probes; l; l = l->next) {
317 probe = (struct probe *)l->data;
318 if (probe->enabled == FALSE)
320 probe_bit = 1 << (probe->index - 1);
321 probe_mask |= probe_bit;
322 if (probe->trigger) {
324 for (tc = probe->trigger; *tc; tc++) {
325 trigger_mask[stage] |= probe_bit;
327 trigger_value[stage] |= probe_bit;
329 if (stage > NUM_TRIGGER_STAGES)
337 * We didn't configure any triggers, make sure acquisition
338 * doesn't wait for any.
340 trigger_stage = TRIGGER_FIRED;
351 static int hw_init(char *deviceinfo)
353 struct sigrok_device_instance *sdi;
354 struct libusb_device_descriptor des;
355 libusb_device **devlist;
358 if (libusb_init(&usb_context) != 0) {
359 g_warning("Failed to initialize USB.");
362 libusb_set_debug(usb_context, 3);
364 /* Find all Saleae Logic devices and upload firmware to all of them. */
366 libusb_get_device_list(usb_context, &devlist);
367 for (i = 0; devlist[i]; i++) {
368 err = libusb_get_device_descriptor(devlist[i], &des);
370 g_warning("failed to get device descriptor: %d", err);
374 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
375 continue; /* Not a Saleae Logic... */
377 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
378 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
381 device_instances = g_slist_append(device_instances, sdi);
383 if (check_conf_profile(devlist[i]) == 0) {
385 * Continue on the off chance that the device is in a
386 * working state. TODO: Could maybe try a USB reset,
387 * or uploading the firmware again.
389 if (upload_firmware(devlist[i]) > 0)
390 g_warning("firmware upload failed for device %d", devcnt);
392 sdi->usb = usb_device_instance_new
393 (libusb_get_bus_number(devlist[i]), 0, NULL);
395 /* Already has the firmware, so fix the new address. */
396 sdi->usb = usb_device_instance_new
397 (libusb_get_bus_number(devlist[i]),
398 libusb_get_device_address(devlist[i]),
403 libusb_free_device_list(devlist, 1);
408 static int hw_opendev(int device_index)
411 struct sigrok_device_instance *sdi;
413 unsigned int cur, upd;
415 if (firmware_updated.tv_sec > 0) {
416 /* Firmware was recently uploaded. */
417 g_get_current_time(&cur_time);
418 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
419 upd = firmware_updated.tv_sec * 1000 +
420 firmware_updated.tv_usec / 1000;
421 timediff = cur - upd;
422 if (timediff < FIRMWARE_RENUM_DELAY) {
423 timediff = FIRMWARE_RENUM_DELAY - timediff;
424 g_message("waiting %d ms for device to reset",
426 g_usleep(timediff * 1000);
427 firmware_updated.tv_sec = 0;
431 if (!(sdi = sl_open_device(device_index))) {
432 g_warning("unable to open device");
436 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
438 g_warning("Unable to claim interface: %d", err);
442 if (cur_samplerate == 0) {
443 /* Samplerate hasn't been set; default to the slowest one. */
444 if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
445 &supported_samplerates[0]) == SIGROK_ERR)
452 static void hw_closedev(int device_index)
454 struct sigrok_device_instance *sdi;
456 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
460 static void hw_cleanup(void)
464 /* Properly close all devices... */
465 for (l = device_instances; l; l = l->next)
466 close_device((struct sigrok_device_instance *)l->data);
468 /* ...and free all their memory. */
469 for (l = device_instances; l; l = l->next)
471 g_slist_free(device_instances);
472 device_instances = NULL;
475 libusb_exit(usb_context);
479 static void *hw_get_device_info(int device_index, int device_info_id)
481 struct sigrok_device_instance *sdi;
484 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
487 switch (device_info_id) {
492 info = GINT_TO_POINTER(NUM_PROBES);
497 case DI_TRIGGER_TYPES:
498 info = TRIGGER_TYPES;
500 case DI_CUR_SAMPLERATE:
501 info = &cur_samplerate;
508 static int hw_get_status(int device_index)
510 struct sigrok_device_instance *sdi;
512 sdi = get_sigrok_device_instance(device_instances, device_index);
519 static int *hw_get_capabilities(void)
524 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
529 unsigned char buf[2];
531 for (i = 0; supported_samplerates[i]; i++) {
532 if (supported_samplerates[i] == samplerate)
535 if (supported_samplerates[i] == 0)
536 return SIGROK_ERR_SAMPLERATE;
538 divider = (uint8_t) (48 / (float)(samplerate / 1000000)) - 1;
540 g_message("setting samplerate to %" PRIu64 " Hz (divider %d)",
541 samplerate, divider);
544 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
545 buf, 2, &result, 500);
547 g_warning("failed to set samplerate: %d", ret);
550 cur_samplerate = samplerate;
555 static int hw_set_configuration(int device_index, int capability, void *value)
557 struct sigrok_device_instance *sdi;
561 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
564 if (capability == HWCAP_SAMPLERATE) {
566 ret = set_configuration_samplerate(sdi, *tmp_u64);
567 } else if (capability == HWCAP_PROBECONFIG) {
568 ret = configure_probes((GSList *) value);
569 } else if (capability == HWCAP_LIMIT_SAMPLES) {
570 limit_samples = strtoull(value, NULL, 10);
579 static int receive_data(int fd, int revents, void *user_data)
583 tv.tv_sec = tv.tv_usec = 0;
584 libusb_handle_events_timeout(usb_context, &tv);
589 void receive_transfer(struct libusb_transfer *transfer)
591 static int num_samples = 0;
592 static int empty_transfer_count = 0;
593 struct datafeed_packet packet;
595 int cur_buflen, trigger_offset, i;
596 unsigned char *cur_buf, *new_buf;
598 /* hw_stop_acquisition() is telling us to stop. */
599 if (transfer == NULL)
603 * If acquisition has already ended, just free any queued up
604 * transfer that come in.
606 if (num_samples == -1) {
607 libusb_free_transfer(transfer);
611 g_message("receive_transfer(): status %d received %d bytes",
612 transfer->status, transfer->actual_length);
614 /* Save incoming transfer before reusing the transfer struct. */
615 cur_buf = transfer->buffer;
616 cur_buflen = transfer->actual_length;
617 user_data = transfer->user_data;
619 /* Fire off a new request. */
620 new_buf = g_malloc(4096);
621 transfer->buffer = new_buf;
622 transfer->length = 4096;
623 if (libusb_submit_transfer(transfer) != 0) {
624 /* TODO: Stop session? */
628 if (cur_buflen == 0) {
629 empty_transfer_count++;
630 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
632 * The FX2 gave up. End the acquisition, the frontend
633 * will work out that the samplecount is short.
635 packet.type = DF_END;
636 session_bus(user_data, &packet);
641 empty_transfer_count = 0;
645 if (trigger_stage >= 0) {
646 for (i = 0; i < cur_buflen; i++) {
647 if ((cur_buf[i] & trigger_mask[trigger_stage])
648 == trigger_value[trigger_stage]) {
649 /* Match on this trigger stage. */
650 trigger_buffer[trigger_stage] = cur_buf[i];
652 if (trigger_stage == NUM_TRIGGER_STAGES
653 || trigger_mask[trigger_stage] == 0) {
654 /* Match on all trigger stages, we're done */
655 trigger_offset = i + 1;
657 /* TODO: Send pre-trigger buffer to session bus. Tell the frontend we hit the trigger here. */
658 packet.type = DF_TRIGGER;
660 session_bus(user_data, &packet);
662 /* Send the samples that triggered it, since we're skipping past them. */
663 packet.type = DF_LOGIC8;
664 packet.length = trigger_stage;
665 packet.payload = trigger_buffer;
666 session_bus(user_data, &packet);
669 trigger_stage = TRIGGER_FIRED;
671 } else if (trigger_stage > 0) {
673 * We had a match before, but not in the next sample. However, we may
674 * have a match on this stage in the next bit -- trigger on 0001 will
675 * fail on seeing 00001, so we need to go back to stage 0 -- but at
676 * the next sample from the one that matched originally, which the
677 * counter increment at the end of the loop takes care of.
681 /* Oops, went back past this buffer. */
683 /* Reset trigger stage. */
689 if (trigger_stage == TRIGGER_FIRED) {
690 /* Send the incoming transfer to the session bus. */
691 packet.type = DF_LOGIC8;
692 packet.length = cur_buflen - trigger_offset;
693 packet.payload = cur_buf + trigger_offset;
694 session_bus(user_data, &packet);
697 num_samples += cur_buflen;
698 if (num_samples > limit_samples) {
699 /* End the acquisition. */
700 packet.type = DF_END;
701 session_bus(user_data, &packet);
706 * TODO: Buffer pre-trigger data in capture
707 * ratio-sized buffer.
712 static int hw_start_acquisition(int device_index, gpointer session_device_id)
714 struct sigrok_device_instance *sdi;
715 struct datafeed_packet *packet;
716 struct datafeed_header *header;
717 struct libusb_transfer *transfer;
718 const struct libusb_pollfd **lupfd;
722 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
725 packet = g_malloc(sizeof(struct datafeed_packet));
726 header = g_malloc(sizeof(struct datafeed_header));
727 if (!packet || !header)
730 /* Start with 2K transfer, subsequently increased to 4K. */
732 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
733 buf = g_malloc(size);
734 transfer = libusb_alloc_transfer(0);
735 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
736 2 | LIBUSB_ENDPOINT_IN, buf, size,
737 receive_transfer, session_device_id, 40);
738 if (libusb_submit_transfer(transfer) != 0) {
739 /* TODO: Free them all. */
740 libusb_free_transfer(transfer);
747 lupfd = libusb_get_pollfds(usb_context);
748 for (i = 0; lupfd[i]; i++)
749 source_add(lupfd[i]->fd, lupfd[i]->events, -1, receive_data,
753 packet->type = DF_HEADER;
754 packet->length = sizeof(struct datafeed_header);
755 packet->payload = (unsigned char *)header;
756 header->feed_version = 1;
757 gettimeofday(&header->starttime, NULL);
758 header->samplerate = cur_samplerate;
759 header->protocol_id = PROTO_RAW;
760 header->num_probes = NUM_PROBES;
761 session_bus(session_device_id, packet);
768 /* This stops acquisition on ALL devices, ignoring device_index. */
769 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
771 struct datafeed_packet packet;
773 packet.type = DF_END;
774 session_bus(session_device_id, &packet);
776 receive_transfer(NULL);
778 /* TODO: Need to cancel and free any queued up transfers. */
781 struct device_plugin saleae_logic_plugin_info = {
792 hw_set_configuration,
793 hw_start_acquisition,