From 7ef085140a67ee159078d38d196831ef87a285be Mon Sep 17 00:00:00 2001 From: sys64738 Date: Tue, 5 Oct 2021 19:34:48 +0200 Subject: [PATCH] add -V option to specify a VID/PID pair, or override the default --- .gitignore | 2 ++ Makefile | 2 ++ drivers/bsl.c | 3 ++- drivers/device.h | 3 +++ drivers/fet.c | 32 ++++++++++++++++++++------------ transport/rf2500.c | 6 ++++-- transport/rf2500.h | 6 ++++-- transport/rf2500hidapi.c | 6 ++++-- transport/ti3410.c | 6 ++++-- transport/ti3410.h | 3 ++- ui/main.c | 17 ++++++++++++++++- 11 files changed, 63 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index dede4b8..12b1651 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.o mspdebug mspdebug.exe +inst/ +config.mk diff --git a/Makefile b/Makefile index 6e0cb2d..7af522b 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +-include config.mk + CC ?= gcc INSTALL = /usr/bin/install PREFIX ?= /usr/local diff --git a/drivers/bsl.c b/drivers/bsl.c index c2fa678..bf176c9 100644 --- a/drivers/bsl.c +++ b/drivers/bsl.c @@ -364,7 +364,8 @@ static device_t bsl_open(const struct device_args *args) if (args->flags & DEVICE_FLAG_TTY) dev->serial = comport_open(args->path, 460800); else - dev->serial = ti3410_open(args->path, args->requested_serial); + dev->serial = ti3410_open(args->path, args->requested_serial, + args->has_vid_pid, args->vid, args->pid); if (!dev->serial) { free(dev); diff --git a/drivers/device.h b/drivers/device.h index 2ac099d..92898ec 100644 --- a/drivers/device.h +++ b/drivers/device.h @@ -75,6 +75,7 @@ struct device_breakpoint { #define DEVICE_FLAG_DO_FWUPDATE 0x10 #define DEVICE_FLAG_SKIP_CLOSE 0x20 #define DEVICE_FLAG_BSL_NME 0x40 /* BSL no-mass-erase */ +#define DEVICE_FLAG_HAS_VID_PID 0x80 struct device_args { int flags; @@ -88,6 +89,8 @@ struct device_args { int bsl_gpio_rts; int bsl_gpio_dtr; uint8_t bsl_entry_password[32]; + uint16_t vid, pid; + int has_vid_pid; }; struct device_class { diff --git a/drivers/fet.c b/drivers/fet.c index 51a03f2..61e8cd1 100644 --- a/drivers/fet.c +++ b/drivers/fet.c @@ -44,9 +44,11 @@ static device_t fet_open_rf2500(const struct device_args *args) } #if defined(__APPLE__) - trans = rf2500hidapi_open(args->path, args->requested_serial); + trans = rf2500hidapi_open(args->path, args->requested_serial, + args->has_vid_pid, args->vid, args->pid); #else - trans = rf2500_open(args->path, args->requested_serial); + trans = rf2500_open(args->path, args->requested_serial, + args->has_vid_pid, args->vid, args->pid); #endif if (!trans) return NULL; @@ -79,8 +81,9 @@ static device_t fet_open_olimex_iso_mk2(const struct device_args *args) if (args->flags & DEVICE_FLAG_TTY) trans = comport_open(args->path, 115200); else - trans = cdc_acm_open(args->path, args->requested_serial, - 115200, 0x15ba, 0x0100); + trans = cdc_acm_open(args->path, args->requested_serial, 115200, + args->has_vid_pid ? args->vid : 0x15ba, + args->has_vid_pid ? args->pid : 0x0100); if (!trans) return NULL; @@ -101,8 +104,9 @@ static device_t fet_open_olimex_iso_mk2(const struct device_args *args) trans = comport_open(args->path, 115200); else trans = cdc_acm_open(args->path, - args->requested_serial, - 115200, 0x15ba, 0x0100); + args->requested_serial, 115200, + args->has_vid_pid ? args->vid : 0x15ba, + args->has_vid_pid ? args->pid : 0x0100); if (!trans) return NULL; @@ -141,8 +145,9 @@ static device_t fet_open_olimex(const struct device_args *args) if (args->flags & DEVICE_FLAG_TTY) trans = comport_open(args->path, 115200); else - trans = cdc_acm_open(args->path, args->requested_serial, - 115200, 0x15ba, 0x0031); + trans = cdc_acm_open(args->path, args->requested_serial, 115200, + args->has_vid_pid ? args->vid : 0x15ba, + args->has_vid_pid ? args->pid : 0x0031); if (!trans) return NULL; @@ -177,8 +182,9 @@ static device_t fet_open_olimex_v1(const struct device_args *args) if (args->flags & DEVICE_FLAG_TTY) trans = comport_open(args->path, 500000); else - trans = cp210x_open(args->path, args->requested_serial, - 500000, 0x15ba, 0x0002); + trans = cp210x_open(args->path, args->requested_serial, 500000, + args->has_vid_pid ? args->vid : 0x15ba, + args->has_vid_pid ? args->pid : 0x0002); if (!trans) return NULL; @@ -213,7 +219,8 @@ static device_t fet_open_olimex_iso(const struct device_args *args) trans = comport_open(args->path, 200000); else trans = ftdi_open(args->path, args->requested_serial, - 0x15ba, 0x0008, 200000); + args->has_vid_pid ? args->vid : 0x15ba, + args->has_vid_pid ? args->pid : 0x0008, 200000); if (!trans) return NULL; @@ -248,7 +255,8 @@ static device_t fet_open_uif(const struct device_args *args) if (args->flags & DEVICE_FLAG_TTY) trans = comport_open(args->path, 460800); else - trans = ti3410_open(args->path, args->requested_serial); + trans = ti3410_open(args->path, args->requested_serial, + args->has_vid_pid, args->vid, args->pid); if (!trans) return NULL; diff --git a/transport/rf2500.c b/transport/rf2500.c index f0544ae..35e4486 100644 --- a/transport/rf2500.c +++ b/transport/rf2500.c @@ -228,7 +228,8 @@ static const struct transport_class rf2500_transport = { .set_modem = usbtr_set_modem }; -transport_t rf2500_open(const char *devpath, const char *requested_serial) +transport_t rf2500_open(const char *devpath, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid) { struct rf2500_transport *tr = malloc(sizeof(*tr)); struct usb_device *dev; @@ -248,7 +249,8 @@ transport_t rf2500_open(const char *devpath, const char *requested_serial) if (devpath) dev = usbutil_find_by_loc(devpath); else - dev = usbutil_find_by_id(USB_FET_VENDOR, USB_FET_PRODUCT, + dev = usbutil_find_by_id(has_vid_pid ? vid : USB_FET_VENDOR, + has_vid_pid ? pid : USB_FET_PRODUCT, requested_serial); if (!dev) { diff --git a/transport/rf2500.h b/transport/rf2500.h index f523071..e637336 100644 --- a/transport/rf2500.h +++ b/transport/rf2500.h @@ -27,7 +27,9 @@ * * A particular device may be specified in bus:dev form. */ -transport_t rf2500_open(const char *dev_path, const char *requested_serial); -transport_t rf2500hidapi_open(const char *dev_path, const char *requested_serial); +transport_t rf2500_open(const char *dev_path, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid); +transport_t rf2500hidapi_open(const char *dev_path, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid); #endif diff --git a/transport/rf2500hidapi.c b/transport/rf2500hidapi.c index d971f6c..749c902 100644 --- a/transport/rf2500hidapi.c +++ b/transport/rf2500hidapi.c @@ -161,7 +161,8 @@ static const wchar_t * get_wc(const char *c) return wc; } -transport_t rf2500hidapi_open(const char *devpath, const char *requested_serial) +transport_t rf2500hidapi_open(const char *devpath, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid) { struct rf2500_transport *tr = malloc(sizeof(*tr)); hid_device *handle; @@ -186,7 +187,8 @@ transport_t rf2500hidapi_open(const char *devpath, const char *requested_serial) } else { wc_serial = NULL; } - handle = hid_open(USB_FET_VENDOR, USB_FET_PRODUCT, wc_serial); + handle = hid_open(has_vid_pid ? vid : USB_FET_VENDOR, + has_vid_pid ? pid : USB_FET_PRODUCT, wc_serial); if ( wc_serial ) { free((wchar_t *)wc_serial); } diff --git a/transport/ti3410.c b/transport/ti3410.c index b96fed8..c17f51f 100644 --- a/transport/ti3410.c +++ b/transport/ti3410.c @@ -559,7 +559,8 @@ static const struct transport_class ti3410_transport = { .set_modem = ti3410_set_modem }; -transport_t ti3410_open(const char *devpath, const char *requested_serial) +transport_t ti3410_open(const char *devpath, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid) { struct ti3410_transport *tr = malloc(sizeof(*tr)); struct usb_device *dev; @@ -578,7 +579,8 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial) if (devpath) dev = usbutil_find_by_loc(devpath); else - dev = usbutil_find_by_id(USB_FET_VENDOR, USB_FET_PRODUCT, + dev = usbutil_find_by_id(has_vid_pid ? vid : USB_FET_VENDOR, + has_vid_pid ? pid : USB_FET_PRODUCT, requested_serial); if (!dev) { diff --git a/transport/ti3410.h b/transport/ti3410.h index 8ef879e..f97e98d 100644 --- a/transport/ti3410.h +++ b/transport/ti3410.h @@ -24,6 +24,7 @@ /* This function is for opening an eZ430-F2013 or FET430UIF device via * libusb. */ -transport_t ti3410_open(const char *dev_path, const char *requested_serial); +transport_t ti3410_open(const char *dev_path, const char *requested_serial, + int has_vid_pid, uint16_t vid, uint16_t pid); #endif diff --git a/ui/main.c b/ui/main.c index 7a914ac..87524ff 100644 --- a/ui/main.c +++ b/ui/main.c @@ -92,7 +92,7 @@ static const struct device_class *const driver_table[] = { &device_loadbsl, &device_ezfet, &device_rom_bsl, - &device_bp + &device_bp, }; static const char *version_text = @@ -114,6 +114,9 @@ static void usage(const char *progname) " Connect via the given tty device, rather than USB.\n" " -U bus:dev\n" " Specify a particular USB device to connect to.\n" +" -V vid:pid\n" +" Specify a particular vid:pid par of a USB to connect to, instead of\n" +" a driver-specific default.\n" " -s serial\n" " Specify a particular device serial number to connect to.\n" " -j\n" @@ -398,6 +401,18 @@ static int parse_cmdline_args(int argc, char **argv, want_usb = 1; break; + case 'V': + // optarg is in "vid:pid" format, which we now need to parse + if (sscanf(optarg, "%04hx:%04hx", &args->devarg.vid, &args->devarg.pid) == 2) { + args->devarg.flags |= DEVICE_FLAG_HAS_VID_PID; + want_usb = 1; + } else { + printc("Invalid -V option specified: %s." + "Format should be ':'\n", optarg); + exit(1); + } + break; + case 's': args->devarg.requested_serial = optarg; break;