linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7] driver-core : add class iteration api
@ 2008-01-12  9:47 Dave Young
  2008-01-12 10:50 ` Stefan Richter
                   ` (4 more replies)
  0 siblings, 5 replies; 19+ messages in thread
From: Dave Young @ 2008-01-12  9:47 UTC (permalink / raw)
  To: Greg KH
  Cc: stefanr, James.Bottomley, a.zummo, peterz, cbou, linux-kernel,
	David Brownell, krh, stern, dwmw2, davem, jarkao2

Add the following class iteration functions for driver use:
class_for_each_device
class_find_device
class_for_each_child
class_find_child

Signed-off-by: Dave Young <hidave.darkstar@gmail.com> 

---
 drivers/base/class.c   |  159 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h |    8 ++
 2 files changed, 167 insertions(+)

diff -upr linux/drivers/base/class.c linux.new/drivers/base/class.c
--- linux/drivers/base/class.c	2008-01-12 14:42:24.000000000 +0800
+++ linux.new/drivers/base/class.c	2008-01-12 14:42:24.000000000 +0800
@@ -798,6 +798,165 @@ void class_device_put(struct class_devic
 		kobject_put(&class_dev->kobj);
 }
 
+/**
+ *	class_for_each_device - device iterator
+ *	@class:	the class we're iterating
+ *	@data: data for the callback
+ *	@fn: function to be called for each device
+ *
+ *	Iterate over @class's list of devices, and call @fn for each,
+ *	passing it @data.
+ *
+ *	We check the return of @fn each time. If it returns anything
+ *	other than 0, we break out and return that value.
+ */
+int class_for_each_device(struct class *class, void *data,
+			   int (*fn)(struct device *, void *))
+{
+	struct device *dev;
+	int error = 0;
+
+	if (!class)
+		return -EINVAL;
+	down(&class->sem);
+	list_for_each_entry(dev, &class->devices, node) {
+		dev = get_device(dev);
+		if (dev) {
+			error = fn(dev, data);
+			put_device(dev);
+		} else
+			error = -ENODEV;
+		if (error)
+			break;
+	}
+	up(&class->sem);
+
+	return error;
+}
+EXPORT_SYMBOL_GPL(class_for_each_device);
+
+/**
+ * class_find_device - device iterator for locating a particular device
+ * @class: the class we're iterating
+ * @data: data for the match function
+ * @match: function to check device
+ *
+ * This is similar to the class_for_each_dev() function above, but it
+ * returns a reference to a device that is 'found' for later use, as
+ * determined by the @match callback.
+ *
+ * The callback should return 0 if the device doesn't match and non-zero
+ * if it does.  If the callback returns non-zero, this function will
+ * return to the caller and not iterate over any more devices.
+ */
+struct device *class_find_device(struct class *class, void *data,
+				   int (*match)(struct device *, void *))
+{
+	struct device *dev;
+	int error = 1;
+
+	if (!class)
+		return NULL;
+
+	down(&class->sem);
+	list_for_each_entry(dev, &class->devices, node) {
+		dev = get_device(dev);
+		if (dev) {
+			if (match(dev, data)) {
+				error = 0;
+				break;
+			} else
+				put_device(dev);
+		} else
+			break;
+	}
+	up(&class->sem);
+
+	if (error)
+		return NULL;
+	return dev;
+}
+EXPORT_SYMBOL_GPL(class_find_device);
+
+/**
+ *	class_for_each_child - class child iterator
+ *	@class:	the class we're iterating
+ *	@data: data for the callback
+ *	@fn: function to be called for each child of the class
+ *
+ *	Iterate over @class's list of children, and call @fn for each,
+ *	passing it @data.
+ *
+ *	We check the return of @fn each time. If it returns anything
+ *	other than 0, we break out and return that value.
+ */
+int class_for_each_child(struct class *class, void *data,
+			   int (*fn)(struct class_device *, void *))
+{
+	struct class_device *dev;
+	int error = 0;
+
+	if (!class)
+		return -EINVAL;
+	down(&class->sem);
+	list_for_each_entry(dev, &class->children, node) {
+		dev = class_device_get(dev);
+		if (dev) {
+			error = fn(dev, data);
+			class_device_put(dev);
+		} else
+			error = -ENODEV;
+		if (error)
+			break;
+	}
+	up(&class->sem);
+
+	return error;
+}
+EXPORT_SYMBOL_GPL(class_for_each_child);
+
+/**
+ * class_find_child - device iterator for locating a particular class_device
+ * @class: the class we're iterating
+ * @data: data for the match function
+ * @match: function to check class_device
+ *
+ * This is similar to the class_for_each_child() function above, but it
+ * returns a reference to a class_device that is 'found' for later use, as
+ * determined by the @match callback.
+ *
+ * The callback should return 0 if the class_device doesn't match and non-zero
+ * if it does.  If the callback returns non-zero, this function will
+ * return to the caller and not iterate over any more class_devices.
+ */
+struct class_device *class_find_child(struct class *class, void *data,
+				   int (*match)(struct class_device *, void *))
+{
+	struct class_device *dev;
+	int error = 1;
+
+	if (!class)
+		return NULL;
+
+	down(&class->sem);
+	list_for_each_entry(dev, &class->children, node) {
+		dev = class_device_get(dev);
+		if (dev) {
+			if (match(dev, data)) {
+				error = 0;
+				break;
+			} else
+				class_device_put(dev);
+		} else
+			break;
+	}
+	up(&class->sem);
+
+	if (error)
+		return NULL;
+	return dev;
+}
+EXPORT_SYMBOL_GPL(class_find_child);
 
 int class_interface_register(struct class_interface *class_intf)
 {
diff -upr linux/include/linux/device.h linux.new/include/linux/device.h
--- linux/include/linux/device.h	2008-01-12 14:42:24.000000000 +0800
+++ linux.new/include/linux/device.h	2008-01-12 14:42:24.000000000 +0800
@@ -199,6 +199,14 @@ struct class {
 
 extern int __must_check class_register(struct class *);
 extern void class_unregister(struct class *);
+extern int class_for_each_device(struct class *class, void *data,
+				int (*fn)(struct device *dev, void *data));
+extern struct device *class_find_device(struct class *class, void *data,
+				   int (*match)(struct device *, void *));
+extern int class_for_each_child(struct class *class, void *data,
+			   int (*fn)(struct class_device *, void *));
+extern struct class_device *class_find_child(struct class *class, void *data,
+				   int (*match)(struct class_device *, void *));
 
 
 struct class_attribute {

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2008-01-23  1:03 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-12  9:47 [PATCH 1/7] driver-core : add class iteration api Dave Young
2008-01-12 10:50 ` Stefan Richter
2008-01-14  1:32   ` Dave Young
2008-01-12 20:11 ` Jarek Poplawski
2008-01-14  1:36   ` Dave Young
2008-01-14  6:58     ` Jarek Poplawski
2008-01-14  7:00       ` Dave Young
2008-01-14 12:13 ` Cornelia Huck
2008-01-15  0:17   ` Dave Young
2008-01-15  9:13 ` Dave Young
2008-01-15  9:45   ` Cornelia Huck
2008-01-22  5:54 ` [PATCH 1/6] " Dave Young
2008-01-22  6:06   ` Dave Young
2008-01-22  6:24   ` David Brownell
2008-01-22  6:30     ` Dave Young
2008-01-22  7:27     ` Dave Young
2008-01-22  8:44       ` Cornelia Huck
2008-01-22 22:25       ` Greg KH
2008-01-23  1:02         ` Dave Young

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).