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
85 char desc_string[256];
86 retval = libusb_get_string_descriptor_ascii(
device, str_index, (
unsigned char *)desc_string,
89 LOG_ERROR(
"libusb_get_string_descriptor_ascii() failed with %s", libusb_error_name(retval));
92 return strncmp(
string, desc_string,
sizeof(desc_string)) == 0;
98 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
99 char *loc = strdup(location);
100 uint8_t port_path[7];
101 int path_step, path_len;
102 uint8_t dev_bus = libusb_get_bus_number(
device);
105 path_len = libusb_get_port_numbers(
device, port_path, 7);
106 if (path_len == LIBUSB_ERROR_OVERFLOW) {
107 LOG_ERROR(
"cannot determine path to usb device! (more than 7 ports in path)");
111 LOG_DEBUG(
"device path has %i steps", path_len);
113 ptr = strtok(loc,
"-:");
118 if (atoi(ptr) != dev_bus) {
124 while (path_step < 7) {
125 ptr = strtok(
NULL,
".,");
127 LOG_DEBUG(
"no more tokens in path at step %i", path_step);
131 if (path_step < path_len
132 && atoi(ptr) != port_path[path_step]) {
133 LOG_DEBUG(
"path mismatch at step %i", path_step);
141 if (path_step == path_len)
155 const char *product,
const char *
serial,
const char *location)
157 struct libusb_device **list;
158 struct libusb_device_descriptor desc;
159 struct libusb_config_descriptor *config0;
162 ssize_t cnt = libusb_get_device_list(ctx->
usb_ctx, &list);
164 LOG_ERROR(
"libusb_get_device_list() failed with %s", libusb_error_name(cnt));
166 for (ssize_t i = 0; i < cnt; i++) {
167 struct libusb_device *
device = list[i];
169 err = libusb_get_device_descriptor(
device, &desc);
170 if (err != LIBUSB_SUCCESS) {
171 LOG_ERROR(
"libusb_get_device_descriptor() failed with %s", libusb_error_name(err));
179 if (err != LIBUSB_SUCCESS) {
180 LOG_ERROR(
"libusb_open() failed with %s",
181 libusb_error_name(err));
204 libusb_free_device_list(list, 1);
211 err = libusb_get_config_descriptor(libusb_get_device(ctx->
usb_dev), 0, &config0);
212 if (err != LIBUSB_SUCCESS) {
213 LOG_ERROR(
"libusb_get_config_descriptor() failed with %s", libusb_error_name(err));
220 err = libusb_get_configuration(ctx->
usb_dev, &cfg);
221 if (err != LIBUSB_SUCCESS) {
222 LOG_ERROR(
"libusb_get_configuration() failed with %s", libusb_error_name(err));
226 if (desc.bNumConfigurations > 0 && cfg != config0->bConfigurationValue) {
227 err = libusb_set_configuration(ctx->
usb_dev, config0->bConfigurationValue);
228 if (err != LIBUSB_SUCCESS) {
229 LOG_ERROR(
"libusb_set_configuration() failed with %s", libusb_error_name(err));
236 if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_NOT_FOUND
237 && err != LIBUSB_ERROR_NOT_SUPPORTED) {
238 LOG_WARNING(
"libusb_detach_kernel_driver() failed with %s, trying to continue anyway",
239 libusb_error_name(err));
243 if (err != LIBUSB_SUCCESS) {
244 LOG_ERROR(
"libusb_claim_interface() failed with %s", libusb_error_name(err));
253 LOG_ERROR(
"failed to reset FTDI device: %s", libusb_error_name(err));
257 switch (desc.bcdDevice) {
292 LOG_ERROR(
"unsupported FTDI chip type: 0x%04x", desc.bcdDevice);
297 if (!(desc.bNumConfigurations > 0 && ctx->
interface < config0->bNumInterfaces
298 && config0->interface[ctx->
interface].num_altsetting > 0))
301 const struct libusb_interface_descriptor *descriptor;
302 descriptor = &config0->interface[ctx->
interface].altsetting[0];
303 if (descriptor->bNumEndpoints != 2)
308 for (
int i = 0; i < descriptor->bNumEndpoints; i++) {
309 if (descriptor->endpoint[i].bEndpointAddress & 0x80) {
310 ctx->
in_ep = descriptor->endpoint[i].bEndpointAddress;
312 descriptor->endpoint[i].wMaxPacketSize;
314 ctx->
out_ep = descriptor->endpoint[i].bEndpointAddress;
321 libusb_free_config_descriptor(config0);
325 LOG_ERROR(
"unrecognized USB device descriptor");
327 libusb_free_config_descriptor(config0);
333 const char *
serial,
const char *location,
int channel)
335 struct mpsse_ctx *ctx = calloc(1,
sizeof(*ctx));
358 ctx->
index = channel + 1;
362 err = libusb_init(&ctx->
usb_ctx);
363 if (err != LIBUSB_SUCCESS) {
364 LOG_ERROR(
"libusb_init() failed with %s", libusb_error_name(err));
369 LOG_ERROR(
"unable to open ftdi device with description '%s', "
370 "serial '%s' at bus location '%s'",
373 location ? location :
"*");
382 LOG_ERROR(
"unable to set latency timer: %s", libusb_error_name(err));
386 err = libusb_control_transfer(ctx->
usb_dev,
395 LOG_ERROR(
"unable to set MPSSE bitmode: %s", libusb_error_name(err));
437 LOG_ERROR(
"unable to purge ftdi rx buffers: %s", libusb_error_name(err));
444 LOG_ERROR(
"unable to purge ftdi tx buffers: %s", libusb_error_name(err));
468 unsigned int bit_count)
478 unsigned int bit_count,
unsigned int offset)
501 unsigned int in_offset,
unsigned int length, uint8_t
mode)
512 if (out || (!out && !in))
536 unsigned int this_bytes =
length / 8;
538 if (this_bytes > 65536)
547 if (this_bytes > 0) {
563 for (
unsigned int n = 0; n < this_bytes; n++)
578 unsigned int in_offset,
unsigned int length,
bool tdi, uint8_t
mode)
598 unsigned int this_bits =
length;
614 bit_copy(&data, 0, out, out_offset, this_bits);
615 out_offset += this_bits;
695 uint8_t val_if_false)
756 assert(frequency >= 0);
765 base_clock = 60000000;
768 base_clock = 12000000;
771 int divisor = (base_clock / 2 + frequency - 1) / frequency - 1;
774 assert(divisor >= 0);
778 frequency = base_clock / 2 / (1 + divisor);
802 unsigned int num_packets =
DIV_ROUND_UP(transfer->actual_length, packet_size);
803 unsigned int chunk_remains = transfer->actual_length;
804 for (
unsigned int i = 0; i < num_packets && chunk_remains > 2; i++) {
805 unsigned int this_size = packet_size - 2;
806 if (this_size > chunk_remains - 2)
807 this_size = chunk_remains - 2;
814 chunk_remains -= this_size + 2;
825 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
845 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
868 struct libusb_transfer *read_transfer =
NULL;
872 read_result.
done =
false;
878 struct libusb_transfer *write_transfer = libusb_alloc_transfer(0);
881 retval = libusb_submit_transfer(write_transfer);
882 if (retval != LIBUSB_SUCCESS)
886 read_transfer = libusb_alloc_transfer(0);
890 retval = libusb_submit_transfer(read_transfer);
891 if (retval != LIBUSB_SUCCESS)
897 int64_t warn_after = 2000;
898 while (!write_result.
done || !read_result.
done) {
904 retval = libusb_handle_events_timeout_completed(ctx->
usb_ctx, &timeout_usb,
NULL);
908 if (now -
start > warn_after) {
909 LOG_WARNING(
"Haven't made progress in mpsse_flush() for %" PRId64
914 if (retval == LIBUSB_ERROR_INTERRUPTED)
917 if (retval != LIBUSB_SUCCESS) {
918 libusb_cancel_transfer(write_transfer);
920 libusb_cancel_transfer(read_transfer);
925 if (retval != LIBUSB_SUCCESS) {
926 LOG_ERROR(
"libusb_handle_events() failed with %s", libusb_error_name(retval));
929 LOG_ERROR(
"ftdi device did not accept all data: %d, tried %d",
934 LOG_ERROR(
"ftdi device did not return all data: %d, expected %d",
952 libusb_free_transfer(write_transfer);
954 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 void mpsse_set_divisor(struct mpsse_ctx *ctx, uint16_t divisor)
static int mpsse_rtck_config(struct mpsse_ctx *ctx, bool enable)
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)
static void mpsse_purge(struct mpsse_ctx *ctx)
#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)
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_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)
static int mpsse_divide_by_5_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.