24 #define DEBUG_PRINT_BUF(buf, len) \
26 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) { \
27 char buf_string[32 * 3 + 1]; \
28 int buf_string_pos = 0; \
29 for (int i = 0; i < len; i++) { \
30 buf_string_pos += sprintf(buf_string + buf_string_pos, " %02x", buf[i]); \
31 if (i % 32 == 32 - 1) { \
32 LOG_DEBUG_IO("%s", buf_string); \
36 if (buf_string_pos > 0) \
37 LOG_DEBUG_IO("%s", buf_string);\
41 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
42 #define FTDI_DEVICE_IN_REQTYPE (0x80 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
44 #define BITMODE_MPSSE 0x02
46 #define SIO_RESET_REQUEST 0x00
47 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
48 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
49 #define SIO_SET_BITMODE_REQUEST 0x0B
51 #define SIO_RESET_SIO 0
52 #define SIO_RESET_PURGE_RX 1
53 #define SIO_RESET_PURGE_TX 2
83 char desc_string[256];
84 retval = libusb_get_string_descriptor_ascii(
device, str_index, (
unsigned char *)desc_string,
87 LOG_ERROR(
"libusb_get_string_descriptor_ascii() failed with %s", libusb_error_name(retval));
90 return strncmp(
string, desc_string,
sizeof(desc_string)) == 0;
96 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
97 char *loc = strdup(location);
99 int path_step, path_len;
100 uint8_t dev_bus = libusb_get_bus_number(
device);
103 path_len = libusb_get_port_numbers(
device, port_path, 7);
104 if (path_len == LIBUSB_ERROR_OVERFLOW) {
105 LOG_ERROR(
"cannot determine path to usb device! (more than 7 ports in path)");
109 LOG_DEBUG(
"device path has %i steps", path_len);
111 ptr = strtok(loc,
"-:");
116 if (atoi(ptr) != dev_bus) {
122 while (path_step < 7) {
123 ptr = strtok(
NULL,
".,");
125 LOG_DEBUG(
"no more tokens in path at step %i", path_step);
129 if (path_step < path_len
130 && atoi(ptr) != port_path[path_step]) {
131 LOG_DEBUG(
"path mismatch at step %i", path_step);
139 if (path_step == path_len)
153 const char *product,
const char *
serial,
const char *location)
155 struct libusb_device **list;
156 struct libusb_device_descriptor desc;
157 struct libusb_config_descriptor *config0;
160 ssize_t cnt = libusb_get_device_list(ctx->
usb_ctx, &list);
162 LOG_ERROR(
"libusb_get_device_list() failed with %s", libusb_error_name(cnt));
164 for (ssize_t i = 0; i < cnt; i++) {
165 struct libusb_device *
device = list[i];
167 err = libusb_get_device_descriptor(
device, &desc);
168 if (err != LIBUSB_SUCCESS) {
169 LOG_ERROR(
"libusb_get_device_descriptor() failed with %s", libusb_error_name(err));
177 if (err != LIBUSB_SUCCESS) {
178 LOG_ERROR(
"libusb_open() failed with %s",
179 libusb_error_name(err));
202 libusb_free_device_list(list, 1);
209 err = libusb_get_config_descriptor(libusb_get_device(ctx->
usb_dev), 0, &config0);
210 if (err != LIBUSB_SUCCESS) {
211 LOG_ERROR(
"libusb_get_config_descriptor() failed with %s", libusb_error_name(err));
218 err = libusb_get_configuration(ctx->
usb_dev, &cfg);
219 if (err != LIBUSB_SUCCESS) {
220 LOG_ERROR(
"libusb_get_configuration() failed with %s", libusb_error_name(err));
224 if (desc.bNumConfigurations > 0 && cfg != config0->bConfigurationValue) {
225 err = libusb_set_configuration(ctx->
usb_dev, config0->bConfigurationValue);
226 if (err != LIBUSB_SUCCESS) {
227 LOG_ERROR(
"libusb_set_configuration() failed with %s", libusb_error_name(err));
234 if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_NOT_FOUND
235 && err != LIBUSB_ERROR_NOT_SUPPORTED) {
236 LOG_WARNING(
"libusb_detach_kernel_driver() failed with %s, trying to continue anyway",
237 libusb_error_name(err));
241 if (err != LIBUSB_SUCCESS) {
242 LOG_ERROR(
"libusb_claim_interface() failed with %s", libusb_error_name(err));
251 LOG_ERROR(
"failed to reset FTDI device: %s", libusb_error_name(err));
255 switch (desc.bcdDevice) {
290 LOG_ERROR(
"unsupported FTDI chip type: 0x%04x", desc.bcdDevice);
295 if (!(desc.bNumConfigurations > 0 && ctx->
interface < config0->bNumInterfaces
296 && config0->interface[ctx->
interface].num_altsetting > 0))
299 const struct libusb_interface_descriptor *descriptor;
300 descriptor = &config0->interface[ctx->
interface].altsetting[0];
301 if (descriptor->bNumEndpoints != 2)
306 for (
int i = 0; i < descriptor->bNumEndpoints; i++) {
307 if (descriptor->endpoint[i].bEndpointAddress & 0x80) {
308 ctx->
in_ep = descriptor->endpoint[i].bEndpointAddress;
310 descriptor->endpoint[i].wMaxPacketSize;
312 ctx->
out_ep = descriptor->endpoint[i].bEndpointAddress;
319 libusb_free_config_descriptor(config0);
323 LOG_ERROR(
"unrecognized USB device descriptor");
325 libusb_free_config_descriptor(config0);
331 const char *
serial,
const char *location,
int channel)
333 struct mpsse_ctx *ctx = calloc(1,
sizeof(*ctx));
356 ctx->
index = channel + 1;
360 err = libusb_init(&ctx->
usb_ctx);
361 if (err != LIBUSB_SUCCESS) {
362 LOG_ERROR(
"libusb_init() failed with %s", libusb_error_name(err));
367 LOG_ERROR(
"unable to open ftdi device with description '%s', "
368 "serial '%s' at bus location '%s'",
371 location ? location :
"*");
380 LOG_ERROR(
"unable to set latency timer: %s", libusb_error_name(err));
384 err = libusb_control_transfer(ctx->
usb_dev,
393 LOG_ERROR(
"unable to set MPSSE bitmode: %s", libusb_error_name(err));
435 LOG_ERROR(
"unable to purge ftdi rx buffers: %s", libusb_error_name(err));
442 LOG_ERROR(
"unable to purge ftdi tx buffers: %s", libusb_error_name(err));
466 unsigned int bit_count)
476 unsigned int bit_count,
unsigned int offset)
499 unsigned int in_offset,
unsigned int length, uint8_t
mode)
510 if (out || (!out && !in))
534 unsigned int this_bytes =
length / 8;
536 if (this_bytes > 65536)
545 if (this_bytes > 0) {
561 for (
unsigned int n = 0; n < this_bytes; n++)
576 unsigned int in_offset,
unsigned int length,
bool tdi, uint8_t
mode)
596 unsigned int this_bits =
length;
612 bit_copy(&data, 0, out, out_offset, this_bits);
613 out_offset += this_bits;
693 uint8_t val_if_false)
754 assert(frequency >= 0);
763 base_clock = 60000000;
766 base_clock = 12000000;
769 int divisor = (base_clock / 2 + frequency - 1) / frequency - 1;
772 assert(divisor >= 0);
776 frequency = base_clock / 2 / (1 + divisor);
800 unsigned int num_packets =
DIV_ROUND_UP(transfer->actual_length, packet_size);
801 unsigned int chunk_remains = transfer->actual_length;
802 for (
unsigned int i = 0; i < num_packets && chunk_remains > 2; i++) {
803 unsigned int this_size = packet_size - 2;
804 if (this_size > chunk_remains - 2)
805 this_size = chunk_remains - 2;
812 chunk_remains -= this_size + 2;
823 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
843 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
866 struct libusb_transfer *read_transfer =
NULL;
870 read_result.
done =
false;
876 struct libusb_transfer *write_transfer = libusb_alloc_transfer(0);
879 retval = libusb_submit_transfer(write_transfer);
880 if (retval != LIBUSB_SUCCESS)
884 read_transfer = libusb_alloc_transfer(0);
888 retval = libusb_submit_transfer(read_transfer);
889 if (retval != LIBUSB_SUCCESS)
895 int64_t warn_after = 2000;
896 while (!write_result.
done || !read_result.
done) {
902 retval = libusb_handle_events_timeout_completed(ctx->
usb_ctx, &timeout_usb,
NULL);
906 if (now -
start > warn_after) {
907 LOG_WARNING(
"Haven't made progress in mpsse_flush() for %" PRId64
912 if (retval == LIBUSB_ERROR_INTERRUPTED)
915 if (retval != LIBUSB_SUCCESS) {
916 libusb_cancel_transfer(write_transfer);
918 libusb_cancel_transfer(read_transfer);
923 if (retval != LIBUSB_SUCCESS) {
924 LOG_ERROR(
"libusb_handle_events() failed with %s", libusb_error_name(retval));
927 LOG_ERROR(
"ftdi device did not accept all data: %d, tried %d",
932 LOG_ERROR(
"ftdi device did not return all data: %d, expected %d",
950 libusb_free_transfer(write_transfer);
952 libusb_free_transfer(read_transfer);
static const struct device_t * device
void bit_copy_queue_init(struct bit_copy_queue *q)
void bit_copy_discard(struct bit_copy_queue *q)
void bit_copy_execute(struct bit_copy_queue *q)
int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
bool jtag_libusb_match_ids(struct libusb_device_descriptor *dev_desc, const uint16_t vids[], const uint16_t pids[])
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
int mpsse_flush(struct mpsse_ctx *ctx)
static unsigned int buffer_add_read(struct mpsse_ctx *ctx, uint8_t *in, unsigned int in_offset, unsigned int bit_count, unsigned int offset)
static LIBUSB_CALL void write_cb(struct libusb_transfer *transfer)
void mpsse_read_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t *data)
static bool device_location_equal(struct libusb_device *device, const char *location)
#define SIO_RESET_PURGE_TX
void mpsse_set_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
static void buffer_write_byte(struct mpsse_ctx *ctx, uint8_t data)
static LIBUSB_CALL void read_cb(struct libusb_transfer *transfer)
#define DEBUG_PRINT_BUF(buf, len)
void mpsse_set_divisor(struct mpsse_ctx *ctx, uint16_t divisor)
int mpsse_set_frequency(struct mpsse_ctx *ctx, int frequency)
void mpsse_read_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t *data)
#define SIO_RESET_REQUEST
static bool string_descriptor_equal(struct libusb_device_handle *device, uint8_t str_index, const char *string)
void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, bool tdi, uint8_t mode)
static void single_byte_boolean_helper(struct mpsse_ctx *ctx, bool var, uint8_t val_if_true, uint8_t val_if_false)
#define SIO_SET_LATENCY_TIMER_REQUEST
void mpsse_purge(struct mpsse_ctx *ctx)
void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, uint8_t mode)
void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
struct mpsse_ctx * mpsse_open(const uint16_t vids[], const uint16_t pids[], const char *description, const char *serial, const char *location, int channel)
bool mpsse_is_high_speed(struct mpsse_ctx *ctx)
static unsigned int buffer_write_space(struct mpsse_ctx *ctx)
static unsigned int buffer_write(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int bit_count)
void mpsse_close(struct mpsse_ctx *ctx)
void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
void mpsse_loopback_config(struct mpsse_ctx *ctx, bool enable)
void mpsse_set_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
int mpsse_divide_by_5_config(struct mpsse_ctx *ctx, bool enable)
int mpsse_rtck_config(struct mpsse_ctx *ctx, bool enable)
static unsigned int buffer_read_space(struct mpsse_ctx *ctx)
static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t vids[], const uint16_t pids[], const char *product, const char *serial, const char *location)
#define SIO_RESET_PURGE_RX
#define SIO_SET_BITMODE_REQUEST
#define FTDI_DEVICE_OUT_REQTYPE
struct libusb_device_handle * usb_dev
unsigned int read_chunk_size
unsigned int usb_write_timeout
unsigned int usb_read_timeout
struct bit_copy_queue read_queue
struct libusb_context * usb_ctx
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.