drivers/char/hw_random/histb-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/histb-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/histb-rng.c- Extension
.c- Size
- 3857 bytes
- Lines
- 174
- Domain
- Driver Families
- Bucket
- drivers/char
- Inferred role
- Driver Families: implementation source
- Status
- source 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.
- 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/err.hlinux/hw_random.hlinux/io.hlinux/iopoll.hlinux/kernel.hlinux/mod_devicetable.hlinux/module.hlinux/platform_device.h
Detected Declarations
struct histb_rng_privfunction histb_rng_waitfunction histb_rng_initfunction histb_rng_readfunction histb_rng_get_depthfunction depth_showfunction depth_storefunction histb_rng_probe
Annotated Snippet
struct histb_rng_priv {
struct hwrng rng;
void __iomem *base;
};
/*
* Observed:
* depth = 1 -> ~1ms
* depth = 255 -> ~16ms
*/
static int histb_rng_wait(void __iomem *base)
{
u32 val;
return readl_relaxed_poll_timeout(base + RNG_STAT, val,
val & DATA_COUNT, 1000, 30 * 1000);
}
static void histb_rng_init(void __iomem *base, unsigned int depth)
{
u32 val;
val = readl_relaxed(base + RNG_CTRL);
val &= ~RNG_SOURCE;
val |= 2;
val &= ~POST_PROCESS_DEPTH;
val |= min(depth, 0xffu) << 8;
val |= POST_PROCESS_ENABLE;
val |= DROP_ENABLE;
writel_relaxed(val, base + RNG_CTRL);
}
static int histb_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
struct histb_rng_priv *priv = container_of(rng, typeof(*priv), rng);
void __iomem *base = priv->base;
for (int i = 0; i < max; i += sizeof(u32)) {
if (!(readl_relaxed(base + RNG_STAT) & DATA_COUNT)) {
if (!wait)
return i;
if (histb_rng_wait(base)) {
pr_err("failed to generate random number, generated %d\n",
i);
return i ? i : -ETIMEDOUT;
}
}
*(u32 *) (data + i) = readl_relaxed(base + RNG_NUMBER);
}
return max;
}
static unsigned int histb_rng_get_depth(void __iomem *base)
{
return (readl_relaxed(base + RNG_CTRL) & POST_PROCESS_DEPTH) >> 8;
}
static ssize_t
depth_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct histb_rng_priv *priv = dev_get_drvdata(dev);
void __iomem *base = priv->base;
return sprintf(buf, "%u\n", histb_rng_get_depth(base));
}
static ssize_t
depth_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct histb_rng_priv *priv = dev_get_drvdata(dev);
void __iomem *base = priv->base;
unsigned int depth;
if (kstrtouint(buf, 0, &depth))
return -ERANGE;
histb_rng_init(base, depth);
return count;
}
static DEVICE_ATTR_RW(depth);
static struct attribute *histb_rng_attrs[] = {
&dev_attr_depth.attr,
Annotation
- Immediate include surface: `linux/err.h`, `linux/hw_random.h`, `linux/io.h`, `linux/iopoll.h`, `linux/kernel.h`, `linux/mod_devicetable.h`, `linux/module.h`, `linux/platform_device.h`.
- Detected declarations: `struct histb_rng_priv`, `function histb_rng_wait`, `function histb_rng_init`, `function histb_rng_read`, `function histb_rng_get_depth`, `function depth_show`, `function depth_store`, `function histb_rng_probe`.
- Atlas domain: Driver Families / drivers/char.
- Implementation status: source 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.