xref: /linux/drivers/base/class.c (revision a69ea7a76d52353b17d7bedf43818c2578517e9e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * class.c - basic device class management
4  *
5  * Copyright (c) 2002-3 Patrick Mochel
6  * Copyright (c) 2002-3 Open Source Development Labs
7  * Copyright (c) 2003-2004 Greg Kroah-Hartman
8  * Copyright (c) 2003-2004 IBM Corp.
9  */
10 
11 #include <linux/device/class.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include <linux/kdev_t.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/blkdev.h>
20 #include <linux/mutex.h>
21 #include "base.h"
22 
23 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
24 
25 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
26 			       char *buf)
27 {
28 	struct class_attribute *class_attr = to_class_attr(attr);
29 	struct subsys_private *cp = to_subsys_private(kobj);
30 	ssize_t ret = -EIO;
31 
32 	if (class_attr->show)
33 		ret = class_attr->show(cp->class, class_attr, buf);
34 	return ret;
35 }
36 
37 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
38 				const char *buf, size_t count)
39 {
40 	struct class_attribute *class_attr = to_class_attr(attr);
41 	struct subsys_private *cp = to_subsys_private(kobj);
42 	ssize_t ret = -EIO;
43 
44 	if (class_attr->store)
45 		ret = class_attr->store(cp->class, class_attr, buf, count);
46 	return ret;
47 }
48 
49 static void class_release(struct kobject *kobj)
50 {
51 	struct subsys_private *cp = to_subsys_private(kobj);
52 	struct class *class = cp->class;
53 
54 	pr_debug("class '%s': release.\n", class->name);
55 
56 	class->p = NULL;
57 
58 	if (class->class_release)
59 		class->class_release(class);
60 	else
61 		pr_debug("class '%s' does not have a release() function, "
62 			 "be careful\n", class->name);
63 
64 	kfree(cp);
65 }
66 
67 static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj)
68 {
69 	const struct subsys_private *cp = to_subsys_private(kobj);
70 	struct class *class = cp->class;
71 
72 	return class->ns_type;
73 }
74 
75 static const struct sysfs_ops class_sysfs_ops = {
76 	.show	   = class_attr_show,
77 	.store	   = class_attr_store,
78 };
79 
80 static struct kobj_type class_ktype = {
81 	.sysfs_ops	= &class_sysfs_ops,
82 	.release	= class_release,
83 	.child_ns_type	= class_child_ns_type,
84 };
85 
86 /* Hotplug events for classes go to the class subsys */
87 static struct kset *class_kset;
88 
89 
90 int class_create_file_ns(struct class *cls, const struct class_attribute *attr,
91 			 const void *ns)
92 {
93 	int error;
94 
95 	if (cls)
96 		error = sysfs_create_file_ns(&cls->p->subsys.kobj,
97 					     &attr->attr, ns);
98 	else
99 		error = -EINVAL;
100 	return error;
101 }
102 
103 void class_remove_file_ns(struct class *cls, const struct class_attribute *attr,
104 			  const void *ns)
105 {
106 	if (cls)
107 		sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns);
108 }
109 
110 static struct class *class_get(struct class *cls)
111 {
112 	if (cls)
113 		kset_get(&cls->p->subsys);
114 	return cls;
115 }
116 
117 static void class_put(struct class *cls)
118 {
119 	if (cls)
120 		kset_put(&cls->p->subsys);
121 }
122 
123 static struct device *klist_class_to_dev(struct klist_node *n)
124 {
125 	struct device_private *p = to_device_private_class(n);
126 	return p->device;
127 }
128 
129 static void klist_class_dev_get(struct klist_node *n)
130 {
131 	struct device *dev = klist_class_to_dev(n);
132 
133 	get_device(dev);
134 }
135 
136 static void klist_class_dev_put(struct klist_node *n)
137 {
138 	struct device *dev = klist_class_to_dev(n);
139 
140 	put_device(dev);
141 }
142 
143 static int class_add_groups(struct class *cls,
144 			    const struct attribute_group **groups)
145 {
146 	return sysfs_create_groups(&cls->p->subsys.kobj, groups);
147 }
148 
149 static void class_remove_groups(struct class *cls,
150 				const struct attribute_group **groups)
151 {
152 	return sysfs_remove_groups(&cls->p->subsys.kobj, groups);
153 }
154 
155 int __class_register(struct class *cls, struct lock_class_key *key)
156 {
157 	struct subsys_private *cp;
158 	int error;
159 
160 	pr_debug("device class '%s': registering\n", cls->name);
161 
162 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
163 	if (!cp)
164 		return -ENOMEM;
165 	klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put);
166 	INIT_LIST_HEAD(&cp->interfaces);
167 	kset_init(&cp->glue_dirs);
168 	__mutex_init(&cp->mutex, "subsys mutex", key);
169 	error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name);
170 	if (error) {
171 		kfree(cp);
172 		return error;
173 	}
174 
175 	/* set the default /sys/dev directory for devices of this class */
176 	if (!cls->dev_kobj)
177 		cls->dev_kobj = sysfs_dev_char_kobj;
178 
179 #if defined(CONFIG_BLOCK)
180 	/* let the block class directory show up in the root of sysfs */
181 	if (!sysfs_deprecated || cls != &block_class)
182 		cp->subsys.kobj.kset = class_kset;
183 #else
184 	cp->subsys.kobj.kset = class_kset;
185 #endif
186 	cp->subsys.kobj.ktype = &class_ktype;
187 	cp->class = cls;
188 	cls->p = cp;
189 
190 	error = kset_register(&cp->subsys);
191 	if (error)
192 		goto err_out;
193 
194 	error = class_add_groups(class_get(cls), cls->class_groups);
195 	class_put(cls);
196 	if (error) {
197 		kobject_del(&cp->subsys.kobj);
198 		kfree_const(cp->subsys.kobj.name);
199 		goto err_out;
200 	}
201 	return 0;
202 
203 err_out:
204 	kfree(cp);
205 	cls->p = NULL;
206 	return error;
207 }
208 EXPORT_SYMBOL_GPL(__class_register);
209 
210 void class_unregister(struct class *cls)
211 {
212 	pr_debug("device class '%s': unregistering\n", cls->name);
213 	class_remove_groups(cls, cls->class_groups);
214 	kset_unregister(&cls->p->subsys);
215 }
216 
217 static void class_create_release(struct class *cls)
218 {
219 	pr_debug("%s called for %s\n", __func__, cls->name);
220 	kfree(cls);
221 }
222 
223 /**
224  * __class_create - create a struct class structure
225  * @owner: pointer to the module that is to "own" this struct class
226  * @name: pointer to a string for the name of this class.
227  * @key: the lock_class_key for this class; used by mutex lock debugging
228  *
229  * This is used to create a struct class pointer that can then be used
230  * in calls to device_create().
231  *
232  * Returns &struct class pointer on success, or ERR_PTR() on error.
233  *
234  * Note, the pointer created here is to be destroyed when finished by
235  * making a call to class_destroy().
236  */
237 struct class *__class_create(struct module *owner, const char *name,
238 			     struct lock_class_key *key)
239 {
240 	struct class *cls;
241 	int retval;
242 
243 	cls = kzalloc(sizeof(*cls), GFP_KERNEL);
244 	if (!cls) {
245 		retval = -ENOMEM;
246 		goto error;
247 	}
248 
249 	cls->name = name;
250 	cls->owner = owner;
251 	cls->class_release = class_create_release;
252 
253 	retval = __class_register(cls, key);
254 	if (retval)
255 		goto error;
256 
257 	return cls;
258 
259 error:
260 	kfree(cls);
261 	return ERR_PTR(retval);
262 }
263 EXPORT_SYMBOL_GPL(__class_create);
264 
265 /**
266  * class_destroy - destroys a struct class structure
267  * @cls: pointer to the struct class that is to be destroyed
268  *
269  * Note, the pointer to be destroyed must have been created with a call
270  * to class_create().
271  */
272 void class_destroy(struct class *cls)
273 {
274 	if (IS_ERR_OR_NULL(cls))
275 		return;
276 
277 	class_unregister(cls);
278 }
279 
280 /**
281  * class_dev_iter_init - initialize class device iterator
282  * @iter: class iterator to initialize
283  * @class: the class we wanna iterate over
284  * @start: the device to start iterating from, if any
285  * @type: device_type of the devices to iterate over, NULL for all
286  *
287  * Initialize class iterator @iter such that it iterates over devices
288  * of @class.  If @start is set, the list iteration will start there,
289  * otherwise if it is NULL, the iteration starts at the beginning of
290  * the list.
291  */
292 void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
293 			 struct device *start, const struct device_type *type)
294 {
295 	struct klist_node *start_knode = NULL;
296 
297 	if (start)
298 		start_knode = &start->p->knode_class;
299 	klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode);
300 	iter->type = type;
301 }
302 EXPORT_SYMBOL_GPL(class_dev_iter_init);
303 
304 /**
305  * class_dev_iter_next - iterate to the next device
306  * @iter: class iterator to proceed
307  *
308  * Proceed @iter to the next device and return it.  Returns NULL if
309  * iteration is complete.
310  *
311  * The returned device is referenced and won't be released till
312  * iterator is proceed to the next device or exited.  The caller is
313  * free to do whatever it wants to do with the device including
314  * calling back into class code.
315  */
316 struct device *class_dev_iter_next(struct class_dev_iter *iter)
317 {
318 	struct klist_node *knode;
319 	struct device *dev;
320 
321 	while (1) {
322 		knode = klist_next(&iter->ki);
323 		if (!knode)
324 			return NULL;
325 		dev = klist_class_to_dev(knode);
326 		if (!iter->type || iter->type == dev->type)
327 			return dev;
328 	}
329 }
330 EXPORT_SYMBOL_GPL(class_dev_iter_next);
331 
332 /**
333  * class_dev_iter_exit - finish iteration
334  * @iter: class iterator to finish
335  *
336  * Finish an iteration.  Always call this function after iteration is
337  * complete whether the iteration ran till the end or not.
338  */
339 void class_dev_iter_exit(struct class_dev_iter *iter)
340 {
341 	klist_iter_exit(&iter->ki);
342 }
343 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
344 
345 /**
346  * class_for_each_device - device iterator
347  * @class: the class we're iterating
348  * @start: the device to start with in the list, if any.
349  * @data: data for the callback
350  * @fn: function to be called for each device
351  *
352  * Iterate over @class's list of devices, and call @fn for each,
353  * passing it @data.  If @start is set, the list iteration will start
354  * there, otherwise if it is NULL, the iteration starts at the
355  * beginning of the list.
356  *
357  * We check the return of @fn each time. If it returns anything
358  * other than 0, we break out and return that value.
359  *
360  * @fn is allowed to do anything including calling back into class
361  * code.  There's no locking restriction.
362  */
363 int class_for_each_device(struct class *class, struct device *start,
364 			  void *data, int (*fn)(struct device *, void *))
365 {
366 	struct class_dev_iter iter;
367 	struct device *dev;
368 	int error = 0;
369 
370 	if (!class)
371 		return -EINVAL;
372 	if (!class->p) {
373 		WARN(1, "%s called for class '%s' before it was initialized",
374 		     __func__, class->name);
375 		return -EINVAL;
376 	}
377 
378 	class_dev_iter_init(&iter, class, start, NULL);
379 	while ((dev = class_dev_iter_next(&iter))) {
380 		error = fn(dev, data);
381 		if (error)
382 			break;
383 	}
384 	class_dev_iter_exit(&iter);
385 
386 	return error;
387 }
388 EXPORT_SYMBOL_GPL(class_for_each_device);
389 
390 /**
391  * class_find_device - device iterator for locating a particular device
392  * @class: the class we're iterating
393  * @start: Device to begin with
394  * @data: data for the match function
395  * @match: function to check device
396  *
397  * This is similar to the class_for_each_dev() function above, but it
398  * returns a reference to a device that is 'found' for later use, as
399  * determined by the @match callback.
400  *
401  * The callback should return 0 if the device doesn't match and non-zero
402  * if it does.  If the callback returns non-zero, this function will
403  * return to the caller and not iterate over any more devices.
404  *
405  * Note, you will need to drop the reference with put_device() after use.
406  *
407  * @match is allowed to do anything including calling back into class
408  * code.  There's no locking restriction.
409  */
410 struct device *class_find_device(struct class *class, struct device *start,
411 				 const void *data,
412 				 int (*match)(struct device *, const void *))
413 {
414 	struct class_dev_iter iter;
415 	struct device *dev;
416 
417 	if (!class)
418 		return NULL;
419 	if (!class->p) {
420 		WARN(1, "%s called for class '%s' before it was initialized",
421 		     __func__, class->name);
422 		return NULL;
423 	}
424 
425 	class_dev_iter_init(&iter, class, start, NULL);
426 	while ((dev = class_dev_iter_next(&iter))) {
427 		if (match(dev, data)) {
428 			get_device(dev);
429 			break;
430 		}
431 	}
432 	class_dev_iter_exit(&iter);
433 
434 	return dev;
435 }
436 EXPORT_SYMBOL_GPL(class_find_device);
437 
438 int class_interface_register(struct class_interface *class_intf)
439 {
440 	struct class *parent;
441 	struct class_dev_iter iter;
442 	struct device *dev;
443 
444 	if (!class_intf || !class_intf->class)
445 		return -ENODEV;
446 
447 	parent = class_get(class_intf->class);
448 	if (!parent)
449 		return -EINVAL;
450 
451 	mutex_lock(&parent->p->mutex);
452 	list_add_tail(&class_intf->node, &parent->p->interfaces);
453 	if (class_intf->add_dev) {
454 		class_dev_iter_init(&iter, parent, NULL, NULL);
455 		while ((dev = class_dev_iter_next(&iter)))
456 			class_intf->add_dev(dev, class_intf);
457 		class_dev_iter_exit(&iter);
458 	}
459 	mutex_unlock(&parent->p->mutex);
460 
461 	return 0;
462 }
463 
464 void class_interface_unregister(struct class_interface *class_intf)
465 {
466 	struct class *parent = class_intf->class;
467 	struct class_dev_iter iter;
468 	struct device *dev;
469 
470 	if (!parent)
471 		return;
472 
473 	mutex_lock(&parent->p->mutex);
474 	list_del_init(&class_intf->node);
475 	if (class_intf->remove_dev) {
476 		class_dev_iter_init(&iter, parent, NULL, NULL);
477 		while ((dev = class_dev_iter_next(&iter)))
478 			class_intf->remove_dev(dev, class_intf);
479 		class_dev_iter_exit(&iter);
480 	}
481 	mutex_unlock(&parent->p->mutex);
482 
483 	class_put(parent);
484 }
485 
486 ssize_t show_class_attr_string(struct class *class,
487 			       struct class_attribute *attr, char *buf)
488 {
489 	struct class_attribute_string *cs;
490 
491 	cs = container_of(attr, struct class_attribute_string, attr);
492 	return sysfs_emit(buf, "%s\n", cs->str);
493 }
494 
495 EXPORT_SYMBOL_GPL(show_class_attr_string);
496 
497 struct class_compat {
498 	struct kobject *kobj;
499 };
500 
501 /**
502  * class_compat_register - register a compatibility class
503  * @name: the name of the class
504  *
505  * Compatibility class are meant as a temporary user-space compatibility
506  * workaround when converting a family of class devices to a bus devices.
507  */
508 struct class_compat *class_compat_register(const char *name)
509 {
510 	struct class_compat *cls;
511 
512 	cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL);
513 	if (!cls)
514 		return NULL;
515 	cls->kobj = kobject_create_and_add(name, &class_kset->kobj);
516 	if (!cls->kobj) {
517 		kfree(cls);
518 		return NULL;
519 	}
520 	return cls;
521 }
522 EXPORT_SYMBOL_GPL(class_compat_register);
523 
524 /**
525  * class_compat_unregister - unregister a compatibility class
526  * @cls: the class to unregister
527  */
528 void class_compat_unregister(struct class_compat *cls)
529 {
530 	kobject_put(cls->kobj);
531 	kfree(cls);
532 }
533 EXPORT_SYMBOL_GPL(class_compat_unregister);
534 
535 /**
536  * class_compat_create_link - create a compatibility class device link to
537  *			      a bus device
538  * @cls: the compatibility class
539  * @dev: the target bus device
540  * @device_link: an optional device to which a "device" link should be created
541  */
542 int class_compat_create_link(struct class_compat *cls, struct device *dev,
543 			     struct device *device_link)
544 {
545 	int error;
546 
547 	error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev));
548 	if (error)
549 		return error;
550 
551 	/*
552 	 * Optionally add a "device" link (typically to the parent), as a
553 	 * class device would have one and we want to provide as much
554 	 * backwards compatibility as possible.
555 	 */
556 	if (device_link) {
557 		error = sysfs_create_link(&dev->kobj, &device_link->kobj,
558 					  "device");
559 		if (error)
560 			sysfs_remove_link(cls->kobj, dev_name(dev));
561 	}
562 
563 	return error;
564 }
565 EXPORT_SYMBOL_GPL(class_compat_create_link);
566 
567 /**
568  * class_compat_remove_link - remove a compatibility class device link to
569  *			      a bus device
570  * @cls: the compatibility class
571  * @dev: the target bus device
572  * @device_link: an optional device to which a "device" link was previously
573  * 		 created
574  */
575 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
576 			      struct device *device_link)
577 {
578 	if (device_link)
579 		sysfs_remove_link(&dev->kobj, "device");
580 	sysfs_remove_link(cls->kobj, dev_name(dev));
581 }
582 EXPORT_SYMBOL_GPL(class_compat_remove_link);
583 
584 int __init classes_init(void)
585 {
586 	class_kset = kset_create_and_add("class", NULL, NULL);
587 	if (!class_kset)
588 		return -ENOMEM;
589 	return 0;
590 }
591 
592 EXPORT_SYMBOL_GPL(class_create_file_ns);
593 EXPORT_SYMBOL_GPL(class_remove_file_ns);
594 EXPORT_SYMBOL_GPL(class_unregister);
595 EXPORT_SYMBOL_GPL(class_destroy);
596 
597 EXPORT_SYMBOL_GPL(class_interface_register);
598 EXPORT_SYMBOL_GPL(class_interface_unregister);
599