blob: 8fe3254ed952a620095e52277a6cf18176884a7b [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;
57 unsigned long start;
58 unsigned long end;
59 struct videobuf_queue *q;
60};
61
62enum videobuf_state {
Brandon Philips0fc06862007-11-06 20:02:36 -030063 VIDEOBUF_NEEDS_INIT = 0,
64 VIDEOBUF_PREPARED = 1,
65 VIDEOBUF_QUEUED = 2,
66 VIDEOBUF_ACTIVE = 3,
67 VIDEOBUF_DONE = 4,
68 VIDEOBUF_ERROR = 5,
69 VIDEOBUF_IDLE = 6,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030070};
71
72struct videobuf_buffer {
73 unsigned int i;
74 u32 magic;
75
76 /* info about the buffer */
77 unsigned int width;
78 unsigned int height;
79 unsigned int bytesperline; /* use only if != 0 */
80 unsigned long size;
81 unsigned int input;
82 enum v4l2_field field;
83 enum videobuf_state state;
84 struct list_head stream; /* QBUF/DQBUF list */
85
86 /* touched by irq handler */
87 struct list_head queue;
88 wait_queue_head_t done;
89 unsigned int field_count;
90 struct timeval ts;
91
92 /* Memory type */
93 enum v4l2_memory memory;
94
95 /* buffer size */
96 size_t bsize;
97
98 /* buffer offset (mmap + overlay) */
99 size_t boff;
100
101 /* buffer addr (userland ptr!) */
102 unsigned long baddr;
103
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -0300104 /* for mmap'ed buffers */
105 struct videobuf_mapping *map;
106
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300107 /* Private pointer to allow specific methods to store their data */
108 int privsize;
109 void *priv;
110};
111
112struct videobuf_queue_ops {
113 int (*buf_setup)(struct videobuf_queue *q,
114 unsigned int *count, unsigned int *size);
115 int (*buf_prepare)(struct videobuf_queue *q,
116 struct videobuf_buffer *vb,
117 enum v4l2_field field);
118 void (*buf_queue)(struct videobuf_queue *q,
119 struct videobuf_buffer *vb);
120 void (*buf_release)(struct videobuf_queue *q,
121 struct videobuf_buffer *vb);
122};
123
124#define MAGIC_QTYPE_OPS 0x12261003
125
126/* Helper operations - device type dependent */
127struct videobuf_qtype_ops {
128 u32 magic;
129
Hans Verkuila4cf4ca2010-03-28 08:09:44 -0300130 struct videobuf_buffer *(*alloc)(size_t size);
Hans Verkuil037c75e2010-03-28 08:18:37 -0300131 void *(*vaddr) (struct videobuf_buffer *buf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300132 int (*iolock) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300133 struct videobuf_buffer *vb,
134 struct v4l2_framebuffer *fbuf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300135 int (*sync) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300136 struct videobuf_buffer *buf);
Al Viro13bcd5d2007-10-13 08:25:24 +0100137 int (*video_copy_to_user)(struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300138 char __user *data,
139 size_t count,
140 int nonblocking);
141 int (*copy_stream) (struct videobuf_queue *q,
142 char __user *data,
143 size_t count,
144 size_t pos,
145 int vbihack,
146 int nonblocking);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300147 int (*mmap_mapper) (struct videobuf_queue *q,
148 struct vm_area_struct *vma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300149};
150
151struct videobuf_queue {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300152 struct mutex vb_lock;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300153 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300154 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300155
Brandon Philips137d1cb2008-04-02 18:10:59 -0300156 wait_queue_head_t wait; /* wait if queue is empty */
157
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300158 enum v4l2_buf_type type;
159 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
160 unsigned int msize;
161 enum v4l2_field field;
162 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
163 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300164 const struct videobuf_queue_ops *ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300165 struct videobuf_qtype_ops *int_ops;
166
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300167 unsigned int streaming:1;
168 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300169
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300170 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300171 struct list_head stream;
172
173 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300174 unsigned int read_off;
175 struct videobuf_buffer *read_buf;
176
177 /* driver private data */
178 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300179};
180
181int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr);
Pawel Osciak7a022642010-03-17 04:01:04 -0300182int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300183 struct v4l2_framebuffer *fbuf);
184
Hans Verkuila4cf4ca2010-03-28 08:09:44 -0300185struct videobuf_buffer *videobuf_alloc(struct videobuf_queue *q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300186
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300187/* Used on videobuf-dvb */
Pawel Osciak7a022642010-03-17 04:01:04 -0300188void *videobuf_queue_to_vmalloc(struct videobuf_queue *q,
189 struct videobuf_buffer *buf);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300190
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300191void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300192 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300193 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300194 spinlock_t *irqlock,
195 enum v4l2_buf_type type,
196 enum v4l2_field field,
197 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300198 void *priv,
199 struct videobuf_qtype_ops *int_ops);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300200int videobuf_queue_is_busy(struct videobuf_queue *q);
201void videobuf_queue_cancel(struct videobuf_queue *q);
202
203enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
204int videobuf_reqbufs(struct videobuf_queue *q,
205 struct v4l2_requestbuffers *req);
206int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
207int videobuf_qbuf(struct videobuf_queue *q,
208 struct v4l2_buffer *b);
209int videobuf_dqbuf(struct videobuf_queue *q,
210 struct v4l2_buffer *b, int nonblocking);
211#ifdef CONFIG_VIDEO_V4L1_COMPAT
212int videobuf_cgmbuf(struct videobuf_queue *q,
213 struct video_mbuf *mbuf, int count);
214#endif
215int videobuf_streamon(struct videobuf_queue *q);
216int videobuf_streamoff(struct videobuf_queue *q);
217
Brandon Philips19bc5132007-11-13 20:05:38 -0300218void videobuf_stop(struct videobuf_queue *q);
219
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300220int videobuf_read_start(struct videobuf_queue *q);
221void videobuf_read_stop(struct videobuf_queue *q);
222ssize_t videobuf_read_stream(struct videobuf_queue *q,
223 char __user *data, size_t count, loff_t *ppos,
224 int vbihack, int nonblocking);
225ssize_t videobuf_read_one(struct videobuf_queue *q,
226 char __user *data, size_t count, loff_t *ppos,
227 int nonblocking);
228unsigned int videobuf_poll_stream(struct file *file,
229 struct videobuf_queue *q,
230 poll_table *wait);
231
232int videobuf_mmap_setup(struct videobuf_queue *q,
233 unsigned int bcount, unsigned int bsize,
234 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700235int __videobuf_mmap_setup(struct videobuf_queue *q,
236 unsigned int bcount, unsigned int bsize,
237 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300238int videobuf_mmap_free(struct videobuf_queue *q);
239int videobuf_mmap_mapper(struct videobuf_queue *q,
240 struct vm_area_struct *vma);
241
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300242#endif