blob: 1d3835fc26be0d4a7e20408fb9b242f70f30a459 [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
Mauro Carvalho Chehab5e585ef2009-03-10 18:30:27 -030021#define __MIN_V4L1
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030022#include <linux/videodev.h>
23#endif
24#include <linux/videodev2.h>
25
26#define UNSET (-1U)
27
28
29struct videobuf_buffer;
30struct videobuf_queue;
31
32/* --------------------------------------------------------------------- */
33
34/*
35 * A small set of helper functions to manage video4linux buffers.
36 *
37 * struct videobuf_buffer holds the data structures used by the helper
38 * functions, additionally some commonly used fields for v4l buffers
39 * (width, height, lists, waitqueue) are in there. That struct should
40 * be used as first element in the drivers buffer struct.
41 *
42 * about the mmap helpers (videobuf_mmap_*):
43 *
44 * The mmaper function allows to map any subset of contingous buffers.
45 * This includes one mmap() call for all buffers (which the original
46 * video4linux API uses) as well as one mmap() for every single buffer
47 * (which v4l2 uses).
48 *
49 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
50 * userspace address + size which can be feeded into the
51 * videobuf_dma_init_user function listed above.
52 *
53 */
54
55struct videobuf_mapping {
56 unsigned int count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030057 struct videobuf_queue *q;
58};
59
60enum videobuf_state {
Brandon Philips0fc06862007-11-06 20:02:36 -030061 VIDEOBUF_NEEDS_INIT = 0,
62 VIDEOBUF_PREPARED = 1,
63 VIDEOBUF_QUEUED = 2,
64 VIDEOBUF_ACTIVE = 3,
65 VIDEOBUF_DONE = 4,
66 VIDEOBUF_ERROR = 5,
67 VIDEOBUF_IDLE = 6,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030068};
69
70struct videobuf_buffer {
71 unsigned int i;
72 u32 magic;
73
74 /* info about the buffer */
75 unsigned int width;
76 unsigned int height;
77 unsigned int bytesperline; /* use only if != 0 */
78 unsigned long size;
79 unsigned int input;
80 enum v4l2_field field;
81 enum videobuf_state state;
82 struct list_head stream; /* QBUF/DQBUF list */
83
84 /* touched by irq handler */
85 struct list_head queue;
86 wait_queue_head_t done;
87 unsigned int field_count;
88 struct timeval ts;
89
90 /* Memory type */
91 enum v4l2_memory memory;
92
93 /* buffer size */
94 size_t bsize;
95
96 /* buffer offset (mmap + overlay) */
97 size_t boff;
98
99 /* buffer addr (userland ptr!) */
100 unsigned long baddr;
101
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300102 /* for mmap'ed buffers */
103 struct videobuf_mapping *map;
104
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300105 /* Private pointer to allow specific methods to store their data */
106 int privsize;
107 void *priv;
108};
109
110struct videobuf_queue_ops {
111 int (*buf_setup)(struct videobuf_queue *q,
112 unsigned int *count, unsigned int *size);
113 int (*buf_prepare)(struct videobuf_queue *q,
114 struct videobuf_buffer *vb,
115 enum v4l2_field field);
116 void (*buf_queue)(struct videobuf_queue *q,
117 struct videobuf_buffer *vb);
118 void (*buf_release)(struct videobuf_queue *q,
119 struct videobuf_buffer *vb);
120};
121
122#define MAGIC_QTYPE_OPS 0x12261003
123
124/* Helper operations - device type dependent */
125struct videobuf_qtype_ops {
126 u32 magic;
127
Pawel Osciak33c38282010-05-11 10:36:28 -0300128 struct videobuf_buffer *(*alloc_vb)(size_t size);
Hans Verkuil037c75e2010-03-28 08:18:37 -0300129 void *(*vaddr) (struct videobuf_buffer *buf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300130 int (*iolock) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300131 struct videobuf_buffer *vb,
132 struct v4l2_framebuffer *fbuf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300133 int (*sync) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300134 struct videobuf_buffer *buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300135 int (*mmap_mapper) (struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300136 struct videobuf_buffer *buf,
137 struct vm_area_struct *vma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138};
139
140struct videobuf_queue {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300141 struct mutex vb_lock;
Hans Verkuil97397682010-09-20 17:24:30 -0300142 struct mutex *ext_lock;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300143 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300144 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300145
Brandon Philips137d1cb2008-04-02 18:10:59 -0300146 wait_queue_head_t wait; /* wait if queue is empty */
147
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300148 enum v4l2_buf_type type;
149 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
150 unsigned int msize;
151 enum v4l2_field field;
152 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
153 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300154 const struct videobuf_queue_ops *ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300155 struct videobuf_qtype_ops *int_ops;
156
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300157 unsigned int streaming:1;
158 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300159
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300161 struct list_head stream;
162
163 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300164 unsigned int read_off;
165 struct videobuf_buffer *read_buf;
166
167 /* driver private data */
168 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300169};
170
Hans Verkuil97397682010-09-20 17:24:30 -0300171static inline void videobuf_queue_lock(struct videobuf_queue *q)
172{
173 if (!q->ext_lock)
174 mutex_lock(&q->vb_lock);
175}
176
177static inline void videobuf_queue_unlock(struct videobuf_queue *q)
178{
179 if (!q->ext_lock)
180 mutex_unlock(&q->vb_lock);
181}
182
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300183int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
184 int non_blocking, int intr);
Pawel Osciak7a022642010-03-17 04:01:04 -0300185int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300186 struct v4l2_framebuffer *fbuf);
187
Pawel Osciak33c38282010-05-11 10:36:28 -0300188struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300189
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300190/* Used on videobuf-dvb */
Hans Verkuilf4fce602010-03-28 08:33:23 -0300191void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
192 struct videobuf_buffer *buf);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300193
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300194void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300195 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300196 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300197 spinlock_t *irqlock,
198 enum v4l2_buf_type type,
199 enum v4l2_field field,
200 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300201 void *priv,
Hans Verkuil08bff032010-09-20 17:39:46 -0300202 struct videobuf_qtype_ops *int_ops,
203 struct mutex *ext_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300204int videobuf_queue_is_busy(struct videobuf_queue *q);
205void videobuf_queue_cancel(struct videobuf_queue *q);
206
207enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
208int videobuf_reqbufs(struct videobuf_queue *q,
209 struct v4l2_requestbuffers *req);
210int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
211int videobuf_qbuf(struct videobuf_queue *q,
212 struct v4l2_buffer *b);
213int videobuf_dqbuf(struct videobuf_queue *q,
214 struct v4l2_buffer *b, int nonblocking);
215#ifdef CONFIG_VIDEO_V4L1_COMPAT
216int videobuf_cgmbuf(struct videobuf_queue *q,
217 struct video_mbuf *mbuf, int count);
218#endif
219int videobuf_streamon(struct videobuf_queue *q);
220int videobuf_streamoff(struct videobuf_queue *q);
221
Brandon Philips19bc5132007-11-13 20:05:38 -0300222void videobuf_stop(struct videobuf_queue *q);
223
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300224int videobuf_read_start(struct videobuf_queue *q);
225void videobuf_read_stop(struct videobuf_queue *q);
226ssize_t videobuf_read_stream(struct videobuf_queue *q,
227 char __user *data, size_t count, loff_t *ppos,
228 int vbihack, int nonblocking);
229ssize_t videobuf_read_one(struct videobuf_queue *q,
230 char __user *data, size_t count, loff_t *ppos,
231 int nonblocking);
232unsigned int videobuf_poll_stream(struct file *file,
233 struct videobuf_queue *q,
234 poll_table *wait);
235
236int videobuf_mmap_setup(struct videobuf_queue *q,
237 unsigned int bcount, unsigned int bsize,
238 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700239int __videobuf_mmap_setup(struct videobuf_queue *q,
240 unsigned int bcount, unsigned int bsize,
241 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300242int videobuf_mmap_free(struct videobuf_queue *q);
243int videobuf_mmap_mapper(struct videobuf_queue *q,
244 struct vm_area_struct *vma);
245
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300246#endif