Skip to content

Commit

Permalink
jtag/drivers: Extension of jtag_libusb_open
Browse files Browse the repository at this point in the history
In jtag_libusb_open I've added a parameter for delivering the device
description for which this function should search and adjusted all
callers of this function. A new driver for WCH CH347 JTAG chips
will use this new parameter.
See also: https://review.openocd.org/c/openocd/+/7937

Change-Id: I85e1d7b1f7912ba5e223f0f26323ff3b7600e17d
Signed-off-by: EasyDevKits <[email protected]>
Reviewed-on: https://review.openocd.org/c/openocd/+/7938
Tested-by: jenkins
Reviewed-by: Antonio Borneo <[email protected]>
  • Loading branch information
EasyDevKits authored and borneoa committed Oct 25, 2023
1 parent 1d555d2 commit a080d97
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/jtag/drivers/angie.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ static int angie_usb_open(struct angie *device)
const uint16_t vids[] = {ANGIE_VID, ANGIE_VID, ANGIE_VID, ANGIE_VID, 0};
const uint16_t pids[] = {ANGIE_PID, ANGIE_PID_2, ANGIE_PID_3, ANGIE_PID_4, 0};

int ret = jtag_libusb_open(vids, pids, &usb_device_handle, NULL);
int ret = jtag_libusb_open(vids, pids, NULL, &usb_device_handle, NULL);

if (ret != ERROR_OK)
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/arm-jtag-ew.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ static struct armjtagew *armjtagew_usb_open(void)
const uint16_t pids[] = { USB_PID, 0 };
struct libusb_device_handle *dev;

if (jtag_libusb_open(vids, pids, &dev, NULL) != ERROR_OK)
if (jtag_libusb_open(vids, pids, NULL, &dev, NULL) != ERROR_OK)
return NULL;

struct armjtagew *result = malloc(sizeof(struct armjtagew));
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/esp_usb_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ static int esp_usb_jtag_init(void)
bitq_interface->in_rdy = esp_usb_jtag_in_rdy;
bitq_interface->in = esp_usb_jtag_in;

int r = jtag_libusb_open(vids, pids, &priv->usb_device, NULL);
int r = jtag_libusb_open(vids, pids, NULL, &priv->usb_device, NULL);
if (r != ERROR_OK) {
LOG_ERROR("esp_usb_jtag: could not find or open device!");
goto out;
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/ft232r.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ static int ft232r_init(void)
{
uint16_t avids[] = {ft232r_vid, 0};
uint16_t apids[] = {ft232r_pid, 0};
if (jtag_libusb_open(avids, apids, &adapter, NULL)) {
if (jtag_libusb_open(avids, apids, NULL, &adapter, NULL)) {
const char *ft232r_serial_desc = adapter_get_required_serial();
LOG_ERROR("ft232r not found: vid=%04x, pid=%04x, serial=%s\n",
ft232r_vid, ft232r_pid, (!ft232r_serial_desc) ? "[any]" : ft232r_serial_desc);
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/kitprog.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static int kitprog_usb_open(void)
const uint16_t vids[] = { VID, 0 };
const uint16_t pids[] = { PID, 0 };

if (jtag_libusb_open(vids, pids, &kitprog_handle->usb_handle, NULL) != ERROR_OK) {
if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) {
LOG_ERROR("Failed to open or find the device");
return ERROR_FAIL;
}
Expand Down
14 changes: 13 additions & 1 deletion src/jtag/drivers/libusb_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ static bool jtag_libusb_match_serial(struct libusb_device_handle *device,
}

int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
struct libusb_device_handle **out,
const char *product, struct libusb_device_handle **out,
adapter_get_alternate_serial_fn adapter_get_alternate_serial)
{
int cnt, idx, err_code;
int retval = ERROR_FAIL;
bool serial_mismatch = false;
bool product_mismatch = false;
struct libusb_device_handle *libusb_handle = NULL;
const char *serial = adapter_get_required_serial();

Expand Down Expand Up @@ -188,10 +189,18 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
continue;
}

if (product &&
!string_descriptor_equal(libusb_handle, dev_desc.iProduct, product)) {
product_mismatch = true;
libusb_close(libusb_handle);
continue;
}

/* Success. */
*out = libusb_handle;
retval = ERROR_OK;
serial_mismatch = false;
product_mismatch = false;
break;
}
if (cnt >= 0)
Expand All @@ -200,6 +209,9 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
if (serial_mismatch)
LOG_INFO("No device matches the serial string");

if (product_mismatch)
LOG_INFO("No device matches the product string");

if (retval != ERROR_OK)
libusb_exit(jtag_libusb_context);

Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/libusb_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef char * (*adapter_get_alternate_serial_fn)(struct libusb_device_handle *d
bool jtag_libusb_match_ids(struct libusb_device_descriptor *dev_desc,
const uint16_t vids[], const uint16_t pids[]);
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
struct libusb_device_handle **out,
const char *product, struct libusb_device_handle **out,
adapter_get_alternate_serial_fn adapter_get_alternate_serial);
void jtag_libusb_close(struct libusb_device_handle *dev);
int jtag_libusb_control_transfer(struct libusb_device_handle *dev,
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/opendous.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ struct opendous_jtag *opendous_usb_open(void)
struct opendous_jtag *result;

struct libusb_device_handle *devh;
if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, &devh, NULL) != ERROR_OK)
if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, NULL, &devh, NULL) != ERROR_OK)
return NULL;

