blob: d6f079476db34dc7b020177205b04a53a483deb1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * generic helper functions for video4linux capture buffers, to handle
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03004 * memory management and PCI DMA.
5 * Right now, bttv, saa7134, saa7146 and cx88 use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030010 * to touch the video data.
11 *
12 * device specific map/unmap/sync stuff now are mapped as file operations
13 * to allow its usage by USB and virtual devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030016 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
17 * (c) 2006 Ted Walther and John Sokol
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 */
24
Mauro Carvalho Chehab79436632005-11-08 21:37:49 -080025#include <linux/videodev2.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030026#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define UNSET (-1U)
29
30/* --------------------------------------------------------------------- */
31
32/*
33 * Return a scatterlist for some page-aligned vmalloc()'ed memory
34 * block (NULL on errors). Memory for the scatterlist is allocated
35 * using kmalloc. The caller must free the memory.
36 */
37struct scatterlist* videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages);
38
39/*
40 * Return a scatterlist for a an array of userpages (NULL on errors).
41 * Memory for the scatterlist is allocated using kmalloc. The caller
42 * must free the memory.
43 */
44struct scatterlist* videobuf_pages_to_sg(struct page **pages, int nr_pages,
45 int offset);
46
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030047struct videobuf_buffer;
48struct videobuf_queue;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* --------------------------------------------------------------------- */
51
52/*
53 * A small set of helper functions to manage buffers (both userland
54 * and kernel) for DMA.
55 *
56 * videobuf_dma_init_*()
57 * creates a buffer. The userland version takes a userspace
58 * pointer + length. The kernel version just wants the size and
59 * does memory allocation too using vmalloc_32().
60 *
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030061 * videobuf_dma_*()
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * see Documentation/DMA-mapping.txt, these functions to
63 * basically the same. The map function does also build a
64 * scatterlist for the buffer (and unmap frees it ...)
65 *
66 * videobuf_dma_free()
67 * no comment ...
68 *
69 */
70
71struct videobuf_dmabuf {
72 u32 magic;
73
74 /* for userland buffer */
75 int offset;
76 struct page **pages;
77
78 /* for kernel buffers */
79 void *vmalloc;
80
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -030081 /* Stores the userspace pointer to vmalloc area */
82 void *varea;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* for overlay buffers (pci-pci dma) */
85 dma_addr_t bus_addr;
86
87 /* common */
88 struct scatterlist *sglist;
89 int sglen;
90 int nr_pages;
91 int direction;
92};
93
94void videobuf_dma_init(struct videobuf_dmabuf *dma);
95int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
96 unsigned long data, unsigned long size);
97int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
98 int nr_pages);
99int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
100 dma_addr_t addr, int nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101int videobuf_dma_free(struct videobuf_dmabuf *dma);
102
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300103int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
104int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
105int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma);
106
107 /*FIXME: these variants are used only on *-alsa code, where videobuf is
108 * used without queue
109 */
110int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma);
111int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma);
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* --------------------------------------------------------------------- */
114
115/*
116 * A small set of helper functions to manage video4linux buffers.
117 *
118 * struct videobuf_buffer holds the data structures used by the helper
119 * functions, additionally some commonly used fields for v4l buffers
120 * (width, height, lists, waitqueue) are in there. That struct should
121 * be used as first element in the drivers buffer struct.
122 *
123 * about the mmap helpers (videobuf_mmap_*):
124 *
125 * The mmaper function allows to map any subset of contingous buffers.
126 * This includes one mmap() call for all buffers (which the original
127 * video4linux API uses) as well as one mmap() for every single buffer
128 * (which v4l2 uses).
129 *
130 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
131 * userspace address + size which can be feeded into the
132 * videobuf_dma_init_user function listed above.
133 *
134 */
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct videobuf_mapping {
137 unsigned int count;
138 unsigned long start;
139 unsigned long end;
140 struct videobuf_queue *q;
141};
142
143enum videobuf_state {
144 STATE_NEEDS_INIT = 0,
145 STATE_PREPARED = 1,
146 STATE_QUEUED = 2,
147 STATE_ACTIVE = 3,
148 STATE_DONE = 4,
149 STATE_ERROR = 5,
150 STATE_IDLE = 6,
151};
152
153struct videobuf_buffer {
154 unsigned int i;
155 u32 magic;
156
157 /* info about the buffer */
158 unsigned int width;
159 unsigned int height;
160 unsigned int bytesperline; /* use only if != 0 */
161 unsigned long size;
162 unsigned int input;
163 enum v4l2_field field;
164 enum videobuf_state state;
165 struct videobuf_dmabuf dma;
166 struct list_head stream; /* QBUF/DQBUF list */
167
168 /* for mmap'ed buffers */
169 enum v4l2_memory memory;
170 size_t boff; /* buffer offset (mmap + overlay) */
171 size_t bsize; /* buffer size */
172 unsigned long baddr; /* buffer addr (userland ptr!) */
173 struct videobuf_mapping *map;
174
175 /* touched by irq handler */
176 struct list_head queue;
177 wait_queue_head_t done;
178 unsigned int field_count;
179 struct timeval ts;
180};
181
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300182typedef int (vb_map_sg_t)(void *dev,struct scatterlist *sglist,int nr_pages,
183 int direction);
184
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186struct videobuf_queue_ops {
187 int (*buf_setup)(struct videobuf_queue *q,
188 unsigned int *count, unsigned int *size);
189 int (*buf_prepare)(struct videobuf_queue *q,
190 struct videobuf_buffer *vb,
191 enum v4l2_field field);
192 void (*buf_queue)(struct videobuf_queue *q,
193 struct videobuf_buffer *vb);
194 void (*buf_release)(struct videobuf_queue *q,
195 struct videobuf_buffer *vb);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300196
197 /* Helper operations - device dependent.
198 * If null, videobuf_init defaults all to PCI handling
199 */
200
201 vb_map_sg_t *vb_map_sg;
202 vb_map_sg_t *vb_dma_sync_sg;
203 vb_map_sg_t *vb_unmap_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206struct videobuf_queue {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200207 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spinlock_t *irqlock;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300209 void *dev; /* on pci, points to struct pci_dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 enum v4l2_buf_type type;
212 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
213 unsigned int msize;
214 enum v4l2_field field;
215 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
216 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
217 struct videobuf_queue_ops *ops;
218
219 /* capture via mmap() + ioctl(QBUF/DQBUF) */
220 unsigned int streaming;
221 struct list_head stream;
222
223 /* capture via read() */
224 unsigned int reading;
225 unsigned int read_off;
226 struct videobuf_buffer *read_buf;
227
228 /* driver private data */
229 void *priv_data;
230};
231
232void* videobuf_alloc(unsigned int size);
233int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300234int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
235 struct v4l2_framebuffer *fbuf);
236
237/* Maps fops to PCI stuff */
238void videobuf_queue_pci(struct videobuf_queue* q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240void videobuf_queue_init(struct videobuf_queue *q,
241 struct videobuf_queue_ops *ops,
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300242 void *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spinlock_t *irqlock,
244 enum v4l2_buf_type type,
245 enum v4l2_field field,
246 unsigned int msize,
247 void *priv);
248int videobuf_queue_is_busy(struct videobuf_queue *q);
249void videobuf_queue_cancel(struct videobuf_queue *q);
250
251enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
252void videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
253 enum v4l2_buf_type type);
254int videobuf_reqbufs(struct videobuf_queue *q,
255 struct v4l2_requestbuffers *req);
256int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
257int videobuf_qbuf(struct videobuf_queue *q,
258 struct v4l2_buffer *b);
259int videobuf_dqbuf(struct videobuf_queue *q,
260 struct v4l2_buffer *b, int nonblocking);
261int videobuf_streamon(struct videobuf_queue *q);
262int videobuf_streamoff(struct videobuf_queue *q);
263
264int videobuf_read_start(struct videobuf_queue *q);
265void videobuf_read_stop(struct videobuf_queue *q);
266ssize_t videobuf_read_stream(struct videobuf_queue *q,
267 char __user *data, size_t count, loff_t *ppos,
268 int vbihack, int nonblocking);
269ssize_t videobuf_read_one(struct videobuf_queue *q,
270 char __user *data, size_t count, loff_t *ppos,
271 int nonblocking);
272unsigned int videobuf_poll_stream(struct file *file,
273 struct videobuf_queue *q,
274 poll_table *wait);
275
276int videobuf_mmap_setup(struct videobuf_queue *q,
277 unsigned int bcount, unsigned int bsize,
278 enum v4l2_memory memory);
279int videobuf_mmap_free(struct videobuf_queue *q);
280int videobuf_mmap_mapper(struct videobuf_queue *q,
281 struct vm_area_struct *vma);
282
283/* --------------------------------------------------------------------- */
284
285/*
286 * Local variables:
287 * c-basic-offset: 8
288 * End:
289 */