drivers/base/class.c
Source file repositories/reference/linux-study-clean/drivers/base/class.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/base/class.c- Extension
.c- Size
- 16833 bytes
- Lines
- 655
- Domain
- Driver Families
- Bucket
- drivers/base
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/device/class.hlinux/device.hlinux/module.hlinux/init.hlinux/string.hlinux/kdev_t.hlinux/err.hlinux/slab.hlinux/blkdev.hlinux/mutex.hbase.h
Detected Declarations
struct class_compatfunction list_for_each_entryfunction class_attr_showfunction class_attr_storefunction class_releasefunction class_create_file_nsfunction class_remove_file_nsfunction klist_class_dev_getfunction klist_class_dev_putfunction class_registerfunction class_unregisterfunction class_create_releasefunction device_createfunction class_createfunction class_dev_iter_initfunction class_dev_iter_exitfunction class_for_each_devicefunction class_for_each_devfunction class_interface_registerfunction class_interface_unregisterfunction show_class_attr_stringfunction class_compat_unregisterfunction class_compat_create_linkfunction class_compat_remove_linkfunction class_is_registeredfunction classes_initexport class_create_file_nsexport class_remove_file_nsexport class_registerexport class_unregisterexport class_createexport class_destroyexport class_dev_iter_initexport class_dev_iter_nextexport class_dev_iter_exitexport class_for_each_deviceexport class_find_deviceexport class_interface_registerexport class_interface_unregisterexport show_class_attr_stringexport class_compat_registerexport class_compat_unregisterexport class_compat_create_linkexport class_compat_remove_linkexport class_is_registered
Annotated Snippet
* @class: pointer to the struct bus_type to look up
*
* The driver core internals need to work on the subsys_private structure, not
* the external struct class pointer. This function walks the list of
* registered classes in the system and finds the matching one and returns the
* internal struct subsys_private that relates to that class.
*
* Note, the reference count of the return value is INCREMENTED if it is not
* NULL. A call to subsys_put() must be done when finished with the pointer in
* order for it to be properly freed.
*/
struct subsys_private *class_to_subsys(const struct class *class)
{
struct subsys_private *sp = NULL;
struct kobject *kobj;
if (!class || !class_kset)
return NULL;
spin_lock(&class_kset->list_lock);
if (list_empty(&class_kset->list))
goto done;
list_for_each_entry(kobj, &class_kset->list, entry) {
struct kset *kset = container_of(kobj, struct kset, kobj);
sp = container_of_const(kset, struct subsys_private, subsys);
if (sp->class == class)
goto done;
}
sp = NULL;
done:
sp = subsys_get(sp);
spin_unlock(&class_kset->list_lock);
return sp;
}
static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct class_attribute *class_attr = to_class_attr(attr);
struct subsys_private *cp = to_subsys_private(kobj);
ssize_t ret = -EIO;
if (class_attr->show)
ret = class_attr->show(cp->class, class_attr, buf);
return ret;
}
static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t count)
{
struct class_attribute *class_attr = to_class_attr(attr);
struct subsys_private *cp = to_subsys_private(kobj);
ssize_t ret = -EIO;
if (class_attr->store)
ret = class_attr->store(cp->class, class_attr, buf, count);
return ret;
}
static void class_release(struct kobject *kobj)
{
struct subsys_private *cp = to_subsys_private(kobj);
const struct class *class = cp->class;
pr_debug("class '%s': release.\n", class->name);
if (class->class_release)
class->class_release(class);
else
pr_debug("class '%s' does not have a release() function, "
"be careful\n", class->name);
lockdep_unregister_key(&cp->lock_key);
kfree(cp);
}
static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj)
{
const struct subsys_private *cp = to_subsys_private(kobj);
const struct class *class = cp->class;
return class->ns_type;
}
static const struct sysfs_ops class_sysfs_ops = {
.show = class_attr_show,
.store = class_attr_store,
Annotation
- Immediate include surface: `linux/device/class.h`, `linux/device.h`, `linux/module.h`, `linux/init.h`, `linux/string.h`, `linux/kdev_t.h`, `linux/err.h`, `linux/slab.h`.
- Detected declarations: `struct class_compat`, `function list_for_each_entry`, `function class_attr_show`, `function class_attr_store`, `function class_release`, `function class_create_file_ns`, `function class_remove_file_ns`, `function klist_class_dev_get`, `function klist_class_dev_put`, `function class_register`.
- Atlas domain: Driver Families / drivers/base.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.