[PATCH 02/13] Create a unique cdev number for on demand devices

Juergen Beisert jbe at pengutronix.de
Thu Nov 24 07:43:40 EST 2011


For disk like devices attached to MCI, ATA or USB it depends on the order they
will be recognized. So an unique number for all disk like devices is required.

Signed-off-by: Juergen Beisert <jbe at pengutronix.de>
---
 fs/devfs-core.c  |   14 ++++++++++++++
 include/driver.h |    1 +
 2 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 519e18e..9bc3126 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -40,6 +40,20 @@ struct cdev *cdev_by_name(const char *filename)
 	return NULL;
 }
 
+int cdev_find_free_index(const char *basename)
+{
+	int i;
+	char fname[100];
+
+	for (i = 0; i < 1000; i++) {
+		snprintf(fname, sizeof(fname), "%s%d", basename, i);
+		if (cdev_by_name(fname) == NULL)
+			return i;
+	}
+
+	return -EBUSY;	/* all indexes are used */
+}
+
 struct cdev *cdev_open(const char *name, unsigned long flags)
 {
 	struct cdev *cdev = cdev_by_name(name);
diff --git a/include/driver.h b/include/driver.h
index 80de0c8..fe9d37a 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -394,6 +394,7 @@ struct cdev {
 
 int devfs_create(struct cdev *);
 int devfs_remove(struct cdev *);
+int cdev_find_free_index(const char *);
 struct cdev *cdev_by_name(const char *filename);
 struct cdev *cdev_open(const char *name, unsigned long flags);
 void cdev_close(struct cdev *cdev);
-- 
1.7.7.1




More information about the barebox mailing list