Hi all,

This patch unwinds the logic in jlink_usb_message.

Cheers,

Zach
Index: src/jtag/jlink.c
===================================================================
--- src/jtag/jlink.c	(revision 1497)
+++ src/jtag/jlink.c	(working copy)
@@ -792,59 +779,48 @@
 	int result2;
 
 	result = jlink_usb_write(jlink_jtag, out_length);
-	if (result == out_length)
+	if (result != out_length)
 	{
-		result = jlink_usb_read(jlink_jtag, in_length);
-		if (result == in_length || result == in_length+1)
+		LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
+				out_length, result);
+		return ERROR_JTAG_DEVICE_ERROR;
+	}
+
+	result = jlink_usb_read(jlink_jtag, in_length);
+	if ((result != in_length) && (result != in_length + 1))
+	{
+		LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
+				in_length, result);
+		return ERROR_JTAG_DEVICE_ERROR;
+	}
+
+	if (result == in_length)
+	{
+		/* Must read the result from the EMU too */
+		result2 = jlink_usb_read_emu_result(jlink_jtag);
+		if (1 != result2)
 		{
-			if (result == in_length)
-			{
-				/* Must read the result from the EMU too */
-				result2 = jlink_usb_read_emu_result(jlink_jtag);
-				if (1 == result2)
-				{
-					/* Check the result itself */
-					if (0 == usb_emu_result_buffer[0])
-					{
-						return result;
-					}
-					else
-					{
-						LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_emu_result_buffer[0]);
-						return -1;
-					}
-				}
-				else
-				{
-					LOG_ERROR("jlink_usb_read_emu_result len (requested=1, result=%d)", result2);
-					return -1;
-				}
-			}
-			else
-			{
-				/* Check the result itself */
-				if (0 == usb_in_buffer[result-1])
-				{
-					return result-1;
-				}
-				else
-				{
-					LOG_ERROR("jlink_usb_read_emu_result (requested=0, result=%d)", usb_in_buffer[result]);
-					return -1;
-				}
-			}
+			LOG_ERROR("jlink_usb_read_emu_result failed "
+				"(requested=1, result=%d)", result2);
+			return ERROR_JTAG_DEVICE_ERROR;
 		}
-		else
-		{
-			LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
-			return -1;
-		}
+
+		/* Check the result itself */
+		result2 = usb_emu_result_buffer[0];
 	}
 	else
 	{
-		LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
-		return -1;
+		/* Save the result, then remove it from return value */
+		result2 = usb_in_buffer[result--];
 	}
+
+	if (result2)
+	{
+		LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
+		return ERROR_JTAG_DEVICE_ERROR;
+	}
+
+	return result;
 }
 
 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts  */
_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to