sysfsgpio: give time to udev to change gpio permission
[openocd.git] / src / jtag / drivers / sysfsgpio.c
index eb4941e6b241a3852dab87f12ade519116eb85ff..c6ffa3205a15ad6a6d7699c629cb9b8a717c942d 100644 (file)
@@ -52,6 +52,7 @@
 #include "config.h"
 #endif
 
+#include <helper/time_support.h>
 #include <jtag/interface.h>
 #include "bitbang.h"
 
@@ -112,6 +113,7 @@ static void unexport_sysfs_gpio(int gpio)
  */
 static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
 {
+       struct timeval timeout, now;
        char buf[40];
        char gpiostr[5];
        int ret;
@@ -131,8 +133,19 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
                }
        }
 
+       gettimeofday(&timeout, NULL);
+       timeval_add_time(&timeout, 0, 500000);
+
        snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
-       ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+       for (;;) {
+               ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+               if (ret >= 0 || errno != EACCES)
+                       break;
+               gettimeofday(&now, NULL);
+               if (timeval_compare(&now, &timeout) >= 0)
+                       break;
+               jtag_sleep(10000);
+       }
        if (ret < 0) {
                LOG_ERROR("Couldn't set direction for gpio %d", gpio);
                perror("sysfsgpio: ");
@@ -141,7 +154,15 @@ static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
        }
 
        snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
-       ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+       for (;;) {
+               ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+               if (ret >= 0 || errno != EACCES)
+                       break;
+               gettimeofday(&now, NULL);
+               if (timeval_compare(&now, &timeout) >= 0)
+                       break;
+               jtag_sleep(10000);
+       }
        if (ret < 0) {
                LOG_ERROR("Couldn't open value for gpio %d", gpio);
                perror("sysfsgpio: ");

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)