0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/device/class.h>
0012 #include <linux/device.h>
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/string.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/err.h>
0018 #include <linux/slab.h>
0019 #include <linux/blkdev.h>
0020 #include <linux/mutex.h>
0021 #include "base.h"
0022
0023 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
0024
0025 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
0026 char *buf)
0027 {
0028 struct class_attribute *class_attr = to_class_attr(attr);
0029 struct subsys_private *cp = to_subsys_private(kobj);
0030 ssize_t ret = -EIO;
0031
0032 if (class_attr->show)
0033 ret = class_attr->show(cp->class, class_attr, buf);
0034 return ret;
0035 }
0036
0037 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
0038 const char *buf, size_t count)
0039 {
0040 struct class_attribute *class_attr = to_class_attr(attr);
0041 struct subsys_private *cp = to_subsys_private(kobj);
0042 ssize_t ret = -EIO;
0043
0044 if (class_attr->store)
0045 ret = class_attr->store(cp->class, class_attr, buf, count);
0046 return ret;
0047 }
0048
0049 static void class_release(struct kobject *kobj)
0050 {
0051 struct subsys_private *cp = to_subsys_private(kobj);
0052 struct class *class = cp->class;
0053
0054 pr_debug("class '%s': release.\n", class->name);
0055
0056 if (class->class_release)
0057 class->class_release(class);
0058 else
0059 pr_debug("class '%s' does not have a release() function, "
0060 "be careful\n", class->name);
0061
0062 kfree(cp);
0063 }
0064
0065 static const struct kobj_ns_type_operations *class_child_ns_type(struct kobject *kobj)
0066 {
0067 struct subsys_private *cp = to_subsys_private(kobj);
0068 struct class *class = cp->class;
0069
0070 return class->ns_type;
0071 }
0072
0073 static const struct sysfs_ops class_sysfs_ops = {
0074 .show = class_attr_show,
0075 .store = class_attr_store,
0076 };
0077
0078 static struct kobj_type class_ktype = {
0079 .sysfs_ops = &class_sysfs_ops,
0080 .release = class_release,
0081 .child_ns_type = class_child_ns_type,
0082 };
0083
0084
0085 static struct kset *class_kset;
0086
0087
0088 int class_create_file_ns(struct class *cls, const struct class_attribute *attr,
0089 const void *ns)
0090 {
0091 int error;
0092
0093 if (cls)
0094 error = sysfs_create_file_ns(&cls->p->subsys.kobj,
0095 &attr->attr, ns);
0096 else
0097 error = -EINVAL;
0098 return error;
0099 }
0100
0101 void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
0102 const void *ns)
0103 {
0104 if (cls)
0105 sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
0106 }
0107
0108 static struct class *class_get(struct class *cls)
0109 {
0110 if (cls)
0111 kset_get(&cls->p->subsys);
0112 return cls;
0113 }
0114
0115 static void class_put(struct class *cls)
0116 {
0117 if (cls)
0118 kset_put(&cls->p->subsys);
0119 }
0120
0121 static struct device *klist_class_to_dev(struct klist_node *n)
0122 {
0123 struct device_private *p = to_device_private_class(n);
0124 return p->device;
0125 }
0126
0127 static void klist_class_dev_get(struct klist_node *n)
0128 {
0129 struct device *dev = klist_class_to_dev(n);
0130
0131 get_device(dev);
0132 }
0133
0134 static void klist_class_dev_put(struct klist_node *n)
0135 {
0136 struct device *dev = klist_class_to_dev(n);
0137
0138 put_device(dev);
0139 }
0140
0141 static int class_add_groups(struct class *cls,
0142 const struct attribute_group **groups)
0143 {
0144 return sysfs_create_groups(&cls->p->subsys.kobj, groups);
0145 }
0146
0147 static void class_remove_groups(struct class *cls,
0148 const struct attribute_group **groups)
0149 {
0150 return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
0151 }
0152
0153 int __class_register(struct class *cls, struct lock_class_key *key)
0154 {
0155 struct subsys_private *cp;
0156 int error;
0157
0158 pr_debug("device class '%s': registering\n", cls->name);
0159
0160 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
0161 if (!cp)
0162 return -ENOMEM;
0163 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
0164 INIT_LIST_HEAD(&cp->interfaces);
0165 kset_init(&cp->glue_dirs);
0166 __mutex_init(&cp->mutex, "subsys mutex", key);
0167 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
0168 if (error) {
0169 kfree(cp);
0170 return error;
0171 }
0172
0173
0174 if (!cls->dev_kobj)
0175 cls->dev_kobj = sysfs_dev_char_kobj;
0176
0177 #if defined(CONFIG_BLOCK)
0178
0179 if (!sysfs_deprecated || cls != &block_class)
0180 cp->subsys.kobj.kset = class_kset;
0181 #else
0182 cp->subsys.kobj.kset = class_kset;
0183 #endif
0184 cp->subsys.kobj.ktype = &class_ktype;
0185 cp->class = cls;
0186 cls->p = cp;
0187
0188 error = kset_register(&cp->subsys);
0189 if (error) {
0190 kfree(cp);
0191 return error;
0192 }
0193 error = class_add_groups(class_get(cls), cls->class_groups);
0194 class_put(cls);
0195 return error;
0196 }
0197 EXPORT_SYMBOL_GPL(__class_register);
0198
0199 void class_unregister(struct class *cls)
0200 {
0201 pr_debug("device class '%s': unregistering\n", cls->name);
0202 class_remove_groups(cls, cls->class_groups);
0203 kset_unregister(&cls->p->subsys);
0204 }
0205
0206 static void class_create_release(struct class *cls)
0207 {
0208 pr_debug("%s called for %s\n", __func__, cls->name);
0209 kfree(cls);
0210 }
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226 struct class *__class_create(struct module *owner, const char *name,
0227 struct lock_class_key *key)
0228 {
0229 struct class *cls;
0230 int retval;
0231
0232 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
0233 if (!cls) {
0234 retval = -ENOMEM;
0235 goto error;
0236 }
0237
0238 cls->name = name;
0239 cls->owner = owner;
0240 cls->class_release = class_create_release;
0241
0242 retval = __class_register(cls, key);
0243 if (retval)
0244 goto error;
0245
0246 return cls;
0247
0248 error:
0249 kfree(cls);
0250 return ERR_PTR(retval);
0251 }
0252 EXPORT_SYMBOL_GPL(__class_create);
0253
0254
0255
0256
0257
0258
0259
0260
0261 void class_destroy(struct class *cls)
0262 {
0263 if ((cls == NULL) || (IS_ERR(cls)))
0264 return;
0265
0266 class_unregister(cls);
0267 }
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
0282 struct device *start, const struct device_type *type)
0283 {
0284 struct klist_node *start_knode = NULL;
0285
0286 if (start)
0287 start_knode = &start->p->knode_class;
0288 klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
0289 iter->type = type;
0290 }
0291 EXPORT_SYMBOL_GPL(class_dev_iter_init);
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 struct device *class_dev_iter_next(struct class_dev_iter *iter)
0306 {
0307 struct klist_node *knode;
0308 struct device *dev;
0309
0310 while (1) {
0311 knode = klist_next(&iter->ki);
0312 if (!knode)
0313 return NULL;
0314 dev = klist_class_to_dev(knode);
0315 if (!iter->type || iter->type == dev->type)
0316 return dev;
0317 }
0318 }
0319 EXPORT_SYMBOL_GPL(class_dev_iter_next);
0320
0321
0322
0323
0324
0325
0326
0327
0328 void class_dev_iter_exit(struct class_dev_iter *iter)
0329 {
0330 klist_iter_exit(&iter->ki);
0331 }
0332 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 int class_for_each_device(struct class *class, struct device *start,
0353 void *data, int (*fn)(struct device *, void *))
0354 {
0355 struct class_dev_iter iter;
0356 struct device *dev;
0357 int error = 0;
0358
0359 if (!class)
0360 return -EINVAL;
0361 if (!class->p) {
0362 WARN(1, "%s called for class '%s' before it was initialized",
0363 __func__, class->name);
0364 return -EINVAL;
0365 }
0366
0367 class_dev_iter_init(&iter, class, start, NULL);
0368 while ((dev = class_dev_iter_next(&iter))) {
0369 error = fn(dev, data);
0370 if (error)
0371 break;
0372 }
0373 class_dev_iter_exit(&iter);
0374
0375 return error;
0376 }
0377 EXPORT_SYMBOL_GPL(class_for_each_device);
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399 struct device *class_find_device(struct class *class, struct device *start,
0400 const void *data,
0401 int (*match)(struct device *, const void *))
0402 {
0403 struct class_dev_iter iter;
0404 struct device *dev;
0405
0406 if (!class)
0407 return NULL;
0408 if (!class->p) {
0409 WARN(1, "%s called for class '%s' before it was initialized",
0410 __func__, class->name);
0411 return NULL;
0412 }
0413
0414 class_dev_iter_init(&iter, class, start, NULL);
0415 while ((dev = class_dev_iter_next(&iter))) {
0416 if (match(dev, data)) {
0417 get_device(dev);
0418 break;
0419 }
0420 }
0421 class_dev_iter_exit(&iter);
0422
0423 return dev;
0424 }
0425 EXPORT_SYMBOL_GPL(class_find_device);
0426
0427 int class_interface_register(struct class_interface *class_intf)
0428 {
0429 struct class *parent;
0430 struct class_dev_iter iter;
0431 struct device *dev;
0432
0433 if (!class_intf || !class_intf->class)
0434 return -ENODEV;
0435
0436 parent = class_get(class_intf->class);
0437 if (!parent)
0438 return -EINVAL;
0439
0440 mutex_lock(&parent->p->mutex);
0441 list_add_tail(&class_intf->node, &parent->p->interfaces);
0442 if (class_intf->add_dev) {
0443 class_dev_iter_init(&iter, parent, NULL, NULL);
0444 while ((dev = class_dev_iter_next(&iter)))
0445 class_intf->add_dev(dev, class_intf);
0446 class_dev_iter_exit(&iter);
0447 }
0448 mutex_unlock(&parent->p->mutex);
0449
0450 return 0;
0451 }
0452
0453 void class_interface_unregister(struct class_interface *class_intf)
0454 {
0455 struct class *parent = class_intf->class;
0456 struct class_dev_iter iter;
0457 struct device *dev;
0458
0459 if (!parent)
0460 return;
0461
0462 mutex_lock(&parent->p->mutex);
0463 list_del_init(&class_intf->node);
0464 if (class_intf->remove_dev) {
0465 class_dev_iter_init(&iter, parent, NULL, NULL);
0466 while ((dev = class_dev_iter_next(&iter)))
0467 class_intf->remove_dev(dev, class_intf);
0468 class_dev_iter_exit(&iter);
0469 }
0470 mutex_unlock(&parent->p->mutex);
0471
0472 class_put(parent);
0473 }
0474
0475 ssize_t show_class_attr_string(struct class *class,
0476 struct class_attribute *attr, char *buf)
0477 {
0478 struct class_attribute_string *cs;
0479
0480 cs = container_of(attr, struct class_attribute_string, attr);
0481 return sysfs_emit(buf, "%s\n", cs->str);
0482 }
0483
0484 EXPORT_SYMBOL_GPL(show_class_attr_string);
0485
0486 struct class_compat {
0487 struct kobject *kobj;
0488 };
0489
0490
0491
0492
0493
0494
0495
0496
0497 struct class_compat *class_compat_register(const char *name)
0498 {
0499 struct class_compat *cls;
0500
0501 cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
0502 if (!cls)
0503 return NULL;
0504 cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
0505 if (!cls->kobj) {
0506 kfree(cls);
0507 return NULL;
0508 }
0509 return cls;
0510 }
0511 EXPORT_SYMBOL_GPL(class_compat_register);
0512
0513
0514
0515
0516
0517 void class_compat_unregister(struct class_compat *cls)
0518 {
0519 kobject_put(cls->kobj);
0520 kfree(cls);
0521 }
0522 EXPORT_SYMBOL_GPL(class_compat_unregister);
0523
0524
0525
0526
0527
0528
0529
0530
0531 int class_compat_create_link(struct class_compat *cls, struct device *dev,
0532 struct device *device_link)
0533 {
0534 int error;
0535
0536 error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
0537 if (error)
0538 return error;
0539
0540
0541
0542
0543
0544
0545 if (device_link) {
0546 error = sysfs_create_link(&dev->kobj, &device_link->kobj,
0547 "device");
0548 if (error)
0549 sysfs_remove_link(cls->kobj, dev_name(dev));
0550 }
0551
0552 return error;
0553 }
0554 EXPORT_SYMBOL_GPL(class_compat_create_link);
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
0565 struct device *device_link)
0566 {
0567 if (device_link)
0568 sysfs_remove_link(&dev->kobj, "device");
0569 sysfs_remove_link(cls->kobj, dev_name(dev));
0570 }
0571 EXPORT_SYMBOL_GPL(class_compat_remove_link);
0572
0573 int __init classes_init(void)
0574 {
0575 class_kset = kset_create_and_add("class", NULL, NULL);
0576 if (!class_kset)
0577 return -ENOMEM;
0578 return 0;
0579 }
0580
0581 EXPORT_SYMBOL_GPL(class_create_file_ns);
0582 EXPORT_SYMBOL_GPL(class_remove_file_ns);
0583 EXPORT_SYMBOL_GPL(class_unregister);
0584 EXPORT_SYMBOL_GPL(class_destroy);
0585
0586 EXPORT_SYMBOL_GPL(class_interface_register);
0587 EXPORT_SYMBOL_GPL(class_interface_unregister);