hardware: Make USB drivers use the libusb_context in struct sr_context
This commit is contained in:
parent
1ebe4b4e69
commit
d4abb463a7
|
@ -194,6 +194,7 @@ static int fx2lafw_dev_open(struct sr_dev_inst *sdi)
|
|||
libusb_device **devlist;
|
||||
struct libusb_device_descriptor des;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = fdi->priv;
|
||||
struct version_info vi;
|
||||
int ret, skip, i;
|
||||
uint8_t revid;
|
||||
|
@ -205,7 +206,8 @@ static int fx2lafw_dev_open(struct sr_dev_inst *sdi)
|
|||
return SR_ERR;
|
||||
|
||||
skip = 0;
|
||||
const int device_count = libusb_get_device_list(NULL, &devlist);
|
||||
const int device_count = libusb_get_device_list(
|
||||
drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
if (device_count < 0) {
|
||||
sr_err("fx2lafw: Failed to retrieve device list (%d)",
|
||||
device_count);
|
||||
|
@ -408,12 +410,6 @@ static int hw_init(struct sr_context *sr_ctx)
|
|||
return SR_ERR_MALLOC;
|
||||
}
|
||||
|
||||
if (libusb_init(NULL) != 0) {
|
||||
g_free(drvc);
|
||||
sr_warn("fx2lafw: Failed to initialize libusb.");
|
||||
return SR_ERR;
|
||||
}
|
||||
|
||||
drvc->sr_ctx = sr_ctx;
|
||||
fdi->priv = drvc;
|
||||
|
||||
|
@ -441,7 +437,7 @@ static GSList *hw_scan(GSList *options)
|
|||
|
||||
/* Find all fx2lafw compatible devices and upload firmware to them. */
|
||||
devices = NULL;
|
||||
libusb_get_device_list(NULL, &devlist);
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
|
||||
if ((ret = libusb_get_device_descriptor(
|
||||
|
@ -613,8 +609,6 @@ static int hw_cleanup(void)
|
|||
|
||||
ret = clear_instances();
|
||||
|
||||
libusb_exit(NULL);
|
||||
|
||||
g_free(drvc);
|
||||
fdi->priv = NULL;
|
||||
|
||||
|
@ -686,13 +680,14 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
|
|||
static int receive_data(int fd, int revents, void *cb_data)
|
||||
{
|
||||
struct timeval tv;
|
||||
struct drv_context *drvc = fdi->priv;
|
||||
|
||||
(void)fd;
|
||||
(void)revents;
|
||||
(void)cb_data;
|
||||
|
||||
tv.tv_sec = tv.tv_usec = 0;
|
||||
libusb_handle_events_timeout(NULL, &tv);
|
||||
libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -712,6 +707,7 @@ static void abort_acquisition(struct dev_context *devc)
|
|||
static void finish_acquisition(struct dev_context *devc)
|
||||
{
|
||||
struct sr_datafeed_packet packet;
|
||||
struct drv_context *drvc = fdi->priv;
|
||||
int i;
|
||||
|
||||
|
||||
|
@ -721,7 +717,7 @@ static void finish_acquisition(struct dev_context *devc)
|
|||
|
||||
/* Remove fds from polling */
|
||||
const struct libusb_pollfd **const lupfd =
|
||||
libusb_get_pollfds(NULL);
|
||||
libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
|
||||
for (i = 0; lupfd[i]; i++)
|
||||
sr_source_remove(lupfd[i]->fd);
|
||||
free(lupfd); /* NOT g_free()! */
|
||||
|
@ -947,6 +943,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
struct sr_datafeed_header header;
|
||||
struct sr_datafeed_meta_logic meta;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = fdi->priv;
|
||||
struct libusb_transfer *transfer;
|
||||
const struct libusb_pollfd **lupfd;
|
||||
unsigned int i;
|
||||
|
@ -997,7 +994,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
devc->submitted_transfers++;
|
||||
}
|
||||
|
||||
lupfd = libusb_get_pollfds(NULL);
|
||||
lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
|
||||
for (i = 0; lupfd[i]; i++)
|
||||
sr_source_add(lupfd[i]->fd, lupfd[i]->events,
|
||||
timeout, receive_data, NULL);
|
||||
|
|
|
@ -262,12 +262,6 @@ static int hw_init(struct sr_context *sr_ctx)
|
|||
return SR_ERR_MALLOC;
|
||||
}
|
||||
|
||||
if (libusb_init(NULL) != 0) {
|
||||
g_free(drvc);
|
||||
sr_err("Failed to initialize USB.");
|
||||
return SR_ERR;
|
||||
}
|
||||
|
||||
drvc->sr_ctx = sr_ctx;
|
||||
hdi->priv = drvc;
|
||||
|
||||
|
@ -295,7 +289,7 @@ static GSList *hw_scan(GSList *options)
|
|||
clear_instances();
|
||||
|
||||
/* Find all Hantek DSO devices and upload firmware to all of them. */
|
||||
libusb_get_device_list(NULL, &devlist);
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
|
||||
sr_err("Failed to get device descriptor: %d.", ret);
|
||||
|
@ -419,8 +413,6 @@ static int hw_cleanup(void)
|
|||
|
||||
clear_instances();
|
||||
|
||||
libusb_exit(NULL);
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
||||
|
@ -740,6 +732,7 @@ static int handle_event(int fd, int revents, void *cb_data)
|
|||
struct sr_datafeed_packet packet;
|
||||
struct timeval tv;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = hdi->priv;
|
||||
const struct libusb_pollfd **lupfd;
|
||||
int num_probes, i;
|
||||
uint32_t trigger_offset;
|
||||
|
@ -757,7 +750,7 @@ static int handle_event(int fd, int revents, void *cb_data)
|
|||
* TODO: Doesn't really cancel pending transfers so they might
|
||||
* come in after SR_DF_END is sent.
|
||||
*/
|
||||
lupfd = libusb_get_pollfds(NULL);
|
||||
lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
|
||||
for (i = 0; lupfd[i]; i++)
|
||||
sr_source_remove(lupfd[i]->fd);
|
||||
free(lupfd);
|
||||
|
@ -772,7 +765,7 @@ static int handle_event(int fd, int revents, void *cb_data)
|
|||
|
||||
/* Always handle pending libusb events. */
|
||||
tv.tv_sec = tv.tv_usec = 0;
|
||||
libusb_handle_events_timeout(NULL, &tv);
|
||||
libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
|
||||
|
||||
/* TODO: ugh */
|
||||
if (devc->dev_state == NEW_CAPTURE) {
|
||||
|
@ -856,6 +849,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
struct sr_datafeed_header header;
|
||||
struct sr_datafeed_meta_analog meta;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = hdi->priv;
|
||||
int i;
|
||||
|
||||
if (sdi->status != SR_ST_ACTIVE)
|
||||
|
@ -876,7 +870,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
return SR_ERR;
|
||||
|
||||
devc->dev_state = CAPTURE;
|
||||
lupfd = libusb_get_pollfds(NULL);
|
||||
lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
|
||||
for (i = 0; lupfd[i]; i++)
|
||||
sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK,
|
||||
handle_event, (void *)sdi);
|
||||
|
|
|
@ -106,6 +106,7 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
|
|||
libusb_device **devlist;
|
||||
struct libusb_device_descriptor des;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = hantek_dso_driver_info.priv;
|
||||
int err, skip, i;
|
||||
|
||||
devc = sdi->priv;
|
||||
|
@ -115,7 +116,7 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi)
|
|||
return SR_ERR;
|
||||
|
||||
skip = 0;
|
||||
libusb_get_device_list(NULL, &devlist);
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
|
||||
sr_err("Failed to get device descriptor: %d.", err);
|
||||
|
|
|
@ -55,7 +55,6 @@ static int clear_instances(void)
|
|||
|
||||
static int hw_init(struct sr_context *sr_ctx, int dmm)
|
||||
{
|
||||
int ret;
|
||||
struct drv_context *drvc;
|
||||
|
||||
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
|
||||
|
@ -63,12 +62,6 @@ static int hw_init(struct sr_context *sr_ctx, int dmm)
|
|||
return SR_ERR_MALLOC;
|
||||
}
|
||||
|
||||
if ((ret = libusb_init(NULL)) < 0) {
|
||||
sr_err("Failed to initialize libusb: %s.",
|
||||
libusb_error_name(ret));
|
||||
return SR_ERR;
|
||||
}
|
||||
|
||||
if (dmm == UNI_T_UT61D)
|
||||
di = di_ut61d;
|
||||
else if (dmm == VOLTCRAFT_VC820)
|
||||
|
@ -107,7 +100,7 @@ static GSList *hw_scan(GSList *options)
|
|||
|
||||
devices = NULL;
|
||||
|
||||
if (!(l = sr_usb_connect(NULL, "1a86.e008")))
|
||||
if (!(l = sr_usb_connect(drvc->sr_ctx->libusb_ctx, "1a86.e008")))
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < (int)g_slist_length(l); i++) {
|
||||
|
@ -170,8 +163,6 @@ static int hw_cleanup(void)
|
|||
{
|
||||
clear_instances();
|
||||
|
||||
// libusb_exit(NULL);
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -112,7 +112,7 @@ static GSList *hw_scan(GSList *options)
|
|||
clear_instances();
|
||||
|
||||
devices = NULL;
|
||||
libusb_get_device_list(NULL, &devlist);
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
|
||||
sr_warn("Failed to get device descriptor: %s",
|
||||
|
@ -164,6 +164,7 @@ static GSList *hw_dev_list(void)
|
|||
static int hw_dev_open(struct sr_dev_inst *sdi)
|
||||
{
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = di->priv;
|
||||
libusb_device **devlist;
|
||||
int ret, i;
|
||||
|
||||
|
@ -173,7 +174,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
|
|||
}
|
||||
|
||||
devc = sdi->priv;
|
||||
libusb_get_device_list(NULL, &devlist);
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
|
||||
|| libusb_get_device_address(devlist[i]) != devc->usb->address)
|
||||
|
@ -353,6 +354,7 @@ static void receive_transfer(struct libusb_transfer *transfer)
|
|||
static int handle_events(int fd, int revents, void *cb_data)
|
||||
{
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = di->priv;
|
||||
struct sr_datafeed_packet packet;
|
||||
struct sr_dev_inst *sdi;
|
||||
struct timeval tv;
|
||||
|
@ -382,7 +384,8 @@ static int handle_events(int fd, int revents, void *cb_data)
|
|||
}
|
||||
|
||||
memset(&tv, 0, sizeof(struct timeval));
|
||||
libusb_handle_events_timeout_completed(NULL, &tv, NULL);
|
||||
libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
|
||||
NULL);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
@ -394,6 +397,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
struct sr_datafeed_header header;
|
||||
struct sr_datafeed_meta_analog meta;
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = di->priv;
|
||||
const struct libusb_pollfd **pfd;
|
||||
struct libusb_transfer *transfer;
|
||||
int ret, i;
|
||||
|
@ -422,7 +426,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
|||
meta.num_probes = 1;
|
||||
sr_session_send(devc->cb_data, &packet);
|
||||
|
||||
pfd = libusb_get_pollfds(NULL);
|
||||
pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
|
||||
for (i = 0; pfd[i]; i++) {
|
||||
/* Handle USB events every 100ms, for decent latency. */
|
||||
sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
|
||||
|
|
|
@ -108,8 +108,6 @@ static const char *probe_names[NUM_PROBES + 1] = {
|
|||
SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
|
||||
static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
|
||||
|
||||
static libusb_context *usb_context = NULL;
|
||||
|
||||
/*
|
||||
* The hardware supports more samplerates than these, but these are the
|
||||
* options hardcoded into the vendor's Windows GUI.
|
||||
|
@ -314,11 +312,6 @@ static int hw_init(struct sr_context *sr_ctx)
|
|||
drvc->sr_ctx = sr_ctx;
|
||||
zdi->priv = drvc;
|
||||
|
||||
if (libusb_init(&usb_context) != 0) {
|
||||
sr_err("zp: Failed to initialize USB.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
||||
|
@ -343,7 +336,7 @@ static GSList *hw_scan(GSList *options)
|
|||
|
||||
/* Find all ZEROPLUS analyzers and add them to device list. */
|
||||
devcnt = 0;
|
||||
libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
|
||||
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
|
||||
|
||||
for (i = 0; devlist[i]; i++) {
|
||||
ret = libusb_get_device_descriptor(devlist[i], &des);
|
||||
|
@ -423,6 +416,7 @@ static GSList *hw_dev_list(void)
|
|||
static int hw_dev_open(struct sr_dev_inst *sdi)
|
||||
{
|
||||
struct dev_context *devc;
|
||||
struct drv_context *drvc = zdi->priv;
|
||||
libusb_device **devlist, *dev;
|
||||
struct libusb_device_descriptor des;
|
||||
int device_count, ret, i;
|
||||
|
@ -432,7 +426,8 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
|
|||
return SR_ERR_ARG;
|
||||
}
|
||||
|
||||
device_count = libusb_get_device_list(usb_context, &devlist);
|
||||
device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
|
||||
&devlist);
|
||||
if (device_count < 0) {
|
||||
sr_err("zp: Failed to retrieve device list");
|
||||
return SR_ERR;
|
||||
|
@ -543,10 +538,6 @@ static int hw_cleanup(void)
|
|||
|
||||
clear_instances();
|
||||
|
||||
if (usb_context)
|
||||
libusb_exit(usb_context);
|
||||
usb_context = NULL;
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue