blob: ae6da6de98ded4600c7e502fe6b16265dc0bc612 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: video-buf.h,v 1.9 2004/11/07 13:17:15 kraxel Exp $
3 *
4 * generic helper functions for video4linux capture buffers, to handle
5 * memory management and PCI DMA. Right now bttv + saa7134 use it.
6 *
7 * The functions expect the hardware being able to scatter gatter
8 * (i.e. the buffers are not linear in physical memory, but fragmented
9 * into PAGE_SIZE chunks). They also assume the driver does not need
10 * to touch the video data (thus it is probably not useful for USB as
11 * data often must be uncompressed by the drivers).
12 *
13 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 */
20
21#include <linux/videodev.h>
22
23#define UNSET (-1U)
24
25/* --------------------------------------------------------------------- */
26
27/*
28 * Return a scatterlist for some page-aligned vmalloc()'ed memory
29 * block (NULL on errors). Memory for the scatterlist is allocated
30 * using kmalloc. The caller must free the memory.
31 */
32struct scatterlist* videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages);
33
34/*
35 * Return a scatterlist for a an array of userpages (NULL on errors).
36 * Memory for the scatterlist is allocated using kmalloc. The caller
37 * must free the memory.
38 */
39struct scatterlist* videobuf_pages_to_sg(struct page **pages, int nr_pages,
40 int offset);
41
42/* --------------------------------------------------------------------- */
43
44/*
45 * A small set of helper functions to manage buffers (both userland
46 * and kernel) for DMA.
47 *
48 * videobuf_dma_init_*()
49 * creates a buffer. The userland version takes a userspace
50 * pointer + length. The kernel version just wants the size and
51 * does memory allocation too using vmalloc_32().
52 *
53 * videobuf_dma_pci_*()
54 * see Documentation/DMA-mapping.txt, these functions to
55 * basically the same. The map function does also build a
56 * scatterlist for the buffer (and unmap frees it ...)
57 *
58 * videobuf_dma_free()
59 * no comment ...
60 *
61 */
62
63struct videobuf_dmabuf {
64 u32 magic;
65
66 /* for userland buffer */
67 int offset;
68 struct page **pages;
69
70 /* for kernel buffers */
71 void *vmalloc;
72
73 /* for overlay buffers (pci-pci dma) */
74 dma_addr_t bus_addr;
75
76 /* common */
77 struct scatterlist *sglist;
78 int sglen;
79 int nr_pages;
80 int direction;
81};
82
83void videobuf_dma_init(struct videobuf_dmabuf *dma);
84int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
85 unsigned long data, unsigned long size);
86int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
87 int nr_pages);
88int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
89 dma_addr_t addr, int nr_pages);
90int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma);
91int videobuf_dma_pci_sync(struct pci_dev *dev,
92 struct videobuf_dmabuf *dma);
93int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma);
94int videobuf_dma_free(struct videobuf_dmabuf *dma);
95
96/* --------------------------------------------------------------------- */
97
98/*
99 * A small set of helper functions to manage video4linux buffers.
100 *
101 * struct videobuf_buffer holds the data structures used by the helper
102 * functions, additionally some commonly used fields for v4l buffers
103 * (width, height, lists, waitqueue) are in there. That struct should
104 * be used as first element in the drivers buffer struct.
105 *
106 * about the mmap helpers (videobuf_mmap_*):
107 *
108 * The mmaper function allows to map any subset of contingous buffers.
109 * This includes one mmap() call for all buffers (which the original
110 * video4linux API uses) as well as one mmap() for every single buffer
111 * (which v4l2 uses).
112 *
113 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
114 * userspace address + size which can be feeded into the
115 * videobuf_dma_init_user function listed above.
116 *
117 */
118
119struct videobuf_buffer;
120struct videobuf_queue;
121
122struct videobuf_mapping {
123 unsigned int count;
124 unsigned long start;
125 unsigned long end;
126 struct videobuf_queue *q;
127};
128
129enum videobuf_state {
130 STATE_NEEDS_INIT = 0,
131 STATE_PREPARED = 1,
132 STATE_QUEUED = 2,
133 STATE_ACTIVE = 3,
134 STATE_DONE = 4,
135 STATE_ERROR = 5,
136 STATE_IDLE = 6,
137};
138
139struct videobuf_buffer {
140 unsigned int i;
141 u32 magic;
142
143 /* info about the buffer */
144 unsigned int width;
145 unsigned int height;
146 unsigned int bytesperline; /* use only if != 0 */
147 unsigned long size;
148 unsigned int input;
149 enum v4l2_field field;
150 enum videobuf_state state;
151 struct videobuf_dmabuf dma;
152 struct list_head stream; /* QBUF/DQBUF list */
153
154 /* for mmap'ed buffers */
155 enum v4l2_memory memory;
156 size_t boff; /* buffer offset (mmap + overlay) */
157 size_t bsize; /* buffer size */
158 unsigned long baddr; /* buffer addr (userland ptr!) */
159 struct videobuf_mapping *map;
160
161 /* touched by irq handler */
162 struct list_head queue;
163 wait_queue_head_t done;
164 unsigned int field_count;
165 struct timeval ts;
166};
167
168struct videobuf_queue_ops {
169 int (*buf_setup)(struct videobuf_queue *q,
170 unsigned int *count, unsigned int *size);
171 int (*buf_prepare)(struct videobuf_queue *q,
172 struct videobuf_buffer *vb,
173 enum v4l2_field field);
174 void (*buf_queue)(struct videobuf_queue *q,
175 struct videobuf_buffer *vb);
176 void (*buf_release)(struct videobuf_queue *q,
177 struct videobuf_buffer *vb);
178};
179
180struct videobuf_queue {
181 struct semaphore lock;
182 spinlock_t *irqlock;
183 struct pci_dev *pci;
184
185 enum v4l2_buf_type type;
186 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
187 unsigned int msize;
188 enum v4l2_field field;
189 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
190 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
191 struct videobuf_queue_ops *ops;
192
193 /* capture via mmap() + ioctl(QBUF/DQBUF) */
194 unsigned int streaming;
195 struct list_head stream;
196
197 /* capture via read() */
198 unsigned int reading;
199 unsigned int read_off;
200 struct videobuf_buffer *read_buf;
201
202 /* driver private data */
203 void *priv_data;
204};
205
206void* videobuf_alloc(unsigned int size);
207int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
208int videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
209 struct v4l2_framebuffer *fbuf);
210
211void videobuf_queue_init(struct videobuf_queue *q,
212 struct videobuf_queue_ops *ops,
213 struct pci_dev *pci,
214 spinlock_t *irqlock,
215 enum v4l2_buf_type type,
216 enum v4l2_field field,
217 unsigned int msize,
218 void *priv);
219int videobuf_queue_is_busy(struct videobuf_queue *q);
220void videobuf_queue_cancel(struct videobuf_queue *q);
221
222enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
223void videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
224 enum v4l2_buf_type type);
225int videobuf_reqbufs(struct videobuf_queue *q,
226 struct v4l2_requestbuffers *req);
227int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
228int videobuf_qbuf(struct videobuf_queue *q,
229 struct v4l2_buffer *b);
230int videobuf_dqbuf(struct videobuf_queue *q,
231 struct v4l2_buffer *b, int nonblocking);
232int videobuf_streamon(struct videobuf_queue *q);
233int videobuf_streamoff(struct videobuf_queue *q);
234
235int videobuf_read_start(struct videobuf_queue *q);
236void videobuf_read_stop(struct videobuf_queue *q);
237ssize_t videobuf_read_stream(struct videobuf_queue *q,
238 char __user *data, size_t count, loff_t *ppos,
239 int vbihack, int nonblocking);
240ssize_t videobuf_read_one(struct videobuf_queue *q,
241 char __user *data, size_t count, loff_t *ppos,
242 int nonblocking);
243unsigned int videobuf_poll_stream(struct file *file,
244 struct videobuf_queue *q,
245 poll_table *wait);
246
247int videobuf_mmap_setup(struct videobuf_queue *q,
248 unsigned int bcount, unsigned int bsize,
249 enum v4l2_memory memory);
250int videobuf_mmap_free(struct videobuf_queue *q);
251int videobuf_mmap_mapper(struct videobuf_queue *q,
252 struct vm_area_struct *vma);
253
254/* --------------------------------------------------------------------- */
255
256/*
257 * Local variables:
258 * c-basic-offset: 8
259 * End:
260 */