blob: f2c41cebf453a65abbe4fc281f20103287e5fea0 [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;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300142 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300143 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300144
Brandon Philips137d1cb2008-04-02 18:10:59 -0300145 wait_queue_head_t wait; /* wait if queue is empty */
146
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300147 enum v4l2_buf_type type;
148 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
149 unsigned int msize;
150 enum v4l2_field field;
151 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
152 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300153 const struct videobuf_queue_ops *ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300154 struct videobuf_qtype_ops *int_ops;
155
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300156 unsigned int streaming:1;
157 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300158
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300159 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160 struct list_head stream;
161
162 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300163 unsigned int read_off;
164 struct videobuf_buffer *read_buf;
165
166 /* driver private data */
167 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300168};
169
170int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
Pawel Osciak7a022642010-03-17 04:01:04 -0300171int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300172 struct v4l2_framebuffer *fbuf);
173
Pawel Osciak33c38282010-05-11 10:36:28 -0300174struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300175
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300176/* Used on videobuf-dvb */
Hans Verkuilf4fce602010-03-28 08:33:23 -0300177void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
178 struct videobuf_buffer *buf);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300179
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300180void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300181 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300182 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300183 spinlock_t *irqlock,
184 enum v4l2_buf_type type,
185 enum v4l2_field field,
186 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300187 void *priv,
188 struct videobuf_qtype_ops *int_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300189int videobuf_queue_is_busy(struct videobuf_queue *q);
190void videobuf_queue_cancel(struct videobuf_queue *q);
191
192enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
193int videobuf_reqbufs(struct videobuf_queue *q,
194 struct v4l2_requestbuffers *req);
195int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
196int videobuf_qbuf(struct videobuf_queue *q,
197 struct v4l2_buffer *b);
198int videobuf_dqbuf(struct videobuf_queue *q,
199 struct v4l2_buffer *b, int nonblocking);
200#ifdef CONFIG_VIDEO_V4L1_COMPAT
201int videobuf_cgmbuf(struct videobuf_queue *q,
202 struct video_mbuf *mbuf, int count);
203#endif
204int videobuf_streamon(struct videobuf_queue *q);
205int videobuf_streamoff(struct videobuf_queue *q);
206
Brandon Philips19bc5132007-11-13 20:05:38 -0300207void videobuf_stop(struct videobuf_queue *q);
208
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300209int videobuf_read_start(struct videobuf_queue *q);
210void videobuf_read_stop(struct videobuf_queue *q);
211ssize_t videobuf_read_stream(struct videobuf_queue *q,
212 char __user *data, size_t count, loff_t *ppos,
213 int vbihack, int nonblocking);
214ssize_t videobuf_read_one(struct videobuf_queue *q,
215 char __user *data, size_t count, loff_t *ppos,
216 int nonblocking);
217unsigned int videobuf_poll_stream(struct file *file,
218 struct videobuf_queue *q,
219 poll_table *wait);
220
221int videobuf_mmap_setup(struct videobuf_queue *q,
222 unsigned int bcount, unsigned int bsize,
223 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700224int __videobuf_mmap_setup(struct videobuf_queue *q,
225 unsigned int bcount, unsigned int bsize,
226 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300227int videobuf_mmap_free(struct videobuf_queue *q);
228int videobuf_mmap_mapper(struct videobuf_queue *q,
229 struct vm_area_struct *vma);
230
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300231#endif