From: Greg KH <greg@kroah.com>
To: linux-hotplug@vger.kernel.org
Subject: [PATCH] add "simple" class device support [1/5]
Date: Tue, 23 Dec 2003 21:26:20 +0000 [thread overview]
Message-ID: <marc-linux-hotplug-107221513414949@msgid-missing> (raw)
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 */
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
reply other threads:[~2003-12-23 21:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=marc-linux-hotplug-107221513414949@msgid-missing \
--to=greg@kroah.com \
--cc=linux-hotplug@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;
as well as URLs for NNTP newsgroup(s).