X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fftdi-la%2Fapi.c;h=0927d3a274387d14d1fe1dd34a1377f72a73ee23;hb=b605edf38fa47943e21b511e46e7d4a89fad22eb;hp=cd70bb7feeeed401de5cc07cb01c6f429c3eabf6;hpb=f9197887e447bb7536e25f3cb24d175d97cd20bb;p=libsigrok.git
diff --git a/src/hardware/ftdi-la/api.c b/src/hardware/ftdi-la/api.c
index cd70bb7f..0927d3a2 100644
--- a/src/hardware/ftdi-la/api.c
+++ b/src/hardware/ftdi-la/api.c
@@ -17,20 +17,23 @@
* along with this program. If not, see .
*/
+#include
#include
#include
+#include
+#include "libsigrok-internal.h"
#include "protocol.h"
-SR_PRIV struct sr_dev_driver ftdi_la_driver_info;
-
static const uint32_t scanopts[] = {
- /* TODO: SR_CONF_CONN to be able to specify the USB address. */
+ SR_CONF_CONN,
};
static const uint32_t devopts[] = {
SR_CONF_LOGIC_ANALYZER,
+ SR_CONF_CONTINUOUS,
SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+ SR_CONF_CONN | SR_CONF_GET,
};
static const uint64_t samplerates[] = {
@@ -63,13 +66,13 @@ static const struct ftdi_chip_desc ft232r_desc = {
.samplerate_div = 30,
.channel_names = {
"TXD",
- "RI#",
- "DCD#",
- "DSR#",
- "DTR#",
- "CTS#",
- "RTS#",
"RXD",
+ "RTS#",
+ "CTS#",
+ "DTR#",
+ "DSR#",
+ "DCD#",
+ "RI#",
NULL
}
};
@@ -79,29 +82,31 @@ static const struct ftdi_chip_desc *chip_descs[] = {
&ft232r_desc,
};
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
+static void scan_device(struct ftdi_context *ftdic,
+ struct libusb_device *dev, GSList **devices)
{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static GSList *scan(struct sr_dev_driver *di, GSList *options)
-{
- struct drv_context *drvc;
- struct dev_context *devc;
- struct sr_dev_inst *sdi;
- GSList *devices;
- struct ftdi_device_list *devlist = 0;
- struct ftdi_device_list *curdev;
struct libusb_device_descriptor usb_desc;
const struct ftdi_chip_desc *desc;
+ struct dev_context *devc;
char *vendor, *model, *serial_num;
- int ret;
+ struct sr_dev_inst *sdi;
+ int rv;
- (void)options;
+ libusb_get_device_descriptor(dev, &usb_desc);
- devices = NULL;
- drvc = di->context;
- drvc->instances = NULL;
+ desc = NULL;
+ for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
+ desc = chip_descs[i];
+ if (desc->vendor == usb_desc.idVendor &&
+ desc->product == usb_desc.idProduct)
+ break;
+ }
+
+ if (!desc) {
+ sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
+ usb_desc.idVendor, usb_desc.idProduct);
+ return;
+ }
/* Allocate memory for our private device context. */
devc = g_malloc0(sizeof(struct dev_context));
@@ -109,82 +114,132 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
/* Allocate memory for the incoming data. */
devc->data_buf = g_malloc0(DATA_BUF_SIZE);
- /* Allocate memory for the FTDI context (ftdic) and initialize it. */
- devc->ftdic = ftdi_new();
- if (!devc->ftdic) {
- sr_err("Failed to initialize libftdi.");
- goto err_free_data_buf;
+ devc->desc = desc;
+
+ vendor = g_malloc(32);
+ model = g_malloc(32);
+ serial_num = g_malloc(32);
+ rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32,
+ model, 32, serial_num, 32);
+ switch (rv) {
+ case 0:
+ break;
+ case -9:
+ sr_dbg("The device lacks a serial number.");
+ g_free(serial_num);
+ serial_num = NULL;
+ break;
+ default:
+ sr_err("Failed to get the FTDI strings: %d", rv);
+ goto err_free_strings;
}
+ sr_dbg("Found an FTDI device: %s.", model);
- ret = ftdi_usb_find_all(devc->ftdic, &devlist, 0, 0);
+ /* Register the device with libsigrok. */
+ sdi = g_malloc0(sizeof(struct sr_dev_inst));
+ sdi->status = SR_ST_INACTIVE;
+ sdi->vendor = vendor;
+ sdi->model = model;
+ sdi->serial_num = serial_num;
+ sdi->priv = devc;
+ sdi->connection_id = g_strdup_printf("d:%u/%u",
+ libusb_get_bus_number(dev), libusb_get_device_address(dev));
+
+ for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
+ sr_channel_new(sdi, chan - &(desc->channel_names[0]),
+ SR_CHANNEL_LOGIC, TRUE, *chan);
+
+ *devices = g_slist_append(*devices, sdi);
+ return;
+
+err_free_strings:
+ g_free(vendor);
+ g_free(model);
+ g_free(serial_num);
+ g_free(devc->data_buf);
+ g_free(devc);
+}
+
+static GSList *scan_all(struct ftdi_context *ftdic, GSList *options)
+{
+ GSList *devices;
+ struct ftdi_device_list *devlist = 0;
+ struct ftdi_device_list *curdev;
+ int ret;
+
+ (void)options;
+
+ devices = NULL;
+
+ ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
if (ret < 0) {
sr_err("Failed to list devices (%d): %s", ret,
- ftdi_get_error_string(devc->ftdic));
- goto err_free_ftdic;
+ ftdi_get_error_string(ftdic));
+ return NULL;
}
sr_dbg("Number of FTDI devices found: %d", ret);
curdev = devlist;
while (curdev) {
- libusb_get_device_descriptor(curdev->dev, &usb_desc);
-
- desc = NULL;
- for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
- desc = chip_descs[i];
- if (desc->vendor == usb_desc.idVendor &&
- desc->product == usb_desc.idProduct)
- break;
- }
-
- if (!desc) {
- sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
- usb_desc.idVendor, usb_desc.idProduct);
- continue;
- }
- devc->usbdev = curdev->dev;
- devc->desc = desc;
-
- vendor = g_malloc(32);
- model = g_malloc(32);
- serial_num = g_malloc(32);
- ftdi_usb_get_strings(devc->ftdic, curdev->dev, vendor, 32,
- model, 32, serial_num, 32);
- sr_dbg("Found an FTDI device: %s.", model);
-
- /* Register the device with libsigrok. */
- sdi = g_malloc0(sizeof(struct sr_dev_inst));
- sdi->status = SR_ST_INITIALIZING;
- sdi->vendor = vendor;
- sdi->model = model;
- sdi->serial_num = serial_num;
- sdi->driver = di;
- sdi->priv = devc;
-
- for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
- sr_channel_new(sdi, &(desc->channel_names[0]) - chan,
- SR_CHANNEL_LOGIC, TRUE, *chan);
-
- devices = g_slist_append(devices, sdi);
- drvc->instances = g_slist_append(drvc->instances, sdi);
-
+ scan_device(ftdic, curdev->dev, &devices);
curdev = curdev->next;
}
- return devices;
-
-err_free_ftdic:
- ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
-err_free_data_buf:
- g_free(devc->data_buf);
- g_free(devc);
+ ftdi_list_free(&devlist);
- return NULL;
+ return devices;
}
-static GSList *dev_list(const struct sr_dev_driver *di)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
- return ((struct drv_context *)(di->context))->instances;
+ struct ftdi_context *ftdic;
+ struct sr_config *src;
+ struct sr_usb_dev_inst *usb;
+ const char *conn;
+ GSList *l, *conn_devices;
+ GSList *devices;
+ struct drv_context *drvc;
+ libusb_device **devlist;
+ int i;
+
+ drvc = di->context;
+ conn = NULL;
+ for (l = options; l; l = l->next) {
+ src = l->data;
+ if (src->key == SR_CONF_CONN) {
+ conn = g_variant_get_string(src->data, NULL);
+ break;
+ }
+ }
+
+ /* Allocate memory for the FTDI context (ftdic) and initialize it. */
+ ftdic = ftdi_new();
+ if (!ftdic) {
+ sr_err("Failed to initialize libftdi.");
+ return NULL;
+ }
+
+ if (conn) {
+ devices = NULL;
+ libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+ for (i = 0; devlist[i]; i++) {
+ conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
+ for (l = conn_devices; l; l = l->next) {
+ usb = l->data;
+ if (usb->bus == libusb_get_bus_number(devlist[i])
+ && usb->address == libusb_get_device_address(devlist[i])) {
+ scan_device(ftdic, devlist[i], &devices);
+ }
+ }
+ }
+ libusb_free_device_list(devlist, 1);
+ } else
+ devices = scan_all(ftdic, options);
+
+ ftdi_free(ftdic);
+
+ return std_scan_complete(di, devices);
}
static void clear_helper(void *priv)
@@ -192,8 +247,6 @@ static void clear_helper(void *priv)
struct dev_context *devc;
devc = priv;
-
- ftdi_free(devc->ftdic);
g_free(devc->data_buf);
g_free(devc);
}
@@ -210,13 +263,17 @@ static int dev_open(struct sr_dev_inst *sdi)
devc = sdi->priv;
- ret = ftdi_usb_open_dev(devc->ftdic, devc->usbdev);
+ devc->ftdic = ftdi_new();
+ if (!devc->ftdic)
+ return SR_ERR;
+
+ ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
if (ret < 0) {
/* Log errors, except for -3 ("device not found"). */
if (ret != -3)
sr_err("Failed to open device (%d): %s", ret,
ftdi_get_error_string(devc->ftdic));
- return SR_ERR;
+ goto err_ftdi_free;
}
/* Purge RX/TX buffers in the FTDI chip. */
@@ -248,9 +305,10 @@ static int dev_open(struct sr_dev_inst *sdi)
sdi->status = SR_ST_ACTIVE;
return SR_OK;
-
err_dev_open_close_ftdic:
ftdi_usb_close(devc->ftdic);
+err_ftdi_free:
+ ftdi_free(devc->ftdic);
return SR_ERR;
}
@@ -260,27 +318,24 @@ static int dev_close(struct sr_dev_inst *sdi)
devc = sdi->priv;
- ftdi_usb_close(devc->ftdic);
+ if (devc->ftdic) {
+ ftdi_usb_close(devc->ftdic);
+ ftdi_free(devc->ftdic);
+ devc->ftdic = NULL;
+ }
sdi->status = SR_ST_INACTIVE;
return SR_OK;
}
-static int cleanup(const struct sr_dev_driver *di)
-{
- dev_clear(di);
-
- /* TODO: Free other driver resources, if any. */
-
- return SR_OK;
-}
-
static int config_get(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
int ret;
struct dev_context *devc;
+ struct sr_usb_dev_inst *usb;
+ char str[128];
(void)cg;
@@ -291,6 +346,13 @@ static int config_get(uint32_t key, GVariant **data,
case SR_CONF_SAMPLERATE:
*data = g_variant_new_uint64(devc->cur_samplerate);
break;
+ case SR_CONF_CONN:
+ if (!sdi || !sdi->conn)
+ return SR_ERR_ARG;
+ usb = sdi->conn;
+ snprintf(str, 128, "%d.%d", usb->bus, usb->address);
+ *data = g_variant_new_string(str);
+ break;
default:
return SR_ERR_NA;
}
@@ -371,12 +433,10 @@ static int config_list(uint32_t key, GVariant **data,
return ret;
}
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
- (void)cb_data;
-
devc = sdi->priv;
if (sdi->status != SR_ST_ACTIVE)
@@ -387,14 +447,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
- devc->cb_data = cb_data;
-
/* Properly reset internal variables before every new acquisition. */
devc->samples_sent = 0;
devc->bytes_received = 0;
- /* Send header packet to the session bus. */
- std_session_send_df_header(sdi, LOG_PREFIX);
+ std_session_send_df_header(sdi);
/* Hook up a dummy handler to receive data from the device. */
sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
@@ -403,34 +460,27 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
return SR_OK;
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
{
- struct sr_datafeed_packet packet;
-
- (void)cb_data;
-
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR_DEV_CLOSED;
sr_dbg("Stopping acquisition.");
sr_session_source_remove(sdi->session, -1);
- /* Send end packet to the session bus. */
- sr_dbg("Sending SR_DF_END.");
- packet.type = SR_DF_END;
- sr_session_send(sdi, &packet);
+ std_session_send_df_end(sdi);
return SR_OK;
}
-SR_PRIV struct sr_dev_driver ftdi_la_driver_info = {
+static struct sr_dev_driver ftdi_la_driver_info = {
.name = "ftdi-la",
.longname = "FTDI LA",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan,
- .dev_list = dev_list,
+ .dev_list = std_dev_list,
.dev_clear = dev_clear,
.config_get = config_get,
.config_set = config_set,
@@ -441,3 +491,4 @@ SR_PRIV struct sr_dev_driver ftdi_la_driver_info = {
.dev_acquisition_stop = dev_acquisition_stop,
.context = NULL,
};
+SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);