From: Keith Packard Date: Sat, 5 Apr 2014 06:38:40 +0000 (-0700) Subject: ao-tools: Wait for device to become ready instead of failing X-Git-Tag: 1.3.2.2~151 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=ca4b20f9781b1dc6974d26952973dfe0d607478c;ds=sidebyside ao-tools: Wait for device to become ready instead of failing For some reason, USB devices take 'a while' to become usable; instead of bailing immediately, sit around waiting to see if the device becomes usable if we get an EBUSY or EACCES error. Signed-off-by: Keith Packard --- diff --git a/ao-tools/lib/cc-usb.c b/ao-tools/lib/cc-usb.c index d7ac138c..a3a19d5c 100644 --- a/ao-tools/lib/cc-usb.c +++ b/ao-tools/lib/cc-usb.c @@ -412,19 +412,35 @@ cc_usb_close_remote(struct cc_usb *cc) static struct termios save_termios; +#include + struct cc_usb * cc_usb_open(char *tty) { struct cc_usb *cc; struct termios termios; + int i; if (!tty) tty = DEFAULT_TTY; cc = calloc (sizeof (struct cc_usb), 1); if (!cc) return NULL; - cc->fd = open(tty, O_RDWR | O_NONBLOCK); - if (cc->fd < 0) { + i = 0; + for (;;) { + cc->fd = open(tty, O_RDWR | O_NONBLOCK); + if (cc->fd >= 0) + break; + i++; + if (errno == EBUSY || errno == EPERM || errno == EACCES) { + fprintf(stderr, "open failed, pausing"); + perror(tty); + if (i < 20) { + sleep(3); + continue; + } + } + perror(tty); free (cc); return NULL;