From fb897cc805fe193a1ee2f295f284e421660793d5 Mon Sep 17 00:00:00 2001
From: Andrey Yurovsky <yurovsky@gmail.com>
Date: Fri, 7 Mar 2014 17:16:52 -0800
Subject: [PATCH] jlink: add support for Jlink-OB (0x0105) devices

The JLink-OB (onboard) devices work the same way as the normal JLink
except that their PID is 0x0105 (and that's the only one we know of so
far) and their endpoint addresses are different due to there being a
CDC-ACM interface as well.  These JLink-OB devices show up on a lot of
vendors' development kits as an integrated debugger.

This change simply checks whether the adapter we opened has a JLink-OB
PID and, if it does, uses the JLink-OB endpoints rather than the
default.  To do this, we add a new routine, jtag_libusb_get_pid() to the
libusb adapter layer, it in turn just calls
libusb_get_device_descriptor(), which previously had no wrapper.

Also, checkpatch.pl doesn't like the VID/PID macros as defined so I
moved them to the array itself.  This should have no effect on the code.

This change adds the 0102 through 0104 PIDs to openocd.rules as well as this
new 0105 PID.

Tested on an Atmel SAM4S Xplained board which has a JLink-OB, also
regression tested by using a 0x0101 PID normal JLink adapter.

Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com>
Change-Id: I121d30e57729cda3adb66e2a5dc72e1fcb7ef8b1
Reviewed-on: http://openocd.zylin.com/2031
Tested-by: jenkins
Reviewed-by: Xiaofan <xiaofanc@gmail.com>
Reviewed-by: Paul Fertser <fercerpav@gmail.com>
---
 contrib/99-openocd.rules          |  6 ++++++
 src/jtag/drivers/jlink.c          | 19 +++++++++++++++----
 src/jtag/drivers/libusb0_common.c | 13 +++++++++++++
 src/jtag/drivers/libusb0_common.h |  1 +
 src/jtag/drivers/libusb1_common.c | 13 +++++++++++++
 src/jtag/drivers/libusb1_common.h |  1 +
 6 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/contrib/99-openocd.rules b/contrib/99-openocd.rules
index 44ab9cc87..b5a5e68bd 100644
--- a/contrib/99-openocd.rules
+++ b/contrib/99-openocd.rules
@@ -77,6 +77,12 @@ ATTRS{idVendor}=="0fbb", ATTRS{idProduct}=="1000", MODE="664", GROUP="plugdev"
 
 # IAR J-Link USB
 ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0101", MODE="664", GROUP="plugdev"
+ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0102", MODE="664", GROUP="plugdev"
+ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0103", MODE="664", GROUP="plugdev"
+ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0104", MODE="664", GROUP="plugdev"
+
+# J-Link-OB (onboard)
+ATTRS{idVendor}=="1366", ATTRS{idProduct}=="0105", MODE="664", GROUP="plugdev"
 
 # Raisonance RLink
 ATTRS{idVendor}=="138e", ATTRS{idProduct}=="9000", MODE="664", GROUP="plugdev"
diff --git a/src/jtag/drivers/jlink.c b/src/jtag/drivers/jlink.c
index 728a05b87..029db38c9 100644
--- a/src/jtag/drivers/jlink.c
+++ b/src/jtag/drivers/jlink.c
@@ -46,12 +46,14 @@
  * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
  */
 
-#define VID 0x1366, 0x1366, 0x1366, 0x1366
-#define PID 0x0101, 0x0102, 0x0103, 0x0104
+#define JLINK_OB_PID  0x0105
 
 #define JLINK_WRITE_ENDPOINT	0x02
 #define JLINK_READ_ENDPOINT		0x81
 
+#define JLINK_OB_WRITE_ENDPOINT	0x06
+#define JLINK_OB_READ_ENDPOINT	0x85
+
 static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT;
 static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT;
 static unsigned int jlink_hw_jtag_version = 2;
@@ -246,8 +248,8 @@ static enum tap_state jlink_last_state = TAP_RESET;
 static struct jlink *jlink_handle;
 
 /* pid could be specified at runtime */
-static uint16_t vids[] = { VID, 0 };
-static uint16_t pids[] = { PID, 0 };
+static uint16_t vids[] = { 0x1366, 0x1366, 0x1366, 0x1366, 0x1366, 0 };
+static uint16_t pids[] = { 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0 };
 
 static uint32_t jlink_caps;
 static uint32_t jlink_hw_type;
@@ -1517,6 +1519,15 @@ static struct jlink *jlink_usb_open()
 	usb_set_altinterface(result->usb_handle, 0);
 #endif
 
+	/* Use the OB endpoints if the JLink we matched is a Jlink-OB adapter */
+	uint16_t matched_pid;
+	if (jtag_libusb_get_pid(udev, &matched_pid) == ERROR_OK) {
+		if (matched_pid == JLINK_OB_PID) {
+			jlink_read_ep = JLINK_OB_WRITE_ENDPOINT;
+			jlink_write_ep = JLINK_OB_READ_ENDPOINT;
+		}
+	}
+
 	jtag_libusb_get_endpoints(udev, &jlink_read_ep, &jlink_write_ep);
 
 	struct jlink *result = malloc(sizeof(struct jlink));
diff --git a/src/jtag/drivers/libusb0_common.c b/src/jtag/drivers/libusb0_common.c
index 32111195a..4b3bde1ec 100644
--- a/src/jtag/drivers/libusb0_common.c
+++ b/src/jtag/drivers/libusb0_common.c
@@ -122,3 +122,16 @@ int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
 
 	return 0;
 }
+
+int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
+{
+	struct libusb_device_descriptor dev_desc;
+
+	if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
+		*pid = dev_desc.idProduct;
+
+		return 0;
+	}
+
+	return -ENODEV;
+}
diff --git a/src/jtag/drivers/libusb0_common.h b/src/jtag/drivers/libusb0_common.h
index bea743dcf..db247ad85 100644
--- a/src/jtag/drivers/libusb0_common.h
+++ b/src/jtag/drivers/libusb0_common.h
@@ -68,5 +68,6 @@ int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
 int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
 		unsigned int *usb_read_ep,
 		unsigned int *usb_write_ep);
+int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid);
 
 #endif /* JTAG_USB_COMMON_H */
diff --git a/src/jtag/drivers/libusb1_common.c b/src/jtag/drivers/libusb1_common.c
index c4478cb9b..81e8ca888 100644
--- a/src/jtag/drivers/libusb1_common.c
+++ b/src/jtag/drivers/libusb1_common.c
@@ -166,3 +166,16 @@ int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
 
 	return 0;
 }
+
+int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
+{
+	struct libusb_device_descriptor dev_desc;
+
+	if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
+		*pid = dev_desc.idProduct;
+
+		return 0;
+	}
+
+	return -ENODEV;
+}
diff --git a/src/jtag/drivers/libusb1_common.h b/src/jtag/drivers/libusb1_common.h
index 0e653d845..e33b83ad8 100644
--- a/src/jtag/drivers/libusb1_common.h
+++ b/src/jtag/drivers/libusb1_common.h
@@ -62,5 +62,6 @@ int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
 int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
 		unsigned int *usb_read_ep,
 		unsigned int *usb_write_ep);
+int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid);
 
 #endif /* JTAG_USB_COMMON_H */