int class_create_file(struct class *cls, const struct class_attribute *attr)
{ //create an attribute file for an object.
error = sysfs_create_file(&cls->p->class_subsys.kobj,
&attr->attr);
...
}
static int add_class_attrs(struct class *cls)
{
...
error = class_create_file(cls, &cls->class_attrs[i]);
...
}
int __class_register(struct class *cls, struct lock_class_key *key)
{ //
struct class_private *cp;
klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put);
INIT_LIST_HEAD(&cp->class_interfaces);
kset_init(&cp->class_dirs);
__mutex_init(&cp->class_mutex, "struct class mutex", key);
error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
...
cp->class_subsys.kobj.ktype = &class_ktype;
cp->class = cls;
cls->p = cp;
error = kset_register(&cp->class_subsys);
error = add_class_attrs(class_get(cls));
...
}
struct class *__class_create(struct module *owner, const char *name,
struct lock_class_key *key)
{
struct class *cls;
cls = kzalloc(sizeof(*cls), GFP_KERNEL);
...
cls->name = name;
cls->owner = owner;
cls->class_release = class_create_release;
retval = __class_register(cls, key);
...
}
void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
struct device *start, const struct device_type *type)
{
klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode);
}
struct device *class_dev_iter_next(struct class_dev_iter *iter)
{
while (1) {
knode = klist_next(&iter->ki);
if (!knode)
return NULL;
dev = container_of(knode, struct device, knode_class);
if (!iter->type || iter->type == dev->type)
return dev;
}
}
int class_for_each_device(struct class *class, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
class_dev_iter_init(&iter, class, start, NULL);
while ((dev = class_dev_iter_next(&iter))){
....
}
}
struct device *class_find_device(struct class *class, struct device *start,
void *data,
int (*match)(struct device *, void *))
{
class_dev_iter_init(&iter, class, start, NULL);
while ((dev = class_dev_iter_next(&iter)){
...
}
}
int class_interface_register(struct class_interface *class_intf){
parent = class_get(class_intf->class);
mutex_lock(&parent->p->class_mutex);
list_add_tail(&class_intf->node, &parent->p->class_interfaces);
...
class_dev_iter_init(&iter, parent, NULL, NULL);
while ((dev = class_dev_iter_next(&iter))){...}
}
阅读(1256) | 评论(0) | 转发(0) |