X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fscpi%2Fscpi_usbtmc_libusb.c;h=d45d1f4c8f2b01876230fb1b3ba3814008546238;hb=f6129c8f0c92e45de5d70b4101bf2bd759a5fdf7;hp=cd9ade47c18ff736bb1dab1e583be160d7380860;hpb=01b062390aa2fa488571f02b743fc4e6e6167657;p=libsigrok.git diff --git a/src/scpi/scpi_usbtmc_libusb.c b/src/scpi/scpi_usbtmc_libusb.c index cd9ade47..d45d1f4c 100644 --- a/src/scpi/scpi_usbtmc_libusb.c +++ b/src/scpi/scpi_usbtmc_libusb.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include "libsigrok-internal.h" @@ -48,8 +49,8 @@ struct scpi_usbtmc_libusb { }; /* Some USBTMC-specific enums, as defined in the USBTMC standard. */ -#define SUBCLASS_USBTMC 0x03 -#define USBTMC_USB488 0x01 +#define SUBCLASS_USBTMC 0x03 +#define USBTMC_USB488 0x01 enum { /* USBTMC control requests */ @@ -85,27 +86,37 @@ enum { #define USB488_DEV_CAP_SCPI 0x08 /* Bulk messages constants */ -#define USBTMC_BULK_HEADER_SIZE 12 +#define USBTMC_BULK_HEADER_SIZE 12 /* Bulk MsgID values */ -#define DEV_DEP_MSG_OUT 1 -#define REQUEST_DEV_DEP_MSG_IN 2 -#define DEV_DEP_MSG_IN 2 +#define DEV_DEP_MSG_OUT 1 +#define REQUEST_DEV_DEP_MSG_IN 2 +#define DEV_DEP_MSG_IN 2 /* bmTransferAttributes */ -#define EOM 0x01 -#define TERM_CHAR_ENABLED 0x02 +#define EOM 0x01 +#define TERM_CHAR_ENABLED 0x02 struct usbtmc_blacklist { uint16_t vid; uint16_t pid; }; +/* Devices that publish RL1 support, but don't support it. */ static struct usbtmc_blacklist blacklist_remote[] = { - /* Rigol DS1000 series publishes RL1 support, but doesn't support it. */ - { 0x1ab1, 0x0588 }, - /* Agilent DSO1000 series are rebadged versions of the Rigol DS1000. */ - { 0x0957, 0x0588 }, + { 0x1ab1, 0x0588 }, /* Rigol DS1000 series */ + { 0x1ab1, 0x04b0 }, /* Rigol DS2000 series */ + { 0x1ab1, 0x04b1 }, /* Rigol DS4000 series */ + { 0x1ab1, 0x0515 }, /* Rigol MSO5000 series */ + { 0x0957, 0x0588 }, /* Agilent DSO1000 series (rebadged Rigol DS1000) */ + { 0x0b21, 0xffff }, /* All Yokogawa devices */ + { 0xf4ec, 0xffff }, /* All Siglent SDS devices */ + ALL_ZERO +}; + +/* Devices that shall get reset during open(). */ +static struct usbtmc_blacklist whitelist_usb_reset[] = { + { 0xf4ec, 0xffff }, /* All Siglent SDS devices */ ALL_ZERO }; @@ -130,8 +141,9 @@ static GSList *scpi_usbtmc_libusb_scan(struct drv_context *drvc) for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { if ((ret = libusb_get_config_descriptor(devlist[i], confidx, &confdes)) < 0) { - sr_dbg("Failed to get configuration descriptor: %s, " - "ignoring device.", libusb_error_name(ret)); + if (ret != LIBUSB_ERROR_NOT_FOUND) + sr_dbg("Failed to get configuration descriptor: %s, " + "ignoring device.", libusb_error_name(ret)); break; } for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { @@ -154,7 +166,7 @@ static GSList *scpi_usbtmc_libusb_scan(struct drv_context *drvc) } libusb_free_device_list(devlist, 1); - sr_dbg("Found %d device(s).", g_slist_length(resources)); + /* No log message for #devices found (caller will log that). */ return resources; } @@ -192,7 +204,8 @@ static int check_usbtmc_blacklist(struct usbtmc_blacklist *blacklist, int i; for (i = 0; blacklist[i].vid; i++) { - if (blacklist[i].vid == vid && blacklist[i].pid == pid) + if ((blacklist[i].vid == vid && blacklist[i].pid == 0xFFFF) || + (blacklist[i].vid == vid && blacklist[i].pid == pid)) return TRUE; } @@ -216,14 +229,9 @@ static int scpi_usbtmc_remote(struct scpi_usbtmc_libusb *uscpi) return SR_OK; sr_dbg("Locking out local control."); - ret = libusb_control_transfer(usb->devhdl, - LIBUSB_ENDPOINT_IN | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - REN_CONTROL, 1, - uscpi->interface, - &status, 1, - TRANSFER_TIMEOUT); + ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + REN_CONTROL, 1, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { if (ret < 0) sr_dbg("Failed to enter REN state: %s.", libusb_error_name(ret)); @@ -232,14 +240,10 @@ static int scpi_usbtmc_remote(struct scpi_usbtmc_libusb *uscpi) return SR_ERR; } - ret = libusb_control_transfer(usb->devhdl, - LIBUSB_ENDPOINT_IN | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - LOCAL_LOCKOUT, 1, - uscpi->interface, - &status, 1, - TRANSFER_TIMEOUT); + ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + LOCAL_LOCKOUT, 0, uscpi->interface, &status, 1, + TRANSFER_TIMEOUT); if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { if (ret < 0) sr_dbg("Failed to enter local lockout state: %s.", @@ -270,14 +274,9 @@ static void scpi_usbtmc_local(struct scpi_usbtmc_libusb *uscpi) return; sr_dbg("Returning local control."); - ret = libusb_control_transfer(usb->devhdl, - LIBUSB_ENDPOINT_IN | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - GO_TO_LOCAL, 1, - uscpi->interface, - &status, 1, - TRANSFER_TIMEOUT); + ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + GO_TO_LOCAL, 0, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { if (ret < 0) sr_dbg("Failed to clear local lockout state: %s.", @@ -299,9 +298,10 @@ static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) struct libusb_config_descriptor *confdes; const struct libusb_interface_descriptor *intfdes; const struct libusb_endpoint_descriptor *ep; - int confidx, intfidx, epidx, config = 0; + int confidx, intfidx, epidx, config = 0, current_config; uint8_t capabilities[24]; int ret, found = 0; + int do_reset; if (usb->devhdl) return SR_OK; @@ -314,14 +314,15 @@ static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { if ((ret = libusb_get_config_descriptor(dev, confidx, &confdes)) < 0) { - sr_dbg("Failed to get configuration descriptor: %s, " - "ignoring device.", libusb_error_name(ret)); + if (ret != LIBUSB_ERROR_NOT_FOUND) + sr_dbg("Failed to get configuration descriptor: %s, " + "ignoring device.", libusb_error_name(ret)); continue; } for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { intfdes = confdes->interface[intfidx].altsetting; if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION || - intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || + intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || intfdes->bInterfaceProtocol != USBTMC_USB488) continue; uscpi->interface = intfdes->bInterfaceNumber; @@ -367,10 +368,13 @@ static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) uscpi->detached_kernel_driver = 1; } - if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) { - sr_err("Failed to set configuration: %s.", - libusb_error_name(ret)); - return SR_ERR; + if (libusb_get_configuration(usb->devhdl, ¤t_config) == 0 + && current_config != config) { + if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) { + sr_err("Failed to set configuration: %s.", + libusb_error_name(ret)); + return SR_ERR; + } } if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface)) < 0) { @@ -379,36 +383,50 @@ static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) return SR_ERR; } + /* Optionally reset the USB device. */ + do_reset = check_usbtmc_blacklist(whitelist_usb_reset, + des.idVendor, des.idProduct); + if (do_reset) + libusb_reset_device(usb->devhdl); + /* Get capabilities. */ - ret = libusb_control_transfer(usb->devhdl, - LIBUSB_ENDPOINT_IN | - LIBUSB_REQUEST_TYPE_CLASS | - LIBUSB_RECIPIENT_INTERFACE, - GET_CAPABILITIES, 0, - uscpi->interface, - capabilities, sizeof(capabilities), - TRANSFER_TIMEOUT); + ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | + LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, + GET_CAPABILITIES, 0, uscpi->interface, capabilities, + sizeof(capabilities), TRANSFER_TIMEOUT); if (ret == sizeof(capabilities)) { uscpi->usbtmc_int_cap = capabilities[ 4]; uscpi->usbtmc_dev_cap = capabilities[ 5]; uscpi->usb488_dev_cap = capabilities[15]; } sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s", - uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI ? "SCPI, " : "", - uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR ? "TermChar, ": "", - uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "L3, " : - uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "" : "L4, ", - uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "T5, " : - uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "" : "T6, ", - uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1 ? "SR1" : "SR0", - uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1 ? "RL1" : "RL0", - uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1 ? "DT1" : "DT0"); + uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI ? "SCPI, " : "", + uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR ? "TermChar, ": "", + uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY ? "L3, " : + uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "" : "L4, ", + uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "T5, " : + uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY ? "" : "T6, ", + uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1 ? "SR1" : "SR0", + uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1 ? "RL1" : "RL0", + uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1 ? "DT1" : "DT0"); scpi_usbtmc_remote(uscpi); return SR_OK; } +static int scpi_usbtmc_libusb_connection_id(struct sr_scpi_dev_inst *scpi, + char **connection_id) +{ + struct scpi_usbtmc_libusb *uscpi = scpi->priv; + struct sr_usb_dev_inst *usb = uscpi->usb; + + *connection_id = g_strdup_printf("%s/%" PRIu8 ".%" PRIu8 "", + scpi->prefix, usb->bus, usb->address); + + return SR_OK; +} + static int scpi_usbtmc_libusb_source_add(struct sr_session *session, void *priv, int events, int timeout, sr_receive_data_callback cb, void *cb_data) @@ -431,14 +449,14 @@ static void usbtmc_bulk_out_header_write(void *header, uint8_t MsgID, uint8_t bmTransferAttributes, char TermChar) { - W8(header+ 0, MsgID); - W8(header+ 1, bTag); - W8(header+ 2, ~bTag); - W8(header+ 3, 0); - WL32(header+ 4, TransferSize); - W8(header+ 8, bmTransferAttributes); - W8(header+ 9, TermChar); - WL16(header+10, 0); + W8(header + 0, MsgID); + W8(header + 1, bTag); + W8(header + 2, ~bTag); + W8(header + 3, 0); + WL32(header + 4, TransferSize); + W8(header + 8, bmTransferAttributes); + W8(header + 9, TermChar); + WL16(header + 10, 0); } static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID, @@ -446,14 +464,15 @@ static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID, int32_t *TransferSize, uint8_t *bmTransferAttributes) { - if (R8(header+0) != MsgID || - R8(header+1) != bTag || - R8(header+2) != (unsigned char)~bTag) + if (R8(header + 0) != MsgID || + R8(header + 1) != bTag || + R8(header + 2) != (unsigned char)~bTag) return SR_ERR; if (TransferSize) - *TransferSize = RL32(header+4); + *TransferSize = RL32(header + 4); if (bmTransferAttributes) - *bmTransferAttributes = R8(header+8); + *bmTransferAttributes = R8(header + 8); + return SR_OK; } @@ -464,23 +483,23 @@ static int scpi_usbtmc_bulkout(struct scpi_usbtmc_libusb *uscpi, struct sr_usb_dev_inst *usb = uscpi->usb; int padded_size, ret, transferred; - if (data && size+USBTMC_BULK_HEADER_SIZE+3 > (int)sizeof(uscpi->buffer)) { + if (data && (size + USBTMC_BULK_HEADER_SIZE + 3) > (int)sizeof(uscpi->buffer)) { sr_err("USBTMC bulk out transfer is too big."); return SR_ERR; } uscpi->bTag++; - uscpi->bTag += !uscpi->bTag; /* bTag == 0 is invalid so avoid it. */ + uscpi->bTag += !uscpi->bTag; /* bTag == 0 is invalid so avoid it. */ usbtmc_bulk_out_header_write(uscpi->buffer, msg_id, uscpi->bTag, size, transfer_attributes, 0); if (data) - memcpy(uscpi->buffer+USBTMC_BULK_HEADER_SIZE, data, size); + memcpy(uscpi->buffer + USBTMC_BULK_HEADER_SIZE, data, size); else size = 0; size += USBTMC_BULK_HEADER_SIZE; padded_size = (size + 3) & ~0x3; - memset(uscpi->buffer+size, 0, padded_size - size); + memset(uscpi->buffer + size, 0, padded_size - size); ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_out_ep, uscpi->buffer, padded_size, &transferred, @@ -619,32 +638,17 @@ static int scpi_usbtmc_libusb_close(struct sr_scpi_dev_inst *scpi) { struct scpi_usbtmc_libusb *uscpi = scpi->priv; struct sr_usb_dev_inst *usb = uscpi->usb; - struct libusb_device *dev; - struct libusb_device_descriptor des; int ret; if (!usb->devhdl) return SR_ERR; - dev = libusb_get_device(usb->devhdl); - libusb_get_device_descriptor(dev, &des); - if (des.idVendor == 0x1ab1 && des.idProduct == 0x0588 - && scpi->firmware_version >= 24) { - /* Rigol DS1000 with firmware > 0.2.4 needs this. */ - if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) - sr_err("Failed to clear halt/stall condition for EP %d: %s.", - uscpi->bulk_out_ep, libusb_error_name(ret)); - if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) - sr_err("Failed to clear halt/stall condition for EP %d: %s.", - uscpi->bulk_out_ep, libusb_error_name(ret)); - } - scpi_usbtmc_local(uscpi); if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0) sr_err("Failed to release interface: %s.", libusb_error_name(ret)); - + if (uscpi->detached_kernel_driver) { if ((ret = libusb_attach_kernel_driver(usb->devhdl, uscpi->interface)) < 0) @@ -667,10 +671,12 @@ static void scpi_usbtmc_libusb_free(void *priv) SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev = { .name = "USBTMC", .prefix = "usbtmc", + .transport = SCPI_TRANSPORT_USBTMC, .priv_size = sizeof(struct scpi_usbtmc_libusb), .scan = scpi_usbtmc_libusb_scan, .dev_inst_new = scpi_usbtmc_libusb_dev_inst_new, .open = scpi_usbtmc_libusb_open, + .connection_id = scpi_usbtmc_libusb_connection_id, .source_add = scpi_usbtmc_libusb_source_add, .source_remove = scpi_usbtmc_libusb_source_remove, .send = scpi_usbtmc_libusb_send,