drivers/counter/counter-core.c
Source file repositories/reference/linux-study-clean/drivers/counter/counter-core.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/counter/counter-core.c- Extension
.c- Size
- 6553 bytes
- Lines
- 285
- Domain
- Driver Families
- Bucket
- drivers/counter
- 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/cdev.hlinux/counter.hlinux/device.hlinux/device/bus.hlinux/export.hlinux/fs.hlinux/gfp.hlinux/idr.hlinux/init.hlinux/kdev_t.hlinux/module.hlinux/mutex.hlinux/slab.hlinux/types.hlinux/wait.hcounter-chrdev.hcounter-sysfs.h
Detected Declarations
struct counter_device_allochelperfunction counter_device_releasefunction counter_putfunction counter_addfunction counter_unregisterfunction devm_counter_releasefunction devm_counter_putfunction counter_addfunction counter_addfunction counter_initfunction counter_exitmodule init counter_init
Annotated Snippet
static const struct bus_type counter_bus_type = {
.name = "counter",
.dev_name = "counter",
};
static dev_t counter_devt;
/**
* counter_priv - access counter device private data
* @counter: counter device
*
* Get the counter device private data
*/
void *counter_priv(const struct counter_device *const counter)
{
struct counter_device_allochelper *ch =
container_of(counter, struct counter_device_allochelper, counter);
return &ch->privdata;
}
EXPORT_SYMBOL_NS_GPL(counter_priv, "COUNTER");
/**
* counter_alloc - allocate a counter_device
* @sizeof_priv: size of the driver private data
*
* This is part one of counter registration. The structure is allocated
* dynamically to ensure the right lifetime for the embedded struct device.
*
* If this succeeds, call counter_put() to get rid of the counter_device again.
*/
struct counter_device *counter_alloc(size_t sizeof_priv)
{
struct counter_device_allochelper *ch;
struct counter_device *counter;
struct device *dev;
int err;
ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
if (!ch)
return NULL;
counter = &ch->counter;
dev = &counter->dev;
/* Acquire unique ID */
err = ida_alloc(&counter_ida, GFP_KERNEL);
if (err < 0)
goto err_ida_alloc;
dev->id = err;
mutex_init(&counter->ops_exist_lock);
dev->type = &counter_device_type;
dev->bus = &counter_bus_type;
dev->devt = MKDEV(MAJOR(counter_devt), dev->id);
err = counter_chrdev_add(counter);
if (err < 0)
goto err_chrdev_add;
device_initialize(dev);
err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
if (err)
goto err_dev_set_name;
return counter;
err_dev_set_name:
put_device(dev);
return NULL;
err_chrdev_add:
ida_free(&counter_ida, dev->id);
err_ida_alloc:
kfree(ch);
return NULL;
}
EXPORT_SYMBOL_NS_GPL(counter_alloc, "COUNTER");
void counter_put(struct counter_device *counter)
{
put_device(&counter->dev);
}
EXPORT_SYMBOL_NS_GPL(counter_put, "COUNTER");
/**
Annotation
- Immediate include surface: `linux/cdev.h`, `linux/counter.h`, `linux/device.h`, `linux/device/bus.h`, `linux/export.h`, `linux/fs.h`, `linux/gfp.h`, `linux/idr.h`.
- Detected declarations: `struct counter_device_allochelper`, `function counter_device_release`, `function counter_put`, `function counter_add`, `function counter_unregister`, `function devm_counter_release`, `function devm_counter_put`, `function counter_add`, `function counter_add`, `function counter_init`.
- Atlas domain: Driver Families / drivers/counter.
- 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.