saleae-logic: support for other FX2 devices (bare FX2)

This enables support for devices that have a different VID/PID
than the Saleae Logic, and yet another after firmware upload.

After firmware upload is checked every 100ms whether it came back,
instead of always waiting for 2 seconds.

If the kernel attaches a driver to a device we know, detact it first.
This commit is contained in:
Bert Vermeulen 2011-06-04 23:20:00 +02:00
parent 8722c31e26
commit e10d6e32e4
4 changed files with 121 additions and 162 deletions

View File

@ -20,8 +20,7 @@
# Local lib, this is NOT meant to be installed! # Local lib, this is NOT meant to be installed!
noinst_LTLIBRARIES = libsigrokhwcommon.la noinst_LTLIBRARIES = libsigrokhwcommon.la
libsigrokhwcommon_la_SOURCES = \ libsigrokhwcommon_la_SOURCES =
misc.c
if LA_SALEAE_LOGIC if LA_SALEAE_LOGIC
libsigrokhwcommon_la_SOURCES += ezusb.c libsigrokhwcommon_la_SOURCES += ezusb.c

View File

@ -96,6 +96,14 @@ int ezusb_upload_firmware(libusb_device *dev, int configuration,
return 1; return 1;
} }
if (libusb_kernel_driver_active(hdl, 0)) {
err = libusb_detach_kernel_driver(hdl, 0);
if (err != 0) {
g_warning("failed to detach kernel driver: %d", err);
return 1;
}
}
err = libusb_set_configuration(hdl, configuration); err = libusb_set_configuration(hdl, configuration);
if (err != 0) { if (err != 0) {
sr_warn("Unable to set configuration: %d", err); sr_warn("Unable to set configuration: %d", err);

View File

@ -1,100 +0,0 @@
/*
* This file is part of the sigrok project.
*
* Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdint.h>
#include <glib.h>
#ifdef HAVE_LIBUSB_1_0
#include <libusb.h>
#endif
#include <sigrok.h>
#include <sigrok-internal.h>
#ifdef HAVE_LIBUSB_1_0
int opendev2(int device_index, struct sr_device_instance **sdi,
libusb_device *dev, struct libusb_device_descriptor *des,
int *skip, uint16_t vid, uint16_t pid, int interface)
{
int err;
if ((err = libusb_get_device_descriptor(dev, des))) {
sr_warn("failed to get device descriptor: %d", err);
return -1;
}
if (des->idVendor != vid || des->idProduct != pid)
return 0;
if (*skip != device_index) {
/* Skip devices of this type that aren't the one we want. */
*skip += 1;
return 0;
}
/*
* Should check the bus here, since we know that already. But what are
* we going to do if it doesn't match after the right number of skips?
*/
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
(*sdi)->usb->address = libusb_get_device_address(dev);
(*sdi)->status = SR_ST_ACTIVE;
sr_info("opened device %d on %d.%d interface %d",
(*sdi)->index, (*sdi)->usb->bus,
(*sdi)->usb->address, interface);
} else {
sr_warn("failed to open device: %d", err);
*sdi = NULL;
}
return 0;
}
int opendev3(struct sr_device_instance **sdi, libusb_device *dev,
struct libusb_device_descriptor *des,
uint16_t vid, uint16_t pid, int interface)
{
int err;
if ((err = libusb_get_device_descriptor(dev, des))) {
sr_warn("failed to get device descriptor: %d", err);
return -1;
}
if (des->idVendor != vid || des->idProduct != pid)
return 0;
if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
&& libusb_get_device_address(dev) == (*sdi)->usb->address) {
/* Found it. */
if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
(*sdi)->status = SR_ST_ACTIVE;
sr_info("opened device %d on %d.%d interface %d",
(*sdi)->index, (*sdi)->usb->bus,
(*sdi)->usb->address, interface);
} else {
sr_warn("failed to open device: %d", err);
*sdi = NULL;
}
}
return 0;
}
#endif

View File

