1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * class.c - basic device class management
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (c) 2002-3 Patrick Mochel
61da177e4SLinus Torvalds * Copyright (c) 2002-3 Open Source Development Labs
71da177e4SLinus Torvalds * Copyright (c) 2003-2004 Greg Kroah-Hartman
81da177e4SLinus Torvalds * Copyright (c) 2003-2004 IBM Corp.
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
11a8ae6085SGreg Kroah-Hartman #include <linux/device/class.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/string.h>
161da177e4SLinus Torvalds #include <linux/kdev_t.h>
17e9ba6365Sgregkh@suse.de #include <linux/err.h>
184e57b681STim Schmielau #include <linux/slab.h>
19322cbb50SChristoph Hellwig #include <linux/blkdev.h>
20f75b1c60SDave Young #include <linux/mutex.h>
211da177e4SLinus Torvalds #include "base.h"
221da177e4SLinus Torvalds
23884f8ce4SGreg Kroah-Hartman /* /sys/class */
24884f8ce4SGreg Kroah-Hartman static struct kset *class_kset;
25884f8ce4SGreg Kroah-Hartman
261da177e4SLinus Torvalds #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
271da177e4SLinus Torvalds
28884f8ce4SGreg Kroah-Hartman /**
29884f8ce4SGreg Kroah-Hartman * class_to_subsys - Turn a struct class into a struct subsys_private
30884f8ce4SGreg Kroah-Hartman *
31884f8ce4SGreg Kroah-Hartman * @class: pointer to the struct bus_type to look up
32884f8ce4SGreg Kroah-Hartman *
33884f8ce4SGreg Kroah-Hartman * The driver core internals need to work on the subsys_private structure, not
34884f8ce4SGreg Kroah-Hartman * the external struct class pointer. This function walks the list of
35884f8ce4SGreg Kroah-Hartman * registered classes in the system and finds the matching one and returns the
36884f8ce4SGreg Kroah-Hartman * internal struct subsys_private that relates to that class.
37884f8ce4SGreg Kroah-Hartman *
38884f8ce4SGreg Kroah-Hartman * Note, the reference count of the return value is INCREMENTED if it is not
39884f8ce4SGreg Kroah-Hartman * NULL. A call to subsys_put() must be done when finished with the pointer in
40884f8ce4SGreg Kroah-Hartman * order for it to be properly freed.
41884f8ce4SGreg Kroah-Hartman */
class_to_subsys(const struct class * class)427d90e81aSGreg Kroah-Hartman struct subsys_private *class_to_subsys(const struct class *class)
43884f8ce4SGreg Kroah-Hartman {
44884f8ce4SGreg Kroah-Hartman struct subsys_private *sp = NULL;
45884f8ce4SGreg Kroah-Hartman struct kobject *kobj;
46884f8ce4SGreg Kroah-Hartman
47884f8ce4SGreg Kroah-Hartman if (!class || !class_kset)
48884f8ce4SGreg Kroah-Hartman return NULL;
49884f8ce4SGreg Kroah-Hartman
50884f8ce4SGreg Kroah-Hartman spin_lock(&class_kset->list_lock);
51884f8ce4SGreg Kroah-Hartman
52884f8ce4SGreg Kroah-Hartman if (list_empty(&class_kset->list))
53884f8ce4SGreg Kroah-Hartman goto done;
54884f8ce4SGreg Kroah-Hartman
55884f8ce4SGreg Kroah-Hartman list_for_each_entry(kobj, &class_kset->list, entry) {
56884f8ce4SGreg Kroah-Hartman struct kset *kset = container_of(kobj, struct kset, kobj);
57884f8ce4SGreg Kroah-Hartman
58884f8ce4SGreg Kroah-Hartman sp = container_of_const(kset, struct subsys_private, subsys);
59884f8ce4SGreg Kroah-Hartman if (sp->class == class)
60884f8ce4SGreg Kroah-Hartman goto done;
61884f8ce4SGreg Kroah-Hartman }
62884f8ce4SGreg Kroah-Hartman sp = NULL;
63884f8ce4SGreg Kroah-Hartman done:
64884f8ce4SGreg Kroah-Hartman sp = subsys_get(sp);
65884f8ce4SGreg Kroah-Hartman spin_unlock(&class_kset->list_lock);
66884f8ce4SGreg Kroah-Hartman return sp;
67884f8ce4SGreg Kroah-Hartman }
68884f8ce4SGreg Kroah-Hartman
class_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)694a3ad20cSGreg Kroah-Hartman static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
704a3ad20cSGreg Kroah-Hartman char *buf)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds struct class_attribute *class_attr = to_class_attr(attr);
736b6e39a6SKay Sievers struct subsys_private *cp = to_subsys_private(kobj);
744a0c20bfSDmitry Torokhov ssize_t ret = -EIO;
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds if (class_attr->show)
7728812fe1SAndi Kleen ret = class_attr->show(cp->class, class_attr, buf);
781da177e4SLinus Torvalds return ret;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds
class_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)814a3ad20cSGreg Kroah-Hartman static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
821da177e4SLinus Torvalds const char *buf, size_t count)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds struct class_attribute *class_attr = to_class_attr(attr);
856b6e39a6SKay Sievers struct subsys_private *cp = to_subsys_private(kobj);
864a0c20bfSDmitry Torokhov ssize_t ret = -EIO;
871da177e4SLinus Torvalds
881da177e4SLinus Torvalds if (class_attr->store)
8928812fe1SAndi Kleen ret = class_attr->store(cp->class, class_attr, buf, count);
901da177e4SLinus Torvalds return ret;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds
class_release(struct kobject * kobj)931da177e4SLinus Torvalds static void class_release(struct kobject *kobj)
941da177e4SLinus Torvalds {
956b6e39a6SKay Sievers struct subsys_private *cp = to_subsys_private(kobj);
9643a7206bSGreg Kroah-Hartman const struct class *class = cp->class;
971da177e4SLinus Torvalds
981da177e4SLinus Torvalds pr_debug("class '%s': release.\n", class->name);
991da177e4SLinus Torvalds
1001da177e4SLinus Torvalds if (class->class_release)
1011da177e4SLinus Torvalds class->class_release(class);
1021da177e4SLinus Torvalds else
1031da177e4SLinus Torvalds pr_debug("class '%s' does not have a release() function, "
1041da177e4SLinus Torvalds "be careful\n", class->name);
10518d19c96SLaurent Pinchart
106f326ea63SGreg Kroah-Hartman lockdep_unregister_key(&cp->lock_key);
10718d19c96SLaurent Pinchart kfree(cp);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds
class_child_ns_type(const struct kobject * kobj)11002a476d9SGreg Kroah-Hartman static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj)
111bc451f20SEric W. Biederman {
1127bbb89b4SGreg Kroah-Hartman const struct subsys_private *cp = to_subsys_private(kobj);
11343a7206bSGreg Kroah-Hartman const struct class *class = cp->class;
114bc451f20SEric W. Biederman
115bc451f20SEric W. Biederman return class->ns_type;
116bc451f20SEric W. Biederman }
117bc451f20SEric W. Biederman
11852cf25d0SEmese Revfy static const struct sysfs_ops class_sysfs_ops = {
1191da177e4SLinus Torvalds .show = class_attr_show,
1201da177e4SLinus Torvalds .store = class_attr_store,
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds
123c83d9ab4SThomas Weißschuh static const struct kobj_type class_ktype = {
1241da177e4SLinus Torvalds .sysfs_ops = &class_sysfs_ops,
1251da177e4SLinus Torvalds .release = class_release,
126bc451f20SEric W. Biederman .child_ns_type = class_child_ns_type,
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds
class_create_file_ns(const struct class * cls,const struct class_attribute * attr,const void * ns)12980842a92SGreg Kroah-Hartman int class_create_file_ns(const struct class *cls, const struct class_attribute *attr,
13058292cbeSTejun Heo const void *ns)
1311da177e4SLinus Torvalds {
1327b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(cls);
1331da177e4SLinus Torvalds int error;
134d34898deSCosmin Tomulescu
1357b884b7fSGreg Kroah-Hartman if (!sp)
1367b884b7fSGreg Kroah-Hartman return -EINVAL;
1377b884b7fSGreg Kroah-Hartman
1387b884b7fSGreg Kroah-Hartman error = sysfs_create_file_ns(&sp->subsys.kobj, &attr->attr, ns);
1397b884b7fSGreg Kroah-Hartman subsys_put(sp);
1407b884b7fSGreg Kroah-Hartman
1411da177e4SLinus Torvalds return error;
1421da177e4SLinus Torvalds }
143779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_create_file_ns);
1441da177e4SLinus Torvalds
class_remove_file_ns(const struct class * cls,const struct class_attribute * attr,const void * ns)14580842a92SGreg Kroah-Hartman void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr,
14658292cbeSTejun Heo const void *ns)
1471da177e4SLinus Torvalds {
1487b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(cls);
1497b884b7fSGreg Kroah-Hartman
1507b884b7fSGreg Kroah-Hartman if (!sp)
1517b884b7fSGreg Kroah-Hartman return;
1527b884b7fSGreg Kroah-Hartman
1537b884b7fSGreg Kroah-Hartman sysfs_remove_file_ns(&sp->subsys.kobj, &attr->attr, ns);
1547b884b7fSGreg Kroah-Hartman subsys_put(sp);
1551da177e4SLinus Torvalds }
156779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_remove_file_ns);
1571da177e4SLinus Torvalds
klist_class_to_dev(struct klist_node * n)158570d0200SWei Yang static struct device *klist_class_to_dev(struct klist_node *n)
159570d0200SWei Yang {
160570d0200SWei Yang struct device_private *p = to_device_private_class(n);
161570d0200SWei Yang return p->device;
162570d0200SWei Yang }
163570d0200SWei Yang
klist_class_dev_get(struct klist_node * n)1645a3ceb86STejun Heo static void klist_class_dev_get(struct klist_node *n)
1655a3ceb86STejun Heo {
166570d0200SWei Yang struct device *dev = klist_class_to_dev(n);
1675a3ceb86STejun Heo
1685a3ceb86STejun Heo get_device(dev);
1695a3ceb86STejun Heo }
1705a3ceb86STejun Heo
klist_class_dev_put(struct klist_node * n)1715a3ceb86STejun Heo static void klist_class_dev_put(struct klist_node *n)
1725a3ceb86STejun Heo {
173570d0200SWei Yang struct device *dev = klist_class_to_dev(n);
1745a3ceb86STejun Heo
1755a3ceb86STejun Heo put_device(dev);
1765a3ceb86STejun Heo }
1775a3ceb86STejun Heo
class_register(const struct class * cls)17843a7206bSGreg Kroah-Hartman int class_register(const struct class *cls)
1791da177e4SLinus Torvalds {
1806b6e39a6SKay Sievers struct subsys_private *cp;
181dcfbb67eSGreg Kroah-Hartman struct lock_class_key *key;
1821da177e4SLinus Torvalds int error;
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds pr_debug("device class '%s': registering\n", cls->name);
1851da177e4SLinus Torvalds
1867c71448bSGreg Kroah-Hartman cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1877c71448bSGreg Kroah-Hartman if (!cp)
1887c71448bSGreg Kroah-Hartman return -ENOMEM;
1896b6e39a6SKay Sievers klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
190ca22e56dSKay Sievers INIT_LIST_HEAD(&cp->interfaces);
1916b6e39a6SKay Sievers kset_init(&cp->glue_dirs);
192dcfbb67eSGreg Kroah-Hartman key = &cp->lock_key;
193dcfbb67eSGreg Kroah-Hartman lockdep_register_key(key);
194ca22e56dSKay Sievers __mutex_init(&cp->mutex, "subsys mutex", key);
1956b6e39a6SKay Sievers error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
1967c71448bSGreg Kroah-Hartman if (error) {
1977c71448bSGreg Kroah-Hartman kfree(cp);
1981da177e4SLinus Torvalds return error;
1997c71448bSGreg Kroah-Hartman }
2001da177e4SLinus Torvalds
2016b6e39a6SKay Sievers cp->subsys.kobj.kset = class_kset;
2026b6e39a6SKay Sievers cp->subsys.kobj.ktype = &class_ktype;
2037c71448bSGreg Kroah-Hartman cp->class = cls;
2041da177e4SLinus Torvalds
2056b6e39a6SKay Sievers error = kset_register(&cp->subsys);
2060b2a1a39SRafael J. Wysocki if (error)
2070b2a1a39SRafael J. Wysocki goto err_out;
2080b2a1a39SRafael J. Wysocki
2097b884b7fSGreg Kroah-Hartman error = sysfs_create_groups(&cp->subsys.kobj, cls->class_groups);
2108c3e8a6bSYang Yingliang if (error) {
2118c3e8a6bSYang Yingliang kobject_del(&cp->subsys.kobj);
2128c3e8a6bSYang Yingliang kfree_const(cp->subsys.kobj.name);
2130b2a1a39SRafael J. Wysocki goto err_out;
2148c3e8a6bSYang Yingliang }
2150b2a1a39SRafael J. Wysocki return 0;
2160b2a1a39SRafael J. Wysocki
2170b2a1a39SRafael J. Wysocki err_out:
218b57196a5SJing Xia lockdep_unregister_key(key);
2190b2a1a39SRafael J. Wysocki kfree(cp);
2201da177e4SLinus Torvalds return error;
2211da177e4SLinus Torvalds }
222dcfbb67eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_register);
2231da177e4SLinus Torvalds
class_unregister(const struct class * cls)224517d4927SGreg Kroah-Hartman void class_unregister(const struct class *cls)
2251da177e4SLinus Torvalds {
2267b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(cls);
2277b884b7fSGreg Kroah-Hartman
2287b884b7fSGreg Kroah-Hartman if (!sp)
2297b884b7fSGreg Kroah-Hartman return;
2307b884b7fSGreg Kroah-Hartman
2311da177e4SLinus Torvalds pr_debug("device class '%s': unregistering\n", cls->name);
2327b884b7fSGreg Kroah-Hartman
2337b884b7fSGreg Kroah-Hartman sysfs_remove_groups(&sp->subsys.kobj, cls->class_groups);
2347b884b7fSGreg Kroah-Hartman kset_unregister(&sp->subsys);
2357b884b7fSGreg Kroah-Hartman subsys_put(sp);
2361da177e4SLinus Torvalds }
237779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_unregister);
2381da177e4SLinus Torvalds
class_create_release(const struct class * cls)239979207caSGreg Kroah-Hartman static void class_create_release(const struct class *cls)
240e9ba6365Sgregkh@suse.de {
2412b3a302aSHarvey Harrison pr_debug("%s called for %s\n", __func__, cls->name);
242e9ba6365Sgregkh@suse.de kfree(cls);
243e9ba6365Sgregkh@suse.de }
244e9ba6365Sgregkh@suse.de
2452fc68447Sgregkh@suse.de /**
246dcfbb67eSGreg Kroah-Hartman * class_create - create a struct class structure
2472fc68447Sgregkh@suse.de * @name: pointer to a string for the name of this class.
2482fc68447Sgregkh@suse.de *
2492fc68447Sgregkh@suse.de * This is used to create a struct class pointer that can then be used
250c3b19ff0SKay Sievers * in calls to device_create().
2512fc68447Sgregkh@suse.de *
252f0eae0edSJani Nikula * Returns &struct class pointer on success, or ERR_PTR() on error.
253f0eae0edSJani Nikula *
2542fc68447Sgregkh@suse.de * Note, the pointer created here is to be destroyed when finished by
2552fc68447Sgregkh@suse.de * making a call to class_destroy().
2562fc68447Sgregkh@suse.de */
class_create(const char * name)257dcfbb67eSGreg Kroah-Hartman struct class *class_create(const char *name)
258e9ba6365Sgregkh@suse.de {
259e9ba6365Sgregkh@suse.de struct class *cls;
260e9ba6365Sgregkh@suse.de int retval;
261e9ba6365Sgregkh@suse.de
2624aed0644SJiri Slaby cls = kzalloc(sizeof(*cls), GFP_KERNEL);
263e9ba6365Sgregkh@suse.de if (!cls) {
264e9ba6365Sgregkh@suse.de retval = -ENOMEM;
265e9ba6365Sgregkh@suse.de goto error;
266e9ba6365Sgregkh@suse.de }
267e9ba6365Sgregkh@suse.de
268e9ba6365Sgregkh@suse.de cls->name = name;
269e9ba6365Sgregkh@suse.de cls->class_release = class_create_release;
270e9ba6365Sgregkh@suse.de
271dcfbb67eSGreg Kroah-Hartman retval = class_register(cls);
272e9ba6365Sgregkh@suse.de if (retval)
273e9ba6365Sgregkh@suse.de goto error;
274e9ba6365Sgregkh@suse.de
275e9ba6365Sgregkh@suse.de return cls;
276e9ba6365Sgregkh@suse.de
277e9ba6365Sgregkh@suse.de error:
278e9ba6365Sgregkh@suse.de kfree(cls);
279e9ba6365Sgregkh@suse.de return ERR_PTR(retval);
280e9ba6365Sgregkh@suse.de }
281dcfbb67eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_create);
282e9ba6365Sgregkh@suse.de
2832fc68447Sgregkh@suse.de /**
2842fc68447Sgregkh@suse.de * class_destroy - destroys a struct class structure
28592a0f861SRolf Eike Beer * @cls: pointer to the struct class that is to be destroyed
2862fc68447Sgregkh@suse.de *
2872fc68447Sgregkh@suse.de * Note, the pointer to be destroyed must have been created with a call
2882fc68447Sgregkh@suse.de * to class_create().
2892fc68447Sgregkh@suse.de */
class_destroy(const struct class * cls)290517d4927SGreg Kroah-Hartman void class_destroy(const struct class *cls)
291e9ba6365Sgregkh@suse.de {
292e9628e01SYang Yingliang if (IS_ERR_OR_NULL(cls))
293e9ba6365Sgregkh@suse.de return;
294e9ba6365Sgregkh@suse.de
295e9ba6365Sgregkh@suse.de class_unregister(cls);
296e9ba6365Sgregkh@suse.de }
297779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_destroy);
2981da177e4SLinus Torvalds
299fd04897bSDave Young /**
3005a3ceb86STejun Heo * class_dev_iter_init - initialize class device iterator
3015a3ceb86STejun Heo * @iter: class iterator to initialize
3025a3ceb86STejun Heo * @class: the class we wanna iterate over
3035a3ceb86STejun Heo * @start: the device to start iterating from, if any
3045a3ceb86STejun Heo * @type: device_type of the devices to iterate over, NULL for all
3055a3ceb86STejun Heo *
3065a3ceb86STejun Heo * Initialize class iterator @iter such that it iterates over devices
3075a3ceb86STejun Heo * of @class. If @start is set, the list iteration will start there,
3085a3ceb86STejun Heo * otherwise if it is NULL, the iteration starts at the beginning of
3095a3ceb86STejun Heo * the list.
3105a3ceb86STejun Heo */
class_dev_iter_init(struct class_dev_iter * iter,const struct class * class,const struct device * start,const struct device_type * type)311a2fd6e42SGreg Kroah-Hartman void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
312a2fd6e42SGreg Kroah-Hartman const struct device *start, const struct device_type *type)
3135a3ceb86STejun Heo {
3147b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(class);
3155a3ceb86STejun Heo struct klist_node *start_knode = NULL;
3165a3ceb86STejun Heo
317*f4b9bc82SZijun Hu memset(iter, 0, sizeof(*iter));
318*f4b9bc82SZijun Hu if (!sp) {
319*f4b9bc82SZijun Hu pr_crit("%s: class %p was not registered yet\n",
320*f4b9bc82SZijun Hu __func__, class);
3217b884b7fSGreg Kroah-Hartman return;
322*f4b9bc82SZijun Hu }
3237b884b7fSGreg Kroah-Hartman
3245a3ceb86STejun Heo if (start)
325570d0200SWei Yang start_knode = &start->p->knode_class;
3267b884b7fSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
3275a3ceb86STejun Heo iter->type = type;
328ddaf098eSGreg Kroah-Hartman iter->sp = sp;
3295a3ceb86STejun Heo }
3305a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_init);
3315a3ceb86STejun Heo
3325a3ceb86STejun Heo /**
3335a3ceb86STejun Heo * class_dev_iter_next - iterate to the next device
3345a3ceb86STejun Heo * @iter: class iterator to proceed
3355a3ceb86STejun Heo *
3365a3ceb86STejun Heo * Proceed @iter to the next device and return it. Returns NULL if
3375a3ceb86STejun Heo * iteration is complete.
3385a3ceb86STejun Heo *
3395a3ceb86STejun Heo * The returned device is referenced and won't be released till
3405a3ceb86STejun Heo * iterator is proceed to the next device or exited. The caller is
3415a3ceb86STejun Heo * free to do whatever it wants to do with the device including
3425a3ceb86STejun Heo * calling back into class code.
3435a3ceb86STejun Heo */
class_dev_iter_next(struct class_dev_iter * iter)3445a3ceb86STejun Heo struct device *class_dev_iter_next(struct class_dev_iter *iter)
3455a3ceb86STejun Heo {
3465a3ceb86STejun Heo struct klist_node *knode;
3475a3ceb86STejun Heo struct device *dev;
3485a3ceb86STejun Heo
349*f4b9bc82SZijun Hu if (!iter->sp)
350*f4b9bc82SZijun Hu return NULL;
351*f4b9bc82SZijun Hu
3525a3ceb86STejun Heo while (1) {
3535a3ceb86STejun Heo knode = klist_next(&iter->ki);
3545a3ceb86STejun Heo if (!knode)
3555a3ceb86STejun Heo return NULL;
356570d0200SWei Yang dev = klist_class_to_dev(knode);
3575a3ceb86STejun Heo if (!iter->type || iter->type == dev->type)
3585a3ceb86STejun Heo return dev;
3595a3ceb86STejun Heo }
3605a3ceb86STejun Heo }
3615a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_next);
3625a3ceb86STejun Heo
3635a3ceb86STejun Heo /**
3645a3ceb86STejun Heo * class_dev_iter_exit - finish iteration
3655a3ceb86STejun Heo * @iter: class iterator to finish
3665a3ceb86STejun Heo *
3675a3ceb86STejun Heo * Finish an iteration. Always call this function after iteration is
3685a3ceb86STejun Heo * complete whether the iteration ran till the end or not.
3695a3ceb86STejun Heo */
class_dev_iter_exit(struct class_dev_iter * iter)3705a3ceb86STejun Heo void class_dev_iter_exit(struct class_dev_iter *iter)
3715a3ceb86STejun Heo {
3725a3ceb86STejun Heo klist_iter_exit(&iter->ki);
373ddaf098eSGreg Kroah-Hartman subsys_put(iter->sp);
3745a3ceb86STejun Heo }
3755a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_exit);
3765a3ceb86STejun Heo
3775a3ceb86STejun Heo /**
378fd04897bSDave Young * class_for_each_device - device iterator
379fd04897bSDave Young * @class: the class we're iterating
38093562b53SGreg Kroah-Hartman * @start: the device to start with in the list, if any.
381fd04897bSDave Young * @data: data for the callback
382fd04897bSDave Young * @fn: function to be called for each device
383fd04897bSDave Young *
384fd04897bSDave Young * Iterate over @class's list of devices, and call @fn for each,
38593562b53SGreg Kroah-Hartman * passing it @data. If @start is set, the list iteration will start
38693562b53SGreg Kroah-Hartman * there, otherwise if it is NULL, the iteration starts at the
38793562b53SGreg Kroah-Hartman * beginning of the list.
388fd04897bSDave Young *
389fd04897bSDave Young * We check the return of @fn each time. If it returns anything
390fd04897bSDave Young * other than 0, we break out and return that value.
391fd04897bSDave Young *
3925a3ceb86STejun Heo * @fn is allowed to do anything including calling back into class
3935a3ceb86STejun Heo * code. There's no locking restriction.
394fd04897bSDave Young */
class_for_each_device(const struct class * class,const struct device * start,void * data,int (* fn)(struct device *,void *))39569df024eSGreg Kroah-Hartman int class_for_each_device(const struct class *class, const struct device *start,
39693562b53SGreg Kroah-Hartman void *data, int (*fn)(struct device *, void *))
397fd04897bSDave Young {
3987b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(class);
3995a3ceb86STejun Heo struct class_dev_iter iter;
400fd04897bSDave Young struct device *dev;
401fd04897bSDave Young int error = 0;
402fd04897bSDave Young
403fd04897bSDave Young if (!class)
404fd04897bSDave Young return -EINVAL;
4057b884b7fSGreg Kroah-Hartman if (!sp) {
4067c225035SDavid Brownell WARN(1, "%s called for class '%s' before it was initialized",
4077c225035SDavid Brownell __func__, class->name);
4087c225035SDavid Brownell return -EINVAL;
4097c225035SDavid Brownell }
4107c225035SDavid Brownell
4115a3ceb86STejun Heo class_dev_iter_init(&iter, class, start, NULL);
4125a3ceb86STejun Heo while ((dev = class_dev_iter_next(&iter))) {
413fd04897bSDave Young error = fn(dev, data);
414fd04897bSDave Young if (error)
415fd04897bSDave Young break;
416fd04897bSDave Young }
4175a3ceb86STejun Heo class_dev_iter_exit(&iter);
4187b884b7fSGreg Kroah-Hartman subsys_put(sp);
419fd04897bSDave Young
420fd04897bSDave Young return error;
421fd04897bSDave Young }
422fd04897bSDave Young EXPORT_SYMBOL_GPL(class_for_each_device);
423fd04897bSDave Young
424fd04897bSDave Young /**
425fd04897bSDave Young * class_find_device - device iterator for locating a particular device
426fd04897bSDave Young * @class: the class we're iterating
427695794aeSGreg Kroah-Hartman * @start: Device to begin with
428fd04897bSDave Young * @data: data for the match function
429fd04897bSDave Young * @match: function to check device
430fd04897bSDave Young *
431fd04897bSDave Young * This is similar to the class_for_each_dev() function above, but it
432fd04897bSDave Young * returns a reference to a device that is 'found' for later use, as
433fd04897bSDave Young * determined by the @match callback.
434fd04897bSDave Young *
435fd04897bSDave Young * The callback should return 0 if the device doesn't match and non-zero
436fd04897bSDave Young * if it does. If the callback returns non-zero, this function will
437fd04897bSDave Young * return to the caller and not iterate over any more devices.
438a63ca8f6SRandy Dunlap *
439fd04897bSDave Young * Note, you will need to drop the reference with put_device() after use.
440fd04897bSDave Young *
44112077754SRolf Eike Beer * @match is allowed to do anything including calling back into class
4425a3ceb86STejun Heo * code. There's no locking restriction.
443fd04897bSDave Young */
class_find_device(const struct class * class,const struct device * start,const void * data,int (* match)(struct device *,const void *))444cf41015eSGreg Kroah-Hartman struct device *class_find_device(const struct class *class, const struct device *start,
4459f3b795aSMichał Mirosław const void *data,
4469f3b795aSMichał Mirosław int (*match)(struct device *, const void *))
447fd04897bSDave Young {
4487b884b7fSGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(class);
4495a3ceb86STejun Heo struct class_dev_iter iter;
450fd04897bSDave Young struct device *dev;
451fd04897bSDave Young
452fd04897bSDave Young if (!class)
453fd04897bSDave Young return NULL;
4547b884b7fSGreg Kroah-Hartman if (!sp) {
4557c225035SDavid Brownell WARN(1, "%s called for class '%s' before it was initialized",
4567c225035SDavid Brownell __func__, class->name);
4577c225035SDavid Brownell return NULL;
4587c225035SDavid Brownell }
459fd04897bSDave Young
4605a3ceb86STejun Heo class_dev_iter_init(&iter, class, start, NULL);
4615a3ceb86STejun Heo while ((dev = class_dev_iter_next(&iter))) {
462fd04897bSDave Young if (match(dev, data)) {
4635a3ceb86STejun Heo get_device(dev);
464fd04897bSDave Young break;
465fd04897bSDave Young }
4665a3ceb86STejun Heo }
4675a3ceb86STejun Heo class_dev_iter_exit(&iter);
4687b884b7fSGreg Kroah-Hartman subsys_put(sp);
469fd04897bSDave Young
4705a3ceb86STejun Heo return dev;
471fd04897bSDave Young }
472fd04897bSDave Young EXPORT_SYMBOL_GPL(class_find_device);
473fd04897bSDave Young
class_interface_register(struct class_interface * class_intf)4741da177e4SLinus Torvalds int class_interface_register(struct class_interface *class_intf)
4751da177e4SLinus Torvalds {
4767b884b7fSGreg Kroah-Hartman struct subsys_private *sp;
477884f8ce4SGreg Kroah-Hartman const struct class *parent;
4785a3ceb86STejun Heo struct class_dev_iter iter;
479c47ed219SGreg Kroah-Hartman struct device *dev;
4801da177e4SLinus Torvalds
4811da177e4SLinus Torvalds if (!class_intf || !class_intf->class)
4821da177e4SLinus Torvalds return -ENODEV;
4831da177e4SLinus Torvalds
4847b884b7fSGreg Kroah-Hartman parent = class_intf->class;
4857b884b7fSGreg Kroah-Hartman sp = class_to_subsys(parent);
4867b884b7fSGreg Kroah-Hartman if (!sp)
4871da177e4SLinus Torvalds return -EINVAL;
4881da177e4SLinus Torvalds
4897b884b7fSGreg Kroah-Hartman /*
4907b884b7fSGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when
4917b884b7fSGreg Kroah-Hartman * the interface is removed in the call to class_interface_unregister()
4927b884b7fSGreg Kroah-Hartman */
4937b884b7fSGreg Kroah-Hartman
4947b884b7fSGreg Kroah-Hartman mutex_lock(&sp->mutex);
4957b884b7fSGreg Kroah-Hartman list_add_tail(&class_intf->node, &sp->interfaces);
496c47ed219SGreg Kroah-Hartman if (class_intf->add_dev) {
4975a3ceb86STejun Heo class_dev_iter_init(&iter, parent, NULL, NULL);
4985a3ceb86STejun Heo while ((dev = class_dev_iter_next(&iter)))
4992243acd5SGreg Kroah-Hartman class_intf->add_dev(dev);
5005a3ceb86STejun Heo class_dev_iter_exit(&iter);
501c47ed219SGreg Kroah-Hartman }
5027b884b7fSGreg Kroah-Hartman mutex_unlock(&sp->mutex);
5031da177e4SLinus Torvalds
5041da177e4SLinus Torvalds return 0;
5051da177e4SLinus Torvalds }
506779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_interface_register);
5071da177e4SLinus Torvalds
class_interface_unregister(struct class_interface * class_intf)5081da177e4SLinus Torvalds void class_interface_unregister(struct class_interface *class_intf)
5091da177e4SLinus Torvalds {
5107b884b7fSGreg Kroah-Hartman struct subsys_private *sp;
5116b0d49beSGreg Kroah-Hartman const struct class *parent = class_intf->class;
5125a3ceb86STejun Heo struct class_dev_iter iter;
513c47ed219SGreg Kroah-Hartman struct device *dev;
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds if (!parent)
5161da177e4SLinus Torvalds return;
5171da177e4SLinus Torvalds
5187b884b7fSGreg Kroah-Hartman sp = class_to_subsys(parent);
5197b884b7fSGreg Kroah-Hartman if (!sp)
5207b884b7fSGreg Kroah-Hartman return;
5217b884b7fSGreg Kroah-Hartman
5227b884b7fSGreg Kroah-Hartman mutex_lock(&sp->mutex);
5231da177e4SLinus Torvalds list_del_init(&class_intf->node);
524c47ed219SGreg Kroah-Hartman if (class_intf->remove_dev) {
5255a3ceb86STejun Heo class_dev_iter_init(&iter, parent, NULL, NULL);
5265a3ceb86STejun Heo while ((dev = class_dev_iter_next(&iter)))
5272243acd5SGreg Kroah-Hartman class_intf->remove_dev(dev);
5285a3ceb86STejun Heo class_dev_iter_exit(&iter);
529c47ed219SGreg Kroah-Hartman }
5307b884b7fSGreg Kroah-Hartman mutex_unlock(&sp->mutex);
5311da177e4SLinus Torvalds
5327b884b7fSGreg Kroah-Hartman /*
5337b884b7fSGreg Kroah-Hartman * Decrement the reference count twice, once for the class_to_subsys()
5347b884b7fSGreg Kroah-Hartman * call in the start of this function, and the second one from the
5357b884b7fSGreg Kroah-Hartman * reference increment in class_interface_register()
5367b884b7fSGreg Kroah-Hartman */
5377b884b7fSGreg Kroah-Hartman subsys_put(sp);
5387b884b7fSGreg Kroah-Hartman subsys_put(sp);
5391da177e4SLinus Torvalds }
540779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_interface_unregister);
5411da177e4SLinus Torvalds
show_class_attr_string(const struct class * class,const struct class_attribute * attr,char * buf)54275a2d422SGreg Kroah-Hartman ssize_t show_class_attr_string(const struct class *class,
54375a2d422SGreg Kroah-Hartman const struct class_attribute *attr, char *buf)
544869dfc87SAndi Kleen {
545869dfc87SAndi Kleen struct class_attribute_string *cs;
546d34898deSCosmin Tomulescu
547869dfc87SAndi Kleen cs = container_of(attr, struct class_attribute_string, attr);
548948b3edbSJoe Perches return sysfs_emit(buf, "%s\n", cs->str);
549869dfc87SAndi Kleen }
550869dfc87SAndi Kleen
551869dfc87SAndi Kleen EXPORT_SYMBOL_GPL(show_class_attr_string);
552869dfc87SAndi Kleen
55346227094SJean Delvare struct class_compat {
55446227094SJean Delvare struct kobject *kobj;
55546227094SJean Delvare };
55646227094SJean Delvare
55746227094SJean Delvare /**
55846227094SJean Delvare * class_compat_register - register a compatibility class
55946227094SJean Delvare * @name: the name of the class
56046227094SJean Delvare *
56146227094SJean Delvare * Compatibility class are meant as a temporary user-space compatibility
56246227094SJean Delvare * workaround when converting a family of class devices to a bus devices.
56346227094SJean Delvare */
class_compat_register(const char * name)56446227094SJean Delvare struct class_compat *class_compat_register(const char *name)
56546227094SJean Delvare {
56646227094SJean Delvare struct class_compat *cls;
56746227094SJean Delvare
56846227094SJean Delvare cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
56946227094SJean Delvare if (!cls)
57046227094SJean Delvare return NULL;
57146227094SJean Delvare cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
57246227094SJean Delvare if (!cls->kobj) {
57346227094SJean Delvare kfree(cls);
57446227094SJean Delvare return NULL;
57546227094SJean Delvare }
57646227094SJean Delvare return cls;
57746227094SJean Delvare }
57846227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_register);
57946227094SJean Delvare
58046227094SJean Delvare /**
58146227094SJean Delvare * class_compat_unregister - unregister a compatibility class
58246227094SJean Delvare * @cls: the class to unregister
58346227094SJean Delvare */
class_compat_unregister(struct class_compat * cls)58446227094SJean Delvare void class_compat_unregister(struct class_compat *cls)
58546227094SJean Delvare {
58646227094SJean Delvare kobject_put(cls->kobj);
58746227094SJean Delvare kfree(cls);
58846227094SJean Delvare }
58946227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_unregister);
59046227094SJean Delvare
59146227094SJean Delvare /**
59246227094SJean Delvare * class_compat_create_link - create a compatibility class device link to
59346227094SJean Delvare * a bus device
59446227094SJean Delvare * @cls: the compatibility class
59546227094SJean Delvare * @dev: the target bus device
59646227094SJean Delvare * @device_link: an optional device to which a "device" link should be created
59746227094SJean Delvare */
class_compat_create_link(struct class_compat * cls,struct device * dev,struct device * device_link)59846227094SJean Delvare int class_compat_create_link(struct class_compat *cls, struct device *dev,
59946227094SJean Delvare struct device *device_link)
60046227094SJean Delvare {
60146227094SJean Delvare int error;
60246227094SJean Delvare
60346227094SJean Delvare error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
60446227094SJean Delvare if (error)
60546227094SJean Delvare return error;
60646227094SJean Delvare
60746227094SJean Delvare /*
60846227094SJean Delvare * Optionally add a "device" link (typically to the parent), as a
60946227094SJean Delvare * class device would have one and we want to provide as much
61046227094SJean Delvare * backwards compatibility as possible.
61146227094SJean Delvare */
61246227094SJean Delvare if (device_link) {
61346227094SJean Delvare error = sysfs_create_link(&dev->kobj, &device_link->kobj,
61446227094SJean Delvare "device");
61546227094SJean Delvare if (error)
61646227094SJean Delvare sysfs_remove_link(cls->kobj, dev_name(dev));
61746227094SJean Delvare }
61846227094SJean Delvare
61946227094SJean Delvare return error;
62046227094SJean Delvare }
62146227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_create_link);
62246227094SJean Delvare
62346227094SJean Delvare /**
62446227094SJean Delvare * class_compat_remove_link - remove a compatibility class device link to
62546227094SJean Delvare * a bus device
62646227094SJean Delvare * @cls: the compatibility class
62746227094SJean Delvare * @dev: the target bus device
62846227094SJean Delvare * @device_link: an optional device to which a "device" link was previously
62946227094SJean Delvare * created
63046227094SJean Delvare */
class_compat_remove_link(struct class_compat * cls,struct device * dev,struct device * device_link)63146227094SJean Delvare void class_compat_remove_link(struct class_compat *cls, struct device *dev,
63246227094SJean Delvare struct device *device_link)
63346227094SJean Delvare {
63446227094SJean Delvare if (device_link)
63546227094SJean Delvare sysfs_remove_link(&dev->kobj, "device");
63646227094SJean Delvare sysfs_remove_link(cls->kobj, dev_name(dev));
63746227094SJean Delvare }
63846227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_remove_link);
63946227094SJean Delvare
6406f14c022SGreg Kroah-Hartman /**
6416f14c022SGreg Kroah-Hartman * class_is_registered - determine if at this moment in time, a class is
6426f14c022SGreg Kroah-Hartman * registered in the driver core or not.
6436f14c022SGreg Kroah-Hartman * @class: the class to check
6446f14c022SGreg Kroah-Hartman *
6456f14c022SGreg Kroah-Hartman * Returns a boolean to state if the class is registered in the driver core
6466f14c022SGreg Kroah-Hartman * or not. Note that the value could switch right after this call is made,
6476f14c022SGreg Kroah-Hartman * so only use this in places where you "know" it is safe to do so (usually
6486f14c022SGreg Kroah-Hartman * to determine if the specific class has been registered yet or not).
6496f14c022SGreg Kroah-Hartman *
6506f14c022SGreg Kroah-Hartman * Be careful in using this.
6516f14c022SGreg Kroah-Hartman */
class_is_registered(const struct class * class)6526f14c022SGreg Kroah-Hartman bool class_is_registered(const struct class *class)
6536f14c022SGreg Kroah-Hartman {
6546f14c022SGreg Kroah-Hartman struct subsys_private *sp = class_to_subsys(class);
6556f14c022SGreg Kroah-Hartman bool is_initialized = false;
6566f14c022SGreg Kroah-Hartman
6576f14c022SGreg Kroah-Hartman if (sp) {
6586f14c022SGreg Kroah-Hartman is_initialized = true;
6596f14c022SGreg Kroah-Hartman subsys_put(sp);
6606f14c022SGreg Kroah-Hartman }
6616f14c022SGreg Kroah-Hartman return is_initialized;
6626f14c022SGreg Kroah-Hartman }
6636f14c022SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_is_registered);
6646f14c022SGreg Kroah-Hartman
classes_init(void)6651da177e4SLinus Torvalds int __init classes_init(void)
6661da177e4SLinus Torvalds {
667443dbf90SGreg Kroah-Hartman class_kset = kset_create_and_add("class", NULL, NULL);
668443dbf90SGreg Kroah-Hartman if (!class_kset)
669443dbf90SGreg Kroah-Hartman return -ENOMEM;
6701da177e4SLinus Torvalds return 0;
6711da177e4SLinus Torvalds }
672