ao-tools: Wait for device to become ready instead of failing
authorKeith Packard <keithp@keithp.com>
Sat, 5 Apr 2014 06:38:40 +0000 (23:38 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 5 Apr 2014 07:22:30 +0000 (00:22 -0700)
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 <keithp@keithp.com>
ao-tools/lib/cc-usb.c

index d7ac138c6e1210b87c6bae203d76569c23812e26..a3a19d5cda58c875fce4c0dc90d54d15013fa3a2 100644 (file)
@@ -412,19 +412,35 @@ cc_usb_close_remote(struct cc_usb *cc)
 
 static struct termios  save_termios;
 
+#include <errno.h>
+
 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;