@ -27,27 +27,42 @@
#include <sigrok.h> #include <sigrok.h>
#include <sigrok-internal.h> #include <sigrok-internal.h>
#define USB_VENDOR 0x0925
#define USB_PRODUCT 0x3881
#define USB_VENDOR_NAME "Saleae"
#define USB_MODEL_NAME "Logic"
#define USB_MODEL_VERSION ""
#define USB_INTERFACE 0 #define USB_INTERFACE 0
#define USB_CONFIGURATION 1 #define USB_CONFIGURATION 1
#define NUM_PROBES 8
#define NUM_TRIGGER_STAGES 4 #define NUM_TRIGGER_STAGES 4
#define TRIGGER_TYPES "01" #define TRIGGER_TYPES "01"
#define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw" #define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
#define GTV_TO_MSEC(gtv) (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
/* delay in ms */ /* delay in ms */
#define FIRMWARE_RENUM_DELAY 2000 #define MAX_RENUM_DELAY 3000
#define NUM_SIMUL_TRANSFERS 10 #define NUM_SIMUL_TRANSFERS 10
#define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2) #define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
/* Software trigger implementation: positive values indicate trigger stage. */ /* Software trigger implementation: positive values indicate trigger stage. */
#define TRIGGER_FIRED -1 #define TRIGGER_FIRED -1
struct fx2_device {
/* VID/PID when first found */
uint16_t orig_vid;
uint16_t orig_pid;
/* VID/PID after firmware upload */
uint16_t fw_vid;
uint16_t fw_pid;
char *vendor;
char *model;
char *model_version;
int num_probes;
};
static struct fx2_device supported_fx2[] = {
/* Saleae Logic */
{ 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
/* default Cypress FX2 without EEPROM */
{ 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
{ 0, 0, 0, 0, NULL, NULL, NULL, 0 }
};
/* There is only one model Saleae Logic, and this is what it supports: */ /* There is only one model Saleae Logic, and this is what it supports: */
static int capabilities[] = { static int capabilities[] = {
SR_HWCAP_LOGIC_ANALYZER, SR_HWCAP_LOGIC_ANALYZER,
@ -68,7 +83,7 @@ static GSList *device_instances = NULL;
* upgrade -- this is like a global lock. No device will open until a proper * upgrade -- this is like a global lock. No device will open until a proper
* delay after the last device was upgraded. * delay after the last device was upgraded.
*/ */
static GTimeVal firmware_updated = { 0, 0 }; static GTimeVal fw_updated = { 0, 0 };
static libusb_context *usb_context = NULL; static libusb_context *usb_context = NULL;
@ -100,7 +115,7 @@ static uint8_t probe_mask = 0;
static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 }; static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 }; static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 }; static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
static struct fx2_device *fx2 = NULL;
static int trigger_stage = TRIGGER_FIRED; static int trigger_stage = TRIGGER_FIRED;
static int hw_set_configuration(int device_index, int capability, void *value); static int hw_set_configuration(int device_index, int capability, void *value);
@ -168,43 +183,62 @@ static int check_conf_profile(libusb_device *dev)
static struct sr_device_instance *sl_open_device(int device_index) static struct sr_device_instance *sl_open_device(int device_index)
{ {
struct sr_device_instance *sdi;
libusb_device **devlist; libusb_device **devlist;
struct libusb_device_descriptor des; struct libusb_device_descriptor des;
struct sr_device_instance *sdi;
int err, skip, i; int err, skip, i;
if (!(sdi = sr_get_device_instance(device_instances, device_index))) if (!(sdi = sr_get_device_instance(device_instances, device_index)))
return NULL; return NULL;
if (sdi->status == SR_ST_ACTIVE)
/* already in use */
return NULL;
skip = 0;
libusb_get_device_list(usb_context, &devlist); libusb_get_device_list(usb_context, &devlist);
if (sdi->status == SR_ST_INITIALIZING) { for (i = 0; devlist[i]; i++) {
/* if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
* This device was renumerating last time we touched it. g_warning("failed to get device descriptor: %d", err);
* opendev() guarantees we've waited long enough for it to continue;
* have booted properly, so now we need to find it on
* the bus and record its new address.
*/
skip = 0;
for (i = 0; devlist[i]; i++) {
/* TODO: Error handling. */
err = opendev2(device_index, &sdi, devlist[i], &des,
&skip, USB_VENDOR, USB_PRODUCT,
USB_INTERFACE);
} }
} else if (sdi->status == SR_ST_INACTIVE) {
/* if (des.idVendor != fx2->fw_vid || des.idProduct != fx2->fw_pid)
* This device is fully enumerated, so we need to find this continue;
* device by vendor, product, bus and address.
*/ if (sdi->status == SR_ST_INITIALIZING) {
libusb_get_device_list(usb_context, &devlist); if (skip != device_index) {
for (i = 0; devlist[i]; i++) { /* Skip devices of this type that aren't the one we want. */
/* TODO: Error handling. */ skip += 1;
err = opendev3(&sdi, devlist[i], &des, USB_VENDOR, continue;
USB_PRODUCT, USB_INTERFACE); }
} else if (sdi->status == SR_ST_INACTIVE) {
/*
* This device is fully enumerated, so we need to find this
* device by vendor, product, bus and address.
*/
if (libusb_get_bus_number(devlist[i]) != sdi->usb->bus
|| libusb_get_device_address(devlist[i]) != sdi->usb->address)
/* this is not the one */
continue;
}
if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) {
if (sdi->usb->address == 0xff)
/*
* first time we touch this device after firmware upload,
* so we don't know the address yet.
*/
sdi->usb->address = libusb_get_device_address(devlist[i]);
sdi->status = SR_ST_ACTIVE;
g_message("saleae: opened device %d on %d.%d interface %d",
sdi->index, sdi->usb->bus,
sdi->usb->address, USB_INTERFACE);
} else {
g_warning("failed to open device: %d", err);
sdi = NULL;
} }
} else {
/* Status must be SR_ST_ACTIVE, i.e. already in use... */
sdi = NULL;
} }
libusb_free_device_list(devlist, 1); libusb_free_device_list(devlist, 1);
@ -223,7 +257,7 @@ static int upload_firmware(libusb_device *dev)
return 1; return 1;
/* Remember when the last firmware update was done. */ /* Remember when the last firmware update was done. */
g_get_current_time(&firmware_updated); g_get_current_time(&fw_updated);
return 0; return 0;
} }
@ -296,7 +330,7 @@ static int hw_init(const char *deviceinfo)
struct sr_device_instance *sdi; struct sr_device_instance *sdi;
struct libusb_device_descriptor des; struct libusb_device_descriptor des;
libusb_device **devlist; libusb_device **devlist;
int err, devcnt, i; int err, devcnt, i, j;
/* Avoid compiler warnings. */ /* Avoid compiler warnings. */
deviceinfo = deviceinfo; deviceinfo = deviceinfo;
@ -307,6 +341,7 @@ static int hw_init(const char *deviceinfo)
} }
/* Find all Saleae Logic devices and upload firmware to all of them. */ /* Find all Saleae Logic devices and upload firmware to all of them. */
fx2 = NULL;
devcnt = 0; devcnt = 0;
libusb_get_device_list(usb_context, &devlist); libusb_get_device_list(usb_context, &devlist);
for (i = 0; devlist[i]; i++) { for (i = 0; devlist[i]; i++) {
@ -316,11 +351,19 @@ static int hw_init(const char *deviceinfo)
continue; continue;
} }
if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT) for (j = 0; supported_fx2[j].orig_vid; j++) {
continue; /* Not a Saleae Logic... */ if (des.idVendor == supported_fx2[j].orig_vid
&& des.idProduct == supported_fx2[j].orig_pid) {
fx2 = &supported_fx2[j];
break;
}
}
if (!fx2)
/* not a supported VID/PID */
continue;
sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); fx2->vendor, fx2->model, fx2->model_version);
if (!sdi) if (!sdi)
return 0; return 0;
device_instances = g_slist_append(device_instances, sdi); device_instances = g_slist_append(device_instances, sdi);
@ -336,14 +379,18 @@ static int hw_init(const char *deviceinfo)
devcnt); devcnt);
sdi->usb = sr_usb_device_instance_new sdi->usb = sr_usb_device_instance_new
(libusb_get_bus_number(devlist[i]), 0, NULL); (libusb_get_bus_number(devlist[i]), 0xff, NULL);
} else { } else {
/* Already has the firmware, so fix the new address. */ /* Already has the firmware, so fix the new address. */
sdi->status = SR_ST_INACTIVE;
sdi->usb = sr_usb_device_instance_new sdi->usb = sr_usb_device_instance_new
(libusb_get_bus_number(devlist[i]), (libusb_get_bus_number(devlist[i]),
libusb_get_device_address(devlist[i]), NULL); libusb_get_device_address(devlist[i]), NULL);
} }
devcnt++; devcnt++;
/* not supporting multiple FX2s in this driver yet */
break;
} }
libusb_free_device_list(devlist, 1); libusb_free_device_list(devlist, 1);
@ -355,25 +402,30 @@ static int hw_opendev(int device_index)
GTimeVal cur_time; GTimeVal cur_time;
struct sr_device_instance *sdi; struct sr_device_instance *sdi;
int timediff, err; int timediff, err;
unsigned int cur, upd;
if (firmware_updated.tv_sec > 0) { /*
/* Firmware was recently uploaded. */ * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
g_get_current_time(&cur_time); * for the FX2 to renumerate
cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000; */
upd = firmware_updated.tv_sec * 1000 + sdi = NULL;
firmware_updated.tv_usec / 1000; if (fw_updated.tv_sec > 0) {
timediff = cur - upd; g_message("saleae: waiting for device to reset");
if (timediff < FIRMWARE_RENUM_DELAY) { /* takes at least 300ms for the FX2 to be gone from the USB bus */
timediff = FIRMWARE_RENUM_DELAY - timediff; g_usleep(300*1000);
sr_info("saleae: waiting %d ms for device to reset", timediff = 0;
timediff); while (timediff < MAX_RENUM_DELAY) {
g_usleep(timediff * 1000); if ((sdi = sl_open_device(device_index)))
firmware_updated.tv_sec = 0; break;
g_usleep(100*1000);
g_get_current_time(&cur_time);
timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fw_updated);
} }
g_message("saleae: device came back after %d ms", timediff);
} else {
sdi = sl_open_device(device_index);
} }
if (!(sdi = sl_open_device(device_index))) { if (!sdi) {
sr_warn("unable to open device"); sr_warn("unable to open device");
return SR_ERR; return SR_ERR;
} }
@ -441,7 +493,7 @@ static void *hw_get_device_info(int device_index, int device_info_id)
info = sdi; info = sdi;
break; break;
case SR_DI_NUM_PROBES: case SR_DI_NUM_PROBES:
info = GINT_TO_POINTER(NUM_PROBES); info = GINT_TO_POINTER(fx2->num_probes);
break; break;
case SR_DI_SAMPLERATES: case SR_DI_SAMPLERATES:
info = &samplerates; info = &samplerates;
@ -735,7 +787,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
gettimeofday(&header->starttime, NULL); gettimeofday(&header->starttime, NULL);
header->samplerate = cur_samplerate; header->samplerate = cur_samplerate;
header->protocol_id = SR_PROTO_RAW; header->protocol_id = SR_PROTO_RAW;
header->num_logic_probes = NUM_PROBES; header->num_logic_probes = fx2->num_probes;
header->num_analog_probes = 0; header->num_analog_probes = 0;
sr_session_bus(session_device_id, packet); sr_session_bus(session_device_id, packet);
g_free(header); g_free(header);