drivers/misc/nsm.c
Source file repositories/reference/linux-study-clean/drivers/misc/nsm.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/misc/nsm.c- Extension
.c- Size
- 11824 bytes
- Lines
- 506
- Domain
- Driver Families
- Bucket
- drivers/misc
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/file.hlinux/fs.hlinux/interrupt.hlinux/hw_random.hlinux/miscdevice.hlinux/module.hlinux/mutex.hlinux/slab.hlinux/string.hlinux/uaccess.hlinux/uio.hlinux/virtio_config.hlinux/virtio_ids.hlinux/virtio.hlinux/wait.huapi/linux/nsm.h
Detected Declarations
struct nsm_data_reqstruct nsm_data_respstruct nsm_msgstruct nsmfunction cbor_object_is_arrayfunction cbor_object_get_arrayfunction fill_req_rawfunction parse_resp_rawfunction nsm_vq_callbackfunction nsm_sendrecv_msg_lockedfunction msecs_to_jiffiesfunction fill_req_get_randomfunction parse_resp_get_randomfunction nsm_rng_readfunction nsm_dev_ioctlfunction nsm_device_init_vqfunction nsm_device_probefunction nsm_device_remove
Annotated Snippet
static const struct file_operations nsm_dev_fops = {
.unlocked_ioctl = nsm_dev_ioctl,
.compat_ioctl = compat_ptr_ioctl,
};
/* Handler for probing the NSM device */
static int nsm_device_probe(struct virtio_device *vdev)
{
struct device *dev = &vdev->dev;
struct nsm *nsm;
int rc;
nsm = devm_kzalloc(&vdev->dev, sizeof(*nsm), GFP_KERNEL);
if (!nsm)
return -ENOMEM;
vdev->priv = nsm;
nsm->vdev = vdev;
rc = nsm_device_init_vq(vdev);
if (rc) {
dev_err(dev, "queue failed to initialize: %d.\n", rc);
goto err_init_vq;
}
mutex_init(&nsm->lock);
/* Register as hwrng provider */
nsm->hwrng = (struct hwrng) {
.read = nsm_rng_read,
.name = "nsm-hwrng",
.quality = 1000,
};
rc = hwrng_register(&nsm->hwrng);
if (rc) {
dev_err(dev, "RNG initialization error: %d.\n", rc);
goto err_hwrng;
}
/* Register /dev/nsm device node */
nsm->misc = (struct miscdevice) {
.minor = MISC_DYNAMIC_MINOR,
.name = "nsm",
.fops = &nsm_dev_fops,
.mode = 0666,
};
rc = misc_register(&nsm->misc);
if (rc) {
dev_err(dev, "misc device registration error: %d.\n", rc);
goto err_misc;
}
return 0;
err_misc:
hwrng_unregister(&nsm->hwrng);
err_hwrng:
vdev->config->del_vqs(vdev);
err_init_vq:
return rc;
}
/* Handler for removing the NSM device */
static void nsm_device_remove(struct virtio_device *vdev)
{
struct nsm *nsm = vdev->priv;
hwrng_unregister(&nsm->hwrng);
vdev->config->del_vqs(vdev);
misc_deregister(&nsm->misc);
}
/* NSM device configuration structure */
static struct virtio_driver virtio_nsm_driver = {
.feature_table = 0,
.feature_table_size = 0,
.feature_table_legacy = 0,
.feature_table_size_legacy = 0,
.driver.name = KBUILD_MODNAME,
.id_table = id_table,
.probe = nsm_device_probe,
.remove = nsm_device_remove,
};
module_virtio_driver(virtio_nsm_driver);
MODULE_DEVICE_TABLE(virtio, id_table);
MODULE_DESCRIPTION("Virtio NSM driver");
Annotation
- Immediate include surface: `linux/file.h`, `linux/fs.h`, `linux/interrupt.h`, `linux/hw_random.h`, `linux/miscdevice.h`, `linux/module.h`, `linux/mutex.h`, `linux/slab.h`.
- Detected declarations: `struct nsm_data_req`, `struct nsm_data_resp`, `struct nsm_msg`, `struct nsm`, `function cbor_object_is_array`, `function cbor_object_get_array`, `function fill_req_raw`, `function parse_resp_raw`, `function nsm_vq_callback`, `function nsm_sendrecv_msg_locked`.
- Atlas domain: Driver Families / drivers/misc.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.