X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Flascar-el-usb%2Fprotocol.c;h=bcdd0b08912d3f392f80287156795f277b35cea9;hb=6944b2d02f23cf562574d3d1b37a2d698bdbde4e;hp=1a9b2be5dca0a441d7f29fcde7e9b3d972af860c;hpb=851d5b2274f138d58be42adbbe584d9a4c4dfe01;p=libsigrok.git diff --git a/hardware/lascar-el-usb/protocol.c b/hardware/lascar-el-usb/protocol.c index 1a9b2be5..bcdd0b08 100644 --- a/hardware/lascar-el-usb/protocol.c +++ b/hardware/lascar-el-usb/protocol.c @@ -20,18 +20,12 @@ #include #include #include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" #include "protocol.h" -#define LASCAR_VENDOR "Lascar" -#define LASCAR_INTERFACE 0 -#define LASCAR_EP_IN 0x82 -#define LASCAR_EP_OUT 2 -/* Max 100ms for a device to positively identify. */ -#define SCAN_TIMEOUT 100000 - extern struct sr_dev_driver lascar_el_usb_driver_info; static struct sr_dev_driver *di = &lascar_el_usb_driver_info; @@ -46,12 +40,12 @@ static const struct elusb_profile profiles[] = { { 8, "EL-USB-LITE", LOG_UNSUPPORTED }, { 9, "EL-USB-CO", LOG_CO }, { 10, "EL-USB-TC", LOG_UNSUPPORTED }, - { 11, "EL-USB-CO300", LOG_UNSUPPORTED }, - { 12, "EL-USB-2-LCD", LOG_UNSUPPORTED }, - { 13, "EL-USB-2+", LOG_UNSUPPORTED }, + { 11, "EL-USB-CO300", LOG_CO }, + { 12, "EL-USB-2-LCD", LOG_TEMP_RH }, + { 13, "EL-USB-2+", LOG_TEMP_RH }, { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED }, { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED }, - { 16, "EL-USB-2-LCD+", LOG_UNSUPPORTED }, + { 16, "EL-USB-2-LCD+", LOG_TEMP_RH }, { 17, "EL-USB-5", LOG_UNSUPPORTED }, { 18, "EL-USB-1-RCG", LOG_UNSUPPORTED }, { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED }, @@ -61,40 +55,51 @@ static const struct elusb_profile profiles[] = { }; -static void scan_xfer(struct libusb_transfer *xfer) +SR_PRIV libusb_device_handle *lascar_open(struct libusb_device *dev) +{ + libusb_device_handle *dev_hdl; + int ret; + + if ((ret = libusb_open(dev, &dev_hdl)) != 0) { + sr_dbg("failed to open device for scan: %s", + libusb_error_name(ret)); + return NULL; + } + + /* Some of these fail, but it needs doing -- some sort of mode + * setup for the SILabs F32x. */ + libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, + 0x00, 0xffff, 0x00, NULL, 0, 50); + libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, + 0x02, 0x0002, 0x00, NULL, 0, 50); + libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, + 0x02, 0x0001, 0x00, NULL, 0, 50); + + return dev_hdl; +} + +static void mark_xfer(struct libusb_transfer *xfer) { xfer->user_data = GINT_TO_POINTER(1); } -static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl) +SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl) { struct drv_context *drvc; - const struct elusb_profile *profile; - struct sr_dev_inst *sdi; struct libusb_transfer *xfer_in, *xfer_out; struct timeval tv; int64_t start; - int modelid, buflen, i; - unsigned char cmd[3], buf[256]; - char firmware[5]; + int buflen; + unsigned char cmd[3], buf[256], *config; drvc = di->priv; - modelid = 0; - - /* Some of these fail, but it needs doing -- some sort of mode - * setup for the SILabs F32x. */ - libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, - 0x00, 0xffff, 0x00, buf, 0, 50); - libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, - 0x02, 0x0002, 0x00, buf, 0, 50); - libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR, - 0x02, 0x0001, 0x00, buf, 0, 50); + config = NULL; if (!(xfer_in = libusb_alloc_transfer(0)) || !(xfer_out = libusb_alloc_transfer(0))) - return 0; + return NULL; /* Flush anything the F321 still has queued. */ while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, @@ -104,7 +109,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl) /* Keep a read request waiting in the wings, ready to pounce * the moment the device sends something. */ libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN, - buf, 256, scan_xfer, 0, 10000); + buf, 256, mark_xfer, 0, 10000); if (libusb_submit_transfer(xfer_in) != 0) goto cleanup; @@ -113,7 +118,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl) cmd[1] = 0xff; cmd[2] = 0xff; libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, - cmd, 3, scan_xfer, 0, 100); + cmd, 3, mark_xfer, 0, 100); if (libusb_submit_transfer(xfer_out) != 0) goto cleanup; @@ -138,7 +143,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl) } /* Got configuration structure header. */ - sr_spew("response to config request: 0x%.2x 0x%.2x 0x%.2x ", + sr_dbg("response to config request: 0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]); buflen = buf[1] | (buf[2] << 8); if (buf[0] != 0x02 || buflen > 256) { @@ -170,7 +175,10 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl) xfer_in->actual_length); goto cleanup; } - modelid = buf[0]; + + if (!(config = g_try_malloc(256))) + return NULL; + memcpy(config, buf, buflen); cleanup: if (!xfer_in->user_data || !xfer_in->user_data) { @@ -189,6 +197,104 @@ cleanup: libusb_free_transfer(xfer_in); libusb_free_transfer(xfer_out); + return config; +} + +/* Currently unused. */ +SR_PRIV int lascar_save_config(libusb_device_handle *dev_hdl, + unsigned char *config, int configlen) +{ + struct drv_context *drvc; + struct libusb_transfer *xfer_in, *xfer_out; + struct timeval tv; + int64_t start; + int buflen, ret; + unsigned char cmd[3], buf[256]; + + drvc = di->priv; + + if (!(xfer_in = libusb_alloc_transfer(0)) || + !(xfer_out = libusb_alloc_transfer(0))) + return SR_ERR; + + /* Flush anything the F321 still has queued. */ + while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen, + 5) == 0 && buflen > 0) + ; + ret = SR_OK; + + /* Keep a read request waiting in the wings, ready to pounce + * the moment the device sends something. */ + libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN, + buf, 256, mark_xfer, 0, 10000); + if (libusb_submit_transfer(xfer_in) != 0) { + ret = SR_ERR; + goto cleanup; + } + + /* Request device configuration structure. */ + cmd[0] = 0x01; + cmd[1] = configlen & 0xff; + cmd[2] = (configlen >> 8) & 0xff; + libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, + cmd, 3, mark_xfer, 0, 100); + if (libusb_submit_transfer(xfer_out) != 0) { + ret = SR_ERR; + goto cleanup; + } + tv.tv_sec = 0; + tv.tv_usec = 0; + while (!xfer_out->user_data) { + g_usleep(5000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + + libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT, + config, configlen, mark_xfer, 0, 100); + if (libusb_submit_transfer(xfer_out) != 0) { + ret = SR_ERR; + goto cleanup; + } + while (!xfer_in->user_data || !xfer_out->user_data) { + g_usleep(5000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + + if (xfer_in->actual_length != 1 || buf[0] != 0xff) { + sr_dbg("unexpected response after transfer"); + ret = SR_ERR; + } + +cleanup: + if (!xfer_in->user_data || !xfer_in->user_data) { + if (!xfer_in->user_data) + libusb_cancel_transfer(xfer_in); + if (!xfer_out->user_data) + libusb_cancel_transfer(xfer_out); + start = g_get_monotonic_time(); + while (!xfer_in->user_data || !xfer_out->user_data) { + if (g_get_monotonic_time() - start > 10000) + break; + g_usleep(1000); + libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); + } + } + libusb_free_transfer(xfer_in); + libusb_free_transfer(xfer_out); + + return ret; +} + +static struct sr_dev_inst *lascar_identify(unsigned char *config) +{ + struct dev_context *devc; + const struct elusb_profile *profile; + struct sr_dev_inst *sdi; + struct sr_probe *probe; + int modelid, i; + char firmware[5]; + + modelid = config[0]; sdi = NULL; if (modelid) { profile = NULL; @@ -203,8 +309,8 @@ cleanup: return NULL; } - i = buf[52] | (buf[53] << 8); - memcpy(firmware, buf + 0x30, 4); + i = config[52] | (config[53] << 8); + memcpy(firmware, config + 0x30, 4); firmware[4] = '\0'; sr_dbg("found %s with firmware version %s serial %d", profile->modelname, firmware, i); @@ -218,6 +324,15 @@ cleanup: profile->modelname, firmware))) return NULL; sdi->driver = di; + + if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) + return NULL; + sdi->probes = g_slist_append(NULL, probe); + + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) + return NULL; + sdi->priv = devc; + devc->profile = profile; } return sdi; @@ -231,6 +346,7 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address) struct libusb_device_descriptor des; libusb_device_handle *dev_hdl; int ret, i; + unsigned char *config; drvc = di->priv; sdi = NULL; @@ -246,34 +362,184 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address) libusb_get_device_address(devlist[i]) != address) continue; - if ((ret = libusb_open(devlist[i], &dev_hdl)) != 0) { - sr_dbg("failed to open device for scan: %s", - libusb_error_name(ret)); + if (!(dev_hdl = lascar_open(devlist[i]))) + continue; + + if (!(config = lascar_get_config(dev_hdl))) continue; - } - sdi = lascar_identify(dev_hdl); libusb_close(dev_hdl); + sdi = lascar_identify(config); + g_free(config); } return sdi; } +static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf, + int buflen) +{ + struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + float *temp, *rh; + uint16_t s; + int samples, samples_left, i, j; + + devc = sdi->priv; + samples = buflen / devc->sample_size; + samples_left = devc->logged_samples - devc->rcvd_samples; + if (samples_left < samples) + samples = samples_left; + switch (devc->profile->logformat) { + case LOG_TEMP_RH: + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + analog.mqflags = 0; + if (!(temp = g_try_malloc(sizeof(float) * samples))) + break; + if (!(rh = g_try_malloc(sizeof(float) * samples))) + break; + for (i = 0, j = 0; i < samples; i++) { + /* Both Celcius and Fahrenheit stored at base -40. */ + if (devc->temp_unit == 0) + /* Celcius is stored in half-degree increments. */ + temp[j] = buf[i * 2] / 2 - 40; + else + temp[j] = buf[i * 2] - 40; + + rh[j] = buf[i * 2 + 1] / 2; + + if (temp[j] == 0.0 && rh[j] == 0.0) + /* Skip invalid measurement. */ + continue; + j++; + } + analog.num_samples = j; + + analog.mq = SR_MQ_TEMPERATURE; + if (devc->temp_unit == 1) + analog.unit = SR_UNIT_FAHRENHEIT; + else + analog.unit = SR_UNIT_CELSIUS; + analog.data = temp; + sr_session_send(devc->cb_data, &packet); + + analog.mq = SR_MQ_RELATIVE_HUMIDITY; + analog.unit = SR_UNIT_PERCENTAGE; + analog.data = rh; + sr_session_send(devc->cb_data, &packet); + g_free(temp); + g_free(rh); + break; + case LOG_CO: + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + analog.num_samples = samples; + analog.mq = SR_MQ_CARBON_MONOXIDE; + analog.unit = SR_UNIT_CONCENTRATION; + analog.mqflags = 0; + if (!(analog.data = g_try_malloc(sizeof(float) * samples))) + break; + for (i = 0; i < samples; i++) { + s = (buf[i * 2] << 8) | buf[i * 2 + 1]; + analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000; + if (analog.data[i] < 0.0) + analog.data[i] = 0.0; + } + sr_session_send(devc->cb_data, &packet); + g_free(analog.data); + break; + default: + /* How did we even get this far? */ + break; + } + devc->rcvd_samples += samples; + +} -SR_PRIV int lascar_el_usb_receive_data(int fd, int revents, void *cb_data) +SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; struct dev_context *devc; + struct drv_context *drvc = di->priv; + struct sr_datafeed_packet packet; + struct sr_dev_inst *sdi; + struct timeval tv; + int i; + + (void)fd; + (void)revents; - if (!(sdi = cb_data)) - return TRUE; + sdi = cb_data; + devc = sdi->priv; - if (!(devc = sdi->priv)) - return TRUE; + if (sdi->status == SR_ST_STOPPING) { + for (i = 0; devc->usbfd[i] != -1; i++) + sr_source_remove(devc->usbfd[i]); - if (revents == G_IO_IN) { - /* TODO */ + sdi->driver->dev_close(sdi); + + packet.type = SR_DF_END; + sr_session_send(cb_data, &packet); } + memset(&tv, 0, sizeof(struct timeval)); + libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, + NULL); + return TRUE; } + +SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer) +{ + struct dev_context *devc; + struct sr_dev_inst *sdi; + int ret; + gboolean packet_has_error; + + sdi = transfer->user_data; + devc = sdi->priv; + + packet_has_error = FALSE; + switch (transfer->status) { + case LIBUSB_TRANSFER_NO_DEVICE: + /* USB device was unplugged. */ + hw_dev_acquisition_stop(sdi, sdi); + return; + case LIBUSB_TRANSFER_COMPLETED: + case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */ + break; + default: + packet_has_error = TRUE; + break; + } + + if (!packet_has_error) { + if (devc->rcvd_samples < devc->logged_samples) + lascar_el_usb_dispatch(sdi, transfer->buffer, + transfer->actual_length); + devc->rcvd_bytes += transfer->actual_length; + sr_spew("received %d/%d bytes (%d/%d samples)", + devc->rcvd_bytes, devc->log_size, + devc->rcvd_samples, devc->logged_samples); + if (devc->rcvd_bytes >= devc->log_size) + hw_dev_acquisition_stop(sdi, sdi); + } + + if (sdi->status == SR_ST_ACTIVE) { + /* Send the same request again. */ + if ((ret = libusb_submit_transfer(transfer) != 0)) { + sr_err("Unable to resubmit transfer: %s.", + libusb_error_name(ret)); + g_free(transfer->buffer); + libusb_free_transfer(transfer); + hw_dev_acquisition_stop(sdi, sdi); + } + } else { + /* This was the last transfer we're going to receive, so + * clean up now. */ + g_free(transfer->buffer); + libusb_free_transfer(transfer); + } + +}