X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fsaleae-logic%2Fsaleae-logic.c;h=42884715acf058a3d022eed90584fb0498c8518c;hb=93a04e3be9cdaa276d9e7fe14da053483b567b74;hp=27938a51bc18623efa3caa25d1f8b0feb5771c58;hpb=69890f7399f086c38670961dc72166f00de89af6;p=libsigrok.git diff --git a/hardware/saleae-logic/saleae-logic.c b/hardware/saleae-logic/saleae-logic.c index 27938a51..42884715 100644 --- a/hardware/saleae-logic/saleae-logic.c +++ b/hardware/saleae-logic/saleae-logic.c @@ -1,7 +1,7 @@ /* * This file is part of the sigrok project. * - * Copyright (C) 2010 Bert Vermeulen + * Copyright (C) 2010-2012 Bert Vermeulen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,13 +17,13 @@ * along with this program. If not, see . */ -#include "config.h" #include #include #include #include #include #include +#include "config.h" #include "sigrok.h" #include "sigrok-internal.h" #include "saleae-logic.h" @@ -91,8 +91,10 @@ static struct sr_samplerates samplerates = { static GSList *device_instances = NULL; static libusb_context *usb_context = NULL; +static int new_saleae_logic_firmware = 0; + static int hw_set_configuration(int device_index, int capability, void *value); -static void hw_stop_acquisition(int device_index, gpointer session_device_id); +static int hw_stop_acquisition(int device_index, gpointer session_device_id); /** * Check the USB configuration to determine if this is a Saleae Logic. @@ -130,20 +132,29 @@ static int check_conf_profile(libusb_device *dev) break; intf_dsc = &(conf_dsc->interface[0].altsetting[0]); - if (intf_dsc->bNumEndpoints != 2) - /* Need 2 endpoints. */ + if (intf_dsc->bNumEndpoints == 4) { + /* The new Saleae Logic firmware has 4 endpoints. */ + new_saleae_logic_firmware = 1; + } else if (intf_dsc->bNumEndpoints == 2) { + /* The old Saleae Logic firmware has 2 endpoints. */ + new_saleae_logic_firmware = 0; + } else { + /* Other number of endpoints -> not a Saleae Logic. */ break; + } if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != (1 | LIBUSB_ENDPOINT_OUT)) - /* First endpoint should be 1 (outbound). */ + /* The first endpoint should be 1 (outbound). */ break; if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) != (2 | LIBUSB_ENDPOINT_IN)) - /* First endpoint should be 2 (inbound). */ + /* The second endpoint should be 2 (inbound). */ break; + /* TODO: The new firmware has 4 endpoints... */ + /* If we made it here, it must be a Saleae Logic. */ ret = 1; } @@ -162,7 +173,7 @@ static int sl_open_device(int device_index) struct fx2_device *fx2; int err, skip, i; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; @@ -174,7 +185,7 @@ static int sl_open_device(int device_index) libusb_get_device_list(usb_context, &devlist); for (i = 0; devlist[i]; i++) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); continue; } @@ -211,7 +222,7 @@ static int sl_open_device(int device_index) sdi->index, fx2->usb->bus, fx2->usb->address, USB_INTERFACE); } else { - sr_warn("failed to open device: %d", err); + sr_err("failed to open device: %d", err); } /* if we made it here, we handled the device one way or another */ @@ -320,7 +331,7 @@ static int hw_init(const char *deviceinfo) (void)deviceinfo; if (libusb_init(&usb_context) != 0) { - sr_warn("Failed to initialize USB."); + sr_err("Failed to initialize USB."); return 0; } @@ -331,7 +342,7 @@ static int hw_init(const char *deviceinfo) fx2_prof = NULL; err = libusb_get_device_descriptor(devlist[i], &des); if (err != 0) { - sr_warn("failed to get device descriptor: %d", err); + sr_err("failed to get device descriptor: %d", err); continue; } @@ -346,7 +357,7 @@ static int hw_init(const char *deviceinfo) /* not a supported VID/PID */ continue; - sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, + sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version); if (!sdi) return 0; @@ -357,8 +368,10 @@ static int hw_init(const char *deviceinfo) if (check_conf_profile(devlist[i])) { /* Already has the firmware, so fix the new address. */ + sr_dbg("Found a Saleae Logic with %s firmware.", + new_saleae_logic_firmware ? "new" : "old"); sdi->status = SR_ST_INACTIVE; - fx2->usb = sr_usb_device_instance_new + fx2->usb = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); } else { @@ -366,8 +379,8 @@ static int hw_init(const char *deviceinfo) /* Remember when the firmware on this device was updated */ g_get_current_time(&fx2->fw_updated); else - sr_warn("firmware upload failed for device %d", devcnt); - fx2->usb = sr_usb_device_instance_new + sr_err("firmware upload failed for device %d", devcnt); + fx2->usb = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]), 0xff, NULL); } devcnt++; @@ -384,7 +397,7 @@ static int hw_opendev(int device_index) struct fx2_device *fx2; int timediff, err; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; @@ -411,14 +424,14 @@ static int hw_opendev(int device_index) } if (err != SR_OK) { - sr_warn("unable to open device"); + sr_err("unable to open device"); return SR_ERR; } fx2 = sdi->priv; err = libusb_claim_interface(fx2->usb->devhdl, USB_INTERFACE); if (err != 0) { - sr_warn("Unable to claim interface: %d", err); + sr_err("Unable to claim interface: %d", err); return SR_ERR; } @@ -436,7 +449,7 @@ static int hw_closedev(int device_index) { struct sr_device_instance *sdi; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) { + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) { sr_err("logic: %s: sdi was NULL", __func__); return SR_ERR; /* TODO: SR_ERR_ARG? */ } @@ -447,19 +460,31 @@ static int hw_closedev(int device_index) return SR_OK; } -static void hw_cleanup(void) +static int hw_cleanup(void) { GSList *l; struct sr_device_instance *sdi; struct fx2_device *fx2; + int ret = SR_OK; /* Properly close and free all devices. */ for (l = device_instances; l; l = l->next) { - sdi = l->data; - fx2 = sdi->priv; + if (!(sdi = l->data)) { + /* Log error, but continue cleaning up the rest. */ + sr_err("fx2: %s: sdi was NULL, continuing", __func__); + ret = SR_ERR_BUG; + continue; + } + if (!(fx2 = sdi->priv)) { + /* Log error, but continue cleaning up the rest. */ + sr_err("fx2: %s: sdi->priv was NULL, continuing", + __func__); + ret = SR_ERR_BUG; + continue; + } close_device(sdi); - sr_usb_device_instance_free(fx2->usb); - sr_device_instance_free(sdi); + sr_usb_dev_inst_free(fx2->usb); + sr_dev_inst_free(sdi); } g_slist_free(device_instances); @@ -468,6 +493,8 @@ static void hw_cleanup(void) if (usb_context) libusb_exit(usb_context); usb_context = NULL; + + return ret; } static void *hw_get_device_info(int device_index, int device_info_id) @@ -476,7 +503,7 @@ static void *hw_get_device_info(int device_index, int device_info_id) struct fx2_device *fx2; void *info = NULL; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) return NULL; fx2 = sdi->priv; @@ -508,7 +535,7 @@ static int hw_get_status(int device_index) { struct sr_device_instance *sdi; - sdi = sr_get_device_instance(device_instances, device_index); + sdi = sr_dev_inst_get(device_instances, device_index); if (sdi) return sdi->status; else @@ -520,6 +547,47 @@ static int *hw_get_capabilities(void) return capabilities; } +static uint8_t new_firmware_divider_value(uint64_t samplerate) +{ + switch (samplerate) { + case SR_MHZ(24): + return 0xe0; + break; + case SR_MHZ(16): + return 0xd5; + break; + case SR_MHZ(12): + return 0xe2; + break; + case SR_MHZ(8): + return 0xd4; + break; + case SR_MHZ(4): + return 0xda; + break; + case SR_MHZ(2): + return 0xe6; + break; + case SR_MHZ(1): + return 0x8e; + break; + case SR_KHZ(500): + return 0xfe; + break; + case SR_KHZ(250): + return 0x9e; + break; + case SR_KHZ(200): + return 0x4e; + break; + } + + /* Shouldn't happen. */ + sr_err("saleae: %s: Invalid samplerate %" PRIu64 "", + __func__, samplerate); + return 0; +} + static int set_configuration_samplerate(struct sr_device_instance *sdi, uint64_t samplerate) { @@ -536,20 +604,23 @@ static int set_configuration_samplerate(struct sr_device_instance *sdi, if (supported_samplerates[i] == 0) return SR_ERR_SAMPLERATE; - divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1; + if (new_saleae_logic_firmware) + divider = new_firmware_divider_value(samplerate); + else + divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1; sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)", samplerate, divider); - buf[0] = 0x01; + + buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01; buf[1] = divider; ret = libusb_bulk_transfer(fx2->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, buf, 2, &result, 500); if (ret != 0) { - sr_warn("failed to set samplerate: %d", ret); + sr_err("failed to set samplerate: %d", ret); return SR_ERR; } fx2->cur_samplerate = samplerate; - fx2->period_ps = 1000000000000 / samplerate; return SR_OK; } @@ -561,7 +632,7 @@ static int hw_set_configuration(int device_index, int capability, void *value) int ret; uint64_t *tmp_u64; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; @@ -632,15 +703,14 @@ static void receive_transfer(struct libusb_transfer *transfer) /* Fire off a new request. */ if (!(new_buf = g_try_malloc(4096))) { sr_err("saleae: %s: new_buf malloc failed", __func__); - // return SR_ERR_MALLOC; - return; /* FIXME */ + return; /* TODO: SR_ERR_MALLOC */ } transfer->buffer = new_buf; transfer->length = 4096; if (libusb_submit_transfer(transfer) != 0) { /* TODO: Stop session? */ - sr_warn("eek"); + sr_err("eek"); } if (cur_buflen == 0) { @@ -675,8 +745,6 @@ static void receive_transfer(struct libusb_transfer *transfer) * Tell the frontend we hit the trigger here. */ packet.type = SR_DF_TRIGGER; - packet.timeoffset = (num_samples + i) * fx2->period_ps; - packet.duration = 0; packet.payload = NULL; sr_session_bus(fx2->session_data, &packet); @@ -685,8 +753,6 @@ static void receive_transfer(struct libusb_transfer *transfer) * skipping past them. */ packet.type = SR_DF_LOGIC; - packet.timeoffset = (num_samples + i) * fx2->period_ps; - packet.duration = fx2->trigger_stage * fx2->period_ps; packet.payload = &logic; logic.length = fx2->trigger_stage; logic.unitsize = 1; @@ -719,8 +785,6 @@ static void receive_transfer(struct libusb_transfer *transfer) if (fx2->trigger_stage == TRIGGER_FIRED) { /* Send the incoming transfer to the session bus. */ packet.type = SR_DF_LOGIC; - packet.timeoffset = num_samples * fx2->period_ps; - packet.duration = cur_buflen * fx2->period_ps; packet.payload = &logic; logic.length = cur_buflen - trigger_offset; logic.unitsize = 1; @@ -751,7 +815,7 @@ static int hw_start_acquisition(int device_index, gpointer session_data) int size, i; unsigned char *buf; - if (!(sdi = sr_get_device_instance(device_instances, device_index))) + if (!(sdi = sr_dev_inst_get(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; fx2->session_data = session_data; @@ -790,7 +854,7 @@ static int hw_start_acquisition(int device_index, gpointer session_data) for (i = 0; lupfd[i]; i++) sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data, NULL); - free(lupfd); + free(lupfd); /* NOT g_free()! */ packet->type = SR_DF_HEADER; packet->payload = header; @@ -798,7 +862,6 @@ static int hw_start_acquisition(int device_index, gpointer session_data) gettimeofday(&header->starttime, NULL); header->samplerate = fx2->cur_samplerate; header->num_logic_probes = fx2->profile->num_probes; - header->num_analog_probes = 0; sr_session_bus(session_data, packet); g_free(header); g_free(packet); @@ -807,7 +870,7 @@ static int hw_start_acquisition(int device_index, gpointer session_data) } /* This stops acquisition on ALL devices, ignoring device_index. */ -static void hw_stop_acquisition(int device_index, gpointer session_data) +static int hw_stop_acquisition(int device_index, gpointer session_data) { struct sr_datafeed_packet packet; @@ -820,9 +883,11 @@ static void hw_stop_acquisition(int device_index, gpointer session_data) receive_transfer(NULL); /* TODO: Need to cancel and free any queued up transfers. */ + + return SR_OK; } -struct sr_device_plugin saleae_logic_plugin_info = { +SR_PRIV struct sr_device_plugin saleae_logic_plugin_info = { .name = "saleae-logic", .longname = "Saleae Logic", .api_version = 1,