blob: 874f1340d0493d9f8f1e66a5186ae984e9e93a51 [file] [log] [blame]
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03001/*
2 * generic helper functions for handling video4linux capture buffers
3 *
4 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
5 *
6 * Highly based on video-buf written originally by:
7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
8 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
9 * (c) 2006 Ted Walther and John Sokol
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2
14 */
15
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -030016#ifndef _VIDEOBUF_CORE_H
17#define _VIDEOBUF_CORE_H
18
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030019#include <linux/poll.h>
20#ifdef CONFIG_VIDEO_V4L1_COMPAT
21#include <linux/videodev.h>
22#endif
23#include <linux/videodev2.h>
24
25#define UNSET (-1U)
26
27
28struct videobuf_buffer;
29struct videobuf_queue;
30
31/* --------------------------------------------------------------------- */
32
33/*
34 * A small set of helper functions to manage video4linux buffers.
35 *
36 * struct videobuf_buffer holds the data structures used by the helper
37 * functions, additionally some commonly used fields for v4l buffers
38 * (width, height, lists, waitqueue) are in there. That struct should
39 * be used as first element in the drivers buffer struct.
40 *
41 * about the mmap helpers (videobuf_mmap_*):
42 *
43 * The mmaper function allows to map any subset of contingous buffers.
44 * This includes one mmap() call for all buffers (which the original
45 * video4linux API uses) as well as one mmap() for every single buffer
46 * (which v4l2 uses).
47 *
48 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
49 * userspace address + size which can be feeded into the
50 * videobuf_dma_init_user function listed above.
51 *
52 */
53
54struct videobuf_mapping {
55 unsigned int count;
56 unsigned long start;
57 unsigned long end;
58 struct videobuf_queue *q;
59};
60
61enum videobuf_state {
Brandon Philips0fc06862007-11-06 20:02:36 -030062 VIDEOBUF_NEEDS_INIT = 0,
63 VIDEOBUF_PREPARED = 1,
64 VIDEOBUF_QUEUED = 2,
65 VIDEOBUF_ACTIVE = 3,
66 VIDEOBUF_DONE = 4,
67 VIDEOBUF_ERROR = 5,
68 VIDEOBUF_IDLE = 6,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030069};
70
71struct videobuf_buffer {
72 unsigned int i;
73 u32 magic;
74
75 /* info about the buffer */
76 unsigned int width;
77 unsigned int height;
78 unsigned int bytesperline; /* use only if != 0 */
79 unsigned long size;
80 unsigned int input;
81 enum v4l2_field field;
82 enum videobuf_state state;
83 struct list_head stream; /* QBUF/DQBUF list */
84
85 /* touched by irq handler */
86 struct list_head queue;
87 wait_queue_head_t done;
88 unsigned int field_count;
89 struct timeval ts;
90
91 /* Memory type */
92 enum v4l2_memory memory;
93
94 /* buffer size */
95 size_t bsize;
96
97 /* buffer offset (mmap + overlay) */
98 size_t boff;
99
100 /* buffer addr (userland ptr!) */
101 unsigned long baddr;
102
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300103 /* for mmap'ed buffers */
104 struct videobuf_mapping *map;
105
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300106 /* Private pointer to allow specific methods to store their data */
107 int privsize;
108 void *priv;
109};
110
111struct videobuf_queue_ops {
112 int (*buf_setup)(struct videobuf_queue *q,
113 unsigned int *count, unsigned int *size);
114 int (*buf_prepare)(struct videobuf_queue *q,
115 struct videobuf_buffer *vb,
116 enum v4l2_field field);
117 void (*buf_queue)(struct videobuf_queue *q,
118 struct videobuf_buffer *vb);
119 void (*buf_release)(struct videobuf_queue *q,
120 struct videobuf_buffer *vb);
121};
122
123#define MAGIC_QTYPE_OPS 0x12261003
124
125/* Helper operations - device type dependent */
126struct videobuf_qtype_ops {
127 u32 magic;
128
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300129 void *(*alloc) (size_t size);
130 void *(*vmalloc) (struct videobuf_buffer *buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300131 int (*iolock) (struct videobuf_queue* q,
132 struct videobuf_buffer *vb,
133 struct v4l2_framebuffer *fbuf);
134 int (*mmap) (struct videobuf_queue *q,
135 unsigned int *count,
136 unsigned int *size,
137 enum v4l2_memory memory);
138 int (*sync) (struct videobuf_queue* q,
139 struct videobuf_buffer *buf);
Al Viro13bcd5d2007-10-13 08:25:24 +0100140 int (*video_copy_to_user)(struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300141 char __user *data,
142 size_t count,
143 int nonblocking);
144 int (*copy_stream) (struct videobuf_queue *q,
145 char __user *data,
146 size_t count,
147 size_t pos,
148 int vbihack,
149 int nonblocking);
150 int (*mmap_free) (struct videobuf_queue *q);
151 int (*mmap_mapper) (struct videobuf_queue *q,
152 struct vm_area_struct *vma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300153};
154
155struct videobuf_queue {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300156 struct mutex vb_lock;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300157 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300158 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300159
Brandon Philips137d1cb2008-04-02 18:10:59 -0300160 wait_queue_head_t wait; /* wait if queue is empty */
161
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300162 enum v4l2_buf_type type;
163 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
164 unsigned int msize;
165 enum v4l2_field field;
166 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
167 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
168 struct videobuf_queue_ops *ops;
169 struct videobuf_qtype_ops *int_ops;
170
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300171 unsigned int streaming:1;
172 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300173 unsigned int is_mmapped:1;
174
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300175 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300176 struct list_head stream;
177
178 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300179 unsigned int read_off;
180 struct videobuf_buffer *read_buf;
181
182 /* driver private data */
183 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300184};
185
186int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
187int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
188 struct v4l2_framebuffer *fbuf);
189
190void *videobuf_alloc(struct videobuf_queue* q);
191
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300192/* Used on videobuf-dvb */
193void *videobuf_queue_to_vmalloc (struct videobuf_queue* q,
194 struct videobuf_buffer *buf);
195
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300196void videobuf_queue_core_init(struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300197 struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300198 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300199 spinlock_t *irqlock,
200 enum v4l2_buf_type type,
201 enum v4l2_field field,
202 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300203 void *priv,
204 struct videobuf_qtype_ops *int_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300205int videobuf_queue_is_busy(struct videobuf_queue *q);
206void videobuf_queue_cancel(struct videobuf_queue *q);
207
208enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
209int videobuf_reqbufs(struct videobuf_queue *q,
210 struct v4l2_requestbuffers *req);
211int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
212int videobuf_qbuf(struct videobuf_queue *q,
213 struct v4l2_buffer *b);
214int videobuf_dqbuf(struct videobuf_queue *q,
215 struct v4l2_buffer *b, int nonblocking);
216#ifdef CONFIG_VIDEO_V4L1_COMPAT
217int videobuf_cgmbuf(struct videobuf_queue *q,
218 struct video_mbuf *mbuf, int count);
219#endif
220int videobuf_streamon(struct videobuf_queue *q);
221int videobuf_streamoff(struct videobuf_queue *q);
222
Brandon Philips19bc5132007-11-13 20:05:38 -0300223void videobuf_stop(struct videobuf_queue *q);
224
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300225int videobuf_read_start(struct videobuf_queue *q);
226void videobuf_read_stop(struct videobuf_queue *q);
227ssize_t videobuf_read_stream(struct videobuf_queue *q,
228 char __user *data, size_t count, loff_t *ppos,
229 int vbihack, int nonblocking);
230ssize_t videobuf_read_one(struct videobuf_queue *q,
231 char __user *data, size_t count, loff_t *ppos,
232 int nonblocking);
233unsigned int videobuf_poll_stream(struct file *file,
234 struct videobuf_queue *q,
235 poll_table *wait);
236
237int videobuf_mmap_setup(struct videobuf_queue *q,
238 unsigned int bcount, unsigned int bsize,
239 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700240int __videobuf_mmap_setup(struct videobuf_queue *q,
241 unsigned int bcount, unsigned int bsize,
242 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300243int videobuf_mmap_free(struct videobuf_queue *q);
244int videobuf_mmap_mapper(struct videobuf_queue *q,
245 struct vm_area_struct *vma);
246
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300247#endif