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
51 /* there is only one model Saleae Logic, and this is what it supports */
52 static int capabilities[] = {
56 /* these are really implemented in the driver, not the hardware */
61 /* list of struct sigrok_device_instance, maintained by opendev() and closedev() */
62 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 = {
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 trigger_mask[NUM_TRIGGER_STAGES] = {0}, \
99 trigger_value[NUM_TRIGGER_STAGES] = {0}, \
100 trigger_buffer[NUM_TRIGGER_STAGES] = {0};;
101 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;
114 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) != (1 | LIBUSB_ENDPOINT_OUT))
148 /* first endpoint should be 1 (outbound) */
151 if((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != (2 | LIBUSB_ENDPOINT_IN))
152 /* first endpoint should be 2 (inbound) */
155 /* if we made it here, it must be a Saleae Logic */
159 libusb_free_config_descriptor(conf_dsc);
165 struct sigrok_device_instance *sl_open_device(int device_index)
167 struct sigrok_device_instance *sdi;
168 libusb_device **devlist;
169 struct libusb_device_descriptor des;
172 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
175 libusb_get_device_list(usb_context, &devlist);
176 if(sdi->status == ST_INITIALIZING)
178 /* this device was renumerating last time we touched it. opendev() guarantees we've
179 * waited long enough for it to have booted properly, so now we need to find it on
180 * the bus and record its new address.
183 for(i = 0; devlist[i]; i++)
185 if( (err = libusb_get_device_descriptor(devlist[i], &des)) )
187 g_warning("failed to get device descriptor: %d", err);
191 if(des.idVendor == USB_VENDOR && des.idProduct == USB_PRODUCT)
193 if(skip != device_index)
195 /* skip past devices of this type that aren't the one we want */
200 /* should check the bus here, since we know that already... but what
201 * are we going to do if it doesn't match after the right number of skips?
204 if( !(err = libusb_open(devlist[i], &(sdi->usb->devhdl))) )
206 sdi->usb->address = libusb_get_device_address(devlist[i]);
207 sdi->status = ST_ACTIVE;
208 g_message("opened device %d on %d.%d interface %d", sdi->index, sdi->usb->bus,
209 sdi->usb->address, USB_INTERFACE);
213 g_warning("failed to open device: %d", err);
219 else if(sdi->status == ST_INACTIVE)
221 /* this device is fully enumerated, so we need to find this device by
222 * vendor, product, bus and address */
223 libusb_get_device_list(usb_context, &devlist);
224 for(i = 0; devlist[i]; i++)
226 if( (err = libusb_get_device_descriptor(devlist[i], &des)) )
228 g_warning("failed to get device descriptor: %d", err);
232 if(des.idVendor == USB_VENDOR && des.idProduct == USB_PRODUCT)
234 if(libusb_get_bus_number(devlist[i]) == sdi->usb->bus &&
235 libusb_get_device_address(devlist[i]) == sdi->usb->address)
238 if( !(err = libusb_open(devlist[i], &(sdi->usb->devhdl))) )
240 sdi->status = ST_ACTIVE;
241 g_message("opened device %d on %d.%d interface %d", sdi->index, sdi->usb->bus,
242 sdi->usb->address, USB_INTERFACE);
246 g_warning("failed to open device: %d", err);
255 /* status must be ST_ACTIVE, i.e. already in use... */
258 libusb_free_device_list(devlist, 1);
260 if(sdi && sdi->status != ST_ACTIVE)
267 int upload_firmware(libusb_device *dev)
269 struct libusb_device_handle *hdl;
272 g_message("uploading firmware to device on %d.%d",
273 libusb_get_bus_number(dev), libusb_get_device_address(dev));
275 err = libusb_open(dev, &hdl);
278 g_warning("failed to open device: %d", err);
282 err = libusb_set_configuration(hdl, USB_CONFIGURATION);
285 g_warning("Unable to set configuration: %d", err);
289 if((ezusb_reset(hdl, 1)) < 0)
292 if(ezusb_install_firmware(hdl, FIRMWARE) != 0)
295 if((ezusb_reset(hdl, 0)) < 0)
300 /* remember when the last firmware update was done */
301 g_get_current_time(&firmware_updated);
307 static void close_device(struct sigrok_device_instance *sdi)
312 g_message("closing device %d on %d.%d interface %d", sdi->index, sdi->usb->bus,
313 sdi->usb->address, USB_INTERFACE);
314 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
315 libusb_close(sdi->usb->devhdl);
316 sdi->usb->devhdl = NULL;
317 sdi->status = ST_INACTIVE;
323 static int configure_probes(GSList *probes)
327 int probe_bit, stage, i;
331 for(i = 0; i < NUM_TRIGGER_STAGES; i++)
334 trigger_value[i] = 0;
338 for(l = probes; l; l = l->next)
340 probe = (struct probe *) l->data;
341 if(probe->enabled == FALSE)
343 probe_bit = 1 << (probe->index - 1);
344 probe_mask |= probe_bit;
348 for(tc = probe->trigger; *tc; tc++)
350 trigger_mask[stage] |= probe_bit;
352 trigger_value[stage] |= probe_bit;
354 if(stage > NUM_TRIGGER_STAGES)
361 /* we didn't configure any triggers, make sure acquisition doesn't wait for any */
362 trigger_stage = TRIGGER_FIRED;
375 static int hw_init(char *deviceinfo)
377 struct sigrok_device_instance *sdi;
378 struct libusb_device_descriptor des;
379 libusb_device **devlist;
382 if(libusb_init(&usb_context) != 0) {
383 g_warning("Failed to initialize USB.");
386 libusb_set_debug(usb_context, 3);
388 /* find all Saleae Logic devices and upload firmware to all of them */
390 libusb_get_device_list(usb_context, &devlist);
391 for(i = 0; devlist[i]; i++) {
392 err = libusb_get_device_descriptor(devlist[i], &des);
394 g_warning("failed to get device descriptor: %d", err);
398 if(des.idVendor == USB_VENDOR && des.idProduct == USB_PRODUCT) {
399 /* definitely a Saleae Logic */
401 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
402 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
405 device_instances = g_slist_append(device_instances, sdi);
407 if(check_conf_profile(devlist[i]) == 0)
409 if(upload_firmware(devlist[i]) > 0)
410 /* continue on the off chance that the device is in a working state */
411 /* TODO: could maybe try a USB reset, or uploading the firmware again... */
412 g_warning("firmware upload failed for device %d", devcnt);
414 sdi->usb = usb_device_instance_new(libusb_get_bus_number(devlist[i]), 0, NULL);
417 /* already has the firmware on it, so fix the new address */
418 sdi->usb = usb_device_instance_new(libusb_get_bus_number(devlist[i]),
419 libusb_get_device_address(devlist[i]), NULL);
424 libusb_free_device_list(devlist, 1);
430 static int hw_opendev(int device_index)
433 struct sigrok_device_instance *sdi;
435 unsigned int cur, upd;
437 if(firmware_updated.tv_sec > 0) {
438 /* firmware was recently uploaded */
439 g_get_current_time(&cur_time);
440 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
441 upd = firmware_updated.tv_sec * 1000 + firmware_updated.tv_usec / 1000;
442 timediff = cur - upd;
443 if(timediff < FIRMWARE_RENUM_DELAY) {
444 timediff = FIRMWARE_RENUM_DELAY - timediff;
445 g_message("waiting %d ms for device to reset", timediff);
446 g_usleep(timediff * 1000);
447 firmware_updated.tv_sec = 0;
451 if( !(sdi = sl_open_device(device_index)) ) {
452 g_warning("unable to open device");
456 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
458 g_warning("Unable to claim interface: %d", err);
462 if(cur_samplerate == 0) {
463 /* sample rate hasn't been set; default to the slowest it has */
464 if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &supported_samplerates[0]) == SIGROK_NOK)
472 static void hw_closedev(int device_index)
474 struct sigrok_device_instance *sdi;
476 if( (sdi = get_sigrok_device_instance(device_instances, device_index)) )
482 static void hw_cleanup(void)
486 /* properly close all devices */
487 for(l = device_instances; l; l = l->next)
488 close_device( (struct sigrok_device_instance *) l->data);
490 /* and free all their memory */
491 for(l = device_instances; l; l = l->next)
493 g_slist_free(device_instances);
494 device_instances = NULL;
497 libusb_exit(usb_context);
503 static void *hw_get_device_info(int device_index, int device_info_id)
505 struct sigrok_device_instance *sdi;
508 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
512 switch(device_info_id)
518 info = GINT_TO_POINTER(NUM_PROBES);
523 case DI_TRIGGER_TYPES:
524 info = TRIGGER_TYPES;
526 case DI_CUR_SAMPLERATE:
527 info = &cur_samplerate;
535 static int hw_get_status(int device_index)
537 struct sigrok_device_instance *sdi;
539 sdi = get_sigrok_device_instance(device_instances, device_index);
547 static int *hw_get_capabilities(void)
554 static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
558 unsigned char buf[2];
560 for(i = 0; supported_samplerates[i]; i++) {
561 if(supported_samplerates[i] == samplerate)
564 if(supported_samplerates[i] == 0)
565 return SIGROK_ERR_BADVALUE;
567 divider = (uint8_t) (48 / (float) (samplerate/1000000)) - 1;
569 g_message("setting samplerate to %"PRIu64" Hz (divider %d)", samplerate, divider);
572 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, buf, 2, &result, 500);
574 g_warning("failed to set samplerate: %d", ret);
577 cur_samplerate = samplerate;
583 static int hw_set_configuration(int device_index, int capability, void *value)
585 struct sigrok_device_instance *sdi;
589 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
592 if(capability == HWCAP_SAMPLERATE) {
594 ret = set_configuration_samplerate(sdi, *tmp_u64);
596 else if(capability == HWCAP_PROBECONFIG)
597 ret = configure_probes( (GSList *) value);
598 else if(capability == HWCAP_LIMIT_SAMPLES) {
599 limit_samples = strtoull(value, NULL, 10);
609 static int receive_data(int fd, int revents, void *user_data)
613 tv.tv_sec = tv.tv_usec = 0;
614 libusb_handle_events_timeout(usb_context, &tv);
620 void receive_transfer(struct libusb_transfer *transfer)
622 static int num_samples = 0;
623 static int empty_transfer_count = 0;
625 struct datafeed_packet packet;
627 int cur_buflen, trigger_offset, i;
628 unsigned char *cur_buf, *new_buf;
630 if(transfer == NULL) {
631 /* hw_stop_acquisition() telling us to stop */
635 if(num_samples == -1) {
636 /* acquisition has already ended, just free any queued up transfer that come in */
637 libusb_free_transfer(transfer);
640 g_message("receive_transfer(): status %d received %d bytes", transfer->status, transfer->actual_length);
642 /* save the incoming transfer before reusing the transfer struct */
643 cur_buf = transfer->buffer;
644 cur_buflen = transfer->actual_length;
645 user_data = transfer->user_data;
647 /* fire off a new request */
648 new_buf = g_malloc(4096);
649 transfer->buffer = new_buf;
650 transfer->length = 4096;
651 if(libusb_submit_transfer(transfer) != 0) {
652 /* TODO: stop session? */
656 if(cur_buflen == 0) {
657 empty_transfer_count++;
658 if(empty_transfer_count > MAX_EMPTY_TRANSFERS) {
659 /* the FX2 gave up... end the acquisition, the frontend will work
660 * out that the samplecount is short
662 packet.type = DF_END;
663 session_bus(user_data, &packet);
669 empty_transfer_count = 0;
672 if(trigger_stage >= 0)
674 for(i = 0; i < cur_buflen; i++)
676 if((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage])
678 /* match on this trigger stage */
679 trigger_buffer[trigger_stage] = cur_buf[i];
681 if(trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0)
683 /* match on all trigger stages, we're done */
684 trigger_offset = i+1;
686 /* TODO: send pre-trigger buffer to session bus */
688 /* tell the frontend we hit the trigger here */
689 packet.type = DF_TRIGGER;
691 session_bus(user_data, &packet);
693 /* send the samples that triggered it, since we're skipping past them */
694 packet.type = DF_LOGIC8;
695 packet.length = trigger_stage;
696 packet.payload = trigger_buffer;
697 session_bus(user_data, &packet);
700 trigger_stage = TRIGGER_FIRED;
703 else if(trigger_stage > 0)
705 /* we had a match before, but not in the next sample. however, we may
706 * have a match on this stage in the next bit -- trigger on 0001 will
707 * fail on seeing 00001, so we need to go back to stage 0 -- but at
708 * the next sample from the one that matched originally, which the
709 * counter increment at the end of the loop takes care of.
713 /* oops, went back past this buffer */
715 /* reset trigger stage */
721 if(trigger_stage == TRIGGER_FIRED)
723 /* send the incoming transfer to the session bus */
724 packet.type = DF_LOGIC8;
725 packet.length = cur_buflen - trigger_offset;
726 packet.payload = cur_buf + trigger_offset;
727 session_bus(user_data, &packet);
730 num_samples += cur_buflen;
731 if(num_samples > limit_samples)
733 /* end the acquisition */
734 packet.type = DF_END;
735 session_bus(user_data, &packet);
741 /* TODO: buffer pre-trigger data in capture ratio-sized buffer */
749 static int hw_start_acquisition(int device_index, gpointer session_device_id)
751 struct sigrok_device_instance *sdi;
752 struct datafeed_packet *packet;
753 struct datafeed_header *header;
754 struct libusb_transfer *transfer;
755 const struct libusb_pollfd **lupfd;
759 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)))
762 packet = g_malloc(sizeof(struct datafeed_packet));
763 header = g_malloc(sizeof(struct datafeed_header));
764 if(!packet || !header)
767 /* start with 2K transfer, subsequently increased to 4K */
769 for(i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
770 buf = g_malloc(size);
771 transfer = libusb_alloc_transfer(0);
772 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size,
773 receive_transfer, session_device_id, 40);
774 if(libusb_submit_transfer(transfer) != 0) {
775 /* TODO: free them all */
776 libusb_free_transfer(transfer);
783 lupfd = libusb_get_pollfds(usb_context);
784 for(i = 0; lupfd[i]; i++)
785 source_add(lupfd[i]->fd, lupfd[i]->events, -1, receive_data, NULL);
788 packet->type = DF_HEADER;
789 packet->length = sizeof(struct datafeed_header);
790 packet->payload = (unsigned char *) header;
791 header->feed_version = 1;
792 gettimeofday(&header->starttime, NULL);
793 header->samplerate = cur_samplerate;
794 header->protocol_id = PROTO_RAW;
795 header->num_probes = NUM_PROBES;
796 session_bus(session_device_id, packet);
804 /* this stops acquisition on ALL devices, ignoring device_index */
805 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
807 struct datafeed_packet packet;
809 packet.type = DF_END;
810 session_bus(session_device_id, &packet);
812 receive_transfer(NULL);
814 /* TODO: need to cancel and free any queued up transfers */
820 struct device_plugin saleae_logic_plugin_info = {
831 hw_set_configuration,
832 hw_start_acquisition,