Commit 7c71448b authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

class: move driver core specific parts to a private structure

This moves the portions of struct class that are dynamic (kobject and
lock and lists) out of the main structure and into a dynamic, private,
structure.


Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 16be63fd
...@@ -36,6 +36,33 @@ struct driver_private { ...@@ -36,6 +36,33 @@ struct driver_private {
}; };
#define to_driver(obj) container_of(obj, struct driver_private, kobj) #define to_driver(obj) container_of(obj, struct driver_private, kobj)
/**
* struct class_private - structure to hold the private to the driver core portions of the class structure.
*
* @subsys - the struct kset that defines this class. This is the main kobject
* @children - list of class_devices associated with this class
* @devices - list of devices associated with this class
* @interfaces - list of class_interfaces associated with this class
* @class_dirs -
* @sem - semaphore to protect the children, devices, and interfaces lists.
* @class - pointer back to the struct class that this structure is associated
* with.
*
* This structure is the one that is the actual kobject allowing struct
* class to be statically allocated safely. Nothing outside of the driver
* core should ever touch these fields.
*/
struct class_private {
struct kset subsys;
struct list_head devices;
struct list_head interfaces;
struct kset class_dirs;
struct semaphore sem;
struct class *class;
};
#define to_class(obj) container_of(obj, struct class_private, subsys.kobj)
/* initialisation functions */ /* initialisation functions */
extern int devices_init(void); extern int devices_init(void);
extern int buses_init(void); extern int buses_init(void);
......
...@@ -21,17 +21,16 @@ ...@@ -21,17 +21,16 @@
#include "base.h" #include "base.h"
#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
#define to_class(obj) container_of(obj, struct class, subsys.kobj)
static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf) char *buf)
{ {
struct class_attribute *class_attr = to_class_attr(attr); struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj); struct class_private *cp = to_class(kobj);
ssize_t ret = -EIO; ssize_t ret = -EIO;
if (class_attr->show) if (class_attr->show)
ret = class_attr->show(dc, buf); ret = class_attr->show(cp->class, buf);
return ret; return ret;
} }
...@@ -39,17 +38,18 @@ static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, ...@@ -39,17 +38,18 @@ static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
{ {
struct class_attribute *class_attr = to_class_attr(attr); struct class_attribute *class_attr = to_class_attr(attr);
struct class *dc = to_class(kobj); struct class_private *cp = to_class(kobj);
ssize_t ret = -EIO; ssize_t ret = -EIO;
if (class_attr->store) if (class_attr->store)
ret = class_attr->store(dc, buf, count); ret = class_attr->store(cp->class, buf, count);
return ret; return ret;
} }
static void class_release(struct kobject *kobj) static void class_release(struct kobject *kobj)
{ {
struct class *class = to_class(kobj); struct class_private *cp = to_class(kobj);
struct class *class = cp->class;
pr_debug("class '%s': release.\n", class->name); pr_debug("class '%s': release.\n", class->name);
...@@ -78,7 +78,7 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) ...@@ -78,7 +78,7 @@ int class_create_file(struct class *cls, const struct class_attribute *attr)
{ {
int error; int error;
if (cls) if (cls)
error = sysfs_create_file(&cls->subsys.kobj, &attr->attr); error = sysfs_create_file(&cls->p->subsys.kobj, &attr->attr);
else else
error = -EINVAL; error = -EINVAL;
return error; return error;
...@@ -87,21 +87,20 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) ...@@ -87,21 +87,20 @@ int class_create_file(struct class *cls, const struct class_attribute *attr)
void class_remove_file(struct class *cls, const struct class_attribute *attr) void class_remove_file(struct class *cls, const struct class_attribute *attr)
{ {
if (cls) if (cls)
sysfs_remove_file(&cls->subsys.kobj, &attr->attr); sysfs_remove_file(&cls->p->subsys.kobj, &attr->attr);
} }
static struct class *class_get(struct class *cls) static struct class *class_get(struct class *cls)
{ {
if (cls) if (cls)
return container_of(kset_get(&cls->subsys), kset_get(&cls->p->subsys);
struct class, subsys); return cls;
return NULL;
} }
static void class_put(struct class *cls) static void class_put(struct class *cls)
{ {
if (cls) if (cls)
kset_put(&cls->subsys); kset_put(&cls->p->subsys);
} }
static int add_class_attrs(struct class *cls) static int add_class_attrs(struct class *cls)
...@@ -136,17 +135,23 @@ static void remove_class_attrs(struct class *cls) ...@@ -136,17 +135,23 @@ static void remove_class_attrs(struct class *cls)
int class_register(struct class *cls) int class_register(struct class *cls)
{ {
struct class_private *cp;
int error; int error;
pr_debug("device class '%s': registering\n", cls->name); pr_debug("device class '%s': registering\n", cls->name);
INIT_LIST_HEAD(&cls->devices); cp = kzalloc(sizeof(*cp), GFP_KERNEL);
INIT_LIST_HEAD(&cls->interfaces); if (!cp)
kset_init(&cls->class_dirs); return -ENOMEM;
init_MUTEX(&cls->sem); INIT_LIST_HEAD(&cp->devices);
error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name); INIT_LIST_HEAD(&cp->interfaces);
if (error) kset_init(&cp->class_dirs);
init_MUTEX(&cp->sem);
error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
if (error) {
kfree(cp);
return error; return error;
}
/* set the default /sys/dev directory for devices of this class */ /* set the default /sys/dev directory for devices of this class */
if (!cls->dev_kobj) if (!cls->dev_kobj)
...@@ -155,17 +160,21 @@ int class_register(struct class *cls) ...@@ -155,17 +160,21 @@ int class_register(struct class *cls)
#if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
/* let the block class directory show up in the root of sysfs */ /* let the block class directory show up in the root of sysfs */
if (cls != &block_class) if (cls != &block_class)
cls->subsys.kobj.kset = class_kset; cp->subsys.kobj.kset = class_kset;
#else #else
cls->subsys.kobj.kset = class_kset; cp->subsys.kobj.kset = class_kset;
#endif #endif
cls->subsys.kobj.ktype = &class_ktype; cp->subsys.kobj.ktype = &class_ktype;
cp->class = cls;
cls->p = cp;
error = kset_register(&cls->subsys); error = kset_register(&cp->subsys);
if (!error) { if (error) {
error = add_class_attrs(class_get(cls)); kfree(cp);
class_put(cls); return error;
} }
error = add_class_attrs(class_get(cls));
class_put(cls);
return error; return error;
} }
...@@ -173,7 +182,7 @@ void class_unregister(struct class *cls) ...@@ -173,7 +182,7 @@ void class_unregister(struct class *cls)
{ {
pr_debug("device class '%s': unregistering\n", cls->name); pr_debug("device class '%s': unregistering\n", cls->name);
remove_class_attrs(cls); remove_class_attrs(cls);
kset_unregister(&cls->subsys); kset_unregister(&cls->p->subsys);
} }
static void class_create_release(struct class *cls) static void class_create_release(struct class *cls)
...@@ -280,8 +289,8 @@ int class_for_each_device(struct class *class, struct device *start, ...@@ -280,8 +289,8 @@ int class_for_each_device(struct class *class, struct device *start,
if (!class) if (!class)
return -EINVAL; return -EINVAL;
down(&class->sem); down(&class->p->sem);
list_for_each_entry(dev, &class->devices, node) { list_for_each_entry(dev, &class->p->devices, node) {
if (start) { if (start) {
if (start == dev) if (start == dev)
start = NULL; start = NULL;
...@@ -293,7 +302,7 @@ int class_for_each_device(struct class *class, struct device *start, ...@@ -293,7 +302,7 @@ int class_for_each_device(struct class *class, struct device *start,
if (error) if (error)
break; break;
} }
up(&class->sem); up(&class->p->sem);
return error; return error;
} }
...@@ -330,8 +339,8 @@ struct device *class_find_device(struct class *class, struct device *start, ...@@ -330,8 +339,8 @@ struct device *class_find_device(struct class *class, struct device *start,
if (!class) if (!class)
return NULL; return NULL;
down(&class->sem); down(&class->p->sem);
list_for_each_entry(dev, &class->devices, node) { list_for_each_entry(dev, &class->p->devices, node) {
if (start) { if (start) {
if (start == dev) if (start == dev)
start = NULL; start = NULL;
...@@ -344,7 +353,7 @@ struct device *class_find_device(struct class *class, struct device *start, ...@@ -344,7 +353,7 @@ struct device *class_find_device(struct class *class, struct device *start,
} else } else
put_device(dev); put_device(dev);
} }
up(&class->sem); up(&class->p->sem);
return found ? dev : NULL; return found ? dev : NULL;
} }
...@@ -362,13 +371,13 @@ int class_interface_register(struct class_interface *class_intf) ...@@ -362,13 +371,13 @@ int class_interface_register(struct class_interface *class_intf)
if (!parent) if (!parent)
return -EINVAL; return -EINVAL;
down(&parent->sem); down(&parent->p->sem);
list_add_tail(&class_intf->node, &parent->interfaces); list_add_tail(&class_intf->node, &parent->p->interfaces);
if (class_intf->add_dev) { if (class_intf->add_dev) {
list_for_each_entry(dev, &parent->devices, node) list_for_each_entry(dev, &parent->p->devices, node)
class_intf->add_dev(dev, class_intf); class_intf->add_dev(dev, class_intf);
} }
up(&parent->sem); up(&parent->p->sem);
return 0; return 0;
} }
...@@ -381,13 +390,13 @@ void class_interface_unregister(struct class_interface *class_intf) ...@@ -381,13 +390,13 @@ void class_interface_unregister(struct class_interface *class_intf)
if (!parent) if (!parent)
return; return;
down(&parent->sem); down(&parent->p->sem);
list_del_init(&class_intf->node); list_del_init(&class_intf->node);
if (class_intf->remove_dev) { if (class_intf->remove_dev) {
list_for_each_entry(dev, &parent->devices, node) list_for_each_entry(dev, &parent->p->devices, node)
class_intf->remove_dev(dev, class_intf); class_intf->remove_dev(dev, class_intf);
} }
up(&parent->sem); up(&parent->p->sem);
class_put(parent); class_put(parent);
} }
......
...@@ -551,7 +551,7 @@ static struct kobject *get_device_parent(struct device *dev, ...@@ -551,7 +551,7 @@ static struct kobject *get_device_parent(struct device *dev,
{ {
/* class devices without a parent live in /sys/class/<classname>/ */ /* class devices without a parent live in /sys/class/<classname>/ */
if (dev->class && (!parent || parent->class != dev->class)) if (dev->class && (!parent || parent->class != dev->class))
return &dev->class->subsys.kobj; return &dev->class->p->subsys.kobj;
/* all other devices keep their parent */ /* all other devices keep their parent */
else if (parent) else if (parent)
return &parent->kobj; return &parent->kobj;
...@@ -597,13 +597,13 @@ static struct kobject *get_device_parent(struct device *dev, ...@@ -597,13 +597,13 @@ static struct kobject *get_device_parent(struct device *dev,
parent_kobj = &parent->kobj; parent_kobj = &parent->kobj;
/* find our class-directory at the parent and reference it */ /* find our class-directory at the parent and reference it */
spin_lock(&dev->class->class_dirs.list_lock); spin_lock(&dev->class->p->class_dirs.list_lock);
list_for_each_entry(k, &dev->class->class_dirs.list, entry) list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
if (k->parent == parent_kobj) { if (k->parent == parent_kobj) {
kobj = kobject_get(k); kobj = kobject_get(k);
break; break;
} }
spin_unlock(&dev->class->class_dirs.list_lock); spin_unlock(&dev->class->p->class_dirs.list_lock);
if (kobj) if (kobj)
return kobj; return kobj;
...@@ -611,7 +611,7 @@ static struct kobject *get_device_parent(struct device *dev, ...@@ -611,7 +611,7 @@ static struct kobject *get_device_parent(struct device *dev,
k = kobject_create(); k = kobject_create();
if (!k) if (!k)
return NULL; return NULL;
k->kset = &dev->class->class_dirs; k->kset = &dev->class->p->class_dirs;
retval = kobject_add(k, parent_kobj, "%s", dev->class->name); retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
if (retval < 0) { if (retval < 0) {
kobject_put(k); kobject_put(k);
...@@ -630,7 +630,7 @@ static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) ...@@ -630,7 +630,7 @@ static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
{ {
/* see if we live in a "glue" directory */ /* see if we live in a "glue" directory */
if (!glue_dir || !dev->class || if (!glue_dir || !dev->class ||
glue_dir->kset != &dev->class->class_dirs) glue_dir->kset != &dev->class->p->class_dirs)
return; return;
kobject_put(glue_dir); kobject_put(glue_dir);
...@@ -657,17 +657,17 @@ static int device_add_class_symlinks(struct device *dev) ...@@ -657,17 +657,17 @@ static int device_add_class_symlinks(struct device *dev)
if (!dev->class) if (!dev->class)
return 0; return 0;
error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, error = sysfs_create_link(&dev->kobj, &dev->class->p->subsys.kobj,
"subsystem"); "subsystem");
if (error) if (error)
goto out; goto out;
#ifdef CONFIG_SYSFS_DEPRECATED #ifdef CONFIG_SYSFS_DEPRECATED
/* stacked class devices need a symlink in the class directory */ /* stacked class devices need a symlink in the class directory */
if (dev->kobj.parent != &dev->class->subsys.kobj && if (dev->kobj.parent != &dev->class->p->subsys.kobj &&
device_is_not_partition(dev)) { device_is_not_partition(dev)) {
error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, error = sysfs_create_link(&dev->class->p->subsys.kobj,
dev->bus_id); &dev->kobj, dev->bus_id);
if (error) if (error)
goto out_subsys; goto out_subsys;
} }
...@@ -704,12 +704,12 @@ static int device_add_class_symlinks(struct device *dev) ...@@ -704,12 +704,12 @@ static int device_add_class_symlinks(struct device *dev)
if (dev->parent && device_is_not_partition(dev)) if (dev->parent && device_is_not_partition(dev))
sysfs_remove_link(&dev->kobj, "device"); sysfs_remove_link(&dev->kobj, "device");
out_busid: out_busid:
if (dev->kobj.parent != &dev->class->subsys.kobj && if (dev->kobj.parent != &dev->class->p->subsys.kobj &&
device_is_not_partition(dev)) device_is_not_partition(dev))
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); sysfs_remove_link(&dev->class->p->subsys.kobj, dev->bus_id);
#else #else
/* link in the class directory pointing to the device */ /* link in the class directory pointing to the device */
error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, error = sysfs_create_link(&dev->class->p->subsys.kobj, &dev->kobj,
dev->bus_id); dev->bus_id);
if (error) if (error)
goto out_subsys; goto out_subsys;
...@@ -723,7 +723,7 @@ static int device_add_class_symlinks(struct device *dev) ...@@ -723,7 +723,7 @@ static int device_add_class_symlinks(struct device *dev)
return 0; return 0;
out_busid: out_busid:
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); sysfs_remove_link(&dev->class->p->subsys.kobj, dev->bus_id);
#endif #endif
out_subsys: out_subsys:
...@@ -749,14 +749,14 @@ static void device_remove_class_symlinks(struct device *dev) ...@@ -749,14 +749,14 @@ static void device_remove_class_symlinks(struct device *dev)
sysfs_remove_link(&dev->kobj, "device"); sysfs_remove_link(&dev->kobj, "device");
} }
if (dev->kobj.parent != &dev->class->subsys.kobj && if (dev->kobj.parent != &dev->class->p->subsys.kobj &&
device_is_not_partition(dev)) device_is_not_partition(dev))
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); sysfs_remove_link(&dev->class->p->subsys.kobj, dev->bus_id);
#else #else
if (dev->parent && device_is_not_partition(dev)) if (dev->parent && device_is_not_partition(dev))
sysfs_remove_link(&dev->kobj, "device"); sysfs_remove_link(&dev->kobj, "device");
sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); sysfs_remove_link(&dev->class->p->subsys.kobj, dev->bus_id);
#endif #endif
sysfs_remove_link(&dev->kobj, "subsystem"); sysfs_remove_link(&dev->kobj, "subsystem");
...@@ -904,15 +904,16 @@ int device_add(struct device *dev) ...@@ -904,15 +904,16 @@ int device_add(struct device *dev)
klist_add_tail(&dev->knode_parent, &parent->klist_children); klist_add_tail(&dev->knode_parent, &parent->klist_children);
if (dev->class) { if (dev->class) {
down(&dev->class->sem); down(&dev->class->p->sem);
/* tie the class to the device */ /* tie the class to the device */
list_add_tail(&dev->node, &dev->class->devices); list_add_tail(&dev->node, &dev->class->p->devices);
/* notify any interfaces that the device is here */ /* notify any interfaces that the device is here */
list_for_each_entry(class_intf, &dev->class->interfaces, node) list_for_each_entry(class_intf, &dev->class->p->interfaces,
node)
if (class_intf->add_dev) if (class_intf->add_dev)
class_intf->add_dev(dev, class_intf); class_intf->add_dev(dev, class_intf);
up(&dev->class->sem); up(&dev->class->p->sem);
} }
Done: Done:
put_device(dev); put_device(dev);
...@@ -1013,14 +1014,15 @@ void device_del(struct device *dev) ...@@ -1013,14 +1014,15 @@ void device_del(struct device *dev)
if (dev->class) { if (dev->class) {
device_remove_class_symlinks(dev); device_remove_class_symlinks(dev);
down(&dev->class->sem); down(&dev->class->p->sem);
/* notify any interfaces that the device is now gone */ /* notify any interfaces that the device is now gone */
list_for_each_entry(class_intf, &dev->class->interfaces, node) list_for_each_entry(class_intf, &dev->class->p->interfaces,
node)
if (class_intf->remove_dev) if (class_intf->remove_dev)
class_intf->remove_dev(dev, class_intf); class_intf->remove_dev(dev, class_intf);
/* remove the device from the class list */ /* remove the device from the class list */
list_del_init(&dev->node); list_del_init(&dev->node);
up(&dev->class->sem); up(&dev->class->p->sem);
} }
device_remove_file(dev, &uevent_attr); device_remove_file(dev, &uevent_attr);
device_remove_attrs(dev); device_remove_attrs(dev);
...@@ -1348,11 +1350,12 @@ int device_rename(struct device *dev, char *new_name) ...@@ -1348,11 +1350,12 @@ int device_rename(struct device *dev, char *new_name)
} }
#else #else
if (dev->class) { if (dev->class) {
error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, error = sysfs_create_link(&dev->class->p->subsys.kobj,
dev->bus_id); &dev->kobj, dev->bus_id);
if (error) if (error)
goto out; goto out;
sysfs_remove_link(&dev->class->subsys.kobj, old_device_name); sysfs_remove_link(&dev->class->p->subsys.kobj,
old_device_name);
} }
#endif #endif
......
...@@ -35,6 +35,7 @@ struct device; ...@@ -35,6 +35,7 @@ struct device;
struct device_driver; struct device_driver;
struct driver_private; struct driver_private;
struct class; struct class;
struct class_private;
struct bus_type; struct bus_type;
struct bus_type_private; struct bus_type_private;
...@@ -186,11 +187,6 @@ struct class { ...@@ -186,11 +187,6 @@ struct class {
const char *name; const char *name;
struct module *owner; struct module *owner;
struct kset subsys;
struct list_head devices;
struct list_head interfaces;
struct kset class_dirs;
struct semaphore sem; /* locks children, devices, interfaces */
struct class_attribute *class_attrs; struct class_attribute *class_attrs;
struct device_attribute *dev_attrs; struct device_attribute *dev_attrs;
struct kobject *dev_kobj; struct kobject *dev_kobj;
...@@ -204,6 +200,7 @@ struct class { ...@@ -204,6 +200,7 @@ struct class {
int (*resume)(struct device *dev); int (*resume)(struct device *dev);
struct pm_ops *pm; struct pm_ops *pm;
struct class_private *p;
}; };
extern struct kobject *sysfs_dev_block_kobj; extern struct kobject *sysfs_dev_block_kobj;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment