mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marc Reilly <marc@cpdesign.com.au>
To: s.hauer@pengutronix.de
Cc: barebox@lists.infradead.org
Subject: [PATCH] devfs: new func make_cdev_name: allocate unique cdev name
Date: Sun, 16 Jan 2011 12:04:18 +1100	[thread overview]
Message-ID: <1295139858-9193-2-git-send-email-marc@cpdesign.com.au> (raw)
In-Reply-To: <20110114093937.GF24373@pengutronix.de>

Signed-off-by: Marc Reilly <marc@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


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2011-01-16  1:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-14  0:06 Add basic driver for 24clxx eeproms Marc Reilly
2011-01-14  0:07 ` [PATCH 1/2] i2c: add platform_data for i2c_board_info Marc Reilly
2011-01-14  0:07 ` [PATCH 2/2] at24: add I2C eeprom for 24cl02 Marc Reilly
2011-01-14  9:19   ` Sascha Hauer
2011-01-14  9:39     ` Sascha Hauer
2011-01-16  1:04       ` Marc Reilly [this message]
     [not found]       ` <1295139858-9193-1-git-send-email-marc@cpdesign.com.au>
2011-01-17 18:01         ` cdev name generation Sascha Hauer
2011-01-17 22:22           ` Marc Reilly
2011-01-19  8:41             ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1295139858-9193-2-git-send-email-marc@cpdesign.com.au \
    --to=marc@cpdesign.com.au \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox