1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /**************************************************************************
4 * Copyright (C) 2012 by Andreas Fritiofson *
5 * andreas.fritiofson@gmail.com *
6 ***************************************************************************/
13 #include "helper/log.h"
14 #include "helper/replacements.h"
15 #include "helper/time_support.h"
16 #include "libusb_helper.h"
19 /* Compatibility define for older libusb-1.0 */
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
56 struct libusb_context
*usb_ctx
;
57 struct libusb_device_handle
*usb_dev
;
58 unsigned int usb_write_timeout
;
59 unsigned int usb_read_timeout
;
62 uint16_t max_packet_size
;
65 enum ftdi_chip_type type
;
66 uint8_t *write_buffer
;
73 unsigned read_chunk_size
;
74 struct bit_copy_queue read_queue
;
78 /* Returns true if the string descriptor indexed by str_index in device matches string */
79 static bool string_descriptor_equal(struct libusb_device_handle
*device
, uint8_t str_index
,
83 char desc_string
[256]; /* Max size of string descriptor */
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;
93 static bool device_location_equal(struct libusb_device
*device
, const char *location
)
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
, "-:");
113 LOG_DEBUG("no ':' in path");
116 if (atoi(ptr
) != dev_bus
) {
117 LOG_DEBUG("bus mismatch");
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
);
138 /* walked the full path, all elements match */
139 if (path_step
== path_len
)
148 /* Helper to open a libusb device that matches vid, pid, product string and/or serial string.
149 * Set any field to 0 as a wildcard. If the device is found true is returned, with ctx containing
150 * the already opened handle. ctx->interface must be set to the desired interface (channel) number
151 * prior to calling this function. */
152 static bool open_matching_device(struct mpsse_ctx
*ctx
, const uint16_t vids
[], const uint16_t pids
[],
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
));
173 if (!jtag_libusb_match_ids(&desc
, vids
, pids
))
176 err
= libusb_open(device
, &ctx
->usb_dev
);
177 if (err
!= LIBUSB_SUCCESS
) {
178 LOG_ERROR("libusb_open() failed with %s",
179 libusb_error_name(err
));
183 if (location
&& !device_location_equal(device
, location
)) {
184 libusb_close(ctx
->usb_dev
);
188 if (product
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iProduct
, product
)) {
189 libusb_close(ctx
->usb_dev
);
193 if (serial
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iSerialNumber
, serial
)) {
194 libusb_close(ctx
->usb_dev
);
202 libusb_free_device_list(list
, 1);
205 /* The caller reports detailed error desc */
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
));
212 libusb_close(ctx
->usb_dev
);
216 /* Make sure the first configuration is selected */
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
));
232 /* Try to detach ftdi_sio kernel module */
233 err
= libusb_detach_kernel_driver(ctx
->usb_dev
, ctx
->interface
);
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
));
240 err
= libusb_claim_interface(ctx
->usb_dev
, ctx
->interface
);
241 if (err
!= LIBUSB_SUCCESS
) {
242 LOG_ERROR("libusb_claim_interface() failed with %s", libusb_error_name(err
));
246 /* Reset FTDI device */
247 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
248 SIO_RESET_REQUEST
, SIO_RESET_SIO
,
249 ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
251 LOG_ERROR("failed to reset FTDI device: %s", libusb_error_name(err
));
255 switch (desc
.bcdDevice
) {
257 ctx
->type
= TYPE_FT2232C
;
260 ctx
->type
= TYPE_FT2232H
;
263 ctx
->type
= TYPE_FT4232H
;
266 ctx
->type
= TYPE_FT232H
;
269 ctx
->type
= TYPE_FT2233HP
;
272 ctx
->type
= TYPE_FT4233HP
;
275 ctx
->type
= TYPE_FT2232HP
;
278 ctx
->type
= TYPE_FT4232HP
;
281 ctx
->type
= TYPE_FT233HP
;
284 ctx
->type
= TYPE_FT232HP
;
287 LOG_ERROR("unsupported FTDI chip type: 0x%04x", desc
.bcdDevice
);
291 /* Determine maximum packet size and endpoint addresses */
292 if (!(desc
.bNumConfigurations
> 0 && ctx
->interface
< config0
->bNumInterfaces
293 && config0
->interface
[ctx
->interface
].num_altsetting
> 0))
296 const struct libusb_interface_descriptor
*descriptor
;
297 descriptor
= &config0
->interface
[ctx
->interface
].altsetting
[0];
298 if (descriptor
->bNumEndpoints
!= 2)
303 for (int i
= 0; i
< descriptor
->bNumEndpoints
; i
++) {
304 if (descriptor
->endpoint
[i
].bEndpointAddress
& 0x80) {
305 ctx
->in_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
306 ctx
->max_packet_size
=
307 descriptor
->endpoint
[i
].wMaxPacketSize
;
309 ctx
->out_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
313 if (ctx
->in_ep
== 0 || ctx
->out_ep
== 0)
316 libusb_free_config_descriptor(config0
);
320 LOG_ERROR("unrecognized USB device descriptor");
322 libusb_free_config_descriptor(config0
);
323 libusb_close(ctx
->usb_dev
);
327 struct mpsse_ctx
*mpsse_open(const uint16_t vids
[], const uint16_t pids
[], const char *description
,
328 const char *serial
, const char *location
, int channel
)
330 struct mpsse_ctx
*ctx
= calloc(1, sizeof(*ctx
));
336 bit_copy_queue_init(&ctx
->read_queue
);
337 ctx
->read_chunk_size
= 16384;
338 ctx
->read_size
= 16384;
339 ctx
->write_size
= 16384;
340 ctx
->read_chunk
= malloc(ctx
->read_chunk_size
);
341 ctx
->read_buffer
= malloc(ctx
->read_size
);
343 /* Use calloc to make valgrind happy: buffer_write() sets payload
344 * on bit basis, so some bits can be left uninitialized in write_buffer.
345 * Although this is perfectly ok with MPSSE, valgrind reports
346 * Syscall param ioctl(USBDEVFS_SUBMITURB).buffer points to uninitialised byte(s) */
347 ctx
->write_buffer
= calloc(1, ctx
->write_size
);
349 if (!ctx
->read_chunk
|| !ctx
->read_buffer
|| !ctx
->write_buffer
)
352 ctx
->interface
= channel
;
353 ctx
->index
= channel
+ 1;
354 ctx
->usb_read_timeout
= 5000;
355 ctx
->usb_write_timeout
= 5000;
357 err
= libusb_init(&ctx
->usb_ctx
);
358 if (err
!= LIBUSB_SUCCESS
) {
359 LOG_ERROR("libusb_init() failed with %s", libusb_error_name(err
));
363 if (!open_matching_device(ctx
, vids
, pids
, description
, serial
, location
)) {
364 LOG_ERROR("unable to open ftdi device with description '%s', "
365 "serial '%s' at bus location '%s'",
366 description
? description
: "*",
367 serial
? serial
: "*",
368 location
? location
: "*");
373 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
374 SIO_SET_LATENCY_TIMER_REQUEST
, 255, ctx
->index
, NULL
, 0,
375 ctx
->usb_write_timeout
);
377 LOG_ERROR("unable to set latency timer: %s", libusb_error_name(err
));
381 err
= libusb_control_transfer(ctx
->usb_dev
,
382 FTDI_DEVICE_OUT_REQTYPE
,
383 SIO_SET_BITMODE_REQUEST
,
384 0x0b | (BITMODE_MPSSE
<< 8),
388 ctx
->usb_write_timeout
);
390 LOG_ERROR("unable to set MPSSE bitmode: %s", libusb_error_name(err
));
402 void mpsse_close(struct mpsse_ctx
*ctx
)
405 libusb_close(ctx
->usb_dev
);
407 libusb_exit(ctx
->usb_ctx
);
408 bit_copy_discard(&ctx
->read_queue
);
410 free(ctx
->write_buffer
);
411 free(ctx
->read_buffer
);
412 free(ctx
->read_chunk
);
416 bool mpsse_is_high_speed(struct mpsse_ctx
*ctx
)
418 return ctx
->type
!= TYPE_FT2232C
;
421 void mpsse_purge(struct mpsse_ctx
*ctx
)
425 ctx
->write_count
= 0;
427 ctx
->retval
= ERROR_OK
;
428 bit_copy_discard(&ctx
->read_queue
);
429 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
430 SIO_RESET_PURGE_RX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
432 LOG_ERROR("unable to purge ftdi rx buffers: %s", libusb_error_name(err
));
436 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
437 SIO_RESET_PURGE_TX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
439 LOG_ERROR("unable to purge ftdi tx buffers: %s", libusb_error_name(err
));
444 static unsigned buffer_write_space(struct mpsse_ctx
*ctx
)
446 /* Reserve one byte for SEND_IMMEDIATE */
447 return ctx
->write_size
- ctx
->write_count
- 1;
450 static unsigned buffer_read_space(struct mpsse_ctx
*ctx
)
452 return ctx
->read_size
- ctx
->read_count
;
455 static void buffer_write_byte(struct mpsse_ctx
*ctx
, uint8_t data
)
457 LOG_DEBUG_IO("%02x", data
);
458 assert(ctx
->write_count
< ctx
->write_size
);
459 ctx
->write_buffer
[ctx
->write_count
++] = data
;
462 static unsigned buffer_write(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
465 LOG_DEBUG_IO("%d bits", bit_count
);
466 assert(ctx
->write_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->write_size
);
467 bit_copy(ctx
->write_buffer
+ ctx
->write_count
, 0, out
, out_offset
, bit_count
);
468 ctx
->write_count
+= DIV_ROUND_UP(bit_count
, 8);
472 static unsigned buffer_add_read(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
,
473 unsigned bit_count
, unsigned offset
)
475 LOG_DEBUG_IO("%d bits, offset %d", bit_count
, offset
);
476 assert(ctx
->read_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->read_size
);
477 bit_copy_queued(&ctx
->read_queue
, in
, in_offset
, ctx
->read_buffer
+ ctx
->read_count
, offset
,
479 ctx
->read_count
+= DIV_ROUND_UP(bit_count
, 8);
483 void mpsse_clock_data_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
484 unsigned length
, uint8_t mode
)
486 mpsse_clock_data(ctx
, out
, out_offset
, NULL
, 0, length
, mode
);
489 void mpsse_clock_data_in(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
, unsigned length
,
492 mpsse_clock_data(ctx
, NULL
, 0, in
, in_offset
, length
, mode
);
495 void mpsse_clock_data(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
496 unsigned in_offset
, unsigned length
, uint8_t mode
)
498 /* TODO: Fix MSB first modes */
499 LOG_DEBUG_IO("%s%s %d bits", in
? "in" : "", out
? "out" : "", length
);
501 if (ctx
->retval
!= ERROR_OK
) {
502 LOG_DEBUG_IO("Ignoring command due to previous error");
506 /* TODO: On H chips, use command 0x8E/0x8F if in and out are both 0 */
507 if (out
|| (!out
&& !in
))
513 /* Guarantee buffer space enough for a minimum size transfer */
514 if (buffer_write_space(ctx
) + (length
< 8) < (out
|| (!out
&& !in
) ? 4 : 3)
515 || (in
&& buffer_read_space(ctx
) < 1))
516 ctx
->retval
= mpsse_flush(ctx
);
519 /* Transfer remaining bits in bit mode */
520 buffer_write_byte(ctx
, 0x02 | mode
);
521 buffer_write_byte(ctx
, length
- 1);
523 out_offset
+= buffer_write(ctx
, out
, out_offset
, length
);
525 in_offset
+= buffer_add_read(ctx
, in
, in_offset
, length
, 8 - length
);
527 buffer_write_byte(ctx
, 0x00);
531 unsigned this_bytes
= length
/ 8;
532 /* MPSSE command limit */
533 if (this_bytes
> 65536)
535 /* Buffer space limit. We already made sure there's space for the minimum
537 if ((out
|| (!out
&& !in
)) && this_bytes
+ 3 > buffer_write_space(ctx
))
538 this_bytes
= buffer_write_space(ctx
) - 3;
539 if (in
&& this_bytes
> buffer_read_space(ctx
))
540 this_bytes
= buffer_read_space(ctx
);
542 if (this_bytes
> 0) {
543 buffer_write_byte(ctx
, mode
);
544 buffer_write_byte(ctx
, (this_bytes
- 1) & 0xff);
545 buffer_write_byte(ctx
, (this_bytes
- 1) >> 8);
547 out_offset
+= buffer_write(ctx
,
552 in_offset
+= buffer_add_read(ctx
,
558 for (unsigned n
= 0; n
< this_bytes
; n
++)
559 buffer_write_byte(ctx
, 0x00);
560 length
-= this_bytes
* 8;
566 void mpsse_clock_tms_cs_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
567 unsigned length
, bool tdi
, uint8_t mode
)
569 mpsse_clock_tms_cs(ctx
, out
, out_offset
, NULL
, 0, length
, tdi
, mode
);
572 void mpsse_clock_tms_cs(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
573 unsigned in_offset
, unsigned length
, bool tdi
, uint8_t mode
)
575 LOG_DEBUG_IO("%sout %d bits, tdi=%d", in
? "in" : "", length
, tdi
);
578 if (ctx
->retval
!= ERROR_OK
) {
579 LOG_DEBUG_IO("Ignoring command due to previous error");
588 /* Guarantee buffer space enough for a minimum size transfer */
589 if (buffer_write_space(ctx
) < 3 || (in
&& buffer_read_space(ctx
) < 1))
590 ctx
->retval
= mpsse_flush(ctx
);
593 unsigned this_bits
= length
;
594 /* MPSSE command limit */
595 /* NOTE: there's a report of an FT2232 bug in this area, where shifting
596 * exactly 7 bits can make problems with TMS signaling for the last
599 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
605 buffer_write_byte(ctx
, mode
);
606 buffer_write_byte(ctx
, this_bits
- 1);
608 /* TODO: Fix MSB first, if allowed in MPSSE */
609 bit_copy(&data
, 0, out
, out_offset
, this_bits
);
610 out_offset
+= this_bits
;
611 buffer_write_byte(ctx
, data
| (tdi
? 0x80 : 0x00));
613 in_offset
+= buffer_add_read(ctx
,
623 void mpsse_set_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
627 if (ctx
->retval
!= ERROR_OK
) {
628 LOG_DEBUG_IO("Ignoring command due to previous error");
632 if (buffer_write_space(ctx
) < 3)
633 ctx
->retval
= mpsse_flush(ctx
);
635 buffer_write_byte(ctx
, 0x80);
636 buffer_write_byte(ctx
, data
);
637 buffer_write_byte(ctx
, dir
);
640 void mpsse_set_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
644 if (ctx
->retval
!= ERROR_OK
) {
645 LOG_DEBUG_IO("Ignoring command due to previous error");
649 if (buffer_write_space(ctx
) < 3)
650 ctx
->retval
= mpsse_flush(ctx
);
652 buffer_write_byte(ctx
, 0x82);
653 buffer_write_byte(ctx
, data
);
654 buffer_write_byte(ctx
, dir
);
657 void mpsse_read_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
661 if (ctx
->retval
!= ERROR_OK
) {
662 LOG_DEBUG_IO("Ignoring command due to previous error");
666 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
667 ctx
->retval
= mpsse_flush(ctx
);
669 buffer_write_byte(ctx
, 0x81);
670 buffer_add_read(ctx
, data
, 0, 8, 0);
673 void mpsse_read_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
677 if (ctx
->retval
!= ERROR_OK
) {
678 LOG_DEBUG_IO("Ignoring command due to previous error");
682 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
683 ctx
->retval
= mpsse_flush(ctx
);
685 buffer_write_byte(ctx
, 0x83);
686 buffer_add_read(ctx
, data
, 0, 8, 0);
689 static void single_byte_boolean_helper(struct mpsse_ctx
*ctx
, bool var
, uint8_t val_if_true
,
690 uint8_t val_if_false
)
692 if (ctx
->retval
!= ERROR_OK
) {
693 LOG_DEBUG_IO("Ignoring command due to previous error");
697 if (buffer_write_space(ctx
) < 1)
698 ctx
->retval
= mpsse_flush(ctx
);
700 buffer_write_byte(ctx
, var
? val_if_true
: val_if_false
);
703 void mpsse_loopback_config(struct mpsse_ctx
*ctx
, bool enable
)
705 LOG_DEBUG("%s", enable
? "on" : "off");
706 single_byte_boolean_helper(ctx
, enable
, 0x84, 0x85);
709 void mpsse_set_divisor(struct mpsse_ctx
*ctx
, uint16_t divisor
)
711 LOG_DEBUG("%d", divisor
);
713 if (ctx
->retval
!= ERROR_OK
) {
714 LOG_DEBUG_IO("Ignoring command due to previous error");
718 if (buffer_write_space(ctx
) < 3)
719 ctx
->retval
= mpsse_flush(ctx
);
721 buffer_write_byte(ctx
, 0x86);
722 buffer_write_byte(ctx
, divisor
& 0xff);
723 buffer_write_byte(ctx
, divisor
>> 8);
726 int mpsse_divide_by_5_config(struct mpsse_ctx
*ctx
, bool enable
)
728 if (!mpsse_is_high_speed(ctx
))
731 LOG_DEBUG("%s", enable
? "on" : "off");
732 single_byte_boolean_helper(ctx
, enable
, 0x8b, 0x8a);
737 int mpsse_rtck_config(struct mpsse_ctx
*ctx
, bool enable
)
739 if (!mpsse_is_high_speed(ctx
))
742 LOG_DEBUG("%s", enable
? "on" : "off");
743 single_byte_boolean_helper(ctx
, enable
, 0x96, 0x97);
748 int mpsse_set_frequency(struct mpsse_ctx
*ctx
, int frequency
)
750 LOG_DEBUG("target %d Hz", frequency
);
751 assert(frequency
>= 0);
755 return mpsse_rtck_config(ctx
, true);
757 mpsse_rtck_config(ctx
, false); /* just try */
759 if (frequency
> 60000000 / 2 / 65536 && mpsse_divide_by_5_config(ctx
, false) == ERROR_OK
) {
760 base_clock
= 60000000;
762 mpsse_divide_by_5_config(ctx
, true); /* just try */
763 base_clock
= 12000000;
766 int divisor
= (base_clock
/ 2 + frequency
- 1) / frequency
- 1;
769 assert(divisor
>= 0);
771 mpsse_set_divisor(ctx
, divisor
);
773 frequency
= base_clock
/ 2 / (1 + divisor
);
774 LOG_DEBUG("actually %d Hz", frequency
);
779 /* Context needed by the callbacks */
780 struct transfer_result
{
781 struct mpsse_ctx
*ctx
;
783 unsigned transferred
;
786 static LIBUSB_CALL
void read_cb(struct libusb_transfer
*transfer
)
788 struct transfer_result
*res
= transfer
->user_data
;
789 struct mpsse_ctx
*ctx
= res
->ctx
;
791 unsigned packet_size
= ctx
->max_packet_size
;
793 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
795 /* Strip the two status bytes sent at the beginning of each USB packet
796 * while copying the chunk buffer to the read buffer */
797 unsigned num_packets
= DIV_ROUND_UP(transfer
->actual_length
, packet_size
);
798 unsigned chunk_remains
= transfer
->actual_length
;
799 for (unsigned i
= 0; i
< num_packets
&& chunk_remains
> 2; i
++) {
800 unsigned this_size
= packet_size
- 2;
801 if (this_size
> chunk_remains
- 2)
802 this_size
= chunk_remains
- 2;
803 if (this_size
> ctx
->read_count
- res
->transferred
)
804 this_size
= ctx
->read_count
- res
->transferred
;
805 memcpy(ctx
->read_buffer
+ res
->transferred
,
806 ctx
->read_chunk
+ packet_size
* i
+ 2,
808 res
->transferred
+= this_size
;
809 chunk_remains
-= this_size
+ 2;
810 if (res
->transferred
== ctx
->read_count
) {
816 LOG_DEBUG_IO("raw chunk %d, transferred %d of %d", transfer
->actual_length
, res
->transferred
,
820 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
824 static LIBUSB_CALL
void write_cb(struct libusb_transfer
*transfer
)
826 struct transfer_result
*res
= transfer
->user_data
;
827 struct mpsse_ctx
*ctx
= res
->ctx
;
829 res
->transferred
+= transfer
->actual_length
;
831 LOG_DEBUG_IO("transferred %d of %d", res
->transferred
, ctx
->write_count
);
833 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
835 if (res
->transferred
== ctx
->write_count
)
838 transfer
->length
= ctx
->write_count
- res
->transferred
;
839 transfer
->buffer
= ctx
->write_buffer
+ res
->transferred
;
840 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
845 int mpsse_flush(struct mpsse_ctx
*ctx
)
847 int retval
= ctx
->retval
;
849 if (retval
!= ERROR_OK
) {
850 LOG_DEBUG_IO("Ignoring flush due to previous error");
851 assert(ctx
->write_count
== 0 && ctx
->read_count
== 0);
852 ctx
->retval
= ERROR_OK
;
856 LOG_DEBUG_IO("write %d%s, read %d", ctx
->write_count
, ctx
->read_count
? "+1" : "",
858 assert(ctx
->write_count
> 0 || ctx
->read_count
== 0); /* No read data without write data */
860 if (ctx
->write_count
== 0)
863 struct libusb_transfer
*read_transfer
= NULL
;
864 struct transfer_result read_result
= { .ctx
= ctx
, .done
= true };
865 if (ctx
->read_count
) {
866 buffer_write_byte(ctx
, 0x87); /* SEND_IMMEDIATE */
867 read_result
.done
= false;
868 /* delay read transaction to ensure the FTDI chip can support us with data
869 immediately after processing the MPSSE commands in the write transaction */
872 struct transfer_result write_result
= { .ctx
= ctx
, .done
= false };
873 struct libusb_transfer
*write_transfer
= libusb_alloc_transfer(0);
874 libusb_fill_bulk_transfer(write_transfer
, ctx
->usb_dev
, ctx
->out_ep
, ctx
->write_buffer
,
875 ctx
->write_count
, write_cb
, &write_result
, ctx
->usb_write_timeout
);
876 retval
= libusb_submit_transfer(write_transfer
);
877 if (retval
!= LIBUSB_SUCCESS
)
880 if (ctx
->read_count
) {
881 read_transfer
= libusb_alloc_transfer(0);
882 libusb_fill_bulk_transfer(read_transfer
, ctx
->usb_dev
, ctx
->in_ep
, ctx
->read_chunk
,
883 ctx
->read_chunk_size
, read_cb
, &read_result
,
884 ctx
->usb_read_timeout
);
885 retval
= libusb_submit_transfer(read_transfer
);
886 if (retval
!= LIBUSB_SUCCESS
)
890 /* Polling loop, more or less taken from libftdi */
891 int64_t start
= timeval_ms();
892 int64_t warn_after
= 2000;
893 while (!write_result
.done
|| !read_result
.done
) {
894 struct timeval timeout_usb
;
896 timeout_usb
.tv_sec
= 1;
897 timeout_usb
.tv_usec
= 0;
899 retval
= libusb_handle_events_timeout_completed(ctx
->usb_ctx
, &timeout_usb
, NULL
);
902 int64_t now
= timeval_ms();
903 if (now
- start
> warn_after
) {
904 LOG_WARNING("Haven't made progress in mpsse_flush() for %" PRId64
909 if (retval
== LIBUSB_ERROR_INTERRUPTED
)
912 if (retval
!= LIBUSB_SUCCESS
) {
913 libusb_cancel_transfer(write_transfer
);
915 libusb_cancel_transfer(read_transfer
);
920 if (retval
!= LIBUSB_SUCCESS
) {
921 LOG_ERROR("libusb_handle_events() failed with %s", libusb_error_name(retval
));
923 } else if (write_result
.transferred
< ctx
->write_count
) {
924 LOG_ERROR("ftdi device did not accept all data: %d, tried %d",
925 write_result
.transferred
,
928 } else if (read_result
.transferred
< ctx
->read_count
) {
929 LOG_ERROR("ftdi device did not return all data: %d, expected %d",
930 read_result
.transferred
,
933 } else if (ctx
->read_count
) {
934 ctx
->write_count
= 0;
936 bit_copy_execute(&ctx
->read_queue
);
939 ctx
->write_count
= 0;
940 bit_copy_discard(&ctx
->read_queue
);
944 if (retval
!= ERROR_OK
)
947 libusb_free_transfer(write_transfer
);
949 libusb_free_transfer(read_transfer
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)