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. */
61 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
62 static GSList *device_instances = NULL;
65 * Since we can't keep track of a Saleae Logic device after upgrading the
66 * firmware -- it re-enumerates into a different device address after the
67 * upgrade -- this is like a global lock. No device will open until a proper
68 * delay after the last device was upgraded.
70 GTimeVal firmware_updated = { 0, 0 };
72 static libusb_context *usb_context = NULL;
74 static uint64_t supported_samplerates[] = {
88 static struct samplerates samplerates = {
92 supported_samplerates,
95 /* TODO: All of these should go in a device-specific struct. */
96 static uint64_t cur_samplerate = 0;
97 static uint64_t limit_samples = 0;
98 static uint8_t probe_mask = 0;
99 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
100 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
101 static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
103 int trigger_stage = TRIGGER_FIRED;
105 static int hw_set_configuration(int device_index, int capability, void *value);
106 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
109 * Check the USB configuration to determine if this is a Saleae Logic.
111 * @return 1 if the device's configuration profile match the Logic firmware's
112 * configuration, 0 otherwise.
114 int check_conf_profile(libusb_device *dev)
116 struct libusb_device_descriptor des;
117 struct libusb_config_descriptor *conf_dsc = NULL;
118 const struct libusb_interface_descriptor *intf_dsc;
122 /* Assume it's not a Saleae Logic unless proven wrong. */
125 if (libusb_get_device_descriptor(dev, &des) != 0)
128 if (des.bNumConfigurations != 1)
129 /* Need exactly 1 configuration. */
132 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
135 if (conf_dsc->bNumInterfaces != 1)
136 /* Need exactly 1 interface. */
139 if (conf_dsc->interface[0].num_altsetting != 1)
140 /* Need just one alternate setting. */
143 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
144 if (intf_dsc->bNumEndpoints != 2)
145 /* Need 2 endpoints. */
148 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
149 (1 | LIBUSB_ENDPOINT_OUT))
150 /* First endpoint should be 1 (outbound). */
153 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
154 (2 | LIBUSB_ENDPOINT_IN))
155 /* First endpoint should be 2 (inbound). */
158 /* If we made it here, it must be a Saleae Logic. */
163 libusb_free_config_descriptor(conf_dsc);
168 struct sigrok_device_instance *sl_open_device(int device_index)
170 struct sigrok_device_instance *sdi;
171 libusb_device **devlist;
172 struct libusb_device_descriptor des;
175 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
178 libusb_get_device_list(usb_context, &devlist);
179 if (sdi->status == ST_INITIALIZING) {
181 * This device was renumerating last time we touched it.
182 * opendev() guarantees we've waited long enough for it to
183 * have booted properly, so now we need to find it on
184 * the bus and record its new address.
187 for (i = 0; devlist[i]; i++) {
188 /* TODO: Error handling. */
189 err = opendev2(device_index, &sdi, devlist[i], &des,
190 &skip, USB_VENDOR, USB_PRODUCT,
193 } else if (sdi->status == ST_INACTIVE) {
195 * This device is fully enumerated, so we need to find this
196 * device by vendor, product, bus and address.
198 libusb_get_device_list(usb_context, &devlist);
199 for (i = 0; devlist[i]; i++) {
200 /* TODO: Error handling. */
201 err = opendev3(&sdi, devlist[i], &des, USB_VENDOR,
202 USB_PRODUCT, USB_INTERFACE);
205 /* Status must be ST_ACTIVE, i.e. already in use... */
208 libusb_free_device_list(devlist, 1);
210 if (sdi && sdi->status != ST_ACTIVE)
216 int upload_firmware(libusb_device *dev)
220 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
224 /* Remember when the last firmware update was done. */
225 g_get_current_time(&firmware_updated);
230 static void close_device(struct sigrok_device_instance *sdi)
232 if (sdi->usb->devhdl == NULL)
235 g_message("saleae: closing device %d on %d.%d interface %d", sdi->index,
236 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
237 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
238 libusb_close(sdi->usb->devhdl);
239 sdi->usb->devhdl = NULL;
240 sdi->status = ST_INACTIVE;
243 static int configure_probes(GSList *probes)
247 int probe_bit, stage, i;
251 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
253 trigger_value[i] = 0;
257 for (l = probes; l; l = l->next) {
258 probe = (struct probe *)l->data;
259 if (probe->enabled == FALSE)
261 probe_bit = 1 << (probe->index - 1);
262 probe_mask |= probe_bit;
263 if (!(probe->trigger))
267 for (tc = probe->trigger; *tc; tc++) {
268 trigger_mask[stage] |= probe_bit;
270 trigger_value[stage] |= probe_bit;
272 if (stage > NUM_TRIGGER_STAGES)
279 * We didn't configure any triggers, make sure acquisition
280 * doesn't wait for any.
282 trigger_stage = TRIGGER_FIRED;
293 static int hw_init(char *deviceinfo)
295 struct sigrok_device_instance *sdi;
296 struct libusb_device_descriptor des;
297 libusb_device **devlist;
300 /* Avoid compiler warnings. */
301 deviceinfo = deviceinfo;
303 if (libusb_init(&usb_context) != 0) {
304 g_warning("Failed to initialize USB.");
308 /* Find all Saleae Logic devices and upload firmware to all of them. */
310 libusb_get_device_list(usb_context, &devlist);
311 for (i = 0; devlist[i]; i++) {
312 err = libusb_get_device_descriptor(devlist[i], &des);
314 g_warning("failed to get device descriptor: %d", err);
318 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
319 continue; /* Not a Saleae Logic... */
321 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
322 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
325 device_instances = g_slist_append(device_instances, sdi);
327 if (check_conf_profile(devlist[i]) == 0) {
329 * Continue on the off chance that the device is in a
330 * working state. TODO: Could maybe try a USB reset,
331 * or uploading the firmware again.
333 if (upload_firmware(devlist[i]) > 0)
334 g_warning("firmware upload failed for "
335 "device %d", devcnt);
337 sdi->usb = usb_device_instance_new
338 (libusb_get_bus_number(devlist[i]), 0, NULL);
340 /* Already has the firmware, so fix the new address. */
341 sdi->usb = usb_device_instance_new
342 (libusb_get_bus_number(devlist[i]),
343 libusb_get_device_address(devlist[i]), NULL);
347 libusb_free_device_list(devlist, 1);
352 static int hw_opendev(int device_index)
355 struct sigrok_device_instance *sdi;
357 unsigned int cur, upd;
359 if (firmware_updated.tv_sec > 0) {
360 /* Firmware was recently uploaded. */
361 g_get_current_time(&cur_time);
362 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
363 upd = firmware_updated.tv_sec * 1000 +
364 firmware_updated.tv_usec / 1000;
365 timediff = cur - upd;
366 if (timediff < FIRMWARE_RENUM_DELAY) {
367 timediff = FIRMWARE_RENUM_DELAY - timediff;
368 g_message("saleae: waiting %d ms for device to reset",
370 g_usleep(timediff * 1000);
371 firmware_updated.tv_sec = 0;
375 if (!(sdi = sl_open_device(device_index))) {
376 g_warning("unable to open device");
380 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
382 g_warning("Unable to claim interface: %d", err);
386 if (cur_samplerate == 0) {
387 /* Samplerate hasn't been set; default to the slowest one. */
388 if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
389 &supported_samplerates[0]) == SIGROK_ERR)
396 static void hw_closedev(int device_index)
398 struct sigrok_device_instance *sdi;
400 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
404 static void hw_cleanup(void)
408 /* Properly close all devices... */
409 for (l = device_instances; l; l = l->next)
410 close_device((struct sigrok_device_instance *)l->data);
412 /* ...and free all their memory. */
413 for (l = device_instances; l; l = l->next)
415 g_slist_free(device_instances);
416 device_instances = NULL;
419 libusb_exit(usb_context);
423 static void *hw_get_device_info(int device_index, int device_info_id)
425 struct sigrok_device_instance *sdi;
428 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
431 switch (device_info_id) {
436 info = GINT_TO_POINTER(NUM_PROBES);
441 case DI_TRIGGER_TYPES:
442 info = TRIGGER_TYPES;
444 case DI_CUR_SAMPLERATE:
445 info = &cur_samplerate;
452 static int hw_get_status(int device_index)
454 struct sigrok_device_instance *sdi;
456 sdi = get_sigrok_device_instance(device_instances, device_index);
463 static int *hw_get_capabilities(void)
468 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
473 unsigned char buf[2];
475 for (i = 0; supported_samplerates[i]; i++) {
476 if (supported_samplerates[i] == samplerate)
479 if (supported_samplerates[i] == 0)
480 return SIGROK_ERR_SAMPLERATE;
482 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
484 g_message("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
485 samplerate, divider);
488 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
489 buf, 2, &result, 500);
491 g_warning("failed to set samplerate: %d", ret);
494 cur_samplerate = samplerate;
499 static int hw_set_configuration(int device_index, int capability, void *value)
501 struct sigrok_device_instance *sdi;
505 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
508 if (capability == HWCAP_SAMPLERATE) {
510 ret = set_configuration_samplerate(sdi, *tmp_u64);
511 } else if (capability == HWCAP_PROBECONFIG) {
512 ret = configure_probes((GSList *) value);
513 } else if (capability == HWCAP_LIMIT_SAMPLES) {
515 limit_samples = *tmp_u64;
524 static int receive_data(int fd, int revents, void *user_data)
528 /* Avoid compiler warnings. */
531 user_data = user_data;
533 tv.tv_sec = tv.tv_usec = 0;
534 libusb_handle_events_timeout(usb_context, &tv);
539 void receive_transfer(struct libusb_transfer *transfer)
541 static int num_samples = 0;
542 static int empty_transfer_count = 0;
543 struct datafeed_packet packet;
545 int cur_buflen, trigger_offset, i;
546 unsigned char *cur_buf, *new_buf;
548 /* hw_stop_acquisition() is telling us to stop. */
549 if (transfer == NULL)
553 * If acquisition has already ended, just free any queued up
554 * transfer that come in.
556 if (num_samples == -1) {
558 libusb_free_transfer(transfer);
562 g_message("saleae: receive_transfer(): status %d received %d bytes",
563 transfer->status, transfer->actual_length);
565 /* Save incoming transfer before reusing the transfer struct. */
566 cur_buf = transfer->buffer;
567 cur_buflen = transfer->actual_length;
568 user_data = transfer->user_data;
570 /* Fire off a new request. */
571 new_buf = g_malloc(4096);
572 transfer->buffer = new_buf;
573 transfer->length = 4096;
574 if (libusb_submit_transfer(transfer) != 0) {
575 /* TODO: Stop session? */
579 if (cur_buflen == 0) {
580 empty_transfer_count++;
581 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
583 * The FX2 gave up. End the acquisition, the frontend
584 * will work out that the samplecount is short.
586 hw_stop_acquisition(-1, user_data);
590 empty_transfer_count = 0;
594 if (trigger_stage >= 0) {
595 for (i = 0; i < cur_buflen; i++) {
597 if ((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage]) {
598 /* Match on this trigger stage. */
599 trigger_buffer[trigger_stage] = cur_buf[i];
601 if (trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0) {
602 /* Match on all trigger stages, we're done. */
603 trigger_offset = i + 1;
606 * TODO: Send pre-trigger buffer to session bus.
607 * Tell the frontend we hit the trigger here.
609 packet.type = DF_TRIGGER;
611 session_bus(user_data, &packet);
614 * Send the samples that triggered it, since we're
615 * skipping past them.
617 packet.type = DF_LOGIC;
618 packet.length = trigger_stage;
620 packet.payload = trigger_buffer;
621 session_bus(user_data, &packet);
623 trigger_stage = TRIGGER_FIRED;
630 * We had a match before, but not in the next sample. However, we may
631 * have a match on this stage in the next bit -- trigger on 0001 will
632 * fail on seeing 00001, so we need to go back to stage 0 -- but at
633 * the next sample from the one that matched originally, which the
634 * counter increment at the end of the loop takes care of.
636 if (trigger_stage > 0) {
639 i = -1; /* Oops, went back past this buffer. */
640 /* Reset trigger stage. */
648 if (trigger_stage == TRIGGER_FIRED) {
649 /* Send the incoming transfer to the session bus. */
650 packet.type = DF_LOGIC;
651 packet.length = cur_buflen - trigger_offset;
653 packet.payload = cur_buf + trigger_offset;
654 session_bus(user_data, &packet);
657 num_samples += cur_buflen;
658 if (limit_samples && (unsigned int) num_samples > limit_samples) {
659 hw_stop_acquisition(-1, user_data);
663 * TODO: Buffer pre-trigger data in capture
664 * ratio-sized buffer.
669 static int hw_start_acquisition(int device_index, gpointer session_device_id)
671 struct sigrok_device_instance *sdi;
672 struct datafeed_packet *packet;
673 struct datafeed_header *header;
674 struct libusb_transfer *transfer;
675 const struct libusb_pollfd **lupfd;
679 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
682 packet = g_malloc(sizeof(struct datafeed_packet));
683 header = g_malloc(sizeof(struct datafeed_header));
684 if (!packet || !header)
687 /* Start with 2K transfer, subsequently increased to 4K. */
689 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
690 buf = g_malloc(size);
691 transfer = libusb_alloc_transfer(0);
692 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
693 2 | LIBUSB_ENDPOINT_IN, buf, size,
694 receive_transfer, session_device_id, 40);
695 if (libusb_submit_transfer(transfer) != 0) {
696 /* TODO: Free them all. */
697 libusb_free_transfer(transfer);
704 lupfd = libusb_get_pollfds(usb_context);
705 for (i = 0; lupfd[i]; i++)
706 source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
710 packet->type = DF_HEADER;
711 packet->length = sizeof(struct datafeed_header);
712 packet->payload = (unsigned char *)header;
713 header->feed_version = 1;
714 gettimeofday(&header->starttime, NULL);
715 header->samplerate = cur_samplerate;
716 header->protocol_id = PROTO_RAW;
717 header->num_logic_probes = NUM_PROBES;
718 header->num_analog_probes = 0;
719 session_bus(session_device_id, packet);
726 /* This stops acquisition on ALL devices, ignoring device_index. */
727 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
729 struct datafeed_packet packet;
731 /* Avoid compiler warnings. */
732 device_index = device_index;
734 packet.type = DF_END;
735 session_bus(session_device_id, &packet);
737 receive_transfer(NULL);
739 /* TODO: Need to cancel and free any queued up transfers. */
742 struct device_plugin saleae_logic_plugin_info = {
753 hw_set_configuration,
754 hw_start_acquisition,