libusb: introduce jtag_libusb_choose_interface() and use it for JLink
[openocd.git] / src / jtag / drivers / jlink.c
index 8b57ffca234385fb566ef5fdebeba2e214e35efb..57eea64d192ba352aa49d7d8cefe1df88ae53b5c 100644 (file)
  * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
  */
 
-#define JLINK_OB_PID  0x0105
+#define JLINK_USB_INTERFACE_CLASS 0xff
+#define JLINK_USB_INTERFACE_SUBCLASS 0xff
+#define JLINK_USB_INTERFACE_PROTOCOL 0xff
 
-#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_write_ep;
+static unsigned int jlink_read_ep;
 static unsigned int jlink_hw_jtag_version = 2;
 
 #define JLINK_USB_TIMEOUT 1000
@@ -150,7 +146,7 @@ static uint8_t usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
 #define EMU_CAP_RAWTRACE               30
 #define EMU_CAP_RESERVED_3             31
 
-static char *jlink_cap_str[] = {
+static const char * const jlink_cap_str[] = {
        "Always 1.",
        "Supports command EMU_CMD_GET_HARDWARE_VERSION",
        "Supports command EMU_CMD_WRITE_DCC",
@@ -189,17 +185,20 @@ static char *jlink_cap_str[] = {
 #define JLINK_MAX_SPEED 12000
 
 /* J-Link hardware versions */
-#define JLINK_HW_TYPE_JLINK    0
-#define JLINK_HW_TYPE_JTRACE   1
-#define JLINK_HW_TYPE_FLASHER  2
-#define JLINK_HW_TYPE_JLINK_PRO        3
-#define JLINK_HW_TYPE_MAX      4
-
-static char *jlink_hw_type_str[] = {
+#define JLINK_HW_TYPE_JLINK                            0
+#define JLINK_HW_TYPE_JTRACE                   1
+#define JLINK_HW_TYPE_FLASHER                  2
+#define JLINK_HW_TYPE_JLINK_PRO                        3
+#define JLINK_HW_TYPE_JLINK_LITE_ADI   5
+#define JLINK_HW_TYPE_MAX                              6
+
+static const char * const jlink_hw_type_str[] = {
        "J-Link",
        "J-Trace",
        "Flasher",
        "J-Link Pro",
+       "Unknown",
+       "J-Link Lite-ADI",
 };
 
 #define JLINK_TIF_JTAG         0
@@ -1696,7 +1695,7 @@ static void jlink_swd_queue_cmd(struct adiv5_dap *dap, uint8_t cmd, uint32_t *ds
 static struct jlink *jlink_usb_open()
 {
        struct jtag_libusb_device_handle *devh;
-       if (jtag_libusb_open(vids, pids, &devh) != ERROR_OK)
+       if (jtag_libusb_open(vids, pids, NULL, &devh) != ERROR_OK)
                return NULL;
 
        /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
@@ -1710,7 +1709,15 @@ static struct jlink *jlink_usb_open()
         * committing them!
         */
 
-#if IS_WIN32 == 0
+/* This entire block can probably be removed. It was a workaround for
+ * libusb0.1 and old JLink firmware. It has already be removed for
+ * windows and causing problems (LPC Link-2 with JLink firmware) on
+ * Linux with libusb1.0.
+ *
+ * However, for now the behavior will be left unchanged for non-windows
+ * platforms using libusb0.1 due to lack of testing.
+ */
+#if IS_WIN32 == 0 && HAVE_LIBUSB1 == 0
 
        jtag_libusb_reset_device(devh);
 
@@ -1720,7 +1727,7 @@ static struct jlink *jlink_usb_open()
        /* reopen jlink after usb_reset
         * on win32 this may take a second or two to re-enumerate */
        int retval;
-       while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK) {
+       while ((retval = jtag_libusb_open(vids, pids, NULL, &devh)) != ERROR_OK) {
                usleep(1000);
                timeout--;
                if (!timeout)
@@ -1732,29 +1739,15 @@ static struct jlink *jlink_usb_open()
 
 #endif
 
-       /* usb_set_configuration required under win32 */
-       struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
+       /* usb_set_configuration is only required under win32
+        * with libusb 0.1 and libusb0.sys. For libusb 1.0 it is a no-op
+        * since the configuration is already set. */
        jtag_libusb_set_configuration(devh, 0);
-       jtag_libusb_claim_interface(devh, 0);
-
-#if 0
-       /*
-        * This makes problems under Mac OS X. And is not needed
-        * under Windows. Hopefully this will not break a linux build
-        */
-       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);
+       jtag_libusb_choose_interface(devh, &jlink_read_ep, &jlink_write_ep,
+                                    JLINK_USB_INTERFACE_CLASS,
+                                    JLINK_USB_INTERFACE_SUBCLASS,
+                                    JLINK_USB_INTERFACE_PROTOCOL);
 
        struct jlink *result = malloc(sizeof(struct jlink));
        result->usb_handle = devh;

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)