fx2lafw: Added probing for fx2lafw devices

This commit is contained in:
Joel Holdsworth 2012-02-11 16:08:47 +00:00
parent f302a08256
commit 187b358232
2 changed files with 110 additions and 1 deletions

View File

@ -19,12 +19,34 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <glib.h>
#include <libusb.h>
#include "config.h" #include "config.h"
#include "sigrok.h" #include "sigrok.h"
#include "sigrok-internal.h" #include "sigrok-internal.h"
#include "fx2lafw.h" #include "fx2lafw.h"
static struct fx2lafw_profile supported_fx2[] = {
/* USBee AX */
{ 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
{ 0, 0, 0, 0, 0, 0 }
};
static GSList *device_instances = NULL;
static libusb_context *usb_context = NULL;
static struct fx2lafw_device* fx2lafw_device_new(void)
{
struct fx2lafw_device *fx2lafw;
if (!(fx2lafw = g_try_malloc0(sizeof(struct fx2lafw_device)))) {
sr_err("fx2lafw: %s: fx2lafw_device malloc failed", __func__);
return NULL;
}
return fx2lafw;
}
/* /*
* API callbacks * API callbacks
@ -32,10 +54,63 @@
static int hw_init(const char *deviceinfo) static int hw_init(const char *deviceinfo)
{ {
struct sr_dev_inst *sdi;
struct libusb_device_descriptor des;
struct fx2lafw_profile *fx2lafw_prof;
struct fx2lafw_device *fx2lafw_dev;
libusb_device **devlist;
int err;
int devcnt = 0;
int i, j;
/* Avoid compiler warnings. */
(void)deviceinfo; (void)deviceinfo;
if (libusb_init(&usb_context) != 0) {
sr_warn("Failed to initialize USB.");
return 0; return 0;
} }
/* Find all fx2lafw compatible devices and upload firware to all of them. */
libusb_get_device_list(usb_context, &devlist);
for (i = 0; devlist[i]; i++) {
if ((err = libusb_get_device_descriptor(
devlist[i], &des)) != 0) {
sr_warn("failed to get device descriptor: %d", err);
continue;
}
fx2lafw_prof = NULL;
for (j = 0; supported_fx2[j].vid; j++) {
if (des.idVendor == supported_fx2[j].vid &&
des.idProduct == supported_fx2[j].pid) {
fx2lafw_prof = &supported_fx2[j];
}
}
/* Skip if the device was not found */
if(!fx2lafw_prof)
continue;
sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
fx2lafw_prof->vendor, fx2lafw_prof->model,
fx2lafw_prof->model_version);
if(!sdi)
return 0;
fx2lafw_dev = fx2lafw_device_new();
fx2lafw_dev->profile = fx2lafw_prof;
sdi->priv = fx2lafw_dev;
device_instances = g_slist_append(device_instances, sdi);
devcnt++;
}
libusb_free_device_list(devlist, 1);
return devcnt;
}
static int hw_dev_open(int device_index) static int hw_dev_open(int device_index)
{ {
(void)device_index; (void)device_index;
@ -50,6 +125,21 @@ static int hw_dev_close(int device_index)
static int hw_cleanup(void) static int hw_cleanup(void)
{ {
GSList *l;
struct sr_dev_inst *sdi;
for(l = device_instances; l; l = l->next) {
sdi = l->data;
sr_dev_inst_free(sdi);
}
g_slist_free(device_instances);
device_instances = NULL;
if(usb_context)
libusb_exit(usb_context);
usb_context = NULL;
return SR_OK; return SR_OK;
} }

View File

@ -20,4 +20,23 @@
#ifndef LIBSIGROK_HARDWARE_FX2LAFW #ifndef LIBSIGROK_HARDWARE_FX2LAFW
#define LIBSIGROK_HARDWARE_FX2LAFW #define LIBSIGROK_HARDWARE_FX2LAFW
struct fx2lafw_profile {
uint16_t vid;
uint16_t pid;
char *vendor;
char *model;
char *model_version;
int num_probes;
};
struct fx2lafw_device {
struct fx2lafw_profile *profile;
void *session_data;
struct sr_usb_device_instance *usb;
};
#endif #endif