From: Andy Lutomirski Date: Sat, 13 Jun 2020 22:39:17 +0000 (-0700) Subject: scpi_usbtmc_libusb: Retry if a Bulk-IN request starts with an empty packet X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=025bd56f10c7c3afc63eb48b706245a71c4c49c5;ds=sidebyside scpi_usbtmc_libusb: Retry if a Bulk-IN request starts with an empty packet This seems to make the Rigol DS1054Z work. It's still a bit janky -- on a live capture, sample 688 (zero-based) out of the 1200-sample frame seems to consistently contain garbage. I'm not sure what's going on. --- diff --git a/src/scpi/scpi_usbtmc_libusb.c b/src/scpi/scpi_usbtmc_libusb.c index c8bcd0dc..2da67030 100644 --- a/src/scpi/scpi_usbtmc_libusb.c +++ b/src/scpi/scpi_usbtmc_libusb.c @@ -524,19 +524,36 @@ static int scpi_usbtmc_bulkin_start(struct scpi_usbtmc_libusb *uscpi, uint8_t *transfer_attributes) { struct sr_usb_dev_inst *usb = uscpi->usb; - int ret, transferred, message_size; + int ret, transferred, message_size, tries; + + for (tries = 0; ; tries++) { + ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, + size, &transferred, + TRANSFER_TIMEOUT); + if (ret < 0) { + sr_err("USBTMC bulk in transfer error: %s.", + libusb_error_name(ret)); + return SR_ERR; + } - ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size, - &transferred, TRANSFER_TIMEOUT); - if (ret < 0) { - sr_err("USBTMC bulk in transfer error: %s.", - libusb_error_name(ret)); - return SR_ERR; - } + if (transferred == 0 && tries < 1) { + /* + * The DEV_DEP_MSG_IN message is empty, and the TMC + * spec says it should at least contain a header. + * The Rigol DS1054Z seems to do this sometimes, and + * it follows up with a valid message. Give the device + * one more chance to send a header. + */ + sr_warn("USBTMC bulk in start was empty; retrying\n"); + continue; + } - if (transferred < USBTMC_BULK_HEADER_SIZE) { - sr_err("USBTMC bulk in returned too little data: %d/%d bytes\n", transferred, size); - return SR_ERR; + if (transferred < USBTMC_BULK_HEADER_SIZE) { + sr_err("USBTMC bulk in returned too little data: %d/%d bytes\n", transferred, size); + return SR_ERR; + } + + break; } if (usbtmc_bulk_in_header_read(data, msg_id, uscpi->bTag, &message_size,