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 LOG_ERROR("unsupported FTDI chip type: 0x%04x", desc
.bcdDevice
);
273 /* Determine maximum packet size and endpoint addresses */
274 if (!(desc
.bNumConfigurations
> 0 && ctx
->interface
< config0
->bNumInterfaces
275 && config0
->interface
[ctx
->interface
].num_altsetting
> 0))
278 const struct libusb_interface_descriptor
*descriptor
;
279 descriptor
= &config0
->interface
[ctx
->interface
].altsetting
[0];
280 if (descriptor
->bNumEndpoints
!= 2)
285 for (int i
= 0; i
< descriptor
->bNumEndpoints
; i
++) {
286 if (descriptor
->endpoint
[i
].bEndpointAddress
& 0x80) {
287 ctx
->in_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
288 ctx
->max_packet_size
=
289 descriptor
->endpoint
[i
].wMaxPacketSize
;
291 ctx
->out_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
295 if (ctx
->in_ep
== 0 || ctx
->out_ep
== 0)
298 libusb_free_config_descriptor(config0
);
302 LOG_ERROR("unrecognized USB device descriptor");
304 libusb_free_config_descriptor(config0
);
305 libusb_close(ctx
->usb_dev
);
309 struct mpsse_ctx
*mpsse_open(const uint16_t vids
[], const uint16_t pids
[], const char *description
,
310 const char *serial
, const char *location
, int channel
)
312 struct mpsse_ctx
*ctx
= calloc(1, sizeof(*ctx
));
318 bit_copy_queue_init(&ctx
->read_queue
);
319 ctx
->read_chunk_size
= 16384;
320 ctx
->read_size
= 16384;
321 ctx
->write_size
= 16384;
322 ctx
->read_chunk
= malloc(ctx
->read_chunk_size
);
323 ctx
->read_buffer
= malloc(ctx
->read_size
);
325 /* Use calloc to make valgrind happy: buffer_write() sets payload
326 * on bit basis, so some bits can be left uninitialized in write_buffer.
327 * Although this is perfectly ok with MPSSE, valgrind reports
328 * Syscall param ioctl(USBDEVFS_SUBMITURB).buffer points to uninitialised byte(s) */
329 ctx
->write_buffer
= calloc(1, ctx
->write_size
);
331 if (!ctx
->read_chunk
|| !ctx
->read_buffer
|| !ctx
->write_buffer
)
334 ctx
->interface
= channel
;
335 ctx
->index
= channel
+ 1;
336 ctx
->usb_read_timeout
= 5000;
337 ctx
->usb_write_timeout
= 5000;
339 err
= libusb_init(&ctx
->usb_ctx
);
340 if (err
!= LIBUSB_SUCCESS
) {
341 LOG_ERROR("libusb_init() failed with %s", libusb_error_name(err
));
345 if (!open_matching_device(ctx
, vids
, pids
, description
, serial
, location
)) {
346 LOG_ERROR("unable to open ftdi device with description '%s', "
347 "serial '%s' at bus location '%s'",
348 description
? description
: "*",
349 serial
? serial
: "*",
350 location
? location
: "*");
355 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
356 SIO_SET_LATENCY_TIMER_REQUEST
, 255, ctx
->index
, NULL
, 0,
357 ctx
->usb_write_timeout
);
359 LOG_ERROR("unable to set latency timer: %s", libusb_error_name(err
));
363 err
= libusb_control_transfer(ctx
->usb_dev
,
364 FTDI_DEVICE_OUT_REQTYPE
,
365 SIO_SET_BITMODE_REQUEST
,
366 0x0b | (BITMODE_MPSSE
<< 8),
370 ctx
->usb_write_timeout
);
372 LOG_ERROR("unable to set MPSSE bitmode: %s", libusb_error_name(err
));
384 void mpsse_close(struct mpsse_ctx
*ctx
)
387 libusb_close(ctx
->usb_dev
);
389 libusb_exit(ctx
->usb_ctx
);
390 bit_copy_discard(&ctx
->read_queue
);
392 free(ctx
->write_buffer
);
393 free(ctx
->read_buffer
);
394 free(ctx
->read_chunk
);
398 bool mpsse_is_high_speed(struct mpsse_ctx
*ctx
)
400 return ctx
->type
!= TYPE_FT2232C
;
403 void mpsse_purge(struct mpsse_ctx
*ctx
)
407 ctx
->write_count
= 0;
409 ctx
->retval
= ERROR_OK
;
410 bit_copy_discard(&ctx
->read_queue
);
411 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
412 SIO_RESET_PURGE_RX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
414 LOG_ERROR("unable to purge ftdi rx buffers: %s", libusb_error_name(err
));
418 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
419 SIO_RESET_PURGE_TX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
421 LOG_ERROR("unable to purge ftdi tx buffers: %s", libusb_error_name(err
));
426 static unsigned buffer_write_space(struct mpsse_ctx
*ctx
)
428 /* Reserve one byte for SEND_IMMEDIATE */
429 return ctx
->write_size
- ctx
->write_count
- 1;
432 static unsigned buffer_read_space(struct mpsse_ctx
*ctx
)
434 return ctx
->read_size
- ctx
->read_count
;
437 static void buffer_write_byte(struct mpsse_ctx
*ctx
, uint8_t data
)
439 LOG_DEBUG_IO("%02x", data
);
440 assert(ctx
->write_count
< ctx
->write_size
);
441 ctx
->write_buffer
[ctx
->write_count
++] = data
;
444 static unsigned buffer_write(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
447 LOG_DEBUG_IO("%d bits", bit_count
);
448 assert(ctx
->write_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->write_size
);
449 bit_copy(ctx
->write_buffer
+ ctx
->write_count
, 0, out
, out_offset
, bit_count
);
450 ctx
->write_count
+= DIV_ROUND_UP(bit_count
, 8);
454 static unsigned buffer_add_read(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
,
455 unsigned bit_count
, unsigned offset
)
457 LOG_DEBUG_IO("%d bits, offset %d", bit_count
, offset
);
458 assert(ctx
->read_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->read_size
);
459 bit_copy_queued(&ctx
->read_queue
, in
, in_offset
, ctx
->read_buffer
+ ctx
->read_count
, offset
,
461 ctx
->read_count
+= DIV_ROUND_UP(bit_count
, 8);
465 void mpsse_clock_data_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
466 unsigned length
, uint8_t mode
)
468 mpsse_clock_data(ctx
, out
, out_offset
, NULL
, 0, length
, mode
);
471 void mpsse_clock_data_in(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
, unsigned length
,
474 mpsse_clock_data(ctx
, NULL
, 0, in
, in_offset
, length
, mode
);
477 void mpsse_clock_data(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
478 unsigned in_offset
, unsigned length
, uint8_t mode
)
480 /* TODO: Fix MSB first modes */
481 LOG_DEBUG_IO("%s%s %d bits", in
? "in" : "", out
? "out" : "", length
);
483 if (ctx
->retval
!= ERROR_OK
) {
484 LOG_DEBUG_IO("Ignoring command due to previous error");
488 /* TODO: On H chips, use command 0x8E/0x8F if in and out are both 0 */
489 if (out
|| (!out
&& !in
))
495 /* Guarantee buffer space enough for a minimum size transfer */
496 if (buffer_write_space(ctx
) + (length
< 8) < (out
|| (!out
&& !in
) ? 4 : 3)
497 || (in
&& buffer_read_space(ctx
) < 1))
498 ctx
->retval
= mpsse_flush(ctx
);
501 /* Transfer remaining bits in bit mode */
502 buffer_write_byte(ctx
, 0x02 | mode
);
503 buffer_write_byte(ctx
, length
- 1);
505 out_offset
+= buffer_write(ctx
, out
, out_offset
, length
);
507 in_offset
+= buffer_add_read(ctx
, in
, in_offset
, length
, 8 - length
);
509 buffer_write_byte(ctx
, 0x00);
513 unsigned this_bytes
= length
/ 8;
514 /* MPSSE command limit */
515 if (this_bytes
> 65536)
517 /* Buffer space limit. We already made sure there's space for the minimum
519 if ((out
|| (!out
&& !in
)) && this_bytes
+ 3 > buffer_write_space(ctx
))
520 this_bytes
= buffer_write_space(ctx
) - 3;
521 if (in
&& this_bytes
> buffer_read_space(ctx
))
522 this_bytes
= buffer_read_space(ctx
);
524 if (this_bytes
> 0) {
525 buffer_write_byte(ctx
, mode
);
526 buffer_write_byte(ctx
, (this_bytes
- 1) & 0xff);
527 buffer_write_byte(ctx
, (this_bytes
- 1) >> 8);
529 out_offset
+= buffer_write(ctx
,
534 in_offset
+= buffer_add_read(ctx
,
540 for (unsigned n
= 0; n
< this_bytes
; n
++)
541 buffer_write_byte(ctx
, 0x00);
542 length
-= this_bytes
* 8;
548 void mpsse_clock_tms_cs_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
549 unsigned length
, bool tdi
, uint8_t mode
)
551 mpsse_clock_tms_cs(ctx
, out
, out_offset
, NULL
, 0, length
, tdi
, mode
);
554 void mpsse_clock_tms_cs(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
555 unsigned in_offset
, unsigned length
, bool tdi
, uint8_t mode
)
557 LOG_DEBUG_IO("%sout %d bits, tdi=%d", in
? "in" : "", length
, tdi
);
560 if (ctx
->retval
!= ERROR_OK
) {
561 LOG_DEBUG_IO("Ignoring command due to previous error");
570 /* Guarantee buffer space enough for a minimum size transfer */
571 if (buffer_write_space(ctx
) < 3 || (in
&& buffer_read_space(ctx
) < 1))
572 ctx
->retval
= mpsse_flush(ctx
);
575 unsigned this_bits
= length
;
576 /* MPSSE command limit */
577 /* NOTE: there's a report of an FT2232 bug in this area, where shifting
578 * exactly 7 bits can make problems with TMS signaling for the last
581 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
587 buffer_write_byte(ctx
, mode
);
588 buffer_write_byte(ctx
, this_bits
- 1);
590 /* TODO: Fix MSB first, if allowed in MPSSE */
591 bit_copy(&data
, 0, out
, out_offset
, this_bits
);
592 out_offset
+= this_bits
;
593 buffer_write_byte(ctx
, data
| (tdi
? 0x80 : 0x00));
595 in_offset
+= buffer_add_read(ctx
,
605 void mpsse_set_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
609 if (ctx
->retval
!= ERROR_OK
) {
610 LOG_DEBUG_IO("Ignoring command due to previous error");
614 if (buffer_write_space(ctx
) < 3)
615 ctx
->retval
= mpsse_flush(ctx
);
617 buffer_write_byte(ctx
, 0x80);
618 buffer_write_byte(ctx
, data
);
619 buffer_write_byte(ctx
, dir
);
622 void mpsse_set_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
626 if (ctx
->retval
!= ERROR_OK
) {
627 LOG_DEBUG_IO("Ignoring command due to previous error");
631 if (buffer_write_space(ctx
) < 3)
632 ctx
->retval
= mpsse_flush(ctx
);
634 buffer_write_byte(ctx
, 0x82);
635 buffer_write_byte(ctx
, data
);
636 buffer_write_byte(ctx
, dir
);
639 void mpsse_read_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
643 if (ctx
->retval
!= ERROR_OK
) {
644 LOG_DEBUG_IO("Ignoring command due to previous error");
648 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
649 ctx
->retval
= mpsse_flush(ctx
);
651 buffer_write_byte(ctx
, 0x81);
652 buffer_add_read(ctx
, data
, 0, 8, 0);
655 void mpsse_read_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
659 if (ctx
->retval
!= ERROR_OK
) {
660 LOG_DEBUG_IO("Ignoring command due to previous error");
664 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
665 ctx
->retval
= mpsse_flush(ctx
);
667 buffer_write_byte(ctx
, 0x83);
668 buffer_add_read(ctx
, data
, 0, 8, 0);
671 static void single_byte_boolean_helper(struct mpsse_ctx
*ctx
, bool var
, uint8_t val_if_true
,
672 uint8_t val_if_false
)
674 if (ctx
->retval
!= ERROR_OK
) {
675 LOG_DEBUG_IO("Ignoring command due to previous error");
679 if (buffer_write_space(ctx
) < 1)
680 ctx
->retval
= mpsse_flush(ctx
);
682 buffer_write_byte(ctx
, var
? val_if_true
: val_if_false
);
685 void mpsse_loopback_config(struct mpsse_ctx
*ctx
, bool enable
)
687 LOG_DEBUG("%s", enable
? "on" : "off");
688 single_byte_boolean_helper(ctx
, enable
, 0x84, 0x85);
691 void mpsse_set_divisor(struct mpsse_ctx
*ctx
, uint16_t divisor
)
693 LOG_DEBUG("%d", divisor
);
695 if (ctx
->retval
!= ERROR_OK
) {
696 LOG_DEBUG_IO("Ignoring command due to previous error");
700 if (buffer_write_space(ctx
) < 3)
701 ctx
->retval
= mpsse_flush(ctx
);
703 buffer_write_byte(ctx
, 0x86);
704 buffer_write_byte(ctx
, divisor
& 0xff);
705 buffer_write_byte(ctx
, divisor
>> 8);
708 int mpsse_divide_by_5_config(struct mpsse_ctx
*ctx
, bool enable
)
710 if (!mpsse_is_high_speed(ctx
))
713 LOG_DEBUG("%s", enable
? "on" : "off");
714 single_byte_boolean_helper(ctx
, enable
, 0x8b, 0x8a);
719 int mpsse_rtck_config(struct mpsse_ctx
*ctx
, bool enable
)
721 if (!mpsse_is_high_speed(ctx
))
724 LOG_DEBUG("%s", enable
? "on" : "off");
725 single_byte_boolean_helper(ctx
, enable
, 0x96, 0x97);
730 int mpsse_set_frequency(struct mpsse_ctx
*ctx
, int frequency
)
732 LOG_DEBUG("target %d Hz", frequency
);
733 assert(frequency
>= 0);
737 return mpsse_rtck_config(ctx
, true);
739 mpsse_rtck_config(ctx
, false); /* just try */
741 if (frequency
> 60000000 / 2 / 65536 && mpsse_divide_by_5_config(ctx
, false) == ERROR_OK
) {
742 base_clock
= 60000000;
744 mpsse_divide_by_5_config(ctx
, true); /* just try */
745 base_clock
= 12000000;
748 int divisor
= (base_clock
/ 2 + frequency
- 1) / frequency
- 1;
751 assert(divisor
>= 0);
753 mpsse_set_divisor(ctx
, divisor
);
755 frequency
= base_clock
/ 2 / (1 + divisor
);
756 LOG_DEBUG("actually %d Hz", frequency
);
761 /* Context needed by the callbacks */
762 struct transfer_result
{
763 struct mpsse_ctx
*ctx
;
765 unsigned transferred
;
768 static LIBUSB_CALL
void read_cb(struct libusb_transfer
*transfer
)
770 struct transfer_result
*res
= transfer
->user_data
;
771 struct mpsse_ctx
*ctx
= res
->ctx
;
773 unsigned packet_size
= ctx
->max_packet_size
;
775 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
777 /* Strip the two status bytes sent at the beginning of each USB packet
778 * while copying the chunk buffer to the read buffer */
779 unsigned num_packets
= DIV_ROUND_UP(transfer
->actual_length
, packet_size
);
780 unsigned chunk_remains
= transfer
->actual_length
;
781 for (unsigned i
= 0; i
< num_packets
&& chunk_remains
> 2; i
++) {
782 unsigned this_size
= packet_size
- 2;
783 if (this_size
> chunk_remains
- 2)
784 this_size
= chunk_remains
- 2;
785 if (this_size
> ctx
->read_count
- res
->transferred
)
786 this_size
= ctx
->read_count
- res
->transferred
;
787 memcpy(ctx
->read_buffer
+ res
->transferred
,
788 ctx
->read_chunk
+ packet_size
* i
+ 2,
790 res
->transferred
+= this_size
;
791 chunk_remains
-= this_size
+ 2;
792 if (res
->transferred
== ctx
->read_count
) {
798 LOG_DEBUG_IO("raw chunk %d, transferred %d of %d", transfer
->actual_length
, res
->transferred
,
802 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
806 static LIBUSB_CALL
void write_cb(struct libusb_transfer
*transfer
)
808 struct transfer_result
*res
= transfer
->user_data
;
809 struct mpsse_ctx
*ctx
= res
->ctx
;
811 res
->transferred
+= transfer
->actual_length
;
813 LOG_DEBUG_IO("transferred %d of %d", res
->transferred
, ctx
->write_count
);
815 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
817 if (res
->transferred
== ctx
->write_count
)
820 transfer
->length
= ctx
->write_count
- res
->transferred
;
821 transfer
->buffer
= ctx
->write_buffer
+ res
->transferred
;
822 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
827 int mpsse_flush(struct mpsse_ctx
*ctx
)
829 int retval
= ctx
->retval
;
831 if (retval
!= ERROR_OK
) {
832 LOG_DEBUG_IO("Ignoring flush due to previous error");
833 assert(ctx
->write_count
== 0 && ctx
->read_count
== 0);
834 ctx
->retval
= ERROR_OK
;
838 LOG_DEBUG_IO("write %d%s, read %d", ctx
->write_count
, ctx
->read_count
? "+1" : "",
840 assert(ctx
->write_count
> 0 || ctx
->read_count
== 0); /* No read data without write data */
842 if (ctx
->write_count
== 0)
845 struct libusb_transfer
*read_transfer
= NULL
;
846 struct transfer_result read_result
= { .ctx
= ctx
, .done
= true };
847 if (ctx
->read_count
) {
848 buffer_write_byte(ctx
, 0x87); /* SEND_IMMEDIATE */
849 read_result
.done
= false;
850 /* delay read transaction to ensure the FTDI chip can support us with data
851 immediately after processing the MPSSE commands in the write transaction */
854 struct transfer_result write_result
= { .ctx
= ctx
, .done
= false };
855 struct libusb_transfer
*write_transfer
= libusb_alloc_transfer(0);
856 libusb_fill_bulk_transfer(write_transfer
, ctx
->usb_dev
, ctx
->out_ep
, ctx
->write_buffer
,
857 ctx
->write_count
, write_cb
, &write_result
, ctx
->usb_write_timeout
);
858 retval
= libusb_submit_transfer(write_transfer
);
859 if (retval
!= LIBUSB_SUCCESS
)
862 if (ctx
->read_count
) {
863 read_transfer
= libusb_alloc_transfer(0);
864 libusb_fill_bulk_transfer(read_transfer
, ctx
->usb_dev
, ctx
->in_ep
, ctx
->read_chunk
,
865 ctx
->read_chunk_size
, read_cb
, &read_result
,
866 ctx
->usb_read_timeout
);
867 retval
= libusb_submit_transfer(read_transfer
);
868 if (retval
!= LIBUSB_SUCCESS
)
872 /* Polling loop, more or less taken from libftdi */
873 int64_t start
= timeval_ms();
874 int64_t warn_after
= 2000;
875 while (!write_result
.done
|| !read_result
.done
) {
876 struct timeval timeout_usb
;
878 timeout_usb
.tv_sec
= 1;
879 timeout_usb
.tv_usec
= 0;
881 retval
= libusb_handle_events_timeout_completed(ctx
->usb_ctx
, &timeout_usb
, NULL
);
884 int64_t now
= timeval_ms();
885 if (now
- start
> warn_after
) {
886 LOG_WARNING("Haven't made progress in mpsse_flush() for %" PRId64
891 if (retval
== LIBUSB_ERROR_INTERRUPTED
)
894 if (retval
!= LIBUSB_SUCCESS
) {
895 libusb_cancel_transfer(write_transfer
);
897 libusb_cancel_transfer(read_transfer
);
902 if (retval
!= LIBUSB_SUCCESS
) {
903 LOG_ERROR("libusb_handle_events() failed with %s", libusb_error_name(retval
));
905 } else if (write_result
.transferred
< ctx
->write_count
) {
906 LOG_ERROR("ftdi device did not accept all data: %d, tried %d",
907 write_result
.transferred
,
910 } else if (read_result
.transferred
< ctx
->read_count
) {
911 LOG_ERROR("ftdi device did not return all data: %d, expected %d",
912 read_result
.transferred
,
915 } else if (ctx
->read_count
) {
916 ctx
->write_count
= 0;
918 bit_copy_execute(&ctx
->read_queue
);
921 ctx
->write_count
= 0;
922 bit_copy_discard(&ctx
->read_queue
);
926 if (retval
!= ERROR_OK
)
929 libusb_free_transfer(write_transfer
);
931 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)