mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Corbet <corbet@lwn.net>
To: linux-kernel@vger.kernel.org
Cc: viro@parcelfarce.linux.theplanet.co.uk
Subject: [PATCH] RFC: Attributes in /sys/cdev
Date: Fri, 19 Sep 2003 17:10:46 -0600	[thread overview]
Message-ID: <20030919231046.4626.qmail@lwn.net> (raw)

I assume that the (empty, currently) entries in /sys/cdev are there for
some purpose...  for purposes of my own, I've been thinking that it would
be nice to be able to find which device numbers got assigned when using the
new char dev registration functions.  Thus, the following patch, which adds
default "dev" and "count" attributes to /sys/cdev entries.

I have no idea whether this follows the original plan for /sys/cdev.
There *is* a plan for it, no...?

jon

Jonathan Corbet
Executive editor, LWN.net
corbet@lwn.net

diff -urN -X dontdiff test5-vanilla/fs/char_dev.c test5/fs/char_dev.c
--- test5-vanilla/fs/char_dev.c	Mon Sep  8 13:50:01 2003
+++ test5/fs/char_dev.c	Sat Sep 20 06:46:24 2003
@@ -342,7 +342,13 @@
 
 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
 {
-	int err = kobject_add(&p->kobj);
+	int err;
+
+	/* Remember for sysfs; maybe caller should set these? */
+	p->firstdev = dev;
+	p->count = count;
+	
+	err = kobject_add(&p->kobj);
 	if (err)
 		return err;
 	err = kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
@@ -383,6 +389,61 @@
 	}
 }
 
+/*
+ * Simple /sys/cdev attribute stuff, added by corbet@lwn.net.  This is
+ * heavily patterned after the /sys/block code.
+ */
+struct cdev_attribute {
+	struct attribute attr;
+	ssize_t (*show)(struct cdev *, char *);
+};
+
+static ssize_t cdev_dev_read(struct cdev *cd, char *page)
+{
+	return sprintf(page, "%d:%d\n", MAJOR(cd->firstdev),
+			MINOR(cd->firstdev));
+}
+
+static ssize_t cdev_count_read(struct cdev *cd, char *page)
+{
+	return sprintf(page, "%d\n", cd->count);
+}
+
+static struct cdev_attribute cdev_attr_dev = {
+	.attr = { .name = "dev", .mode = S_IRUGO },
+	.show = cdev_dev_read
+};
+
+static struct cdev_attribute cdev_attr_count = {
+	.attr = { .name = "count", .mode = S_IRUGO },
+	.show = cdev_count_read
+};
+
+
+static struct attribute *default_cdev_attrs[] = {
+	&cdev_attr_dev.attr,
+	&cdev_attr_count.attr,
+	NULL
+};
+
+/*
+ * General show function.
+ */
+static ssize_t cdev_attr_show(struct kobject *kobj, struct attribute *attr,
+		char *page)
+{
+	struct cdev *cd = container_of(kobj, struct cdev, kobj);
+	struct cdev_attribute *cattr =
+		container_of(attr, struct cdev_attribute, attr);
+	if (cattr->show)
+		return cattr->show(cd, page);
+	return 0;
+}
+
+static struct sysfs_ops cdev_sysfs_ops = {
+	.show = cdev_attr_show
+};
+	
 static decl_subsys(cdev, NULL, NULL);
 
 static void cdev_default_release(struct kobject *kobj)
@@ -400,10 +461,14 @@
 
 static struct kobj_type ktype_cdev_default = {
 	.release	= cdev_default_release,
+	.sysfs_ops	= &cdev_sysfs_ops,
+	.default_attrs	= default_cdev_attrs,
 };
 
 static struct kobj_type ktype_cdev_dynamic = {
 	.release	= cdev_dynamic_release,
+	.sysfs_ops	= &cdev_sysfs_ops,
+	.default_attrs	= default_cdev_attrs,
 };
 
 static struct kset kset_dynamic = {
diff -urN -X dontdiff test5-vanilla/include/linux/cdev.h test5/include/linux/cdev.h
--- test5-vanilla/include/linux/cdev.h	Mon Sep  8 13:50:09 2003
+++ test5/include/linux/cdev.h	Sat Sep 20 06:12:52 2003
@@ -3,6 +3,8 @@
 #ifdef __KERNEL__
 
 struct cdev {
+	dev_t firstdev;
+	int count;
 	struct kobject kobj;
 	struct module *owner;
 	struct file_operations *ops;

             reply	other threads:[~2003-09-19 23:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-19 23:10 Jonathan Corbet [this message]
2003-09-19 23:32 ` Greg KH
2003-09-20  1:28 ` viro
2003-09-20  2:47   ` Greg KH
2003-09-22 16:29   ` Patrick Mochel
2003-09-22 16:46   ` Jonathan Corbet
2003-09-22 21:00     ` viro
2003-09-22 21:15       ` Jonathan Corbet
2003-09-22 22:37         ` viro
2003-09-24 22:22           ` Greg KH

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=20030919231046.4626.qmail@lwn.net \
    --to=corbet@lwn.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@parcelfarce.linux.theplanet.co.uk \
    /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

all inboxes | Powered by JetHome®