From: Oleksij Rempel <o.rempel@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Subject: [PATCH] scripts: imx imx-usb-loader: add usb path support
Date: Thu, 2 Mar 2017 15:31:14 +0100 [thread overview]
Message-ID: <20170302143114.8319-1-o.rempel@pengutronix.de> (raw)
In some cases we need to work with more then one device attached
to one host. For this situation we need path filter to make sure
we talk with proper device.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
scripts/imx/imx-usb-loader.c | 97 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 84 insertions(+), 13 deletions(-)
diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c
index be0894fa6..d8a72d801 100644
--- a/scripts/imx/imx-usb-loader.c
+++ b/scripts/imx/imx-usb-loader.c
@@ -45,6 +45,11 @@
#define FT_DCD 0xee
#define FT_LOAD_ONLY 0x00
+/* comment from libusb:
+ * As per the USB 3.0 specs, the current maximum limit for the depth is 7.
+ */
+#define MAX_USB_PORTS 7
+
int verbose;
static struct libusb_device_handle *usb_dev_handle;
static struct usb_id *usb_id;
@@ -190,9 +195,63 @@ static struct mach_id *imx_device(unsigned short vid, unsigned short pid)
return NULL;
}
-static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id)
+static int device_location_equal(libusb_device *device, const char *location)
+{
+ uint8_t port_path[MAX_USB_PORTS];
+ uint8_t dev_bus;
+ int path_step, path_len;
+ int result = 0;
+ char *ptr, *loc;
+
+ path_len = libusb_get_port_numbers(device, port_path, MAX_USB_PORTS);
+ if (path_len == LIBUSB_ERROR_OVERFLOW) {
+ fprintf(stderr, "cannot determine path to usb device! (more than %i ports in path)\n",
+ MAX_USB_PORTS);
+ goto done;
+ }
+
+ /* strtok need non const char */
+ loc = strdup(location);
+ ptr = strtok(loc, "-");
+ if (ptr == NULL) {
+ printf("no '-' in path\n");
+ goto done;
+ }
+
+ dev_bus = libusb_get_bus_number(device);
+ /* check bus mismatch */
+ if (atoi(ptr) != dev_bus)
+ goto done;
+
+ path_step = 0;
+ while (path_step < MAX_USB_PORTS) {
+ ptr = strtok(NULL, ".");
+
+ /* no more tokens in path */
+ if (ptr == NULL)
+ break;
+
+ /* path mismatch at some step */
+ if (path_step < path_len && atoi(ptr) != port_path[path_step])
+ break;
+
+ path_step++;
+ };
+
+ /* walked the full path, all elements match */
+ if (path_step == path_len)
+ result = 1;
+
+done:
+ free(loc);
+ return result;
+}
+
+static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id,
+ const char *location)
{
int i = 0;
+ int err;
struct mach_id *p;
for (;;) {
@@ -210,10 +269,24 @@ static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id)
}
p = imx_device(desc.idVendor, desc.idProduct);
- if (p) {
- *pp_id = p;
- return dev;
+ if (!p)
+ continue;
+
+ err = libusb_open(dev, &usb_dev_handle);
+ if (err) {
+ fprintf(stderr, "Could not open device vid=0x%x pid=0x%x err=%d\n",
+ p->vid, p->pid, err);
+ continue;
+ }
+
+ if (location && !device_location_equal(dev, location)) {
+ libusb_close(usb_dev_handle);
+ usb_dev_handle = NULL;
+ continue;
}
+
+ *pp_id = p;
+ return dev;
}
*pp_id = NULL;
@@ -1280,6 +1353,7 @@ static void usage(const char *prgname)
fprintf(stderr, "usage: %s [OPTIONS] [FILENAME]\n\n"
"-c check correctness of flashed image\n"
"-i <cfgfile> Specify custom SoC initialization file\n"
+ "-p <devpath> Specify device path: <bus>-<port>[.<port>]...\n"
"-s skip DCD included in image\n"
"-v verbose (give multiple times to increase)\n"
"-h this help\n", prgname);
@@ -1300,10 +1374,11 @@ int main(int argc, char *argv[])
struct usb_work w = {};
int opt;
char *initfile = NULL;
+ char *devpath = NULL;
w.do_dcd_once = 1;
- while ((opt = getopt(argc, argv, "cvhi:s")) != -1) {
+ while ((opt = getopt(argc, argv, "cvhi:p:s")) != -1) {
switch (opt) {
case 'c':
verify = 1;
@@ -1316,6 +1391,9 @@ int main(int argc, char *argv[])
case 'i':
initfile = optarg;
break;
+ case 'p':
+ devpath = optarg;
+ break;
case 's':
w.do_dcd_once = 0;
break;
@@ -1343,19 +1421,12 @@ int main(int argc, char *argv[])
goto out;
}
- dev = find_imx_dev(devs, &mach);
+ dev = find_imx_dev(devs, &mach, devpath);
if (!dev) {
fprintf(stderr, "no supported device found\n");
goto out;
}
- err = libusb_open(dev, &usb_dev_handle);
- if (err) {
- fprintf(stderr, "Could not open device vid=0x%x pid=0x%x err=%d\n",
- mach->vid, mach->pid, err);
- goto out;
- }
-
libusb_free_device_list(devs, 1);
libusb_get_configuration(usb_dev_handle, &config);
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
reply other threads:[~2017-03-02 14:31 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170302143114.8319-1-o.rempel@pengutronix.de \
--to=o.rempel@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox