xref: /linux-6.15/drivers/base/class.c (revision 827ed8b1)
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>
17e9ba6365S[email protected] #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 
186a169a663SZijun Hu 	if (cls->ns_type && !cls->namespace) {
187a169a663SZijun Hu 		pr_err("%s: class '%s' does not have namespace\n",
188a169a663SZijun Hu 		       __func__, cls->name);
189a169a663SZijun Hu 		return -EINVAL;
190a169a663SZijun Hu 	}
191a169a663SZijun Hu 	if (!cls->ns_type && cls->namespace) {
192a169a663SZijun Hu 		pr_err("%s: class '%s' does not have ns_type\n",
193a169a663SZijun Hu 		       __func__, cls->name);
194a169a663SZijun Hu 		return -EINVAL;
195a169a663SZijun Hu 	}
196a169a663SZijun Hu 
1977c71448bSGreg Kroah-Hartman 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
1987c71448bSGreg Kroah-Hartman 	if (!cp)
1997c71448bSGreg Kroah-Hartman 		return -ENOMEM;
2006b6e39a6SKay Sievers 	klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
201ca22e56dSKay Sievers 	INIT_LIST_HEAD(&cp->interfaces);
2026b6e39a6SKay Sievers 	kset_init(&cp->glue_dirs);
203dcfbb67eSGreg Kroah-Hartman 	key = &cp->lock_key;
204dcfbb67eSGreg Kroah-Hartman 	lockdep_register_key(key);
205ca22e56dSKay Sievers 	__mutex_init(&cp->mutex, "subsys mutex", key);
2066b6e39a6SKay Sievers 	error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
20728f2d57dSMaurizio Lombardi 	if (error)
20828f2d57dSMaurizio Lombardi 		goto err_out;
2091da177e4SLinus Torvalds 
2106b6e39a6SKay Sievers 	cp->subsys.kobj.kset = class_kset;
2116b6e39a6SKay Sievers 	cp->subsys.kobj.ktype = &class_ktype;
2127c71448bSGreg Kroah-Hartman 	cp->class = cls;
2131da177e4SLinus Torvalds 
2146b6e39a6SKay Sievers 	error = kset_register(&cp->subsys);
2150b2a1a39SRafael J. Wysocki 	if (error)
2160b2a1a39SRafael J. Wysocki 		goto err_out;
2170b2a1a39SRafael J. Wysocki 
2187b884b7fSGreg Kroah-Hartman 	error = sysfs_create_groups(&cp->subsys.kobj, cls->class_groups);
2198c3e8a6bSYang Yingliang 	if (error) {
2208c3e8a6bSYang Yingliang 		kobject_del(&cp->subsys.kobj);
2218c3e8a6bSYang Yingliang 		kfree_const(cp->subsys.kobj.name);
2220b2a1a39SRafael J. Wysocki 		goto err_out;
2238c3e8a6bSYang Yingliang 	}
2240b2a1a39SRafael J. Wysocki 	return 0;
2250b2a1a39SRafael J. Wysocki 
2260b2a1a39SRafael J. Wysocki err_out:
22793ec4a3bSJing Xia 	lockdep_unregister_key(key);
2280b2a1a39SRafael J. Wysocki 	kfree(cp);
2291da177e4SLinus Torvalds 	return error;
2301da177e4SLinus Torvalds }
231dcfbb67eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_register);
2321da177e4SLinus Torvalds 
class_unregister(const struct class * cls)233517d4927SGreg Kroah-Hartman void class_unregister(const struct class *cls)
2341da177e4SLinus Torvalds {
2357b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp = class_to_subsys(cls);
2367b884b7fSGreg Kroah-Hartman 
2377b884b7fSGreg Kroah-Hartman 	if (!sp)
2387b884b7fSGreg Kroah-Hartman 		return;
2397b884b7fSGreg Kroah-Hartman 
2401da177e4SLinus Torvalds 	pr_debug("device class '%s': unregistering\n", cls->name);
2417b884b7fSGreg Kroah-Hartman 
2427b884b7fSGreg Kroah-Hartman 	sysfs_remove_groups(&sp->subsys.kobj, cls->class_groups);
2437b884b7fSGreg Kroah-Hartman 	kset_unregister(&sp->subsys);
2447b884b7fSGreg Kroah-Hartman 	subsys_put(sp);
2451da177e4SLinus Torvalds }
246779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_unregister);
2471da177e4SLinus Torvalds 
class_create_release(const struct class * cls)248979207caSGreg Kroah-Hartman static void class_create_release(const struct class *cls)
249e9ba6365S[email protected] {
2502b3a302aSHarvey Harrison 	pr_debug("%s called for %s\n", __func__, cls->name);
251e9ba6365S[email protected] 	kfree(cls);
252e9ba6365S[email protected] }
253e9ba6365S[email protected] 
2542fc68447S[email protected] /**
255dcfbb67eSGreg Kroah-Hartman  * class_create - create a struct class structure
2562fc68447S[email protected]  * @name: pointer to a string for the name of this class.
2572fc68447S[email protected]  *
2582fc68447S[email protected]  * This is used to create a struct class pointer that can then be used
259c3b19ff0SKay Sievers  * in calls to device_create().
2602fc68447S[email protected]  *
261f0eae0edSJani Nikula  * Returns &struct class pointer on success, or ERR_PTR() on error.
262f0eae0edSJani Nikula  *
2632fc68447S[email protected]  * Note, the pointer created here is to be destroyed when finished by
2642fc68447S[email protected]  * making a call to class_destroy().
2652fc68447S[email protected]  */
class_create(const char * name)266dcfbb67eSGreg Kroah-Hartman struct class *class_create(const char *name)
267e9ba6365S[email protected] {
268e9ba6365S[email protected] 	struct class *cls;
269e9ba6365S[email protected] 	int retval;
270e9ba6365S[email protected] 
2714aed0644SJiri Slaby 	cls = kzalloc(sizeof(*cls), GFP_KERNEL);
272e9ba6365S[email protected] 	if (!cls) {
273e9ba6365S[email protected] 		retval = -ENOMEM;
274e9ba6365S[email protected] 		goto error;
275e9ba6365S[email protected] 	}
276e9ba6365S[email protected] 
277e9ba6365S[email protected] 	cls->name = name;
278e9ba6365S[email protected] 	cls->class_release = class_create_release;
279e9ba6365S[email protected] 
280dcfbb67eSGreg Kroah-Hartman 	retval = class_register(cls);
281e9ba6365S[email protected] 	if (retval)
282e9ba6365S[email protected] 		goto error;
283e9ba6365S[email protected] 
284e9ba6365S[email protected] 	return cls;
285e9ba6365S[email protected] 
286e9ba6365S[email protected] error:
287e9ba6365S[email protected] 	kfree(cls);
288e9ba6365S[email protected] 	return ERR_PTR(retval);
289e9ba6365S[email protected] }
290dcfbb67eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_create);
291e9ba6365S[email protected] 
2922fc68447S[email protected] /**
2932fc68447S[email protected]  * class_destroy - destroys a struct class structure
29492a0f861SRolf Eike Beer  * @cls: pointer to the struct class that is to be destroyed
2952fc68447S[email protected]  *
2962fc68447S[email protected]  * Note, the pointer to be destroyed must have been created with a call
2972fc68447S[email protected]  * to class_create().
2982fc68447S[email protected]  */
class_destroy(const struct class * cls)299517d4927SGreg Kroah-Hartman void class_destroy(const struct class *cls)
300e9ba6365S[email protected] {
301e9628e01SYang Yingliang 	if (IS_ERR_OR_NULL(cls))
302e9ba6365S[email protected] 		return;
303e9ba6365S[email protected] 
304e9ba6365S[email protected] 	class_unregister(cls);
305e9ba6365S[email protected] }
306779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_destroy);
3071da177e4SLinus Torvalds 
308fd04897bSDave Young /**
3095a3ceb86STejun Heo  * class_dev_iter_init - initialize class device iterator
3105a3ceb86STejun Heo  * @iter: class iterator to initialize
3115a3ceb86STejun Heo  * @class: the class we wanna iterate over
3125a3ceb86STejun Heo  * @start: the device to start iterating from, if any
3135a3ceb86STejun Heo  * @type: device_type of the devices to iterate over, NULL for all
3145a3ceb86STejun Heo  *
3155a3ceb86STejun Heo  * Initialize class iterator @iter such that it iterates over devices
3165a3ceb86STejun Heo  * of @class.  If @start is set, the list iteration will start there,
3175a3ceb86STejun Heo  * otherwise if it is NULL, the iteration starts at the beginning of
3185a3ceb86STejun Heo  * the list.
3195a3ceb86STejun Heo  */
class_dev_iter_init(struct class_dev_iter * iter,const struct class * class,const struct device * start,const struct device_type * type)320a2fd6e42SGreg Kroah-Hartman void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
321a2fd6e42SGreg Kroah-Hartman 			 const struct device *start, const struct device_type *type)
3225a3ceb86STejun Heo {
3237b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp = class_to_subsys(class);
3245a3ceb86STejun Heo 	struct klist_node *start_knode = NULL;
3255a3ceb86STejun Heo 
326e128f82fSZijun Hu 	memset(iter, 0, sizeof(*iter));
327e128f82fSZijun Hu 	if (!sp) {
328e128f82fSZijun Hu 		pr_crit("%s: class %p was not registered yet\n",
329e128f82fSZijun Hu 			__func__, class);
3307b884b7fSGreg Kroah-Hartman 		return;
331e128f82fSZijun Hu 	}
3327b884b7fSGreg Kroah-Hartman 
3335a3ceb86STejun Heo 	if (start)
334570d0200SWei Yang 		start_knode = &start->p->knode_class;
3357b884b7fSGreg Kroah-Hartman 	klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
3365a3ceb86STejun Heo 	iter->type = type;
337ddaf098eSGreg Kroah-Hartman 	iter->sp = sp;
3385a3ceb86STejun Heo }
3395a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_init);
3405a3ceb86STejun Heo 
3415a3ceb86STejun Heo /**
3425a3ceb86STejun Heo  * class_dev_iter_next - iterate to the next device
3435a3ceb86STejun Heo  * @iter: class iterator to proceed
3445a3ceb86STejun Heo  *
3455a3ceb86STejun Heo  * Proceed @iter to the next device and return it.  Returns NULL if
3465a3ceb86STejun Heo  * iteration is complete.
3475a3ceb86STejun Heo  *
3485a3ceb86STejun Heo  * The returned device is referenced and won't be released till
3495a3ceb86STejun Heo  * iterator is proceed to the next device or exited.  The caller is
3505a3ceb86STejun Heo  * free to do whatever it wants to do with the device including
3515a3ceb86STejun Heo  * calling back into class code.
3525a3ceb86STejun Heo  */
class_dev_iter_next(struct class_dev_iter * iter)3535a3ceb86STejun Heo struct device *class_dev_iter_next(struct class_dev_iter *iter)
3545a3ceb86STejun Heo {
3555a3ceb86STejun Heo 	struct klist_node *knode;
3565a3ceb86STejun Heo 	struct device *dev;
3575a3ceb86STejun Heo 
358e128f82fSZijun Hu 	if (!iter->sp)
359e128f82fSZijun Hu 		return NULL;
360e128f82fSZijun Hu 
3615a3ceb86STejun Heo 	while (1) {
3625a3ceb86STejun Heo 		knode = klist_next(&iter->ki);
3635a3ceb86STejun Heo 		if (!knode)
3645a3ceb86STejun Heo 			return NULL;
365570d0200SWei Yang 		dev = klist_class_to_dev(knode);
3665a3ceb86STejun Heo 		if (!iter->type || iter->type == dev->type)
3675a3ceb86STejun Heo 			return dev;
3685a3ceb86STejun Heo 	}
3695a3ceb86STejun Heo }
3705a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_next);
3715a3ceb86STejun Heo 
3725a3ceb86STejun Heo /**
3735a3ceb86STejun Heo  * class_dev_iter_exit - finish iteration
3745a3ceb86STejun Heo  * @iter: class iterator to finish
3755a3ceb86STejun Heo  *
3765a3ceb86STejun Heo  * Finish an iteration.  Always call this function after iteration is
3775a3ceb86STejun Heo  * complete whether the iteration ran till the end or not.
3785a3ceb86STejun Heo  */
class_dev_iter_exit(struct class_dev_iter * iter)3795a3ceb86STejun Heo void class_dev_iter_exit(struct class_dev_iter *iter)
3805a3ceb86STejun Heo {
3815a3ceb86STejun Heo 	klist_iter_exit(&iter->ki);
382ddaf098eSGreg Kroah-Hartman 	subsys_put(iter->sp);
3835a3ceb86STejun Heo }
3845a3ceb86STejun Heo EXPORT_SYMBOL_GPL(class_dev_iter_exit);
3855a3ceb86STejun Heo 
3865a3ceb86STejun Heo /**
387fd04897bSDave Young  * class_for_each_device - device iterator
388fd04897bSDave Young  * @class: the class we're iterating
38993562b53SGreg Kroah-Hartman  * @start: the device to start with in the list, if any.
390fd04897bSDave Young  * @data: data for the callback
391fd04897bSDave Young  * @fn: function to be called for each device
392fd04897bSDave Young  *
393fd04897bSDave Young  * Iterate over @class's list of devices, and call @fn for each,
39493562b53SGreg Kroah-Hartman  * passing it @data.  If @start is set, the list iteration will start
39593562b53SGreg Kroah-Hartman  * there, otherwise if it is NULL, the iteration starts at the
39693562b53SGreg Kroah-Hartman  * beginning of the list.
397fd04897bSDave Young  *
398fd04897bSDave Young  * We check the return of @fn each time. If it returns anything
399fd04897bSDave Young  * other than 0, we break out and return that value.
400fd04897bSDave Young  *
4015a3ceb86STejun Heo  * @fn is allowed to do anything including calling back into class
4025a3ceb86STejun Heo  * code.  There's no locking restriction.
403fd04897bSDave Young  */
class_for_each_device(const struct class * class,const struct device * start,void * data,device_iter_t fn)40469df024eSGreg Kroah-Hartman int class_for_each_device(const struct class *class, const struct device *start,
405767b74e0SZijun Hu 			  void *data, device_iter_t fn)
406fd04897bSDave Young {
4077b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp = class_to_subsys(class);
4085a3ceb86STejun Heo 	struct class_dev_iter iter;
409fd04897bSDave Young 	struct device *dev;
410fd04897bSDave Young 	int error = 0;
411fd04897bSDave Young 
412fd04897bSDave Young 	if (!class)
413fd04897bSDave Young 		return -EINVAL;
4147b884b7fSGreg Kroah-Hartman 	if (!sp) {
415f659e8fbSZijun Hu 		WARN(1, "%s called for class '%s' before it was registered",
4167c225035SDavid Brownell 		     __func__, class->name);
4177c225035SDavid Brownell 		return -EINVAL;
4187c225035SDavid Brownell 	}
4197c225035SDavid Brownell 
4205a3ceb86STejun Heo 	class_dev_iter_init(&iter, class, start, NULL);
4215a3ceb86STejun Heo 	while ((dev = class_dev_iter_next(&iter))) {
422fd04897bSDave Young 		error = fn(dev, data);
423fd04897bSDave Young 		if (error)
424fd04897bSDave Young 			break;
425fd04897bSDave Young 	}
4265a3ceb86STejun Heo 	class_dev_iter_exit(&iter);
4277b884b7fSGreg Kroah-Hartman 	subsys_put(sp);
428fd04897bSDave Young 
429fd04897bSDave Young 	return error;
430fd04897bSDave Young }
431fd04897bSDave Young EXPORT_SYMBOL_GPL(class_for_each_device);
432fd04897bSDave Young 
433fd04897bSDave Young /**
434fd04897bSDave Young  * class_find_device - device iterator for locating a particular device
435fd04897bSDave Young  * @class: the class we're iterating
436695794aeSGreg Kroah-Hartman  * @start: Device to begin with
437fd04897bSDave Young  * @data: data for the match function
438fd04897bSDave Young  * @match: function to check device
439fd04897bSDave Young  *
440fd04897bSDave Young  * This is similar to the class_for_each_dev() function above, but it
441fd04897bSDave Young  * returns a reference to a device that is 'found' for later use, as
442fd04897bSDave Young  * determined by the @match callback.
443fd04897bSDave Young  *
444fd04897bSDave Young  * The callback should return 0 if the device doesn't match and non-zero
445fd04897bSDave Young  * if it does.  If the callback returns non-zero, this function will
446fd04897bSDave Young  * return to the caller and not iterate over any more devices.
447a63ca8f6SRandy Dunlap  *
448fd04897bSDave Young  * Note, you will need to drop the reference with put_device() after use.
449fd04897bSDave Young  *
45012077754SRolf Eike Beer  * @match is allowed to do anything including calling back into class
4515a3ceb86STejun Heo  * code.  There's no locking restriction.
452fd04897bSDave Young  */
class_find_device(const struct class * class,const struct device * start,const void * data,device_match_t match)453cf41015eSGreg Kroah-Hartman struct device *class_find_device(const struct class *class, const struct device *start,
454b45ed06fSZijun Hu 				 const void *data, device_match_t match)
455fd04897bSDave Young {
4567b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp = class_to_subsys(class);
4575a3ceb86STejun Heo 	struct class_dev_iter iter;
458fd04897bSDave Young 	struct device *dev;
459fd04897bSDave Young 
460fd04897bSDave Young 	if (!class)
461fd04897bSDave Young 		return NULL;
4627b884b7fSGreg Kroah-Hartman 	if (!sp) {
463f659e8fbSZijun Hu 		WARN(1, "%s called for class '%s' before it was registered",
4647c225035SDavid Brownell 		     __func__, class->name);
4657c225035SDavid Brownell 		return NULL;
4667c225035SDavid Brownell 	}
467fd04897bSDave Young 
4685a3ceb86STejun Heo 	class_dev_iter_init(&iter, class, start, NULL);
4695a3ceb86STejun Heo 	while ((dev = class_dev_iter_next(&iter))) {
470fd04897bSDave Young 		if (match(dev, data)) {
4715a3ceb86STejun Heo 			get_device(dev);
472fd04897bSDave Young 			break;
473fd04897bSDave Young 		}
4745a3ceb86STejun Heo 	}
4755a3ceb86STejun Heo 	class_dev_iter_exit(&iter);
4767b884b7fSGreg Kroah-Hartman 	subsys_put(sp);
477fd04897bSDave Young 
4785a3ceb86STejun Heo 	return dev;
479fd04897bSDave Young }
480fd04897bSDave Young EXPORT_SYMBOL_GPL(class_find_device);
481fd04897bSDave Young 
class_interface_register(struct class_interface * class_intf)4821da177e4SLinus Torvalds int class_interface_register(struct class_interface *class_intf)
4831da177e4SLinus Torvalds {
4847b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp;
485884f8ce4SGreg Kroah-Hartman 	const struct class *parent;
4865a3ceb86STejun Heo 	struct class_dev_iter iter;
487c47ed219SGreg Kroah-Hartman 	struct device *dev;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (!class_intf || !class_intf->class)
4901da177e4SLinus Torvalds 		return -ENODEV;
4911da177e4SLinus Torvalds 
4927b884b7fSGreg Kroah-Hartman 	parent = class_intf->class;
4937b884b7fSGreg Kroah-Hartman 	sp = class_to_subsys(parent);
4947b884b7fSGreg Kroah-Hartman 	if (!sp)
4951da177e4SLinus Torvalds 		return -EINVAL;
4961da177e4SLinus Torvalds 
4977b884b7fSGreg Kroah-Hartman 	/*
4987b884b7fSGreg Kroah-Hartman 	 * Reference in sp is now incremented and will be dropped when
4997b884b7fSGreg Kroah-Hartman 	 * the interface is removed in the call to class_interface_unregister()
5007b884b7fSGreg Kroah-Hartman 	 */
5017b884b7fSGreg Kroah-Hartman 
5027b884b7fSGreg Kroah-Hartman 	mutex_lock(&sp->mutex);
5037b884b7fSGreg Kroah-Hartman 	list_add_tail(&class_intf->node, &sp->interfaces);
504c47ed219SGreg Kroah-Hartman 	if (class_intf->add_dev) {
5055a3ceb86STejun Heo 		class_dev_iter_init(&iter, parent, NULL, NULL);
5065a3ceb86STejun Heo 		while ((dev = class_dev_iter_next(&iter)))
5072243acd5SGreg Kroah-Hartman 			class_intf->add_dev(dev);
5085a3ceb86STejun Heo 		class_dev_iter_exit(&iter);
509c47ed219SGreg Kroah-Hartman 	}
5107b884b7fSGreg Kroah-Hartman 	mutex_unlock(&sp->mutex);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	return 0;
5131da177e4SLinus Torvalds }
514779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_interface_register);
5151da177e4SLinus Torvalds 
class_interface_unregister(struct class_interface * class_intf)5161da177e4SLinus Torvalds void class_interface_unregister(struct class_interface *class_intf)
5171da177e4SLinus Torvalds {
5187b884b7fSGreg Kroah-Hartman 	struct subsys_private *sp;
5196b0d49beSGreg Kroah-Hartman 	const struct class *parent = class_intf->class;
5205a3ceb86STejun Heo 	struct class_dev_iter iter;
521c47ed219SGreg Kroah-Hartman 	struct device *dev;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	if (!parent)
5241da177e4SLinus Torvalds 		return;
5251da177e4SLinus Torvalds 
5267b884b7fSGreg Kroah-Hartman 	sp = class_to_subsys(parent);
5277b884b7fSGreg Kroah-Hartman 	if (!sp)
5287b884b7fSGreg Kroah-Hartman 		return;
5297b884b7fSGreg Kroah-Hartman 
5307b884b7fSGreg Kroah-Hartman 	mutex_lock(&sp->mutex);
5311da177e4SLinus Torvalds 	list_del_init(&class_intf->node);
532c47ed219SGreg Kroah-Hartman 	if (class_intf->remove_dev) {
5335a3ceb86STejun Heo 		class_dev_iter_init(&iter, parent, NULL, NULL);
5345a3ceb86STejun Heo 		while ((dev = class_dev_iter_next(&iter)))
5352243acd5SGreg Kroah-Hartman 			class_intf->remove_dev(dev);
5365a3ceb86STejun Heo 		class_dev_iter_exit(&iter);
537c47ed219SGreg Kroah-Hartman 	}
5387b884b7fSGreg Kroah-Hartman 	mutex_unlock(&sp->mutex);
5391da177e4SLinus Torvalds 
5407b884b7fSGreg Kroah-Hartman 	/*
5417b884b7fSGreg Kroah-Hartman 	 * Decrement the reference count twice, once for the class_to_subsys()
5427b884b7fSGreg Kroah-Hartman 	 * call in the start of this function, and the second one from the
5437b884b7fSGreg Kroah-Hartman 	 * reference increment in class_interface_register()
5447b884b7fSGreg Kroah-Hartman 	 */
5457b884b7fSGreg Kroah-Hartman 	subsys_put(sp);
5467b884b7fSGreg Kroah-Hartman 	subsys_put(sp);
5471da177e4SLinus Torvalds }
548779aeb73SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_interface_unregister);
5491da177e4SLinus Torvalds 
show_class_attr_string(const struct class * class,const struct class_attribute * attr,char * buf)55075a2d422SGreg Kroah-Hartman ssize_t show_class_attr_string(const struct class *class,
55175a2d422SGreg Kroah-Hartman 			       const struct class_attribute *attr, char *buf)
552869dfc87SAndi Kleen {
553869dfc87SAndi Kleen 	struct class_attribute_string *cs;
554d34898deSCosmin Tomulescu 
555869dfc87SAndi Kleen 	cs = container_of(attr, struct class_attribute_string, attr);
556948b3edbSJoe Perches 	return sysfs_emit(buf, "%s\n", cs->str);
557869dfc87SAndi Kleen }
558869dfc87SAndi Kleen 
559869dfc87SAndi Kleen EXPORT_SYMBOL_GPL(show_class_attr_string);
560869dfc87SAndi Kleen 
56146227094SJean Delvare struct class_compat {
56246227094SJean Delvare 	struct kobject *kobj;
56346227094SJean Delvare };
56446227094SJean Delvare 
56546227094SJean Delvare /**
56646227094SJean Delvare  * class_compat_register - register a compatibility class
56746227094SJean Delvare  * @name: the name of the class
56846227094SJean Delvare  *
56946227094SJean Delvare  * Compatibility class are meant as a temporary user-space compatibility
57046227094SJean Delvare  * workaround when converting a family of class devices to a bus devices.
57146227094SJean Delvare  */
class_compat_register(const char * name)57246227094SJean Delvare struct class_compat *class_compat_register(const char *name)
57346227094SJean Delvare {
57446227094SJean Delvare 	struct class_compat *cls;
57546227094SJean Delvare 
57646227094SJean Delvare 	cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
57746227094SJean Delvare 	if (!cls)
57846227094SJean Delvare 		return NULL;
57946227094SJean Delvare 	cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
58046227094SJean Delvare 	if (!cls->kobj) {
58146227094SJean Delvare 		kfree(cls);
58246227094SJean Delvare 		return NULL;
58346227094SJean Delvare 	}
58446227094SJean Delvare 	return cls;
58546227094SJean Delvare }
58646227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_register);
58746227094SJean Delvare 
58846227094SJean Delvare /**
58946227094SJean Delvare  * class_compat_unregister - unregister a compatibility class
59046227094SJean Delvare  * @cls: the class to unregister
59146227094SJean Delvare  */
class_compat_unregister(struct class_compat * cls)59246227094SJean Delvare void class_compat_unregister(struct class_compat *cls)
59346227094SJean Delvare {
59446227094SJean Delvare 	kobject_put(cls->kobj);
59546227094SJean Delvare 	kfree(cls);
59646227094SJean Delvare }
59746227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_unregister);
59846227094SJean Delvare 
59946227094SJean Delvare /**
60046227094SJean Delvare  * class_compat_create_link - create a compatibility class device link to
60146227094SJean Delvare  *			      a bus device
60246227094SJean Delvare  * @cls: the compatibility class
60346227094SJean Delvare  * @dev: the target bus device
60446227094SJean Delvare  */
class_compat_create_link(struct class_compat * cls,struct device * dev)605*827ed8b1SHeiner Kallweit int class_compat_create_link(struct class_compat *cls, struct device *dev)
60646227094SJean Delvare {
607*827ed8b1SHeiner Kallweit 	return sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
60846227094SJean Delvare }
60946227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_create_link);
61046227094SJean Delvare 
61146227094SJean Delvare /**
61246227094SJean Delvare  * class_compat_remove_link - remove a compatibility class device link to
61346227094SJean Delvare  *			      a bus device
61446227094SJean Delvare  * @cls: the compatibility class
61546227094SJean Delvare  * @dev: the target bus device
61646227094SJean Delvare  */
class_compat_remove_link(struct class_compat * cls,struct device * dev)617*827ed8b1SHeiner Kallweit void class_compat_remove_link(struct class_compat *cls, struct device *dev)
61846227094SJean Delvare {
61946227094SJean Delvare 	sysfs_remove_link(cls->kobj, dev_name(dev));
62046227094SJean Delvare }
62146227094SJean Delvare EXPORT_SYMBOL_GPL(class_compat_remove_link);
62246227094SJean Delvare 
6236f14c022SGreg Kroah-Hartman /**
6246f14c022SGreg Kroah-Hartman  * class_is_registered - determine if at this moment in time, a class is
6256f14c022SGreg Kroah-Hartman  *			 registered in the driver core or not.
6266f14c022SGreg Kroah-Hartman  * @class: the class to check
6276f14c022SGreg Kroah-Hartman  *
6286f14c022SGreg Kroah-Hartman  * Returns a boolean to state if the class is registered in the driver core
6296f14c022SGreg Kroah-Hartman  * or not.  Note that the value could switch right after this call is made,
6306f14c022SGreg Kroah-Hartman  * so only use this in places where you "know" it is safe to do so (usually
6316f14c022SGreg Kroah-Hartman  * to determine if the specific class has been registered yet or not).
6326f14c022SGreg Kroah-Hartman  *
6336f14c022SGreg Kroah-Hartman  * Be careful in using this.
6346f14c022SGreg Kroah-Hartman  */
class_is_registered(const struct class * class)6356f14c022SGreg Kroah-Hartman bool class_is_registered(const struct class *class)
6366f14c022SGreg Kroah-Hartman {
6376f14c022SGreg Kroah-Hartman 	struct subsys_private *sp = class_to_subsys(class);
6386f14c022SGreg Kroah-Hartman 	bool is_initialized = false;
6396f14c022SGreg Kroah-Hartman 
6406f14c022SGreg Kroah-Hartman 	if (sp) {
6416f14c022SGreg Kroah-Hartman 		is_initialized = true;
6426f14c022SGreg Kroah-Hartman 		subsys_put(sp);
6436f14c022SGreg Kroah-Hartman 	}
6446f14c022SGreg Kroah-Hartman 	return is_initialized;
6456f14c022SGreg Kroah-Hartman }
6466f14c022SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(class_is_registered);
6476f14c022SGreg Kroah-Hartman 
classes_init(void)6481da177e4SLinus Torvalds int __init classes_init(void)
6491da177e4SLinus Torvalds {
650443dbf90SGreg Kroah-Hartman 	class_kset = kset_create_and_add("class", NULL, NULL);
651443dbf90SGreg Kroah-Hartman 	if (!class_kset)
652443dbf90SGreg Kroah-Hartman 		return -ENOMEM;
6531da177e4SLinus Torvalds 	return 0;
6541da177e4SLinus Torvalds }
655