[PATCH] devfs: new func make_cdev_name: allocate unique cdev name

Marc Reilly marc at cpdesign.com.au
Sat Jan 15 20:04:18 EST 2011


Signed-off-by: Marc Reilly <marc at cpdesign.com.au>
---
 fs/devfs.c       |   41 +++++++++++++++++++++++++++++++++++++++++
 include/driver.h |    1 +
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/fs/devfs.c b/fs/devfs.c
index 7019c8d..c1196d1 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -38,6 +38,47 @@
 
 static LIST_HEAD(cdev_list);
 
+/**
+ * Allocates a unique name for a cdev based on existing cdevs.
+ *
+ * @param template Starting point for the name of the device.
+ * The name can optionally contain a "%d" marker to designate where the
+ * device number should be inserted into the device string. If no marker
+ * is specified then one is appended.
+ *
+ * @param prefid Preferred device id. Any id less than zero will default
+ * to start from 0.
+ */
+char *make_cdev_name(const char *template, int prefid)
+{
+	char *name = 0;
+	char *temp;
+	int id;
+	struct cdev *cdev;
+
+	/* if there is no template for number, append one */
+	if (!strstr(template, "%d"))
+		temp = asprintf("%s%%d", template);
+	else
+		temp = strdup(template);
+
+	id = (prefid < 0) ? 0 : prefid;
+
+	do {
+		free(name);
+		name = asprintf(temp, id);
+
+		cdev = cdev_by_name(name);
+		if (cdev && (id == prefid))
+			printf("WARN: preferred device name %s already used\n",
+					name);
+		++id;
+	} while (cdev);
+
+	free(temp);
+	return name;
+}
+
 struct cdev *cdev_by_name(const char *filename)
 {
 	struct cdev *cdev;
diff --git a/include/driver.h b/include/driver.h
index b9edca0..1712637 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -313,6 +313,7 @@ struct cdev {
 
 int devfs_create(struct cdev *);
 int devfs_remove(struct cdev *);
+char *make_cdev_name(const char *template, int prefid);
 struct cdev *cdev_by_name(const char *filename);
 ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, ulong offset, ulong flags);
 ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, ulong offset, ulong flags);
-- 
1.7.1




More information about the barebox mailing list