drivers/base/driver.c
Source file repositories/reference/linux-study-clean/drivers/base/driver.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/base/driver.c- Extension
.c- Size
- 5189 bytes
- Lines
- 205
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/device/driver.hlinux/device.hlinux/module.hlinux/errno.hlinux/slab.hlinux/string.hlinux/sysfs.hbase.h
Detected Declarations
function Copyrightfunction driver_for_each_devicefunction driver_for_each_devicefunction driver_create_filefunction driver_remove_filefunction driver_add_groupsfunction driver_remove_groupsfunction bus_add_driverfunction driver_unregisterexport driver_for_each_deviceexport driver_find_deviceexport driver_create_fileexport driver_remove_fileexport driver_registerexport driver_unregister
Annotated Snippet
int driver_for_each_device(struct device_driver *drv, struct device *start,
void *data, device_iter_t fn)
{
struct klist_iter i;
struct device *dev;
int error = 0;
if (!drv)
return -EINVAL;
klist_iter_init_node(&drv->p->klist_devices, &i,
start ? &start->p->knode_driver : NULL);
while (!error && (dev = next_device(&i)))
error = fn(dev, data);
klist_iter_exit(&i);
return error;
}
EXPORT_SYMBOL_GPL(driver_for_each_device);
/**
* driver_find_device - device iterator for locating a particular device.
* @drv: The device's driver
* @start: Device to begin with
* @data: Data to pass to match function
* @match: Callback function to check device
*
* This is similar to the driver_for_each_device() function above, but
* it returns a reference to a device that is 'found' for later use, as
* determined by the @match callback.
*
* The callback should return 0 if the device doesn't match and non-zero
* if it does. If the callback returns non-zero, this function will
* return to the caller and not iterate over any more devices.
*/
struct device *driver_find_device(const struct device_driver *drv,
struct device *start, const void *data,
device_match_t match)
{
struct klist_iter i;
struct device *dev;
if (!drv || !drv->p)
return NULL;
klist_iter_init_node(&drv->p->klist_devices, &i,
(start ? &start->p->knode_driver : NULL));
while ((dev = next_device(&i))) {
if (match(dev, data)) {
get_device(dev);
break;
}
}
klist_iter_exit(&i);
return dev;
}
EXPORT_SYMBOL_GPL(driver_find_device);
/**
* driver_create_file - create sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
*/
int driver_create_file(const struct device_driver *drv,
const struct driver_attribute *attr)
{
int error;
if (drv)
error = sysfs_create_file(&drv->p->kobj, &attr->attr);
else
error = -EINVAL;
return error;
}
EXPORT_SYMBOL_GPL(driver_create_file);
/**
* driver_remove_file - remove sysfs file for driver.
* @drv: driver.
* @attr: driver attribute descriptor.
*/
void driver_remove_file(const struct device_driver *drv,
const struct driver_attribute *attr)
{
if (drv)
sysfs_remove_file(&drv->p->kobj, &attr->attr);
}
EXPORT_SYMBOL_GPL(driver_remove_file);
int driver_add_groups(const struct device_driver *drv,
const struct attribute_group *const *groups)
Annotation
- Immediate include surface: `linux/device/driver.h`, `linux/device.h`, `linux/module.h`, `linux/errno.h`, `linux/slab.h`, `linux/string.h`, `linux/sysfs.h`, `base.h`.
- Detected declarations: `function Copyright`, `function driver_for_each_device`, `function driver_for_each_device`, `function driver_create_file`, `function driver_remove_file`, `function driver_add_groups`, `function driver_remove_groups`, `function bus_add_driver`, `function driver_unregister`, `export driver_for_each_device`.
- Atlas domain: Driver Families / drivers/base.
- Implementation status: pattern implementation candidate.
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.