rlink: use jtag_usb_open helper
authorZachary T Welch <zw@superlucidity.net>
Thu, 19 Nov 2009 20:25:16 +0000 (12:25 -0800)
committerZachary T Welch <zw@superlucidity.net>
Tue, 24 Nov 2009 15:30:20 +0000 (07:30 -0800)
Rewrite rlink_init routine to use jtag_usb_open helper.  Eliminates
some spurious calls to exit().

Wraps a tremendously long line of comment to fit 80 columns too.

src/jtag/rlink/rlink.c

index 2cdc70a166d0969f8489daa94140d3a24c7ff06c..6fb721d5e4f6d257b2982fd979ae6ba1ee624871 100644 (file)
@@ -34,9 +34,7 @@
 #include "st7.h"
 #include "ep1_cmd.h"
 #include "dtc_cmd.h"
-
-/* system includes */
-#include <usb.h>
+#include "usb_common.h"
 
 
 /* This feature is made useless by running the DTC all the time.  When automatic, the LED is on whenever the DTC is running.  Otherwise, USB messages are sent to turn it on and off. */
@@ -1616,103 +1614,72 @@ int rlink_register_commands(struct command_context *cmd_ctx)
 static
 int rlink_init(void)
 {
-       struct usb_bus *busses;
-       struct usb_bus *bus;
        int i, j, retries;
-       int found = 0;
-       int success = 0;
        uint8_t reply_buffer[USB_EP1IN_SIZE];
 
        usb_init();
-       usb_find_busses();
-       usb_find_devices();
-
-       busses = usb_get_busses();
+       const uint16_t vids[] = { USB_IDVENDOR, 0 };
+       const uint16_t pids[] = { USB_IDPRODUCT, 0 };
+       if (jtag_usb_open(vids, pids, &pHDev) != ERROR_OK)
+               return ERROR_FAIL;
 
-       for (bus = busses; bus; bus = bus->next)
+       struct usb_device *dev = usb_device(pHDev);
+       if (dev->descriptor.bNumConfigurations > 1)
        {
-               struct usb_device *dev;
-
-               for (dev = bus->devices; dev; dev = dev->next)
-               {
-                       if ((dev->descriptor.idVendor != USB_IDVENDOR) ||
-                               (dev->descriptor.idProduct != USB_IDPRODUCT))
-                       {
-                               continue;
-                       }
-                       found = 1;
-                       LOG_DEBUG("Found device on bus.\n");
-
-                       if (dev->descriptor.bNumConfigurations > 1)
-                       {
-                               LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
-                               break;
-                       }
-                       if (dev->config->bNumInterfaces > 1)
-                       {
-                               LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
-                               break;
-                       }
+               LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
+               return ERROR_FAIL;
+       }
+       if (dev->config->bNumInterfaces > 1)
+       {
+               LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
+               return ERROR_FAIL;
+       }
 
-                       pHDev = usb_open(dev);
-                       if (!pHDev)
-                       {
-                               LOG_ERROR ("Failed to open device.\n");
-                               break;
-                       }
-                       LOG_DEBUG("Opened device, pHDev = %p\n",pHDev);
+       LOG_DEBUG("Opened device, pHDev = %p\n", pHDev);
 
-                       /* usb_set_configuration required under win32 */
-                       usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
+       /* usb_set_configuration required under win32 */
+       usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
 
-                       retries = 3;
-                       do
-                       {
-                               i = usb_claim_interface(pHDev,0);
-                               if (i)
-                               {
-                                       LOG_ERROR("usb_claim_interface: %s", usb_strerror());
+       retries = 3;
+       do
+       {
+               i = usb_claim_interface(pHDev,0);
+               if (i)
+               {
+                       LOG_ERROR("usb_claim_interface: %s", usb_strerror());
 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
-                                       j = usb_detach_kernel_driver_np(pHDev, 0);
-                                       if (j)
-                                               LOG_ERROR("detach kernel driver: %s", usb_strerror());
+                       j = usb_detach_kernel_driver_np(pHDev, 0);
+                       if (j)
+                               LOG_ERROR("detach kernel driver: %s", usb_strerror());
 #endif
-                               }
-                               else
-                               {
-                                       LOG_DEBUG("interface claimed!\n");
-                                       break;
-                               }
-                       } while (--retries);
-
-                       if (!i)
-                       {
-                               if (usb_set_altinterface(pHDev,0))
-                               {
-                                       LOG_ERROR("Failed to set interface.\n");
-                                       break;
-                               }
-                               else
-                                       success = 1;
-                       }
                }
-       }
+               else
+               {
+                       LOG_DEBUG("interface claimed!\n");
+                       break;
+               }
+       } while (--retries);
 
-       if (!found)
+       if (i)
        {
-               LOG_ERROR("No device found on bus.\n");
-               exit(1);
+               LOG_ERROR("Initialisation failed.");
+               return ERROR_FAIL;
        }
-
-       if (!success)
+       if (usb_set_altinterface(pHDev,0) != 0)
        {
-               LOG_ERROR("Initialisation failed.");
-               exit(1);
+               LOG_ERROR("Failed to set interface.\n");
+               return ERROR_FAIL;
        }
 
-
-       /* The device starts out in an unknown state on open.  As such, result reads time out, and it's not even known whether the command was accepted.  So, for this first command, we issue it repeatedly until its response doesn't time out.  Also, if sending a command is going to time out, we'll find that out here. */
-       /* It must be possible to open the device in such a way that this special magic isn't needed, but, so far, it escapes us. */
+       /* The device starts out in an unknown state on open.  As such,
+        * result reads time out, and it's not even known whether the
+        * command was accepted.  So, for this first command, we issue
+        * it repeatedly until its response doesn't time out.  Also, if
+        * sending a command is going to time out, we find that out here.
+        *
+        * It must be possible to open the device in such a way that
+        * this special magic isn't needed, but, so far, it escapes us.
+        */
        for (i = 0; i < 5; i++) {
                j = ep1_generic_commandl(
                        pHDev, 1,

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)