X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=hardware%2Fvictor-dmm%2Fapi.c;h=6da326f635e8edae5708df51b7bc8a038c802146;hp=037b0b56c62e9892cbf28f6ceb560adc56c1168e;hb=ba7dd8bbb8168cba432a844259a3e239aa5f36d7;hpb=bd6fbf628abef9fab5293e016ad11e1a1ac4a0a9 diff --git a/hardware/victor-dmm/api.c b/hardware/victor-dmm/api.c index 037b0b56..6da326f6 100644 --- a/hardware/victor-dmm/api.c +++ b/hardware/victor-dmm/api.c @@ -33,8 +33,11 @@ SR_PRIV struct sr_dev_driver victor_dmm_driver_info; static struct sr_dev_driver *di = &victor_dmm_driver_info; -static int hw_dev_close(struct sr_dev_inst *sdi); -static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); +static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); + +static const int32_t hwopts[] = { + SR_CONF_CONN, +}; static const int32_t hwcaps[] = { SR_CONF_MULTIMETER, @@ -43,45 +46,17 @@ static const int32_t hwcaps[] = { SR_CONF_CONTINUOUS, }; -/* Properly close and free all devices. */ -static int clear_instances(void) +static int init(struct sr_context *sr_ctx) { - struct sr_dev_inst *sdi; - struct drv_context *drvc; - struct dev_context *devc; - GSList *l; - - if (!(drvc = di->priv)) - /* Can get called on an unused driver, doesn't matter. */ - return SR_OK; - - for (l = drvc->instances; l; l = l->next) { - if (!(sdi = l->data)) - continue; - if (!(devc = sdi->priv)) - continue; - hw_dev_close(sdi); - sr_usb_dev_inst_free(devc->usb); - sr_dev_inst_free(sdi); - } - - g_slist_free(drvc->instances); - drvc->instances = NULL; - - return SR_OK; + return std_init(sr_ctx, di, LOG_PREFIX); } -static int hw_init(struct sr_context *sr_ctx) -{ - return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); -} - -static GSList *hw_scan(GSList *options) +static GSList *scan(GSList *options) { struct drv_context *drvc; struct dev_context *devc; struct sr_dev_inst *sdi; - struct sr_probe *probe; + struct sr_channel *ch; struct libusb_device_descriptor des; libusb_device **devlist; GSList *devices; @@ -91,9 +66,6 @@ static GSList *hw_scan(GSList *options) drvc = di->priv; - /* USB scan is always authoritative. */ - clear_instances(); - devices = NULL; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { @@ -116,13 +88,14 @@ static GSList *hw_scan(GSList *options) return NULL; sdi->priv = devc; - if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) + if (!(ch = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) return NULL; - sdi->probes = g_slist_append(NULL, probe); + sdi->channels = g_slist_append(NULL, ch); - if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), + if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL))) return NULL; + sdi->inst_type = SR_INST_USB; drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); @@ -132,15 +105,15 @@ static GSList *hw_scan(GSList *options) return devices; } -static GSList *hw_dev_list(void) +static GSList *dev_list(void) { return ((struct drv_context *)(di->priv))->instances; } -static int hw_dev_open(struct sr_dev_inst *sdi) +static int dev_open(struct sr_dev_inst *sdi) { - struct dev_context *devc; struct drv_context *drvc = di->priv; + struct sr_usb_dev_inst *usb; libusb_device **devlist; int ret, i; @@ -149,13 +122,14 @@ static int hw_dev_open(struct sr_dev_inst *sdi) return SR_ERR; } - devc = sdi->priv; + usb = sdi->conn; + 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) + if (libusb_get_bus_number(devlist[i]) != usb->bus + || libusb_get_device_address(devlist[i]) != usb->address) continue; - if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) { + if ((ret = libusb_open(devlist[i], &usb->devhdl))) { sr_err("Failed to open device: %s.", libusb_error_name(ret)); return SR_ERR; } @@ -169,15 +143,15 @@ static int hw_dev_open(struct sr_dev_inst *sdi) /* The device reports as HID class, so the kernel would have * claimed it. */ - if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) { - if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) { + if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { + if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { sr_err("Failed to detach kernel driver: %s.", libusb_error_name(ret)); return SR_ERR; } } - if ((ret = libusb_claim_interface(devc->usb->devhdl, + if ((ret = libusb_claim_interface(usb->devhdl, VICTOR_INTERFACE))) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; @@ -187,59 +161,86 @@ static int hw_dev_open(struct sr_dev_inst *sdi) return SR_OK; } -static int hw_dev_close(struct sr_dev_inst *sdi) +static int dev_close(struct sr_dev_inst *sdi) { - struct dev_context *devc; + struct sr_usb_dev_inst *usb; if (!di->priv) { sr_err("Driver was not initialized."); return SR_ERR; } - devc = sdi->priv; - if (!devc->usb->devhdl) + usb = sdi->conn; + + if (!usb->devhdl) /* Nothing to do. */ return SR_OK; - libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE); - libusb_close(devc->usb->devhdl); - devc->usb->devhdl = NULL; + libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); + libusb_close(usb->devhdl); + usb->devhdl = NULL; sdi->status = SR_ST_INACTIVE; return SR_OK; } -static int hw_cleanup(void) +static int cleanup(void) { + int ret; struct drv_context *drvc; if (!(drvc = di->priv)) /* Can get called on an unused driver, doesn't matter. */ return SR_OK; - clear_instances(); + + ret = std_dev_clear(di, NULL); g_free(drvc); di->priv = NULL; + return ret; +} + +static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg) +{ + struct sr_usb_dev_inst *usb; + char str[128]; + + (void)cg; + + switch (id) { + 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; + } + return SR_OK; } -static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) +static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg) { struct dev_context *devc; gint64 now; int ret; + (void)cg; + + if (sdi->status != SR_ST_ACTIVE) + return SR_ERR_DEV_CLOSED; + if (!di->priv) { sr_err("Driver was not initialized."); return SR_ERR; } - if (sdi->status != SR_ST_ACTIVE) { - sr_err("Device inactive, can't set config options."); - return SR_ERR; - } - devc = sdi->priv; ret = SR_OK; switch (id) { @@ -262,12 +263,17 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) return ret; } -static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) +static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg) { - (void)sdi; + (void)cg; switch (key) { + case SR_CONF_SCAN_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); + break; case SR_CONF_DEVICE_OPTIONS: *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); @@ -289,14 +295,14 @@ static void receive_transfer(struct libusb_transfer *transfer) devc = sdi->priv; if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { /* USB device was unplugged. */ - hw_dev_acquisition_stop(sdi, sdi); + dev_acquisition_stop(sdi, sdi); } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { sr_dbg("Got %d-byte packet.", transfer->actual_length); if (transfer->actual_length == DMM_DATA_SIZE) { victor_dmm_receive_data(sdi, transfer->buffer); if (devc->limit_samples) { if (devc->num_samples >= devc->limit_samples) - hw_dev_acquisition_stop(sdi, sdi); + dev_acquisition_stop(sdi, sdi); } } } @@ -310,7 +316,7 @@ static void receive_transfer(struct libusb_transfer *transfer) libusb_error_name(ret)); g_free(transfer->buffer); libusb_free_transfer(transfer); - hw_dev_acquisition_stop(sdi, sdi); + dev_acquisition_stop(sdi, sdi); } } else { /* This was the last transfer we're going to receive, so @@ -328,7 +334,6 @@ static int handle_events(int fd, int revents, void *cb_data) struct sr_dev_inst *sdi; struct timeval tv; gint64 now; - int i; (void)fd; (void)revents; @@ -339,14 +344,13 @@ static int handle_events(int fd, int revents, void *cb_data) if (devc->limit_msec) { now = g_get_monotonic_time() / 1000; if (now > devc->end_time) - hw_dev_acquisition_stop(sdi, sdi); + dev_acquisition_stop(sdi, sdi); } if (sdi->status == SR_ST_STOPPING) { - for (i = 0; devc->usbfd[i] != -1; i++) - sr_source_remove(devc->usbfd[i]); + usb_source_remove(drvc->sr_ctx); - hw_dev_close(sdi); + dev_close(sdi); packet.type = SR_DF_END; sr_session_send(cb_data, &packet); @@ -359,36 +363,31 @@ static int handle_events(int fd, int revents, void *cb_data) return TRUE; } -static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, - void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { struct dev_context *devc; struct drv_context *drvc = di->priv; - const struct libusb_pollfd **pfd; + struct sr_usb_dev_inst *usb; struct libusb_transfer *transfer; - int ret, i; + int ret; unsigned char *buf; + if (sdi->status != SR_ST_ACTIVE) + return SR_ERR_DEV_CLOSED; + if (!di->priv) { sr_err("Driver was not initialized."); return SR_ERR; } devc = sdi->priv; + usb = sdi->conn; devc->cb_data = cb_data; /* Send header packet to the session bus. */ - std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); - - 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, - handle_events, (void *)sdi); - /* We'll need to remove this fd later. */ - devc->usbfd[i] = pfd[i]->fd; - } - devc->usbfd[i] = -1; + std_session_send_df_header(cb_data, LOG_PREFIX); + + usb_source_add(drvc->sr_ctx, 100, handle_events, (void *)sdi); buf = g_try_malloc(DMM_DATA_SIZE); transfer = libusb_alloc_transfer(0); @@ -396,7 +395,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, * The device only sends 1 transfer/second no matter how many * times you ask, but we want to keep step with the USB events * handling above. */ - libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl, + libusb_fill_interrupt_transfer(transfer, usb->devhdl, VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, cb_data, 100); if ((ret = libusb_submit_transfer(transfer) != 0)) { @@ -409,7 +408,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, return SR_OK; } -static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { (void)cb_data; @@ -432,17 +431,17 @@ SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { .name = "victor-dmm", .longname = "Victor DMMs", .api_version = 1, - .init = hw_init, - .cleanup = hw_cleanup, - .scan = hw_scan, - .dev_list = hw_dev_list, - .dev_clear = clear_instances, - .config_get = NULL, + .init = init, + .cleanup = cleanup, + .scan = scan, + .dev_list = dev_list, + .dev_clear = NULL, + .config_get = config_get, .config_set = config_set, .config_list = config_list, - .dev_open = hw_dev_open, - .dev_close = hw_dev_close, - .dev_acquisition_start = hw_dev_acquisition_start, - .dev_acquisition_stop = hw_dev_acquisition_stop, + .dev_open = dev_open, + .dev_close = dev_close, + .dev_acquisition_start = dev_acquisition_start, + .dev_acquisition_stop = dev_acquisition_stop, .priv = NULL, };