add -V option to specify/override VID and PID of USB device

This commit is contained in:
Triss 2021-10-09 23:43:48 +02:00
parent 4c4d94e43b
commit c576441fb7
10 changed files with 67 additions and 26 deletions

View File

@ -364,7 +364,8 @@ static device_t bsl_open(const struct device_args *args)
if (args->flags & DEVICE_FLAG_TTY) if (args->flags & DEVICE_FLAG_TTY)
dev->serial = comport_open(args->path, 460800); dev->serial = comport_open(args->path, 460800);
else else
dev->serial = ti3410_open(args->path, args->requested_serial); dev->serial = ti3410_open(args->path, args->requested_serial,
(args->flags & DEVICE_FLAG_HAS_VID_PID), args->vid, args->pid);
if (!dev->serial) { if (!dev->serial) {
free(dev); free(dev);

View File

@ -75,6 +75,7 @@ struct device_breakpoint {
#define DEVICE_FLAG_DO_FWUPDATE 0x10 #define DEVICE_FLAG_DO_FWUPDATE 0x10
#define DEVICE_FLAG_SKIP_CLOSE 0x20 #define DEVICE_FLAG_SKIP_CLOSE 0x20
#define DEVICE_FLAG_BSL_NME 0x40 /* BSL no-mass-erase */ #define DEVICE_FLAG_BSL_NME 0x40 /* BSL no-mass-erase */
#define DEVICE_FLAG_HAS_VID_PID 0x80
struct device_args { struct device_args {
int flags; int flags;
@ -88,6 +89,7 @@ struct device_args {
int bsl_gpio_rts; int bsl_gpio_rts;
int bsl_gpio_dtr; int bsl_gpio_dtr;
uint8_t bsl_entry_password[32]; uint8_t bsl_entry_password[32];
uint16_t vid, pid;
}; };
struct device_class { struct device_class {

View File

@ -44,9 +44,11 @@ static device_t fet_open_rf2500(const struct device_args *args)
} }
#if defined(__APPLE__) #if defined(__APPLE__)
trans = rf2500hidapi_open(args->path, args->requested_serial); trans = rf2500hidapi_open(args->path, args->requested_serial,
(args->flags & DEVICE_FLAG_HAS_VID_PID), args->vid, args->pid);
#else #else
trans = rf2500_open(args->path, args->requested_serial); trans = rf2500_open(args->path, args->requested_serial,
(args->flags & DEVICE_FLAG_HAS_VID_PID), args->vid, args->pid);
#endif #endif
if (!trans) if (!trans)
return NULL; 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) if (args->flags & DEVICE_FLAG_TTY)
trans = comport_open(args->path, 115200); trans = comport_open(args->path, 115200);
else else
trans = cdc_acm_open(args->path, args->requested_serial, trans = cdc_acm_open(args->path, args->requested_serial, 115200,
115200, 0x15ba, 0x0100); (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->vid : 0x15ba,
(args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->pid : 0x0100);
if (!trans) if (!trans)
return NULL; return NULL;
@ -100,9 +103,9 @@ static device_t fet_open_olimex_iso_mk2(const struct device_args *args)
if (args->flags & DEVICE_FLAG_TTY) if (args->flags & DEVICE_FLAG_TTY)
trans = comport_open(args->path, 115200); trans = comport_open(args->path, 115200);
else else
trans = cdc_acm_open(args->path, trans = cdc_acm_open(args->path, args->requested_serial, 115200,
args->requested_serial, (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->vid : 0x15ba,
115200, 0x15ba, 0x0100); (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->pid : 0x0100);
if (!trans) if (!trans)
return NULL; return NULL;
@ -141,8 +144,9 @@ static device_t fet_open_olimex(const struct device_args *args)
if (args->flags & DEVICE_FLAG_TTY) if (args->flags & DEVICE_FLAG_TTY)
trans = comport_open(args->path, 115200); trans = comport_open(args->path, 115200);
else else
trans = cdc_acm_open(args->path, args->requested_serial, trans = cdc_acm_open(args->path, args->requested_serial, 115200,
115200, 0x15ba, 0x0031); (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->vid : 0x15ba,
(args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->pid : 0x0031);
if (!trans) if (!trans)
return NULL; return NULL;
@ -177,8 +181,9 @@ static device_t fet_open_olimex_v1(const struct device_args *args)
if (args->flags & DEVICE_FLAG_TTY) if (args->flags & DEVICE_FLAG_TTY)
trans = comport_open(args->path, 500000); trans = comport_open(args->path, 500000);
else else
trans = cp210x_open(args->path, args->requested_serial, trans = cp210x_open(args->path, args->requested_serial, 500000,
500000, 0x15ba, 0x0002); (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->vid : 0x15ba,
(args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->pid : 0x0002);
if (!trans) if (!trans)
return NULL; return NULL;
@ -213,7 +218,9 @@ static device_t fet_open_olimex_iso(const struct device_args *args)
trans = comport_open(args->path, 200000); trans = comport_open(args->path, 200000);
else else
trans = ftdi_open(args->path, args->requested_serial, trans = ftdi_open(args->path, args->requested_serial,
0x15ba, 0x0008, 200000); (args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->vid : 0x15ba,
(args->flags & DEVICE_FLAG_HAS_VID_PID) ? args->pid : 0x0008,
200000);
if (!trans) if (!trans)
return NULL; return NULL;
@ -248,7 +255,8 @@ static device_t fet_open_uif(const struct device_args *args)
if (args->flags & DEVICE_FLAG_TTY) if (args->flags & DEVICE_FLAG_TTY)
trans = comport_open(args->path, 460800); trans = comport_open(args->path, 460800);
else else
trans = ti3410_open(args->path, args->requested_serial); trans = ti3410_open(args->path, args->requested_serial,
(args->flags & DEVICE_FLAG_HAS_VID_PID), args->vid, args->pid);
if (!trans) if (!trans)
return NULL; return NULL;

View File

@ -52,6 +52,10 @@ section \fBDRIVERS\fR below for details.
.IP "\-U \fIbus\fR:\fIdevice\fR" .IP "\-U \fIbus\fR:\fIdevice\fR"
Specify a particular USB device to connect to. Without this option, Specify a particular USB device to connect to. Without this option,
the first device of the appropriate type is opened. the first device of the appropriate type is opened.
.IP "\-V \fIvid\fR:\fIpid\fR"
Specify a VID and PID of the USB device to connect to, or override the
default VID and PID. Some drives, such as \fBmehfet\fR, require this option,
while others have a default VID and PID which can be overridden.
.IP "\-s \fIserial\fR" .IP "\-s \fIserial\fR"
Specify a particular USB device serial number to connect to. Use this Specify a particular USB device serial number to connect to. Use this
option to distinguish between multiple devices of the same type. option to distinguish between multiple devices of the same type.

View File

@ -228,7 +228,8 @@ static const struct transport_class rf2500_transport = {
.set_modem = usbtr_set_modem .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 rf2500_transport *tr = malloc(sizeof(*tr));
struct usb_device *dev; struct usb_device *dev;
@ -248,8 +249,9 @@ transport_t rf2500_open(const char *devpath, const char *requested_serial)
if (devpath) if (devpath)
dev = usbutil_find_by_loc(devpath); dev = usbutil_find_by_loc(devpath);
else else
dev = usbutil_find_by_id(USB_FET_VENDOR, USB_FET_PRODUCT, dev = usbutil_find_by_id(has_vid_pid ? vid : USB_FET_VENDOR,
requested_serial); has_vid_pid ? pid : USB_FET_PRODUCT,
requested_serial);
if (!dev) { if (!dev) {
free(tr); free(tr);

View File

@ -27,7 +27,9 @@
* *
* A particular device may be specified in bus:dev form. * A particular device may be specified in bus:dev form.
*/ */
transport_t rf2500_open(const char *dev_path, const char *requested_serial); transport_t rf2500_open(const char *dev_path, const char *requested_serial,
transport_t rf2500hidapi_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 #endif

View File

@ -161,7 +161,8 @@ static const wchar_t * get_wc(const char *c)
return wc; 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)); struct rf2500_transport *tr = malloc(sizeof(*tr));
hid_device *handle; hid_device *handle;
@ -186,7 +187,9 @@ transport_t rf2500hidapi_open(const char *devpath, const char *requested_serial)
} else { } else {
wc_serial = NULL; 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 ) { if ( wc_serial ) {
free((wchar_t *)wc_serial); free((wchar_t *)wc_serial);
} }

View File

@ -559,7 +559,8 @@ static const struct transport_class ti3410_transport = {
.set_modem = ti3410_set_modem .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 ti3410_transport *tr = malloc(sizeof(*tr));
struct usb_device *dev; struct usb_device *dev;
@ -578,8 +579,9 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial)
if (devpath) if (devpath)
dev = usbutil_find_by_loc(devpath); dev = usbutil_find_by_loc(devpath);
else else
dev = usbutil_find_by_id(USB_FET_VENDOR, USB_FET_PRODUCT, dev = usbutil_find_by_id(has_vid_pid ? vid : USB_FET_VENDOR,
requested_serial); has_vid_pid ? pid : USB_FET_PRODUCT,
requested_serial);
if (!dev) { if (!dev) {
free(tr); free(tr);

View File

@ -24,6 +24,8 @@
/* This function is for opening an eZ430-F2013 or FET430UIF device via /* This function is for opening an eZ430-F2013 or FET430UIF device via
* libusb. * 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 #endif

View File

@ -114,6 +114,9 @@ static void usage(const char *progname)
" Connect via the given tty device, rather than USB.\n" " Connect via the given tty device, rather than USB.\n"
" -U bus:dev\n" " -U bus:dev\n"
" Specify a particular USB device to connect to.\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" " -s serial\n"
" Specify a particular device serial number to connect to.\n" " Specify a particular device serial number to connect to.\n"
" -j\n" " -j\n"
@ -328,7 +331,7 @@ static int parse_cmdline_args(int argc, char **argv,
int opt; int opt;
int want_usb = 0; int want_usb = 0;
while ((opt = getopt_long(argc, argv, "d:jv:nU:s:qC:", while ((opt = getopt_long(argc, argv, "d:jv:nUV:s:qC:",
longopts, NULL)) >= 0) longopts, NULL)) >= 0)
switch (opt) { switch (opt) {
case 'C': case 'C':
@ -398,6 +401,18 @@ static int parse_cmdline_args(int argc, char **argv,
want_usb = 1; want_usb = 1;
break; 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 '<vid>:<pid>'\n", optarg);
exit(1);
}
break;
case 's': case 's':
args->devarg.requested_serial = optarg; args->devarg.requested_serial = optarg;
break; break;