+/*
+ * Send a message and receive the reply - simple messages.
+ *
+ * @param jlink pointer to driver data
+ * @param out_length data length in @c usb_out_buffer
+ * @param in_length data length to be read to @c usb_in_buffer
+ */
+static int jlink_usb_io(struct jlink *jlink, int out_length, int in_length)
+{
+ int result;
+
+ result = jlink_usb_write(jlink, out_length);
+ if (result != out_length) {
+ LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
+ out_length, result);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ result = jlink_usb_read(jlink, in_length);
+ if (result != in_length) {
+ LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
+ in_length, result);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ /*
+ * Section 4.2.4 IN-transaction:
+ * read dummy 0-byte packet if transaction size is
+ * multiple of 64 bytes but not max. size of 0x8000
+ */
+ if ((in_length % 64) == 0 && in_length != 0x8000) {
+ char dummy_buffer;
+ result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
+ &dummy_buffer, 1, JLINK_USB_TIMEOUT);
+ if (result != 0) {
+ LOG_ERROR("dummy byte read failed");
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+ }
+ return ERROR_OK;
+}
+