2 * generic helper functions for video4linux capture buffers, to handle
3 * memory management and PCI DMA. Right now bttv + saa7134 use it.
5 * The functions expect the hardware being able to scatter gatter
6 * (i.e. the buffers are not linear in physical memory, but fragmented
7 * into PAGE_SIZE chunks). They also assume the driver does not need
8 * to touch the video data (thus it is probably not useful for USB as
9 * data often must be uncompressed by the drivers).
11 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
19 #include <linux/videodev.h>
21 /* --------------------------------------------------------------------- */
24 * Return a scatterlist for some page-aligned vmalloc()'ed memory
25 * block (NULL on errors). Memory for the scatterlist is allocated
26 * using kmalloc. The caller must free the memory.
28 struct scatterlist* videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages);
31 * Return a scatterlist for a an array of userpages (NULL on errors).
32 * Memory for the scatterlist is allocated using kmalloc. The caller
33 * must free the memory.
35 struct scatterlist* videobuf_pages_to_sg(struct page **pages, int nr_pages,
37 int videobuf_lock(struct page **pages, int nr_pages);
38 int videobuf_unlock(struct page **pages, int nr_pages);
40 /* --------------------------------------------------------------------- */
43 * A small set of helper functions to manage buffers (both userland
44 * and kernel) for DMA.
46 * videobuf_dma_init_*()
47 * creates a buffer. The userland version takes a userspace
48 * pointer + length. The kernel version just wants the size and
49 * does memory allocation too using vmalloc_32().
51 * videobuf_dma_pci_*()
52 * see Documentation/DMA-mapping.txt, these functions to
53 * basically the same. The map function does also build a
54 * scatterlist for the buffer (and unmap frees it ...)
61 struct videobuf_dmabuf {
62 /* for userland buffer */
66 /* for kernel buffers */
69 /* for overlay buffers (pci-pci dma) */
73 struct scatterlist *sglist;
79 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
80 unsigned long data, unsigned long size);
81 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
83 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
84 dma_addr_t addr, int nr_pages);
85 int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma);
86 int videobuf_dma_pci_sync(struct pci_dev *dev,
87 struct videobuf_dmabuf *dma);
88 int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma);
89 int videobuf_dma_free(struct videobuf_dmabuf *dma);
91 /* --------------------------------------------------------------------- */
94 * A small set of helper functions to manage video4linux buffers.
96 * struct videobuf_buffer holds the data structures used by the helper
97 * functions, additionally some commonly used fields for v4l buffers
98 * (width, height, lists, waitqueue) are in there. That struct should
99 * be used as first element in the drivers buffer struct.
101 * about the mmap helpers (videobuf_mmap_*):
103 * The mmaper function allows to map any subset of contingous buffers.
104 * This includes one mmap() call for all buffers (which the original
105 * video4linux API uses) as well as one mmap() for every single buffer
108 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
109 * userspace address + size which can be feeded into the
110 * videobuf_dma_init_user function listed above.
114 struct videobuf_buffer;
115 struct videobuf_queue;
117 struct videobuf_mapping {
122 struct videobuf_queue *q;
125 enum videobuf_state {
126 STATE_NEEDS_INIT = 0,
135 struct videobuf_buffer {
138 /* info about the buffer */
141 unsigned int bytesperline; /* use only if != 0 */
143 enum v4l2_field field;
144 enum videobuf_state state;
145 struct videobuf_dmabuf dma;
146 struct list_head stream; /* QBUF/DQBUF list */
148 /* for mmap'ed buffers */
149 enum v4l2_memory memory;
150 size_t boff; /* buffer offset (mmap + overlay) */
151 size_t bsize; /* buffer size */
152 unsigned long baddr; /* buffer addr (userland ptr!) */
153 struct videobuf_mapping *map;
155 /* touched by irq handler */
156 struct list_head queue;
157 wait_queue_head_t done;
158 unsigned int field_count;
162 struct videobuf_queue_ops {
163 int (*buf_setup)(struct file *file,
164 unsigned int *count, unsigned int *size);
165 int (*buf_prepare)(struct file *file,struct videobuf_buffer *vb,
166 enum v4l2_field field);
167 void (*buf_queue)(struct file *file,struct videobuf_buffer *vb);
168 void (*buf_release)(struct file *file,struct videobuf_buffer *vb);
171 struct videobuf_queue {
172 struct semaphore lock;
176 enum v4l2_buf_type type;
178 enum v4l2_field field;
179 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
180 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
181 struct videobuf_queue_ops *ops;
183 /* capture via mmap() + ioctl(QBUF/DQBUF) */
184 unsigned int streaming;
185 struct list_head stream;
187 /* capture via read() */
188 unsigned int reading;
189 unsigned int read_off;
190 struct videobuf_buffer *read_buf;
193 void* videobuf_alloc(unsigned int size);
194 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
195 int videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
196 struct v4l2_framebuffer *fbuf);
198 void videobuf_queue_init(struct videobuf_queue *q,
199 struct videobuf_queue_ops *ops,
200 struct pci_dev *pci, spinlock_t *irqlock,
201 enum v4l2_buf_type type,
202 enum v4l2_field field,
204 int videobuf_queue_is_busy(struct videobuf_queue *q);
205 void videobuf_queue_cancel(struct file *file, struct videobuf_queue *q);
207 enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
208 void videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
209 enum v4l2_buf_type type);
210 int videobuf_reqbufs(struct file *file, struct videobuf_queue *q,
211 struct v4l2_requestbuffers *req);
212 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
213 int videobuf_qbuf(struct file *file, struct videobuf_queue *q,
214 struct v4l2_buffer *b);
215 int videobuf_dqbuf(struct file *file, struct videobuf_queue *q,
216 struct v4l2_buffer *b);
217 int videobuf_streamon(struct file *file, struct videobuf_queue *q);
218 int videobuf_streamoff(struct file *file, struct videobuf_queue *q);
220 int videobuf_read_start(struct file *file, struct videobuf_queue *q);
221 void videobuf_read_stop(struct file *file, struct videobuf_queue *q);
222 ssize_t videobuf_read_stream(struct file *file, struct videobuf_queue *q,
223 char *data, size_t count, loff_t *ppos,
225 ssize_t videobuf_read_one(struct file *file, struct videobuf_queue *q,
226 char *data, size_t count, loff_t *ppos);
227 unsigned int videobuf_poll_stream(struct file *file,
228 struct videobuf_queue *q,
231 int videobuf_mmap_setup(struct file *file, struct videobuf_queue *q,
232 unsigned int bcount, unsigned int bsize,
233 enum v4l2_memory memory);
234 int videobuf_mmap_free(struct file *file, struct videobuf_queue *q);
235 int videobuf_mmap_mapper(struct vm_area_struct *vma,
236 struct videobuf_queue *q);
238 /* --------------------------------------------------------------------- */