genericdmm: use driver-private storage for instances
This commit is contained in:
parent
b4750a3a93
commit
301a5e4c4a
|
@ -70,7 +70,8 @@ SR_PRIV libusb_context *genericdmm_usb_context = NULL;
|
||||||
static GSList *connect_usb(const char *conn)
|
static GSList *connect_usb(const char *conn)
|
||||||
{
|
{
|
||||||
struct sr_dev_inst *sdi;
|
struct sr_dev_inst *sdi;
|
||||||
struct context *ctx;
|
struct drv_context *drvc;
|
||||||
|
struct dev_context *devc;
|
||||||
libusb_device **devlist;
|
libusb_device **devlist;
|
||||||
struct libusb_device_descriptor des;
|
struct libusb_device_descriptor des;
|
||||||
GSList *devices;
|
GSList *devices;
|
||||||
|
@ -79,6 +80,8 @@ static GSList *connect_usb(const char *conn)
|
||||||
int vid, pid, bus, addr, devcnt, err, i;
|
int vid, pid, bus, addr, devcnt, err, i;
|
||||||
char *mstr;
|
char *mstr;
|
||||||
|
|
||||||
|
drvc = gdi->priv;
|
||||||
|
|
||||||
vid = pid = bus = addr = 0;
|
vid = pid = bus = addr = 0;
|
||||||
reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
|
reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
|
||||||
if (g_regex_match(reg, conn, 0, &match)) {
|
if (g_regex_match(reg, conn, 0, &match)) {
|
||||||
|
@ -143,19 +146,19 @@ static GSList *connect_usb(const char *conn)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Found one. */
|
/* Found one. */
|
||||||
if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
|
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
|
||||||
sr_err("genericdmm: ctx malloc failed.");
|
sr_err("genericdmm: devc malloc failed.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
devcnt = g_slist_length(gdi->instances);
|
devcnt = g_slist_length(drvc->instances);
|
||||||
if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
|
if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
|
||||||
NULL, NULL, NULL))) {
|
NULL, NULL, NULL))) {
|
||||||
sr_err("genericdmm: sr_dev_inst_new returned NULL.");
|
sr_err("genericdmm: sr_dev_inst_new returned NULL.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
sdi->priv = ctx;
|
sdi->priv = devc;
|
||||||
ctx->usb = sr_usb_dev_inst_new(
|
devc->usb = sr_usb_dev_inst_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);
|
||||||
devices = g_slist_append(devices, sdi);
|
devices = g_slist_append(devices, sdi);
|
||||||
|
@ -218,12 +221,19 @@ static GSList *default_scan(GSList *options)
|
||||||
|
|
||||||
static int hw_init(void)
|
static int hw_init(void)
|
||||||
{
|
{
|
||||||
|
struct drv_context *drvc;
|
||||||
|
|
||||||
|
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
|
||||||
|
sr_err("genericdmm: driver context malloc failed.");
|
||||||
|
return SR_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
if (libusb_init(&genericdmm_usb_context) != 0) {
|
if (libusb_init(&genericdmm_usb_context) != 0) {
|
||||||
sr_err("genericdmm: Failed to initialize USB.");
|
sr_err("genericdmm: Failed to initialize USB.");
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gdi->priv = drvc;
|
||||||
|
|
||||||
return SR_OK;
|
return SR_OK;
|
||||||
}
|
}
|
||||||
|
@ -234,8 +244,11 @@ static GSList *hw_scan(GSList *options)
|
||||||
struct sr_hwopt *opt, *defopt;
|
struct sr_hwopt *opt, *defopt;
|
||||||
struct dev_profile *pr, *profile;
|
struct dev_profile *pr, *profile;
|
||||||
struct sr_dev_inst *sdi;
|
struct sr_dev_inst *sdi;
|
||||||
|
struct drv_context *drvc;
|
||||||
const char *model;
|
const char *model;
|
||||||
|
|
||||||
|
drvc = gdi->priv;
|
||||||
|
|
||||||
/* Separate model from the options list. */
|
/* Separate model from the options list. */
|
||||||
model = NULL;
|
model = NULL;
|
||||||
newopts = NULL;
|
newopts = NULL;
|
||||||
|
@ -318,7 +331,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
if (!sdi->model)
|
if (!sdi->model)
|
||||||
sdi->model = g_strdup(profile->model);
|
sdi->model = g_strdup(profile->model);
|
||||||
/* Add a copy of these new devices to the driver instances. */
|
/* Add a copy of these new devices to the driver instances. */
|
||||||
gdi->instances = g_slist_append(gdi->instances, l->data);
|
drvc->instances = g_slist_append(drvc->instances, l->data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,28 +340,28 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
static int hw_dev_open(struct sr_dev_inst *sdi)
|
static int hw_dev_open(struct sr_dev_inst *sdi)
|
||||||
{
|
{
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
if (!(ctx = sdi->priv)) {
|
if (!(devc = sdi->priv)) {
|
||||||
sr_err("genericdmm: sdi->priv was NULL.");
|
sr_err("genericdmm: sdi->priv was NULL.");
|
||||||
return SR_ERR_BUG;
|
return SR_ERR_BUG;
|
||||||
}
|
}
|
||||||
|
|
||||||
sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
|
sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port);
|
||||||
|
|
||||||
switch (ctx->profile->transport) {
|
switch (devc->profile->transport) {
|
||||||
case DMM_TRANSPORT_USBHID:
|
case DMM_TRANSPORT_USBHID:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
break;
|
break;
|
||||||
case DMM_TRANSPORT_SERIAL:
|
case DMM_TRANSPORT_SERIAL:
|
||||||
/* TODO: O_NONBLOCK? */
|
/* TODO: O_NONBLOCK? */
|
||||||
ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
|
devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
|
||||||
if (ctx->serial->fd == -1) {
|
if (devc->serial->fd == -1) {
|
||||||
sr_err("genericdmm: Couldn't open serial port '%s'.",
|
sr_err("genericdmm: Couldn't open serial port '%s'.",
|
||||||
ctx->serial->port);
|
devc->serial->port);
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
// serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
|
// serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
sr_err("No transport set.");
|
sr_err("No transport set.");
|
||||||
|
@ -359,21 +372,21 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
|
||||||
|
|
||||||
static int hw_dev_close(struct sr_dev_inst *sdi)
|
static int hw_dev_close(struct sr_dev_inst *sdi)
|
||||||
{
|
{
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
if (!(ctx = sdi->priv)) {
|
if (!(devc = sdi->priv)) {
|
||||||
sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
|
sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
|
||||||
return SR_ERR_BUG;
|
return SR_ERR_BUG;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ctx->profile->transport) {
|
switch (devc->profile->transport) {
|
||||||
case DMM_TRANSPORT_USBHID:
|
case DMM_TRANSPORT_USBHID:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
break;
|
break;
|
||||||
case DMM_TRANSPORT_SERIAL:
|
case DMM_TRANSPORT_SERIAL:
|
||||||
if (ctx->serial && ctx->serial->fd != -1) {
|
if (devc->serial && devc->serial->fd != -1) {
|
||||||
serial_close(ctx->serial->fd);
|
serial_close(devc->serial->fd);
|
||||||
ctx->serial->fd = -1;
|
devc->serial->fd = -1;
|
||||||
sdi->status = SR_ST_INACTIVE;
|
sdi->status = SR_ST_INACTIVE;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -386,30 +399,34 @@ static int hw_cleanup(void)
|
||||||
{
|
{
|
||||||
GSList *l;
|
GSList *l;
|
||||||
struct sr_dev_inst *sdi;
|
struct sr_dev_inst *sdi;
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
struct drv_context *drvc;
|
||||||
|
|
||||||
|
if (!(drvc = gdi->priv))
|
||||||
|
return SR_OK;
|
||||||
|
|
||||||
/* Properly close and free all devices. */
|
/* Properly close and free all devices. */
|
||||||
for (l = gdi->instances; l; l = l->next) {
|
for (l = drvc->instances; l; l = l->next) {
|
||||||
if (!(sdi = l->data)) {
|
if (!(sdi = l->data)) {
|
||||||
/* Log error, but continue cleaning up the rest. */
|
/* Log error, but continue cleaning up the rest. */
|
||||||
sr_err("genericdmm: sdi was NULL, continuing.");
|
sr_err("genericdmm: sdi was NULL, continuing.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!(ctx = sdi->priv)) {
|
if (!(devc = sdi->priv)) {
|
||||||
/* Log error, but continue cleaning up the rest. */
|
/* Log error, but continue cleaning up the rest. */
|
||||||
sr_err("genericdmm: sdi->priv was NULL, continuing.");
|
sr_err("genericdmm: sdi->priv was NULL, continuing.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->profile) {
|
if (devc->profile) {
|
||||||
switch (ctx->profile->transport) {
|
switch (devc->profile->transport) {
|
||||||
case DMM_TRANSPORT_USBHID:
|
case DMM_TRANSPORT_USBHID:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
break;
|
break;
|
||||||
case DMM_TRANSPORT_SERIAL:
|
case DMM_TRANSPORT_SERIAL:
|
||||||
if (ctx->serial && ctx->serial->fd != -1)
|
if (devc->serial && devc->serial->fd != -1)
|
||||||
serial_close(ctx->serial->fd);
|
serial_close(devc->serial->fd);
|
||||||
sr_serial_dev_inst_free(ctx->serial);
|
sr_serial_dev_inst_free(devc->serial);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,8 +434,8 @@ static int hw_cleanup(void)
|
||||||
sr_dev_inst_free(sdi);
|
sr_dev_inst_free(sdi);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_slist_free(gdi->instances);
|
g_slist_free(drvc->instances);
|
||||||
gdi->instances = NULL;
|
drvc->instances = NULL;
|
||||||
|
|
||||||
if (genericdmm_usb_context)
|
if (genericdmm_usb_context)
|
||||||
libusb_exit(genericdmm_usb_context);
|
libusb_exit(genericdmm_usb_context);
|
||||||
|
@ -429,10 +446,10 @@ static int hw_cleanup(void)
|
||||||
static int hw_info_get(int info_id, const void **data,
|
static int hw_info_get(int info_id, const void **data,
|
||||||
const struct sr_dev_inst *sdi)
|
const struct sr_dev_inst *sdi)
|
||||||
{
|
{
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
(void)sdi;
|
(void)sdi;
|
||||||
(void)ctx;
|
(void)devc;
|
||||||
|
|
||||||
switch (info_id) {
|
switch (info_id) {
|
||||||
case SR_DI_HWOPTS:
|
case SR_DI_HWOPTS:
|
||||||
|
@ -462,9 +479,9 @@ static int hw_info_get(int info_id, const void **data,
|
||||||
static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
|
static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
|
||||||
const void *value)
|
const void *value)
|
||||||
{
|
{
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
if (!(ctx = sdi->priv)) {
|
if (!(devc = sdi->priv)) {
|
||||||
sr_err("genericdmm: sdi->priv was NULL.");
|
sr_err("genericdmm: sdi->priv was NULL.");
|
||||||
return SR_ERR_BUG;
|
return SR_ERR_BUG;
|
||||||
}
|
}
|
||||||
|
@ -475,14 +492,14 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
|
||||||
sr_err("genericdmm: LIMIT_MSEC can't be 0.");
|
sr_err("genericdmm: LIMIT_MSEC can't be 0.");
|
||||||
return SR_ERR;
|
return SR_ERR;
|
||||||
}
|
}
|
||||||
ctx->limit_msec = *(const uint64_t *)value;
|
devc->limit_msec = *(const uint64_t *)value;
|
||||||
sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
|
sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
|
||||||
ctx->limit_msec);
|
devc->limit_msec);
|
||||||
break;
|
break;
|
||||||
case SR_HWCAP_LIMIT_SAMPLES:
|
case SR_HWCAP_LIMIT_SAMPLES:
|
||||||
ctx->limit_samples = *(const uint64_t *)value;
|
devc->limit_samples = *(const uint64_t *)value;
|
||||||
sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
|
sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
|
||||||
ctx->limit_samples);
|
devc->limit_samples);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
sr_err("genericdmm: Unknown capability: %d.", hwcap);
|
sr_err("genericdmm: Unknown capability: %d.", hwcap);
|
||||||
|
@ -496,12 +513,12 @@ 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)
|
static int receive_data(int fd, int revents, void *cb_data)
|
||||||
{
|
{
|
||||||
struct sr_dev_inst *sdi;
|
struct sr_dev_inst *sdi;
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
if (!(sdi = cb_data))
|
if (!(sdi = cb_data))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
if (!(ctx = sdi->priv))
|
if (!(devc = sdi->priv))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
if (revents != G_IO_IN) {
|
if (revents != G_IO_IN) {
|
||||||
|
@ -509,7 +526,7 @@ static int receive_data(int fd, int revents, void *cb_data)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ctx->profile->transport) {
|
switch (devc->profile->transport) {
|
||||||
case DMM_TRANSPORT_USBHID:
|
case DMM_TRANSPORT_USBHID:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
break;
|
break;
|
||||||
|
@ -527,16 +544,16 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
||||||
struct sr_datafeed_packet packet;
|
struct sr_datafeed_packet packet;
|
||||||
struct sr_datafeed_header header;
|
struct sr_datafeed_header header;
|
||||||
struct sr_datafeed_meta_analog meta;
|
struct sr_datafeed_meta_analog meta;
|
||||||
struct context *ctx;
|
struct dev_context *devc;
|
||||||
|
|
||||||
if (!(ctx = sdi->priv)) {
|
if (!(devc = sdi->priv)) {
|
||||||
sr_err("genericdmm: sdi->priv was NULL.");
|
sr_err("genericdmm: sdi->priv was NULL.");
|
||||||
return SR_ERR_BUG;
|
return SR_ERR_BUG;
|
||||||
}
|
}
|
||||||
|
|
||||||
sr_dbg("genericdmm: Starting acquisition.");
|
sr_dbg("genericdmm: Starting acquisition.");
|
||||||
|
|
||||||
ctx->cb_data = cb_data;
|
devc->cb_data = cb_data;
|
||||||
|
|
||||||
/* Send header packet to the session bus. */
|
/* Send header packet to the session bus. */
|
||||||
sr_dbg("genericdmm: Sending SR_DF_HEADER.");
|
sr_dbg("genericdmm: Sending SR_DF_HEADER.");
|
||||||
|
@ -544,23 +561,23 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
|
||||||
packet.payload = (uint8_t *)&header;
|
packet.payload = (uint8_t *)&header;
|
||||||
header.feed_version = 1;
|
header.feed_version = 1;
|
||||||
gettimeofday(&header.starttime, NULL);
|
gettimeofday(&header.starttime, NULL);
|
||||||
sr_session_send(ctx->cb_data, &packet);
|
sr_session_send(devc->cb_data, &packet);
|
||||||
|
|
||||||
/* Send metadata about the SR_DF_ANALOG packets to come. */
|
/* Send metadata about the SR_DF_ANALOG packets to come. */
|
||||||
sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
|
sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
|
||||||
packet.type = SR_DF_META_ANALOG;
|
packet.type = SR_DF_META_ANALOG;
|
||||||
packet.payload = &meta;
|
packet.payload = &meta;
|
||||||
meta.num_probes = 1;
|
meta.num_probes = 1;
|
||||||
sr_session_send(ctx->cb_data, &packet);
|
sr_session_send(devc->cb_data, &packet);
|
||||||
|
|
||||||
/* Hook up a proxy handler to receive data from the device. */
|
/* Hook up a proxy handler to receive data from the device. */
|
||||||
switch (ctx->profile->transport) {
|
switch (devc->profile->transport) {
|
||||||
case DMM_TRANSPORT_USBHID:
|
case DMM_TRANSPORT_USBHID:
|
||||||
/* TODO libusb FD setup */
|
/* TODO libusb FD setup */
|
||||||
break;
|
break;
|
||||||
case DMM_TRANSPORT_SERIAL:
|
case DMM_TRANSPORT_SERIAL:
|
||||||
/* TODO serial FD setup */
|
/* TODO serial FD setup */
|
||||||
// sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
|
// sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -598,5 +615,5 @@ SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
|
||||||
.dev_config_set = hw_dev_config_set,
|
.dev_config_set = hw_dev_config_set,
|
||||||
.dev_acquisition_start = hw_dev_acquisition_start,
|
.dev_acquisition_start = hw_dev_acquisition_start,
|
||||||
.dev_acquisition_stop = hw_dev_acquisition_stop,
|
.dev_acquisition_stop = hw_dev_acquisition_stop,
|
||||||
.instances = NULL,
|
.priv = NULL,
|
||||||
};
|
};
|
||||||
|
|
|
@ -44,7 +44,13 @@ struct dev_profile {
|
||||||
struct sr_hwopt *defaults_opts;
|
struct sr_hwopt *defaults_opts;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct context {
|
/* Private driver context. */
|
||||||
|
struct drv_context {
|
||||||
|
GSList *instances;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Private, per-device-instance driver context. */
|
||||||
|
struct dev_context {
|
||||||
struct dev_profile *profile;
|
struct dev_profile *profile;
|
||||||
uint64_t limit_samples;
|
uint64_t limit_samples;
|
||||||
uint64_t limit_msec;
|
uint64_t limit_msec;
|
||||||
|
|
Loading…
Reference in New Issue