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_init_*_dmabuf()
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_pci_*_dmabuf()
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 ...)
56 * videobuf_free_dmabuf()
61 struct videobuf_dmabuf
{
62 /* for userland buffer */
66 /* for kernel buffers */
70 struct scatterlist
*sglist
;
76 int videobuf_dma_init_user(struct videobuf_dmabuf
*dma
, int direction
,
77 unsigned long data
, unsigned long size
);
78 int videobuf_dma_init_kernel(struct videobuf_dmabuf
*dma
, int direction
,
80 int videobuf_dma_pci_map(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
);
81 int videobuf_dma_pci_sync(struct pci_dev
*dev
,
82 struct videobuf_dmabuf
*dma
);
83 int videobuf_dma_pci_unmap(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
);
84 int videobuf_dma_free(struct videobuf_dmabuf
*dma
);
86 /* --------------------------------------------------------------------- */
89 * A small set of helper functions to manage video4linux buffers.
91 * struct videobuf_buffer holds the data structures used by the helper
92 * functions, additionally some commonly used fields for v4l buffers
93 * (width, height, lists, waitqueue) are in there. That struct should
94 * be used as first element in the drivers buffer struct.
96 * about the mmap helpers (videobuf_mmap_*):
98 * The mmaper function allows to map any subset of contingous buffers.
99 * This includes one mmap() call for all buffers (which the original
100 * video4linux API uses) as well as one mmap() for every single buffer
103 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
104 * userspace address + size which can be feeded into the
105 * videobuf_dma_init_user function listed above.
109 struct videobuf_buffer
;
110 struct videobuf_queue
;
112 struct videobuf_mapping
{
117 struct videobuf_queue
*q
;
120 enum videobuf_state
{
121 STATE_NEEDS_INIT
= 0,
130 struct videobuf_buffer
{
133 /* info about the buffer */
137 enum v4l2_field field
;
138 enum videobuf_state state
;
139 struct videobuf_dmabuf dma
;
140 struct list_head stream
; /* QBUF/DQBUF list */
142 /* for mmap'ed buffers */
143 size_t boff
; /* buffer offset (mmap) */
144 size_t bsize
; /* buffer size */
145 unsigned long baddr
; /* buffer addr (userland ptr!) */
146 struct videobuf_mapping
*map
;
148 /* touched by irq handler */
149 struct list_head queue
;
150 wait_queue_head_t done
;
151 unsigned int field_count
;
155 struct videobuf_queue_ops
{
156 int (*buf_setup
)(struct file
*file
,
157 unsigned int *count
, unsigned int *size
);
158 int (*buf_prepare
)(struct file
*file
,struct videobuf_buffer
*vb
,
159 enum v4l2_field field
);
160 void (*buf_queue
)(struct file
*file
,struct videobuf_buffer
*vb
);
161 void (*buf_release
)(struct file
*file
,struct videobuf_buffer
*vb
);
164 struct videobuf_queue
{
165 struct semaphore lock
;
169 enum v4l2_buf_type type
;
171 enum v4l2_field field
;
172 enum v4l2_field last
; /* for field=V4L2_FIELD_ALTERNATE */
173 struct videobuf_buffer
*bufs
[VIDEO_MAX_FRAME
];
174 struct videobuf_queue_ops
*ops
;
176 /* capture via mmap() + ioctl(QBUF/DQBUF) */
177 unsigned int streaming
;
178 struct list_head stream
;
180 /* capture via read() */
181 unsigned int reading
;
182 unsigned int read_off
;
183 struct videobuf_buffer
*read_buf
;
186 void* videobuf_alloc(unsigned int size
);
187 int videobuf_waiton(struct videobuf_buffer
*vb
, int non_blocking
, int intr
);
188 int videobuf_iolock(struct pci_dev
*pci
, struct videobuf_buffer
*vb
);
190 void videobuf_queue_init(struct videobuf_queue
*q
,
191 struct videobuf_queue_ops
*ops
,
192 struct pci_dev
*pci
, spinlock_t
*irqlock
,
193 enum v4l2_buf_type type
,
194 enum v4l2_field field
,
196 int videobuf_queue_is_busy(struct videobuf_queue
*q
);
197 void videobuf_queue_cancel(struct file
*file
, struct videobuf_queue
*q
);
199 enum v4l2_field
videobuf_next_field(struct videobuf_queue
*q
);
200 void videobuf_status(struct v4l2_buffer
*b
, struct videobuf_buffer
*vb
,
201 enum v4l2_buf_type type
);
202 int videobuf_reqbufs(struct file
*file
, struct videobuf_queue
*q
,
203 struct v4l2_requestbuffers
*req
);
204 int videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
);
205 int videobuf_qbuf(struct file
*file
, struct videobuf_queue
*q
,
206 struct v4l2_buffer
*b
);
207 int videobuf_dqbuf(struct file
*file
, struct videobuf_queue
*q
,
208 struct v4l2_buffer
*b
);
209 int videobuf_streamon(struct file
*file
, struct videobuf_queue
*q
);
210 int videobuf_streamoff(struct file
*file
, struct videobuf_queue
*q
);
212 int videobuf_read_start(struct file
*file
, struct videobuf_queue
*q
);
213 void videobuf_read_stop(struct file
*file
, struct videobuf_queue
*q
);
214 ssize_t
videobuf_read_stream(struct file
*file
, struct videobuf_queue
*q
,
215 char *data
, size_t count
, loff_t
*ppos
,
217 ssize_t
videobuf_read_one(struct file
*file
, struct videobuf_queue
*q
,
218 char *data
, size_t count
, loff_t
*ppos
);
219 unsigned int videobuf_poll_stream(struct file
*file
,
220 struct videobuf_queue
*q
,
223 int videobuf_mmap_setup(struct file
*file
, struct videobuf_queue
*q
,
224 unsigned int bcount
, unsigned int bsize
);
225 int videobuf_mmap_free(struct file
*file
, struct videobuf_queue
*q
);
226 int videobuf_mmap_mapper(struct vm_area_struct
*vma
,
227 struct videobuf_queue
*q
);
229 /* --------------------------------------------------------------------- */