drivers/char/hw_random/npcm-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/npcm-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/npcm-rng.c- Extension
.c- Size
- 4499 bytes
- Lines
- 187
- 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/kernel.hlinux/module.hlinux/io.hlinux/iopoll.hlinux/init.hlinux/random.hlinux/err.hlinux/of.hlinux/platform_device.hlinux/hw_random.hlinux/delay.hlinux/pm_runtime.h
Detected Declarations
struct npcm_rngfunction npcm_rng_initfunction npcm_rng_cleanupfunction npcm_rng_readfunction npcm_rng_probefunction npcm_rng_removefunction npcm_rng_runtime_suspendfunction npcm_rng_runtime_resume
Annotated Snippet
struct npcm_rng {
void __iomem *base;
struct hwrng rng;
struct device *dev;
u32 clkp;
};
static int npcm_rng_init(struct hwrng *rng)
{
struct npcm_rng *priv = to_npcm_rng(rng);
writel(priv->clkp | NPCM_RNG_ENABLE, priv->base + NPCM_RNGCS_REG);
return 0;
}
static void npcm_rng_cleanup(struct hwrng *rng)
{
struct npcm_rng *priv = to_npcm_rng(rng);
writel(priv->clkp, priv->base + NPCM_RNGCS_REG);
}
static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
struct npcm_rng *priv = to_npcm_rng(rng);
int retval = 0;
int ready;
pm_runtime_get_sync(priv->dev);
while (max) {
if (wait) {
if (readb_poll_timeout(priv->base + NPCM_RNGCS_REG,
ready,
ready & NPCM_RNG_DATA_VALID,
NPCM_RNG_POLL_USEC,
NPCM_RNG_TIMEOUT_USEC))
break;
} else {
if ((readb(priv->base + NPCM_RNGCS_REG) &
NPCM_RNG_DATA_VALID) == 0)
break;
}
*(u8 *)buf = readb(priv->base + NPCM_RNGD_REG);
retval++;
buf++;
max--;
}
pm_runtime_put_sync_autosuspend(priv->dev);
return retval || !wait ? retval : -EIO;
}
static int npcm_rng_probe(struct platform_device *pdev)
{
struct npcm_rng *priv;
int ret;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(priv->base))
return PTR_ERR(priv->base);
dev_set_drvdata(&pdev->dev, priv);
pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
pm_runtime_use_autosuspend(&pdev->dev);
pm_runtime_enable(&pdev->dev);
#ifndef CONFIG_PM
priv->rng.init = npcm_rng_init;
priv->rng.cleanup = npcm_rng_cleanup;
#endif
priv->rng.name = pdev->name;
priv->rng.read = npcm_rng_read;
priv->dev = &pdev->dev;
priv->clkp = (u32)(uintptr_t)of_device_get_match_data(&pdev->dev);
writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
ret = devm_hwrng_register(&pdev->dev, &priv->rng);
if (ret) {
dev_err(&pdev->dev, "Failed to register rng device: %d\n",
ret);
pm_runtime_disable(&pdev->dev);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/io.h`, `linux/iopoll.h`, `linux/init.h`, `linux/random.h`, `linux/err.h`, `linux/of.h`.
- Detected declarations: `struct npcm_rng`, `function npcm_rng_init`, `function npcm_rng_cleanup`, `function npcm_rng_read`, `function npcm_rng_probe`, `function npcm_rng_remove`, `function npcm_rng_runtime_suspend`, `function npcm_rng_runtime_resume`.
- 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.