From 96f0ab894adf606526325cd0b0d0c25413d448b8 Mon Sep 17 00:00:00 2001 From: Zachary T Welch Date: Thu, 19 Nov 2009 12:57:32 -0800 Subject: jlink: rewrite to use jtag_usb_open Rewrite jlink_usb_open to use jtag_usb_open helper. --- src/jtag/jlink.c | 76 +++++++++++--------------------------------------------- 1 file changed, 15 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/jtag/jlink.c b/src/jtag/jlink.c index 8f1d112d..f173ed78 100644 --- a/src/jtag/jlink.c +++ b/src/jtag/jlink.c @@ -27,8 +27,7 @@ #include "interface.h" #include "commands.h" - -#include +#include "usb_common.h" #define VID 0x1366 @@ -818,53 +817,17 @@ static int jlink_tap_execute(void) return ERROR_OK; } -static struct usb_device* find_jlink_device(void) -{ - struct usb_bus *busses; - struct usb_bus *bus; - struct usb_device *dev; - - usb_find_busses(); - usb_find_devices(); - - busses = usb_get_busses(); - - /* find jlink device in usb bus */ - - for (bus = busses; bus; bus = bus->next) - { - for (dev = bus->devices; dev; dev = dev->next) - { - if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID)) { - return dev; - } - } - } - - return NULL; -} - /*****************************************************************************/ /* JLink USB low-level functions */ static struct jlink* jlink_usb_open() { - struct usb_device *dev; - - struct jlink *result; - - result = (struct jlink*) malloc(sizeof(struct jlink)); - usb_init(); - if ((dev = find_jlink_device()) == NULL) { - free(result); - return NULL; - } - - result->usb_handle = usb_open(dev); - - if (NULL == result->usb_handle) + const uint16_t vids[] = { VID, 0 }; + const uint16_t pids[] = { PID, 0 }; + struct usb_dev_handle *dev; + if (jtag_usb_open(vids, pids, &dev) != ERROR_OK) return NULL; /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS @@ -880,15 +843,15 @@ static struct jlink* jlink_usb_open() #if IS_WIN32 == 0 - usb_reset(result->usb_handle); + usb_reset(dev); #if IS_DARWIN == 0 int timeout = 5; - /* reopen jlink after usb_reset * on win32 this may take a second or two to re-enumerate */ - while ((dev = find_jlink_device()) == NULL) + int retval; + while ((retval = jtag_usb_open(vids, pids, &dev)) != ERROR_OK) { usleep(1000); timeout--; @@ -896,27 +859,16 @@ static struct jlink* jlink_usb_open() break; } } - - if (dev == NULL) - { - free(result); + if (ERROR_OK != retval) return NULL; - } - - result->usb_handle = usb_open(dev); #endif #endif - if (NULL == result->usb_handle) - { - free(result); - return NULL; - } - /* usb_set_configuration required under win32 */ - usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue); - usb_claim_interface(result->usb_handle, 0); + struct usb_device *udev = usb_device(dev); + usb_set_configuration(dev, udev->config[0].bConfigurationValue); + usb_claim_interface(dev, 0); #if 0 /* @@ -925,7 +877,7 @@ static struct jlink* jlink_usb_open() */ usb_set_altinterface(result->usb_handle, 0); #endif - struct usb_interface *iface = dev->config->interface; + struct usb_interface *iface = udev->config->interface; struct usb_interface_descriptor *desc = iface->altsetting; for (int i = 0; i < desc->bNumEndpoints; i++) { @@ -938,6 +890,8 @@ static struct jlink* jlink_usb_open() jlink_write_ep = epnum; } + struct jlink *result = malloc(sizeof(struct jlink)); + result->usb_handle = dev; return result; } -- cgit v1.2.3