Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * class.c - basic device class management
0004  *
0005  * Copyright (c) 2002-3 Patrick Mochel
0006  * Copyright (c) 2002-3 Open Source Development Labs
0007  * Copyright (c) 2003-2004 Greg Kroah-Hartman
0008  * Copyright (c) 2003-2004 IBM Corp.
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 /* Hotplug events for classes go to the class subsys */
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     /* set the default /sys/dev directory for devices of this class */
0174     if (!cls->dev_kobj)
0175         cls->dev_kobj = sysfs_dev_char_kobj;
0176 
0177 #if defined(CONFIG_BLOCK)
0178     /* let the block class directory show up in the root of sysfs */
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  * __class_create - create a struct class structure
0214  * @owner: pointer to the module that is to "own" this struct class
0215  * @name: pointer to a string for the name of this class.
0216  * @key: the lock_class_key for this class; used by mutex lock debugging
0217  *
0218  * This is used to create a struct class pointer that can then be used
0219  * in calls to device_create().
0220  *
0221  * Returns &struct class pointer on success, or ERR_PTR() on error.
0222  *
0223  * Note, the pointer created here is to be destroyed when finished by
0224  * making a call to class_destroy().
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  * class_destroy - destroys a struct class structure
0256  * @cls: pointer to the struct class that is to be destroyed
0257  *
0258  * Note, the pointer to be destroyed must have been created with a call
0259  * to class_create().
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  * class_dev_iter_init - initialize class device iterator
0271  * @iter: class iterator to initialize
0272  * @class: the class we wanna iterate over
0273  * @start: the device to start iterating from, if any
0274  * @type: device_type of the devices to iterate over, NULL for all
0275  *
0276  * Initialize class iterator @iter such that it iterates over devices
0277  * of @class.  If @start is set, the list iteration will start there,
0278  * otherwise if it is NULL, the iteration starts at the beginning of
0279  * the list.
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  * class_dev_iter_next - iterate to the next device
0295  * @iter: class iterator to proceed
0296  *
0297  * Proceed @iter to the next device and return it.  Returns NULL if
0298  * iteration is complete.
0299  *
0300  * The returned device is referenced and won't be released till
0301  * iterator is proceed to the next device or exited.  The caller is
0302  * free to do whatever it wants to do with the device including
0303  * calling back into class code.
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  * class_dev_iter_exit - finish iteration
0323  * @iter: class iterator to finish
0324  *
0325  * Finish an iteration.  Always call this function after iteration is
0326  * complete whether the iteration ran till the end or not.
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  * class_for_each_device - device iterator
0336  * @class: the class we're iterating
0337  * @start: the device to start with in the list, if any.
0338  * @data: data for the callback
0339  * @fn: function to be called for each device
0340  *
0341  * Iterate over @class's list of devices, and call @fn for each,
0342  * passing it @data.  If @start is set, the list iteration will start
0343  * there, otherwise if it is NULL, the iteration starts at the
0344  * beginning of the list.
0345  *
0346  * We check the return of @fn each time. If it returns anything
0347  * other than 0, we break out and return that value.
0348  *
0349  * @fn is allowed to do anything including calling back into class
0350  * code.  There's no locking restriction.
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  * class_find_device - device iterator for locating a particular device
0381  * @class: the class we're iterating
0382  * @start: Device to begin with
0383  * @data: data for the match function
0384  * @match: function to check device
0385  *
0386  * This is similar to the class_for_each_dev() function above, but it
0387  * returns a reference to a device that is 'found' for later use, as
0388  * determined by the @match callback.
0389  *
0390  * The callback should return 0 if the device doesn't match and non-zero
0391  * if it does.  If the callback returns non-zero, this function will
0392  * return to the caller and not iterate over any more devices.
0393  *
0394  * Note, you will need to drop the reference with put_device() after use.
0395  *
0396  * @match is allowed to do anything including calling back into class
0397  * code.  There's no locking restriction.
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  * class_compat_register - register a compatibility class
0492  * @name: the name of the class
0493  *
0494  * Compatibility class are meant as a temporary user-space compatibility
0495  * workaround when converting a family of class devices to a bus devices.
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  * class_compat_unregister - unregister a compatibility class
0515  * @cls: the class to unregister
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  * class_compat_create_link - create a compatibility class device link to
0526  *                a bus device
0527  * @cls: the compatibility class
0528  * @dev: the target bus device
0529  * @device_link: an optional device to which a "device" link should be created
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      * Optionally add a "device" link (typically to the parent), as a
0542      * class device would have one and we want to provide as much
0543      * backwards compatibility as possible.
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  * class_compat_remove_link - remove a compatibility class device link to
0558  *                a bus device
0559  * @cls: the compatibility class
0560  * @dev: the target bus device
0561  * @device_link: an optional device to which a "device" link was previously
0562  *       created
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);