jtag_libusb_set_configuration(devh, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/openjtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ static int openjtag_init_cy7c65215(void)
int ret;

usbh = NULL;
ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, &usbh, NULL);
ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, NULL, &usbh, NULL);
if (ret != ERROR_OK) {
LOG_ERROR("unable to open cy7c65215 device");
goto err;
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/osbdm.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ static int osbdm_flush(struct osbdm *osbdm, struct queue *queue)
static int osbdm_open(struct osbdm *osbdm)
{
(void)memset(osbdm, 0, sizeof(*osbdm));
if (jtag_libusb_open(osbdm_vid, osbdm_pid, &osbdm->devh, NULL) != ERROR_OK)
if (jtag_libusb_open(osbdm_vid, osbdm_pid, NULL, &osbdm->devh, NULL) != ERROR_OK)
return ERROR_FAIL;

if (libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/rlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ static int rlink_init(void)

const uint16_t vids[] = { USB_IDVENDOR, 0 };
const uint16_t pids[] = { USB_IDPRODUCT, 0 };
if (jtag_libusb_open(vids, pids, &hdev, NULL) != ERROR_OK)
if (jtag_libusb_open(vids, pids, NULL, &hdev, NULL) != ERROR_OK)
return ERROR_FAIL;

struct libusb_device_descriptor descriptor;
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/stlink_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3406,7 +3406,7 @@ static int stlink_usb_usb_open(void *handle, struct hl_interface_param_s *param)
in order to become operational.
*/
do {
if (jtag_libusb_open(param->vid, param->pid,
if (jtag_libusb_open(param->vid, param->pid, NULL,
&h->usb_backend_priv.fd, stlink_usb_get_alternate_serial) != ERROR_OK) {
LOG_ERROR("open failed");
return ERROR_FAIL;
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/ti_icdi_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ static int icdi_usb_open(struct hl_interface_param_s *param, void **fd)

/* TI (Stellaris) ICDI provides its serial number in the USB descriptor;
no need to provide a callback here. */
jtag_libusb_open(param->vid, param->pid, &h->usb_dev, NULL);
jtag_libusb_open(param->vid, param->pid, NULL, &h->usb_dev, NULL);

if (!h->usb_dev) {
LOG_ERROR("open failed");
Expand Down
6 changes: 3 additions & 3 deletions src/jtag/drivers/usb_blaster/ublast2_access_libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
bool renumeration = false;
int ret;

if (jtag_libusb_open(vids, pids, &temp, NULL) == ERROR_OK) {
if (jtag_libusb_open(vids, pids, NULL, &temp, NULL) == ERROR_OK) {
LOG_INFO("Altera USB-Blaster II (uninitialized) found");
LOG_INFO("Loading firmware...");
ret = load_usb_blaster_firmware(temp, low);
Expand All @@ -216,13 +216,13 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
const uint16_t pids_renum[] = { low->ublast_pid, 0 };

if (renumeration == false) {
if (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK) {
if (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev, NULL) != ERROR_OK) {
LOG_ERROR("Altera USB-Blaster II not found");
return ERROR_FAIL;
}
} else {
int retry = 10;
while (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK && retry--) {
while (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev, NULL) != ERROR_OK && retry--) {
usleep(1000000);
LOG_INFO("Waiting for reenumerate...");
}
Expand Down
2 changes: 1 addition & 1 deletion src/jtag/drivers/usbprog.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ struct usbprog_jtag *usbprog_jtag_open(void)
const uint16_t pids[] = { PID, 0 };
struct libusb_device_handle *dev;

if (jtag_libusb_open(vids, pids, &dev, NULL) != ERROR_OK)
if (jtag_libusb_open(vids, pids, NULL, &dev, NULL) != ERROR_OK)
return NULL;

struct usbprog_jtag *tmp = malloc(sizeof(struct usbprog_jtag));
Expand Down

0 comments on commit a080d97

Please sign in to comment.