public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: Andrew Morton <akpm@osdl.org>
Cc: linux-kernel@vger.kernel.org, linux-hotplug-devel@lists.sourceforge.net
Subject: [PATCH] add "simple" class device support  [1/5]
Date: Tue, 23 Dec 2003 13:26:20 -0800	[thread overview]
Message-ID: <20031223212620.GB15700@kroah.com> (raw)
In-Reply-To: <20031223212459.GA15700@kroah.com>

Adds a "simple" class device interface to the kernel for char devices
that don't want to roll their own struct class_device handling logic.


diff -Nru a/drivers/base/Makefile b/drivers/base/Makefile
--- a/drivers/base/Makefile	Tue Dec 23 12:53:40 2003
+++ b/drivers/base/Makefile	Tue Dec 23 12:53:40 2003
@@ -1,7 +1,7 @@
 # Makefile for the Linux device tree
 
 obj-y			:= core.o sys.o interface.o bus.o \
-			   driver.o class.o platform.o \
+			   driver.o class.o class_simple.o platform.o \
 			   cpu.o firmware.o init.o map.o
 obj-y			+= power/
 obj-$(CONFIG_FW_LOADER)	+= firmware_class.o
diff -Nru a/drivers/base/class_simple.c b/drivers/base/class_simple.c
--- /dev/null	Wed Dec 31 16:00:00 1969
+++ b/drivers/base/class_simple.c	Tue Dec 23 12:53:48 2003
@@ -0,0 +1,123 @@
+/*
+ * class_simple.c - basic char device support
+ *
+ * Copyright (c) 2003 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (c) 2003 IBM Corp.
+ * 
+ * This file is released under the GPLv2
+ *
+ */
+
+#undef DEBUG
+
+#include <linux/device.h>
+#include <linux/kdev_t.h>
+#include <linux/err.h>
+
+struct simple_dev {
+	struct list_head node;
+	dev_t dev;
+	struct class_device class_dev;
+};
+#define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
+
+static LIST_HEAD(simple_dev_list);
+static spinlock_t simple_dev_list_lock = SPIN_LOCK_UNLOCKED;
+
+static void release_simple_dev(struct class_device *class_dev)
+{
+	struct simple_dev *s_dev = to_simple_dev(class_dev);
+	kfree(s_dev);
+}
+
+static ssize_t show_dev(struct class_device *class_dev, char *buf)
+{
+	struct simple_dev *s_dev = to_simple_dev(class_dev);
+	return print_dev_t(buf, s_dev->dev);
+}
+static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
+
+/**
+ * simple_add_class_device - adds a class device to sysfs for a character driver
+ * @class: pointer to the struct class that this device should be registered to.  
+ * @dev: the dev_t for the device to be added.
+ * @device: a pointer to a struct device that is assiociated with this class device.
+ * @fmt: string for the class device's name
+ *
+ * This function can be used by simple char device classes that do not
+ * implement their own class device registration.  A struct class_device will
+ * be created in sysfs, registered to the specified class.  A "dev" file will
+ * be created, showing the dev_t for the device.  The pointer to the struct
+ * class_device will be returned from the call.  Any further sysfs files that
+ * might be required can be created using this pointer.
+ * Note: the struct class passed to this function must have previously been
+ * registered with a call to register_class().
+ */
+struct class_device *simple_add_class_device(struct class *class, dev_t dev, struct device *device, const char *fmt, ...)
+{
+	va_list args;
+	struct simple_dev *s_dev;
+	int retval;
+
+	s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
+	if (!s_dev) {
+		retval = -ENOMEM;
+		goto error;
+	}
+	memset(s_dev, 0x00, sizeof(*s_dev));
+
+	class->release = &release_simple_dev;
+	s_dev->dev = dev;
+	s_dev->class_dev.dev = device;
+	s_dev->class_dev.class = class;
+	
+	va_start(args,fmt);
+	vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args);
+	va_end(args);
+	retval = class_device_register(&s_dev->class_dev);
+	if (retval)
+		goto error;
+	class_device_create_file(&s_dev->class_dev, &class_device_attr_dev);
+	spin_lock(&simple_dev_list_lock);
+	list_add(&s_dev->node, &simple_dev_list);
+	spin_unlock(&simple_dev_list_lock);
+
+	return &s_dev->class_dev;
+
+error:
+	kfree(s_dev);
+	return ERR_PTR(retval);
+}
+EXPORT_SYMBOL(simple_add_class_device);
+
+/**
+ * simple_remove_class_device - removes a class device that was created with simple_add_class_device()
+ * @dev: the dev_t of the device that was previously registered.
+ *
+ * This call unregisters and cleans up a class device that was created with a
+ * call to simple_add_class_device()
+ */
+void simple_remove_class_device(dev_t dev)
+{
+	struct simple_dev *s_dev = NULL;
+	struct list_head *tmp;
+	int found = 0;
+
+	spin_lock(&simple_dev_list_lock);
+	list_for_each(tmp, &simple_dev_list) {
+		s_dev = list_entry(tmp, struct simple_dev, node);
+		if (s_dev->dev == dev) {
+			found = 1;
+			break;
+		}
+	}
+	if (found) {
+		list_del(&s_dev->node);
+		spin_unlock(&simple_dev_list_lock);
+		class_device_unregister(&s_dev->class_dev);
+	} else {
+		spin_unlock(&simple_dev_list_lock);
+	}
+}
+EXPORT_SYMBOL(simple_remove_class_device);
+
diff -Nru a/include/linux/device.h b/include/linux/device.h
--- a/include/linux/device.h	Tue Dec 23 12:53:36 2003
+++ b/include/linux/device.h	Tue Dec 23 12:53:36 2003
@@ -246,6 +246,11 @@
 extern int class_interface_register(struct class_interface *);
 extern void class_interface_unregister(struct class_interface *);
 
+/* interface for simple class devices */
+extern struct class_device *simple_add_class_device(struct class *class, dev_t dev, struct device *device, const char *fmt, ...)
+	__attribute__((format(printf,4,5)));
+extern void simple_remove_class_device(dev_t dev);
+
 
 struct device {
 	struct list_head node;		/* node in sibling list */

  reply	other threads:[~2003-12-23 21:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23 21:24 [PATCH] sysfs class patches - take 2 [0/5] Greg KH
2003-12-23 21:26 ` Greg KH [this message]
2003-12-23 21:27   ` [PATCH] remove tty class device logic [2/5] Greg KH
2003-12-23 21:29     ` [PATCH] add sysfs mem class [3/5] Greg KH
2003-12-23 21:30       ` [PATCH] add sysfs misc class [4/5] Greg KH
2003-12-23 21:31         ` [PATCH] add sysfs vc class [5/5] Greg KH
2003-12-23 21:57 ` [PATCH] sysfs class patches - take 2 [0/5] Jeff Garzik
2003-12-23 22:07   ` Greg KH
2003-12-23 23:01     ` [PATCH] add video sysfs class [6/5] Greg KH
2003-12-24 14:27     ` [PATCH] sysfs class patches - take 2 [0/5] Kronos
2003-12-24 16:45       ` Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2003-12-29 15:36 [PATCH] add "simple" class device support [1/5] Willem Riede
2003-12-30  3:04 Willem Riede

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=20031223212620.GB15700@kroah.com \
    --to=greg@kroah.com \
    --cc=akpm@osdl.org \
    --cc=linux-hotplug-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    /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