LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Dmitry Torokhov <dtor@insightbb.com>,
	Dmitry Torokhov <dtor@mail.ru>,
	Kay Sievers <kay.sievers@novell.com>,
	Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 05/46] driver core: Use attribute groups in struct device_type
Date: Fri, 27 Apr 2007 11:53:19 -0700	[thread overview]
Message-ID: <11777000552084-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <11777000511784-git-send-email-gregkh@suse.de>

From: Dmitry Torokhov <dtor@insightbb.com>

Driver core: use attribute groups in struct device_type

Attribute groups are more flexible than attribute lists
(an attribute list can be represented by anonymous group)
so switch struct device_type to use them.

Also rework attribute creation for devices so that they all
cleaned up properly in case of errors.

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Cc: Kay Sievers <kay.sievers@novell.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
 drivers/base/core.c    |  115 +++++++++++++++++++++++++++++-------------------
 include/linux/device.h |    2 +-
 2 files changed, 70 insertions(+), 47 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 9ea12d9..bb2cc37 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -246,64 +246,95 @@ static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
 	return count;
 }
 
-static int device_add_groups(struct device *dev)
+static int device_add_attributes(struct device *dev,
+				 struct device_attribute *attrs)
+{
+	int error = 0;
+	int i;
+
+	if (attrs) {
+		for (i = 0; attr_name(attrs[i]); i++) {
+			error = device_create_file(dev, &attrs[i]);
+			if (error)
+				break;
+		}
+		if (error)
+			while (--i >= 0)
+				device_remove_file(dev, &attrs[i]);
+	}
+	return error;
+}
+
+static void device_remove_attributes(struct device *dev,
+				     struct device_attribute *attrs)
 {
 	int i;
+
+	if (attrs)
+		for (i = 0; attr_name(attrs[i]); i++)
+			device_remove_file(dev, &attrs[i]);
+}
+
+static int device_add_groups(struct device *dev,
+			     struct attribute_group **groups)
+{
 	int error = 0;
+	int i;
 
-	if (dev->groups) {
-		for (i = 0; dev->groups[i]; i++) {
-			error = sysfs_create_group(&dev->kobj, dev->groups[i]);
+	if (groups) {
+		for (i = 0; groups[i]; i++) {
+			error = sysfs_create_group(&dev->kobj, groups[i]);
 			if (error) {
 				while (--i >= 0)
-					sysfs_remove_group(&dev->kobj, dev->groups[i]);
-				goto out;
+					sysfs_remove_group(&dev->kobj, groups[i]);
+				break;
 			}
 		}
 	}
-out:
 	return error;
 }
 
-static void device_remove_groups(struct device *dev)
+static void device_remove_groups(struct device *dev,
+				 struct attribute_group **groups)
 {
 	int i;
-	if (dev->groups) {
-		for (i = 0; dev->groups[i]; i++) {
-			sysfs_remove_group(&dev->kobj, dev->groups[i]);
-		}
-	}
+
+	if (groups)
+		for (i = 0; groups[i]; i++)
+			sysfs_remove_group(&dev->kobj, groups[i]);
 }
 
 static int device_add_attrs(struct device *dev)
 {
 	struct class *class = dev->class;
 	struct device_type *type = dev->type;
-	int error = 0;
-	int i;
+	int error;
 
-	if (class && class->dev_attrs) {
-		for (i = 0; attr_name(class->dev_attrs[i]); i++) {
-			error = device_create_file(dev, &class->dev_attrs[i]);
-			if (error)
-				break;
-		}
+	if (class) {
+		error = device_add_attributes(dev, class->dev_attrs);
 		if (error)
-			while (--i >= 0)
-				device_remove_file(dev, &class->dev_attrs[i]);
+			return error;
 	}
 
-	if (type && type->attrs) {
-		for (i = 0; attr_name(type->attrs[i]); i++) {
-			error = device_create_file(dev, &type->attrs[i]);
-			if (error)
-				break;
-		}
+	if (type) {
+		error = device_add_groups(dev, type->groups);
 		if (error)
-			while (--i >= 0)
-				device_remove_file(dev, &type->attrs[i]);
+			goto err_remove_class_attrs;
 	}
 
+	error = device_add_groups(dev, dev->groups);
+	if (error)
+		goto err_remove_type_groups;
+
+	return 0;
+
+ err_remove_type_groups:
+	if (type)
+		device_remove_groups(dev, type->groups);
+ err_remove_class_attrs:
+	if (class)
+		device_remove_attributes(dev, class->dev_attrs);
+
 	return error;
 }
 
@@ -311,17 +342,14 @@ static void device_remove_attrs(struct device *dev)
 {
 	struct class *class = dev->class;
 	struct device_type *type = dev->type;
-	int i;
 
-	if (class && class->dev_attrs) {
-		for (i = 0; attr_name(class->dev_attrs[i]); i++)
-			device_remove_file(dev, &class->dev_attrs[i]);
-	}
+	device_remove_groups(dev, dev->groups);
 
-	if (type && type->attrs) {
-		for (i = 0; attr_name(type->attrs[i]); i++)
-			device_remove_file(dev, &type->attrs[i]);
-	}
+	if (type)
+		device_remove_groups(dev, type->groups);
+
+	if (class)
+		device_remove_attributes(dev, class->dev_attrs);
 }
 
 
@@ -638,8 +666,6 @@ int device_add(struct device *dev)
 
 	if ((error = device_add_attrs(dev)))
 		goto AttrsError;
-	if ((error = device_add_groups(dev)))
-		goto GroupError;
 	if ((error = device_pm_add(dev)))
 		goto PMError;
 	if ((error = bus_add_device(dev)))
@@ -663,7 +689,7 @@ int device_add(struct device *dev)
 		up(&dev->class->sem);
 	}
  Done:
- 	kfree(class_name);
+	kfree(class_name);
 	put_device(dev);
 	return error;
  AttachError:
@@ -674,8 +700,6 @@ int device_add(struct device *dev)
 	if (dev->bus)
 		blocking_notifier_call_chain(&dev->bus->bus_notifier,
 					     BUS_NOTIFY_DEL_DEVICE, dev);
-	device_remove_groups(dev);
- GroupError:
 	device_remove_attrs(dev);
  AttrsError:
 	if (dev->devt_attr) {
@@ -838,7 +862,6 @@ void device_del(struct device * dev)
 		}
 	}
 	device_remove_file(dev, &dev->uevent_attr);
-	device_remove_groups(dev);
 	device_remove_attrs(dev);
 	bus_remove_device(dev);
 
diff --git a/include/linux/device.h b/include/linux/device.h
index 9d54fe1..3b64fde 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -332,7 +332,7 @@ extern struct class_device *class_device_create(struct class *cls,
 extern void class_device_destroy(struct class *cls, dev_t devt);
 
 struct device_type {
-	struct device_attribute *attrs;
+	struct attribute_group **groups;
 	int (*uevent)(struct device *dev, char **envp, int num_envp,
 		      char *buffer, int buffer_size);
 	void (*release)(struct device *dev);
-- 
1.5.1.2


  parent reply	other threads:[~2007-04-27 19:11 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-27 18:51 [GIT PATCH] Driver core patches for 2.6.21 Greg KH
2007-04-27 18:53 ` [PATCH 01/46] driver core: fix device_add error path Greg Kroah-Hartman
2007-04-27 18:53   ` [PATCH 02/46] driver core: fix namespace issue with devices assigned to classes Greg Kroah-Hartman
2007-04-27 18:53     ` [PATCH 03/46] dev_printk and new-style class devices Greg Kroah-Hartman
     [not found]       ` <11777000511784-git-send-email-gregkh@suse.de>
2007-04-27 18:53         ` Greg Kroah-Hartman [this message]
2007-04-27 18:53           ` [PATCH 06/46] Driver core: add name to device_type Greg Kroah-Hartman
2007-04-27 18:53             ` [PATCH 07/46] kobject: kobject_shadow_add cleanup Greg Kroah-Hartman
2007-04-27 18:53               ` [PATCH 08/46] driver core: per-subsystem multithreaded probing Greg Kroah-Hartman
2007-04-27 18:53                 ` [PATCH 09/46] powerpc: make it compile for multithread change Greg Kroah-Hartman
2007-04-27 18:53                   ` [PATCH 10/46] driver core: don't fail attaching the device if it cannot be bound Greg Kroah-Hartman
2007-04-27 18:53                     ` [PATCH 11/46] Driver core: remove unneeded completion from driver release path Greg Kroah-Hartman
2007-04-27 18:53                       ` [PATCH 12/46] kref: fix CPU ordering with respect to krefs Greg Kroah-Hartman
2007-04-27 18:53                         ` [PATCH 13/46] Driver core: notify userspace of network device renames Greg Kroah-Hartman
2007-04-27 18:53                           ` [PATCH 14/46] Driver core: suppress uevents via filter Greg Kroah-Hartman
2007-04-27 18:53                             ` [PATCH 15/46] Driver core: switch firmware_class to uevent_suppress Greg Kroah-Hartman
2007-04-27 18:53                               ` [PATCH 16/46] uevent: use add_uevent_var() instead of open coding it Greg Kroah-Hartman
2007-04-27 18:53                                 ` [PATCH 17/46] Driver core: add suspend() and resume() to struct device_type Greg Kroah-Hartman
2007-04-27 18:53                                   ` [PATCH 18/46] Kobject: kobject_uevent.c: Collapse unnecessary loop nesting (top_kobj) Greg Kroah-Hartman
2007-04-27 18:53                                     ` [PATCH 19/46] kobject: kobject_add() reference leak Greg Kroah-Hartman
2007-04-27 18:53                                       ` [PATCH 20/46] Driver core: remove use of rwsem Greg Kroah-Hartman
2007-04-27 18:53                                         ` [PATCH 21/46] SCSI: use the proper semaphore to protect the class lists Greg Kroah-Hartman
2007-04-27 18:53                                           ` [PATCH 22/46] USB: remove use of the bus rwsem, as it doesn't really protect anything Greg Kroah-Hartman
2007-04-27 18:53                                             ` [PATCH 23/46] PNP: stop using the subsystem rwsem Greg Kroah-Hartman
2007-04-27 18:53                                               ` [PATCH 24/46] Input: serio - do not touch bus's rwsem Greg Kroah-Hartman
2007-04-27 18:53                                                 ` [PATCH 25/46] Input: gameport " Greg Kroah-Hartman
2007-04-27 18:53                                                   ` [PATCH 26/46] IDE: remove rwsem use from ide-proc core Greg Kroah-Hartman
2007-04-27 18:53                                                     ` [PATCH 27/46] IEEE1394: remove rwsem use from ieee1394 core Greg Kroah-Hartman
2007-04-27 18:53                                                       ` [PATCH 28/46] PHY: remove rwsem use from phy core Greg Kroah-Hartman
2007-04-27 18:53                                                         ` [PATCH 29/46] qeth: Remove usage of subsys.rwsem Greg Kroah-Hartman
2007-04-27 18:53                                                           ` [PATCH 30/46] kobject core: remove rwsem from struct subsystem Greg Kroah-Hartman
2007-04-27 18:53                                                             ` [PATCH 31/46] Driver core: make uevent-environment available in uevent-file Greg Kroah-Hartman
2007-04-27 18:53                                                               ` [PATCH 32/46] Driver core: warn when userspace writes to the uevent file in a non-supported way Greg Kroah-Hartman
2007-04-27 18:53                                                                 ` [PATCH 33/46] kobject: Comment and warning fixes to kobject.c Greg Kroah-Hartman
2007-04-27 18:53                                                                   ` [PATCH 34/46] the overdue removal of the mount/umount uevents Greg Kroah-Hartman
2007-04-27 18:53                                                                     ` [PATCH 35/46] debugfs: Add debugfs_create_u64() Greg Kroah-Hartman
2007-04-27 18:53                                                                       ` [PATCH 36/46] driver core: bus_add_driver should return an error if no bus Greg Kroah-Hartman
2007-04-27 18:53                                                                         ` [PATCH 37/46] Driver core: use mutex instead of semaphore in DMA pool handler Greg Kroah-Hartman
2007-04-27 18:53                                                                           ` [PATCH 38/46] sysfs: bin.c printk fix Greg Kroah-Hartman
2007-04-27 18:53                                                                             ` [PATCH 39/46] s390: cio: Delay uevents for subchannels Greg Kroah-Hartman
2007-04-27 18:53                                                                               ` [PATCH 40/46] device_schedule_callback() needs a module reference Greg Kroah-Hartman
2007-04-27 18:53                                                                                 ` [PATCH 41/46] security: prevent permission checking of file removal via sysfs_remove_group() Greg Kroah-Hartman
2007-04-27 18:53                                                                                   ` [PATCH 42/46] define platform wakeup hook, use in pci_enable_wake() Greg Kroah-Hartman
2007-04-27 18:53                                                                                     ` [PATCH 43/46] s2ram: add arch irq disable/enable hooks Greg Kroah-Hartman
2007-04-27 18:53                                                                                       ` [PATCH 44/46] mod_sysfs_setup() doesn't return errno when kobject_add_dir() failure occurs Greg Kroah-Hartman
2007-04-27 18:53                                                                                         ` [PATCH 45/46] drivers/base/attribute_container.c: use mutex instead of binary semaphore Greg Kroah-Hartman
2007-04-27 18:54                                                                                           ` [PATCH 46/46] dev_dbg: check dev_dbg() arguments Greg Kroah-Hartman
2007-04-27 21:11                                                         ` [PATCH 28/46] PHY: remove rwsem use from phy core Andy Fleming
2007-04-27 20:27       ` [PATCH 04/46] Driver core: udev triggered device-driver binding Greg Kroah-Hartman

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=11777000552084-git-send-email-gregkh@suse.de \
    --to=gregkh@suse.de \
    --cc=dtor@insightbb.com \
    --cc=dtor@mail.ru \
    --cc=kay.sievers@novell.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH 05/46] driver core: Use attribute groups in struct device_type' \
    /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

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