From 7037858a3e41dd9df4c5ae03e05bd33c6e1b6ae9 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 11 May 2011 03:47:55 -0300 Subject: [PATCH] tools/usbwait/: new tool to wait for a USB device to appear - usbwait/usbwait.c: update the USB device tree until the specified device is detected - usbwait/Makefile: build usbwait (host only) - Makefile (DIRS, BEN_DIRS, upload): separated subdirectories with tools that can run on the Ben from those that can't - Makefile (DIRS): added usbwait/ --- tools/Makefile | 8 ++-- tools/usbwait/Makefile | 40 ++++++++++++++++ tools/usbwait/usbwait.c | 102 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 tools/usbwait/Makefile create mode 100644 tools/usbwait/usbwait.c diff --git a/tools/Makefile b/tools/Makefile index 036f3ef..dd6c518 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -11,8 +11,9 @@ # -DIRS=atrf-id atrf-path atrf-proxy atrf-reset atrf-rssi atrf-trim atrf-txrx \ - atrf-xmit atrf-xtal +BEN_DIRS=atrf-id atrf-path atrf-proxy atrf-reset atrf-rssi atrf-trim \ + atrf-txrx atrf-xmit atrf-xtal +DIRS=$(BEN_DIRS) usbwait TARGET_ONLY_DIRS=lib ifeq ($(TARGET),ben_jlime) @@ -27,4 +28,5 @@ include ../makefiles/Makefile.recurse .PHONY: upload upload: - scp `for n in $(DIRS); do echo $$n/$$n; done` $(HOST):/usr/bin/ + scp `for n in $(BEN_DIRS); do \ + echo $$n/$$n; done` $(HOST):/usr/bin/ diff --git a/tools/usbwait/Makefile b/tools/usbwait/Makefile new file mode 100644 index 0000000..d614a2a --- /dev/null +++ b/tools/usbwait/Makefile @@ -0,0 +1,40 @@ +# +# usbwait/Makefile - Wait for an USB device to appear +# +# Written 2011 by Werner Almesberger +# Copyright 2011 Werner Almesberger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + + +MAIN = usbwait + +ifeq ($(TARGET),) +TARGET = host +endif + +ifeq ($(TARGET),host) + +include ../Makefile.common + +else + +.PHONY: all install uninstall clean spotless + +all: + +install: + +uninstall: + +clean: + rm -f $(MAIN).o + +spotless: clean + rm -f $(MAIN) + +endif diff --git a/tools/usbwait/usbwait.c b/tools/usbwait/usbwait.c new file mode 100644 index 0000000..344da71 --- /dev/null +++ b/tools/usbwait/usbwait.c @@ -0,0 +1,102 @@ +/* + * usbwait/usbwait.c - Wait for a USB device to appear + * + * Written 2011 by Werner Almesberger + * Copyright 2011 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include + +#include "usbopen.h" + + +#define DEFAULT_POLL_S 0.1 + + +static useconds_t interval_us = DEFAULT_POLL_S*1000000; +static unsigned long timeout = 0; + + +static void wait_for_usb(void) +{ + struct timeval to, now; + + gettimeofday(&to, NULL); + to.tv_sec += timeout; + + while (1) { + usb_rescan(); + if (open_usb(0, 0)) + return; + if (timeout) { + gettimeofday(&now, NULL); + if (now.tv_sec > to.tv_sec) + break; + if (now.tv_sec == to.tv_sec && + now.tv_usec > to.tv_usec) + break; + } + if (usleep(interval_us) < 0) { + perror("usleep"); + exit(1); + } + } + + fprintf(stderr, "timeout\n"); + exit(1); +} + + +static void usage(const char *name) +{ + fprintf(stderr, +"usage: %s [-i poll_s] [-p path] [-t timeout_s] [vendor]:[product]\n\n" +" -i poll_s poll interval in seconds (default: %g s)\n" +" -p path USB device path\n" +" -t timeout_s timeout in seconds (default: infinite)\n" + , name, DEFAULT_POLL_S); + exit(1); +} + + +int main(int argc, char **argv) +{ + char *end; + int c; + + while ((c = getopt(argc, argv, "i:p:t:")) != EOF) + switch (c) { + case 'i': + interval_us = strtod(optarg, &end)*1000000; + if (*end || interval_us < 0) + usage(*argv); + break; + case 'p': + restrict_usb_path(optarg); + break; + case 't': + timeout = strtoul(optarg, &end, 0); + if (*end || !timeout) + usage(*argv); + break; + default: + usage(*argv); + } + + if (argc != optind+1) + usage(*argv); + + parse_usb_id(argv[optind]); + wait_for_usb(); + + return 0; +}