23 #define DEBUG_PRINT_BUF(buf, len) \
25 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) { \
26 char buf_string[32 * 3 + 1]; \
27 int buf_string_pos = 0; \
28 for (int i = 0; i < len; i++) { \
29 buf_string_pos += sprintf(buf_string + buf_string_pos, " %02x", buf[i]); \
30 if (i % 32 == 32 - 1) { \
31 LOG_DEBUG_IO("%s", buf_string); \
35 if (buf_string_pos > 0) \
36 LOG_DEBUG_IO("%s", buf_string);\
40 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
41 #define FTDI_DEVICE_IN_REQTYPE (0x80 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
43 #define BITMODE_MPSSE 0x02
45 #define SIO_RESET_REQUEST 0x00
46 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
47 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
48 #define SIO_SET_BITMODE_REQUEST 0x0B
50 #define SIO_RESET_SIO 0
51 #define SIO_RESET_PURGE_RX 1
52 #define SIO_RESET_PURGE_TX 2
82 char desc_string[256];
83 retval = libusb_get_string_descriptor_ascii(
device, str_index, (
unsigned char *)desc_string,
86 LOG_ERROR(
"libusb_get_string_descriptor_ascii() failed with %s", libusb_error_name(retval));
89 return strncmp(
string, desc_string,
sizeof(desc_string)) == 0;
95 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
96 char *loc = strdup(location);
98 int path_step, path_len;
99 uint8_t dev_bus = libusb_get_bus_number(
device);
102 path_len = libusb_get_port_numbers(
device, port_path, 7);
103 if (path_len == LIBUSB_ERROR_OVERFLOW) {
104 LOG_ERROR(
"cannot determine path to usb device! (more than 7 ports in path)");
108 LOG_DEBUG(
"device path has %i steps", path_len);
110 ptr = strtok(loc,
"-:");
115 if (atoi(ptr) != dev_bus) {
121 while (path_step < 7) {
122 ptr = strtok(
NULL,
".,");
124 LOG_DEBUG(
"no more tokens in path at step %i", path_step);
128 if (path_step < path_len
129 && atoi(ptr) != port_path[path_step]) {
130 LOG_DEBUG(
"path mismatch at step %i", path_step);
138 if (path_step == path_len)
152 const char *product,
const char *
serial,
const char *location)
154 struct libusb_device **list;
155 struct libusb_device_descriptor desc;
156 struct libusb_config_descriptor *config0;
159 ssize_t cnt = libusb_get_device_list(ctx->
usb_ctx, &list);
161 LOG_ERROR(
"libusb_get_device_list() failed with %s", libusb_error_name(cnt));
163 for (ssize_t i = 0; i < cnt; i++) {
164 struct libusb_device *
device = list[i];
166 err = libusb_get_device_descriptor(
device, &desc);
167 if (err != LIBUSB_SUCCESS) {
168 LOG_ERROR(
"libusb_get_device_descriptor() failed with %s", libusb_error_name(err));
172 if (vid && *vid != desc.idVendor)
174 if (pid && *pid != desc.idProduct)
178 if (err != LIBUSB_SUCCESS) {
179 LOG_ERROR(
"libusb_open() failed with %s",
180 libusb_error_name(err));
203 libusb_free_device_list(list, 1);
210 err = libusb_get_config_descriptor(libusb_get_device(ctx->
usb_dev), 0, &config0);
211 if (err != LIBUSB_SUCCESS) {
212 LOG_ERROR(
"libusb_get_config_descriptor() failed with %s", libusb_error_name(err));
219 err = libusb_get_configuration(ctx->
usb_dev, &cfg);
220 if (err != LIBUSB_SUCCESS) {
221 LOG_ERROR(
"libusb_get_configuration() failed with %s", libusb_error_name(err));
225 if (desc.bNumConfigurations > 0 && cfg != config0->bConfigurationValue) {
226 err = libusb_set_configuration(ctx->
usb_dev, config0->bConfigurationValue);
227 if (err != LIBUSB_SUCCESS) {
228 LOG_ERROR(
"libusb_set_configuration() failed with %s", libusb_error_name(err));
235 if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_NOT_FOUND
236 && err != LIBUSB_ERROR_NOT_SUPPORTED) {
237 LOG_WARNING(
"libusb_detach_kernel_driver() failed with %s, trying to continue anyway",
238 libusb_error_name(err));
242 if (err != LIBUSB_SUCCESS) {
243 LOG_ERROR(
"libusb_claim_interface() failed with %s", libusb_error_name(err));
252 LOG_ERROR(
"failed to reset FTDI device: %s", libusb_error_name(err));
256 switch (desc.bcdDevice) {
270 LOG_ERROR(
"unsupported FTDI chip type: 0x%04x", desc.bcdDevice);
275 if (!(desc.bNumConfigurations > 0 && ctx->
interface < config0->bNumInterfaces
276 && config0->interface[ctx->
interface].num_altsetting > 0))
279 const struct libusb_interface_descriptor *descriptor;
280 descriptor = &config0->interface[ctx->
interface].altsetting[0];
281 if (descriptor->bNumEndpoints != 2)
286 for (
int i = 0; i < descriptor->bNumEndpoints; i++) {
287 if (descriptor->endpoint[i].bEndpointAddress & 0x80) {
288 ctx->
in_ep = descriptor->endpoint[i].bEndpointAddress;
290 descriptor->endpoint[i].wMaxPacketSize;
292 ctx->
out_ep = descriptor->endpoint[i].bEndpointAddress;
299 libusb_free_config_descriptor(config0);
303 LOG_ERROR(
"unrecognized USB device descriptor");
305 libusb_free_config_descriptor(config0);
311 const char *
serial,
const char *location,
int channel)
313 struct mpsse_ctx *ctx = calloc(1,
sizeof(*ctx));
336 ctx->
index = channel + 1;
340 err = libusb_init(&ctx->
usb_ctx);
341 if (err != LIBUSB_SUCCESS) {
342 LOG_ERROR(
"libusb_init() failed with %s", libusb_error_name(err));
350 LOG_ERROR(
"unable to open ftdi device with vid %s, pid %s, description '%s', "
351 "serial '%s' at bus location '%s'",
352 vid ? sprintf(vidstr,
"%04x", *vid), vidstr :
"*",
353 pid ? sprintf(pidstr,
"%04x", *pid), pidstr :
"*",
356 location ? location :
"*");
365 LOG_ERROR(
"unable to set latency timer: %s", libusb_error_name(err));
369 err = libusb_control_transfer(ctx->
usb_dev,
378 LOG_ERROR(
"unable to set MPSSE bitmode: %s", libusb_error_name(err));
420 LOG_ERROR(
"unable to purge ftdi rx buffers: %s", libusb_error_name(err));
427 LOG_ERROR(
"unable to purge ftdi tx buffers: %s", libusb_error_name(err));
461 unsigned bit_count,
unsigned offset)
484 unsigned in_offset,
unsigned length, uint8_t
mode)
495 if (out || (!out && !in))
519 unsigned this_bytes =
length / 8;
521 if (this_bytes > 65536)
530 if (this_bytes > 0) {
546 for (
unsigned n = 0; n < this_bytes; n++)
561 unsigned in_offset,
unsigned length,
bool tdi, uint8_t
mode)
581 unsigned this_bits =
length;
597 bit_copy(&data, 0, out, out_offset, this_bits);
598 out_offset += this_bits;
678 uint8_t val_if_false)
739 assert(frequency >= 0);
748 base_clock = 60000000;
751 base_clock = 12000000;
754 int divisor = (base_clock / 2 + frequency - 1) / frequency - 1;
757 assert(divisor >= 0);
761 frequency = base_clock / 2 / (1 + divisor);
785 unsigned num_packets =
DIV_ROUND_UP(transfer->actual_length, packet_size);
786 unsigned chunk_remains = transfer->actual_length;
787 for (
unsigned i = 0; i < num_packets && chunk_remains > 2; i++) {
788 unsigned this_size = packet_size - 2;
789 if (this_size > chunk_remains - 2)
790 this_size = chunk_remains - 2;
797 chunk_remains -= this_size + 2;
808 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
828 if (libusb_submit_transfer(transfer) != LIBUSB_SUCCESS)
851 struct libusb_transfer *read_transfer = 0;
855 read_result.
done =
false;
861 struct libusb_transfer *write_transfer = libusb_alloc_transfer(0);
864 retval = libusb_submit_transfer(write_transfer);
865 if (retval != LIBUSB_SUCCESS)
869 read_transfer = libusb_alloc_transfer(0);
873 retval = libusb_submit_transfer(read_transfer);
874 if (retval != LIBUSB_SUCCESS)
880 int64_t warn_after = 2000;
881 while (!write_result.
done || !read_result.
done) {
887 retval = libusb_handle_events_timeout_completed(ctx->
usb_ctx, &timeout_usb,
NULL);
889 if (retval == LIBUSB_ERROR_NO_DEVICE || retval == LIBUSB_ERROR_INTERRUPTED)
892 if (retval != LIBUSB_SUCCESS) {
893 libusb_cancel_transfer(write_transfer);
895 libusb_cancel_transfer(read_transfer);
896 while (!write_result.
done || !read_result.
done) {
897 retval = libusb_handle_events_timeout_completed(ctx->
usb_ctx,
899 if (retval != LIBUSB_SUCCESS)
905 if (now -
start > warn_after) {
906 LOG_WARNING(
"Haven't made progress in mpsse_flush() for %" PRId64
913 if (retval != LIBUSB_SUCCESS) {
914 LOG_ERROR(
"libusb_handle_events() failed with %s", libusb_error_name(retval));
917 LOG_ERROR(
"ftdi device did not accept all data: %d, tried %d",
922 LOG_ERROR(
"ftdi device did not return all data: %d, expected %d",
940 libusb_free_transfer(write_transfer);
942 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)
int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned dst_offset, const uint8_t *src, unsigned src_offset, unsigned bit_count)
void bit_copy_execute(struct bit_copy_queue *q)
static void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src, unsigned src_offset, unsigned bit_count)
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
int mpsse_flush(struct mpsse_ctx *ctx)
void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, unsigned length, uint8_t mode)
static unsigned buffer_write(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, unsigned bit_count)
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
static unsigned buffer_read_space(struct mpsse_ctx *ctx)
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 unsigned buffer_add_read(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_offset, unsigned bit_count, unsigned offset)
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)
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)
struct mpsse_ctx * mpsse_open(const uint16_t *vid, const uint16_t *pid, const char *description, const char *serial, const char *location, int channel)
void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_offset, unsigned length, uint8_t mode)
bool mpsse_is_high_speed(struct mpsse_ctx *ctx)
void mpsse_close(struct mpsse_ctx *ctx)
void mpsse_loopback_config(struct mpsse_ctx *ctx, bool enable)
void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in, unsigned in_offset, unsigned length, bool tdi, uint8_t mode)
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)
static unsigned buffer_write_space(struct mpsse_ctx *ctx)
int mpsse_rtck_config(struct mpsse_ctx *ctx, bool enable)
void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, unsigned length, bool tdi, uint8_t mode)
void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in, unsigned in_offset, unsigned length, uint8_t mode)
#define SIO_RESET_PURGE_RX
static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, const uint16_t *pid, const char *product, const char *serial, const char *location)
#define SIO_SET_BITMODE_REQUEST
#define FTDI_DEVICE_OUT_REQTYPE
struct libusb_device_handle * usb_dev
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.