summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2023-01-30 08:27:04 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2023-01-31 09:19:47 +0100
commit955df99f2a5ad7c56f7677d20f3018db14d7f142 (patch)
tree4a000062912ca8e815b07294f53aa95394778d30
parentcc5666f23a1d84fba3c8765fc0c203e6746042ad (diff)
downloadbarebox-955df99f2a5ad7c56f7677d20f3018db14d7f142.tar.gz
fs: devfs: implement cdev_fdopen
As an alternative to cdev_open and using cdev_read/write, we define a new cdev_fdopen function that returns a file descriptor. The benefit of this is that code using it may use all the more high level helpers we have for reading/writing file descriptors. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Link: https://lore.barebox.org/20230130072707.2423294-2-ahmad@a3f.at Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--fs/devfs-core.c20
-rw-r--r--include/driver.h1
2 files changed, 21 insertions, 0 deletions
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 2a259c2fe0..fbcf68e815 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -177,6 +177,26 @@ int cdev_open(struct cdev *cdev, unsigned long flags)
return 0;
}
+int cdev_fdopen(struct cdev *cdev, unsigned long flags)
+{
+ char *path;
+ int fd;
+
+ if (!cdev)
+ return -ENODEV;
+ if (IS_ERR(cdev))
+ return PTR_ERR(cdev);
+
+ path = basprintf("/dev/%s", cdev->name);
+ if (!path)
+ return -ENOMEM;
+
+ fd = open(path, flags);
+
+ free(path);
+ return fd;
+}
+
struct cdev *cdev_open_by_name(const char *name, unsigned long flags)
{
struct cdev *cdev;
diff --git a/include/driver.h b/include/driver.h
index f0a0b9d6ae..f53668711b 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -535,6 +535,7 @@ struct cdev *cdev_open_by_name(const char *name, unsigned long flags);
struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset);
void cdev_remove_loop(struct cdev *cdev);
int cdev_open(struct cdev *, unsigned long flags);
+int cdev_fdopen(struct cdev *cdev, unsigned long flags);
void cdev_close(struct cdev *cdev);
int cdev_flush(struct cdev *cdev);
ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags);