]> sigrok.org Git - libsigrok.git/commitdiff
fx2lafw: Added probing for fx2lafw devices
authorJoel Holdsworth <redacted>
Sat, 11 Feb 2012 16:08:47 +0000 (16:08 +0000)
committerJoel Holdsworth <redacted>
Sat, 25 Feb 2012 11:11:29 +0000 (11:11 +0000)
hardware/fx2lafw/fx2lafw.c
hardware/fx2lafw/fx2lafw.h

index f37557755d8fcfe42ae5d75b7d9ff8c4832fb411..357a34a1429d67e834dc2b1a336fc55f847a89ef 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <glib.h>
+#include <libusb.h>
 
 #include "config.h"
 #include "sigrok.h"
 #include "sigrok-internal.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
 
 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;
-       return 0;
+
+       if (libusb_init(&usb_context) != 0) {
+               sr_warn("Failed to initialize USB.");
+               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)
@@ -50,6 +125,21 @@ static int hw_dev_close(int device_index)
 
 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;
 }
 
index f0ea12ccd9384f5e6fc61201ee98f13e3b17956f..5735f4c1fc62ada12b930ec7118e953d90c92904 100644 (file)
 #ifndef 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