X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=hardware%2Flascar-el-usb%2Fapi.c;h=04f844b3318b1d575a4eec0a8f23708e59a162a9;hp=8ebb4ca0d4a3a6ba75aa420e2f31902ce0142e62;hb=43cd4637285833706f8a404ca027bcf0ee75b9ae;hpb=0d485e30c607b22b5f328512327e4e0fafc6cbb7 diff --git a/hardware/lascar-el-usb/api.c b/hardware/lascar-el-usb/api.c index 8ebb4ca0..04f844b3 100644 --- a/hardware/lascar-el-usb/api.c +++ b/hardware/lascar-el-usb/api.c @@ -23,92 +23,42 @@ #include "libsigrok-internal.h" #include "protocol.h" -SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address); SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info; static struct sr_dev_driver *di = &lascar_el_usb_driver_info; -static int hw_dev_close(struct sr_dev_inst *sdi); -static const int hwopts[] = { +static const int32_t hwopts[] = { SR_CONF_CONN, - 0, }; -static const int hwcaps[] = { +static const int32_t hwcaps[] = { SR_CONF_THERMOMETER, SR_CONF_HYGROMETER, + SR_CONF_DATALOG, SR_CONF_LIMIT_SAMPLES, - 0 }; -/* 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)) - 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) +static GSList *scan(GSList *options) { struct drv_context *drvc; - - if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { - sr_err("Driver context malloc failed."); - return SR_ERR_MALLOC; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; -} - -static GSList *hw_scan(GSList *options) -{ - struct drv_context *drvc; - struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_usb_dev_inst *usb; struct sr_config *src; GSList *usb_devices, *devices, *l; const char *conn; - (void)options; - - if (!(drvc = di->priv)) { - sr_err("Driver was not initialized."); - return NULL; - } - - /* USB scan is always authoritative. */ - clear_instances(); + drvc = di->priv; conn = NULL; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: - conn = src->value; + conn = g_variant_get_string(src->data, NULL); break; } } @@ -126,8 +76,8 @@ static GSList *hw_scan(GSList *options) g_free(usb); continue; } - devc = sdi->priv; - devc->usb = usb; + sdi->inst_type = SR_INST_USB; + sdi->conn = usb; drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); } @@ -138,22 +88,15 @@ static GSList *hw_scan(GSList *options) return devices; } -static GSList *hw_dev_list(void) +static GSList *dev_list(void) { - struct drv_context *drvc; - - if (!(drvc = di->priv)) { - sr_err("Driver was not initialized."); - return NULL; - } - - return drvc->instances; + 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 drv_context *drvc; - struct dev_context *devc; + struct sr_usb_dev_inst *usb; int ret; if (!(drvc = di->priv)) { @@ -161,11 +104,12 @@ static int hw_dev_open(struct sr_dev_inst *sdi) return SR_ERR; } - devc = sdi->priv; - if (sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb) != SR_OK) + usb = sdi->conn; + + if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; - if ((ret = libusb_claim_interface(devc->usb->devhdl, LASCAR_INTERFACE))) { + if ((ret = libusb_claim_interface(usb->devhdl, LASCAR_INTERFACE))) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } @@ -174,101 +118,139 @@ static int hw_dev_open(struct sr_dev_inst *sdi) return ret; } -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, LASCAR_INTERFACE); - libusb_close(devc->usb->devhdl); - devc->usb->devhdl = NULL; - g_free(devc->config); + libusb_release_interface(usb->devhdl, LASCAR_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 SR_OK; + return ret; } -static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) +static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg) { - (void)sdi; + struct dev_context *devc; + struct sr_usb_dev_inst *usb; + int ret; + char str[128]; + (void)cg; + + devc = sdi->priv; 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; + case SR_CONF_DATALOG: + if (!sdi) + return SR_ERR_ARG; + if ((ret = lascar_is_logging(sdi)) == -1) + return SR_ERR; + *data = g_variant_new_boolean(ret ? TRUE : FALSE); + break; + case SR_CONF_LIMIT_SAMPLES: + *data = g_variant_new_uint64(devc->limit_samples); + break; default: - return SR_ERR_ARG; + return SR_ERR_NA; } return SR_OK; } -static int config_set(int id, const void *value, 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; 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) { + case SR_CONF_DATALOG: + if (g_variant_get_boolean(data)) { + /* Start logging. */ + ret = lascar_start_logging(sdi); + } else { + /* Stop logging. */ + ret = lascar_stop_logging(sdi); + } + break; case SR_CONF_LIMIT_SAMPLES: - devc->limit_samples = *(const uint64_t *)value; + devc->limit_samples = g_variant_get_uint64(data); sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples); break; default: - sr_err("Unknown hardware capability: %d.", id); - ret = SR_ERR_ARG; + ret = SR_ERR_NA; } return ret; } -static int config_list(int key, const void **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 = hwopts; + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); break; case SR_CONF_DEVICE_OPTIONS: - *data = hwcaps; + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); break; default: - return SR_ERR_ARG; + return SR_ERR_NA; } return SR_OK; @@ -306,10 +288,13 @@ static float binary32_le_to_float(unsigned char *buf) static int lascar_proc_config(const struct sr_dev_inst *sdi) { struct dev_context *devc; - int ret; + struct sr_usb_dev_inst *usb; + int dummy, ret; devc = sdi->priv; - if (!(devc->config = lascar_get_config(devc->usb->devhdl))) + usb = sdi->conn; + + if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK) return SR_ERR; ret = SR_OK; @@ -341,25 +326,31 @@ static int lascar_proc_config(const struct sr_dev_inst *sdi) return ret; } -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 sr_datafeed_packet packet; - struct sr_datafeed_header header; + struct sr_datafeed_meta meta; + struct sr_config *src; struct dev_context *devc; - struct drv_context *drvc = di->priv; + struct drv_context *drvc; + struct sr_usb_dev_inst *usb; struct libusb_transfer *xfer_in, *xfer_out; - const struct libusb_pollfd **pfd; struct timeval tv; - int ret, i; + uint64_t interval; + int ret; unsigned char cmd[3], resp[4], *buf; + if (sdi->status != SR_ST_ACTIVE) + return SR_ERR_DEV_CLOSED; + if (!di->priv) { sr_err("Driver was not initialized."); return SR_ERR; } + drvc = di->priv; devc = sdi->priv; + usb = sdi->conn; devc->cb_data = cb_data; if (lascar_proc_config(sdi) != SR_OK) @@ -368,11 +359,15 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, sr_dbg("Starting log retrieval."); /* Send header packet to the session bus. */ - sr_dbg("Sending SR_DF_HEADER."); - packet.type = SR_DF_HEADER; - packet.payload = (uint8_t *)&header; - header.feed_version = 1; + std_session_send_df_header(cb_data, LOG_PREFIX); + + interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000; + packet.type = SR_DF_META; + packet.payload = &meta; + src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval)); + meta.config = g_slist_append(NULL, src); sr_session_send(devc->cb_data, &packet); + g_free(src); if (devc->logged_samples == 0) { /* This ensures the frontend knows the session is done. */ @@ -385,20 +380,20 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, !(xfer_out = libusb_alloc_transfer(0))) return SR_ERR; - libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, + libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, 0x00, 0xffff, 0x00, NULL, 0, 50); - libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, + libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, 0x02, 0x0002, 0x00, NULL, 0, 50); - libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, + libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, 0x02, 0x0001, 0x00, NULL, 0, 50); /* Flush input. The F321 requires this. */ - while (libusb_bulk_transfer(devc->usb->devhdl, LASCAR_EP_IN, resp, + while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp, 256, &ret, 5) == 0 && ret > 0) ; - libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN, + libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN, resp, sizeof(resp), mark_xfer, 0, 10000); if (libusb_submit_transfer(xfer_in) != 0) { libusb_free_transfer(xfer_in); @@ -409,7 +404,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, cmd[0] = 0x03; cmd[1] = 0xff; cmd[2] = 0xff; - libusb_fill_bulk_transfer(xfer_out, devc->usb->devhdl, LASCAR_EP_OUT, + libusb_fill_bulk_transfer(xfer_out, usb->devhdl, LASCAR_EP_OUT, cmd, 3, mark_xfer, 0, 100); if (libusb_submit_transfer(xfer_out) != 0) { libusb_free_transfer(xfer_in); @@ -439,18 +434,11 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, devc->log_size = xfer_in->buffer[1] + (xfer_in->buffer[2] << 8); libusb_free_transfer(xfer_out); - 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, - lascar_el_usb_handle_events, (void *)sdi); - /* We'll need to remove this fd later. */ - devc->usbfd[i] = pfd[i]->fd; - } - devc->usbfd[i] = -1; + usb_source_add(sdi->session, drvc->sr_ctx, 100, + lascar_el_usb_handle_events, (void *)sdi); buf = g_try_malloc(4096); - libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN, + libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN, buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100); if ((ret = libusb_submit_transfer(xfer_in) != 0)) { sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); @@ -462,7 +450,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, return SR_OK; } -SR_PRIV int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +SR_PRIV int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { (void)cb_data; @@ -486,17 +474,17 @@ SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = { .name = "lascar-el-usb", .longname = "Lascar EL-USB", .api_version = 1, - .init = hw_init, - .cleanup = hw_cleanup, - .scan = hw_scan, - .dev_list = hw_dev_list, - .dev_clear = clear_instances, + .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, };