diff options
| author | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-03-24 16:40:15 +0100 |
|---|---|---|
| committer | Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org> | 2021-03-24 16:46:15 +0100 |
| commit | 261b9f6c810dd97061e3e8b47362d110de4d044a (patch) | |
| tree | 990aca8cde0dc0b00dd12c1ef6079f9d39dae02a | |
| parent | 24c1855bfb640e816b864f6af4387661360f0899 (diff) | |
| download | debugfs-gpio-trace-main.tar.gz debugfs-gpio-trace-main.tar.bz2 debugfs-gpio-trace-main.zip | |
In linux/drivers/gpio/gpiolib.c in Linux 5.11.0, we have:
static const struct seq_operations gpiolib_sops = {
.start = gpiolib_seq_start,
.next = gpiolib_seq_next,
.stop = gpiolib_seq_stop,
.show = gpiolib_seq_show,
};
DEFINE_SEQ_ATTRIBUTE(gpiolib);
[...]
debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
With DEFINE_SEQ_ATTRIBUTE being defined in this way in include/linux/seq_file.h:
#define DEFINE_SEQ_ATTRIBUTE(__name) \
static int __name ## _open(struct inode *inode, struct file *file) \
{ \
int ret = seq_open(file, &__name ## _sops); \
if (!ret && inode->i_private) { \
struct seq_file *seq_f = file->private_data; \
seq_f->private = inode->i_private; \
} \
return ret; \
} \
\
static const struct file_operations __name ## _fops = { \
.owner = THIS_MODULE, \
.open = __name ## _open, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = seq_release, \
}
So any userspace polling that uses the kernel .poll() API isn't supposed
to work. In addition the code in this commit also shows that it doesn't
block when there is no changes.
So at this point it's probably better to either read as fast as possible
or use printk in the kernel. The latter will be way more reliable.
With recent kernels there is a character device GPIO API that can also
monitor GPIOs in userspace.
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
| -rw-r--r-- | debugfs-gpio-trace.c | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/debugfs-gpio-trace.c b/debugfs-gpio-trace.c index 7390dfa..3baec31 100644 --- a/debugfs-gpio-trace.c +++ b/debugfs-gpio-trace.c @@ -312,8 +312,9 @@ ssize_t read_file(int fd, char* buf, ssize_t len) rc = read(fd, buf + count, sizeof(buf - count)); nr++; + printf("Total read %d bytes in %d reads with rc %d\n", count, nr, rc); + if (rc == 0) { - printf("Total read %d bytes in %d reads\n", count, nr); return count; } else if (rc == -1) { rc = errno; @@ -327,6 +328,42 @@ ssize_t read_file(int fd, char* buf, ssize_t len) } } +int monitor_loop(int fd, char* buf, ssize_t buf_size) +{ + int rc; + fd_set rfds; + off_t file_size; + struct timespec ts; + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + ts.tv_sec = 5; + + rc = pselect(fd + 1, &rfds, NULL, NULL, &ts, 0); + if (rc == -1){ + rc = errno; + printf("%s: pselect: %s (%d)\n", + __func__, strerror (rc), rc); + } else if (rc) { + if (FD_ISSET(fd, &rfds)) { + printf("[ %d ] " + "--------------------" + "--------------------" + "--------------------" + "--------------------" + "\n", rc); + file_size = read_file(fd, buf, buf_size); + rc = parse_file(fd, buf, file_size); + if (rc) + return rc; + } else { + printf("%s: pselect: no more data?\n", __func__); + } + } + +} + int main(int argc, char * const argv[]) { int fd; @@ -349,6 +386,7 @@ int main(int argc, char * const argv[]) return EX_OSFILE; } +#if 0 printf("%s: fd: %d\n", __func__, fd); file_size = mmap_file(fd, &mmap_buf); /* For tests on files */ @@ -364,7 +402,18 @@ int main(int argc, char * const argv[]) if (rc) return rc; } +#else + file_size = read_file(fd, static_buf, sizeof(static_buf)); + rc = parse_file(fd, static_buf, file_size); + if (rc) + return rc; + while (1) { + rc = monitor_loop(fd, static_buf, sizeof(static_buf)); + if (rc) + return rc; + } +#endif rc = close(fd); if (rc == -1) { rc = errno; |
