Lines Matching +full:class +full:- +full:h
1 // SPDX-License-Identifier: GPL-2.0
3 * class.c - basic device class management
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.
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"
23 /* /sys/class */
29 * class_to_subsys - Turn a struct class into a struct subsys_private
31 * @class: pointer to the struct bus_type to look up
34 * the external struct class pointer. This function walks the list of
36 * internal struct subsys_private that relates to that class.
42 struct subsys_private *class_to_subsys(const struct class *class) in class_to_subsys() argument
47 if (!class || !class_kset) in class_to_subsys()
50 spin_lock(&class_kset->list_lock); in class_to_subsys()
52 if (list_empty(&class_kset->list)) in class_to_subsys()
55 list_for_each_entry(kobj, &class_kset->list, entry) { in class_to_subsys()
59 if (sp->class == class) in class_to_subsys()
65 spin_unlock(&class_kset->list_lock); in class_to_subsys()
74 ssize_t ret = -EIO; in class_attr_show()
76 if (class_attr->show) in class_attr_show()
77 ret = class_attr->show(cp->class, class_attr, buf); in class_attr_show()
86 ssize_t ret = -EIO; in class_attr_store()
88 if (class_attr->store) in class_attr_store()
89 ret = class_attr->store(cp->class, class_attr, buf, count); in class_attr_store()
96 const struct class *class = cp->class; in class_release() local
98 pr_debug("class '%s': release.\n", class->name); in class_release()
100 if (class->class_release) in class_release()
101 class->class_release(class); in class_release()
103 pr_debug("class '%s' does not have a release() function, " in class_release()
104 "be careful\n", class->name); in class_release()
106 lockdep_unregister_key(&cp->lock_key); in class_release()
113 const struct class *class = cp->class; in class_child_ns_type() local
115 return class->ns_type; in class_child_ns_type()
129 int class_create_file_ns(const struct class *cls, const struct class_attribute *attr, in class_create_file_ns()
136 return -EINVAL; in class_create_file_ns()
138 error = sysfs_create_file_ns(&sp->subsys.kobj, &attr->attr, ns); in class_create_file_ns()
145 void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr, in class_remove_file_ns()
153 sysfs_remove_file_ns(&sp->subsys.kobj, &attr->attr, ns); in class_remove_file_ns()
161 return p->device; in klist_class_to_dev()
178 int class_register(const struct class *cls) in class_register()
184 pr_debug("device class '%s': registering\n", cls->name); in class_register()
186 if (cls->ns_type && !cls->namespace) { in class_register()
187 pr_err("%s: class '%s' does not have namespace\n", in class_register()
188 __func__, cls->name); in class_register()
189 return -EINVAL; in class_register()
191 if (!cls->ns_type && cls->namespace) { in class_register()
192 pr_err("%s: class '%s' does not have ns_type\n", in class_register()
193 __func__, cls->name); in class_register()
194 return -EINVAL; in class_register()
199 return -ENOMEM; in class_register()
200 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put); in class_register()
201 INIT_LIST_HEAD(&cp->interfaces); in class_register()
202 kset_init(&cp->glue_dirs); in class_register()
203 key = &cp->lock_key; in class_register()
205 __mutex_init(&cp->mutex, "subsys mutex", key); in class_register()
206 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); in class_register()
210 cp->subsys.kobj.kset = class_kset; in class_register()
211 cp->subsys.kobj.ktype = &class_ktype; in class_register()
212 cp->class = cls; in class_register()
214 error = kset_register(&cp->subsys); in class_register()
218 error = sysfs_create_groups(&cp->subsys.kobj, cls->class_groups); in class_register()
220 kobject_del(&cp->subsys.kobj); in class_register()
221 kfree_const(cp->subsys.kobj.name); in class_register()
233 void class_unregister(const struct class *cls) in class_unregister()
240 pr_debug("device class '%s': unregistering\n", cls->name); in class_unregister()
242 sysfs_remove_groups(&sp->subsys.kobj, cls->class_groups); in class_unregister()
243 kset_unregister(&sp->subsys); in class_unregister()
248 static void class_create_release(const struct class *cls) in class_create_release()
250 pr_debug("%s called for %s\n", __func__, cls->name); in class_create_release()
255 * class_create - create a struct class structure
256 * @name: pointer to a string for the name of this class.
258 * This is used to create a struct class pointer that can then be used
261 * Returns &struct class pointer on success, or ERR_PTR() on error.
266 struct class *class_create(const char *name) in class_create()
268 struct class *cls; in class_create()
273 retval = -ENOMEM; in class_create()
277 cls->name = name; in class_create()
278 cls->class_release = class_create_release; in class_create()
293 * class_destroy - destroys a struct class structure
294 * @cls: pointer to the struct class that is to be destroyed
299 void class_destroy(const struct class *cls) in class_destroy()
309 * class_dev_iter_init - initialize class device iterator
310 * @iter: class iterator to initialize
311 * @class: the class we wanna iterate over
315 * Initialize class iterator @iter such that it iterates over devices
316 * of @class. If @start is set, the list iteration will start there,
320 void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, in class_dev_iter_init() argument
323 struct subsys_private *sp = class_to_subsys(class); in class_dev_iter_init()
328 pr_crit("%s: class %p was not registered yet\n", in class_dev_iter_init()
329 __func__, class); in class_dev_iter_init()
334 start_knode = &start->p->knode_class; in class_dev_iter_init()
335 klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); in class_dev_iter_init()
336 iter->type = type; in class_dev_iter_init()
337 iter->sp = sp; in class_dev_iter_init()
342 * class_dev_iter_next - iterate to the next device
343 * @iter: class iterator to proceed
351 * calling back into class code.
358 if (!iter->sp) in class_dev_iter_next()
362 knode = klist_next(&iter->ki); in class_dev_iter_next()
366 if (!iter->type || iter->type == dev->type) in class_dev_iter_next()
373 * class_dev_iter_exit - finish iteration
374 * @iter: class iterator to finish
381 klist_iter_exit(&iter->ki); in class_dev_iter_exit()
382 subsys_put(iter->sp); in class_dev_iter_exit()
387 * class_for_each_device - device iterator
388 * @class: the class we're iterating
393 * Iterate over @class's list of devices, and call @fn for each,
401 * @fn is allowed to do anything including calling back into class
404 int class_for_each_device(const struct class *class, const struct device *start, in class_for_each_device() argument
407 struct subsys_private *sp = class_to_subsys(class); in class_for_each_device()
412 if (!class) in class_for_each_device()
413 return -EINVAL; in class_for_each_device()
415 WARN(1, "%s called for class '%s' before it was registered", in class_for_each_device()
416 __func__, class->name); in class_for_each_device()
417 return -EINVAL; in class_for_each_device()
420 class_dev_iter_init(&iter, class, start, NULL); in class_for_each_device()
434 * class_find_device - device iterator for locating a particular device
435 * @class: the class we're iterating
444 * The callback should return 0 if the device doesn't match and non-zero
445 * if it does. If the callback returns non-zero, this function will
450 * @match is allowed to do anything including calling back into class
453 struct device *class_find_device(const struct class *class, const struct device *start, in class_find_device() argument
456 struct subsys_private *sp = class_to_subsys(class); in class_find_device()
460 if (!class) in class_find_device()
463 WARN(1, "%s called for class '%s' before it was registered", in class_find_device()
464 __func__, class->name); in class_find_device()
468 class_dev_iter_init(&iter, class, start, NULL); in class_find_device()
485 const struct class *parent; in class_interface_register()
489 if (!class_intf || !class_intf->class) in class_interface_register()
490 return -ENODEV; in class_interface_register()
492 parent = class_intf->class; in class_interface_register()
495 return -EINVAL; in class_interface_register()
502 mutex_lock(&sp->mutex); in class_interface_register()
503 list_add_tail(&class_intf->node, &sp->interfaces); in class_interface_register()
504 if (class_intf->add_dev) { in class_interface_register()
507 class_intf->add_dev(dev); in class_interface_register()
510 mutex_unlock(&sp->mutex); in class_interface_register()
519 const struct class *parent = class_intf->class; in class_interface_unregister()
530 mutex_lock(&sp->mutex); in class_interface_unregister()
531 list_del_init(&class_intf->node); in class_interface_unregister()
532 if (class_intf->remove_dev) { in class_interface_unregister()
535 class_intf->remove_dev(dev); in class_interface_unregister()
538 mutex_unlock(&sp->mutex); in class_interface_unregister()
550 ssize_t show_class_attr_string(const struct class *class, in show_class_attr_string() argument
556 return sysfs_emit(buf, "%s\n", cs->str); in show_class_attr_string()
566 * class_compat_register - register a compatibility class
567 * @name: the name of the class
569 * Compatibility class are meant as a temporary user-space compatibility
570 * workaround when converting a family of class devices to a bus devices.
579 cls->kobj = kobject_create_and_add(name, &class_kset->kobj); in class_compat_register()
580 if (!cls->kobj) { in class_compat_register()
589 * class_compat_unregister - unregister a compatibility class
590 * @cls: the class to unregister
594 kobject_put(cls->kobj); in class_compat_unregister()
600 * class_compat_create_link - create a compatibility class device link to
602 * @cls: the compatibility class
607 return sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev)); in class_compat_create_link()
612 * class_compat_remove_link - remove a compatibility class device link to
614 * @cls: the compatibility class
619 sysfs_remove_link(cls->kobj, dev_name(dev)); in class_compat_remove_link()
624 * class_is_registered - determine if at this moment in time, a class is
626 * @class: the class to check
628 * Returns a boolean to state if the class is registered in the driver core
631 * to determine if the specific class has been registered yet or not).
635 bool class_is_registered(const struct class *class) in class_is_registered() argument
637 struct subsys_private *sp = class_to_subsys(class); in class_is_registered()
650 class_kset = kset_create_and_add("class", NULL, NULL); in classes_init()
652 return -ENOMEM; in classes_init()