drivers/media/common/videobuf2/videobuf2-dma-contig.c
Source file repositories/reference/linux-study-clean/drivers/media/common/videobuf2/videobuf2-dma-contig.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/media/common/videobuf2/videobuf2-dma-contig.c- Extension
.c- Size
- 21198 bytes
- Lines
- 867
- Domain
- Driver Families
- Bucket
- drivers/media
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/dma-buf.hlinux/module.hlinux/refcount.hlinux/scatterlist.hlinux/sched.hlinux/slab.hlinux/dma-mapping.hlinux/highmem.hmedia/videobuf2-v4l2.hmedia/videobuf2-dma-contig.hmedia/videobuf2-memops.h
Detected Declarations
struct vb2_dc_bufstruct vb2_dc_attachmentfunction vb2_dc_get_contiguous_sizefunction for_each_sgtable_dma_sgfunction vb2_dc_num_usersfunction vb2_dc_preparefunction vb2_dc_finishfunction vb2_dc_putfunction vb2_dc_alloc_coherentfunction vb2_dc_alloc_non_coherentfunction vb2_dc_mmapfunction vb2_dc_dmabuf_ops_attachfunction vb2_dc_dmabuf_ops_detachfunction vb2_dc_dmabuf_ops_detachfunction vb2_dc_dmabuf_ops_unmapfunction vb2_dc_dmabuf_ops_begin_cpu_accessfunction vb2_dc_dmabuf_ops_end_cpu_accessfunction vb2_dc_dmabuf_ops_vmapfunction vb2_dc_dmabuf_ops_mmapfunction vb2_dc_put_userptrfunction preparefunction vb2_dc_map_dmabuffunction vb2_dc_unmap_dmabuffunction vb2_dc_detach_dmabuffunction vb2_dma_contig_set_max_seg_sizeexport vb2_dma_contig_memopsexport vb2_dma_contig_set_max_seg_size
Annotated Snippet
struct vb2_dc_buf {
struct device *dev;
void *vaddr;
unsigned long size;
void *cookie;
dma_addr_t dma_addr;
unsigned long attrs;
enum dma_data_direction dma_dir;
struct sg_table *dma_sgt;
struct frame_vector *vec;
/* MMAP related */
struct vb2_vmarea_handler handler;
refcount_t refcount;
struct sg_table *sgt_base;
/* DMABUF related */
struct dma_buf_attachment *db_attach;
struct vb2_buffer *vb;
bool non_coherent_mem;
};
/*********************************************/
/* scatterlist table functions */
/*********************************************/
static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
{
struct scatterlist *s;
dma_addr_t expected = sg_dma_address(sgt->sgl);
unsigned int i;
unsigned long size = 0;
for_each_sgtable_dma_sg(sgt, s, i) {
if (sg_dma_address(s) != expected)
break;
expected += sg_dma_len(s);
size += sg_dma_len(s);
}
return size;
}
/*********************************************/
/* callbacks for all buffers */
/*********************************************/
static void *vb2_dc_cookie(struct vb2_buffer *vb, void *buf_priv)
{
struct vb2_dc_buf *buf = buf_priv;
return &buf->dma_addr;
}
/*
* This function may fail if:
*
* - dma_buf_vmap() fails
* E.g. due to lack of virtual mapping address space, or due to
* dmabuf->ops misconfiguration.
*
* - dma_vmap_noncontiguous() fails
* For instance, when requested buffer size is larger than totalram_pages().
* Relevant for buffers that use non-coherent memory.
*
* - Queue DMA attrs have DMA_ATTR_NO_KERNEL_MAPPING set
* Relevant for buffers that use coherent memory.
*/
static void *vb2_dc_vaddr(struct vb2_buffer *vb, void *buf_priv)
{
struct vb2_dc_buf *buf = buf_priv;
if (buf->vaddr)
return buf->vaddr;
if (buf->db_attach) {
struct iosys_map map;
if (!dma_buf_vmap_unlocked(buf->db_attach->dmabuf, &map))
buf->vaddr = map.vaddr;
return buf->vaddr;
}
if (buf->non_coherent_mem)
buf->vaddr = dma_vmap_noncontiguous(buf->dev, buf->size,
buf->dma_sgt);
return buf->vaddr;
}
Annotation
- Immediate include surface: `linux/dma-buf.h`, `linux/module.h`, `linux/refcount.h`, `linux/scatterlist.h`, `linux/sched.h`, `linux/slab.h`, `linux/dma-mapping.h`, `linux/highmem.h`.
- Detected declarations: `struct vb2_dc_buf`, `struct vb2_dc_attachment`, `function vb2_dc_get_contiguous_size`, `function for_each_sgtable_dma_sg`, `function vb2_dc_num_users`, `function vb2_dc_prepare`, `function vb2_dc_finish`, `function vb2_dc_put`, `function vb2_dc_alloc_coherent`, `function vb2_dc_alloc_non_coherent`.
- Atlas domain: Driver Families / drivers/media.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.