blob: 90ed895e217d3b04eea44269ee9fc9d35da05f69 [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>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030020#include <linux/videodev2.h>
21
22#define UNSET (-1U)
23
24
25struct videobuf_buffer;
26struct videobuf_queue;
27
28/* --------------------------------------------------------------------- */
29
30/*
31 * A small set of helper functions to manage video4linux buffers.
32 *
33 * struct videobuf_buffer holds the data structures used by the helper
34 * functions, additionally some commonly used fields for v4l buffers
35 * (width, height, lists, waitqueue) are in there. That struct should
36 * be used as first element in the drivers buffer struct.
37 *
38 * about the mmap helpers (videobuf_mmap_*):
39 *
40 * The mmaper function allows to map any subset of contingous buffers.
41 * This includes one mmap() call for all buffers (which the original
42 * video4linux API uses) as well as one mmap() for every single buffer
43 * (which v4l2 uses).
44 *
45 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
46 * userspace address + size which can be feeded into the
47 * videobuf_dma_init_user function listed above.
48 *
49 */
50
51struct videobuf_mapping {
52 unsigned int count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030053 struct videobuf_queue *q;
54};
55
56enum videobuf_state {
Brandon Philips0fc06862007-11-06 20:02:36 -030057 VIDEOBUF_NEEDS_INIT = 0,
58 VIDEOBUF_PREPARED = 1,
59 VIDEOBUF_QUEUED = 2,
60 VIDEOBUF_ACTIVE = 3,
61 VIDEOBUF_DONE = 4,
62 VIDEOBUF_ERROR = 5,
63 VIDEOBUF_IDLE = 6,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030064};
65
66struct videobuf_buffer {
67 unsigned int i;
68 u32 magic;
69
70 /* info about the buffer */
71 unsigned int width;
72 unsigned int height;
73 unsigned int bytesperline; /* use only if != 0 */
74 unsigned long size;
75 unsigned int input;
76 enum v4l2_field field;
77 enum videobuf_state state;
78 struct list_head stream; /* QBUF/DQBUF list */
79
80 /* touched by irq handler */
81 struct list_head queue;
82 wait_queue_head_t done;
83 unsigned int field_count;
84 struct timeval ts;
85
86 /* Memory type */
87 enum v4l2_memory memory;
88
89 /* buffer size */
90 size_t bsize;
91
92 /* buffer offset (mmap + overlay) */
93 size_t boff;
94
95 /* buffer addr (userland ptr!) */
96 unsigned long baddr;
97
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -030098 /* for mmap'ed buffers */
99 struct videobuf_mapping *map;
100
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300101 /* Private pointer to allow specific methods to store their data */
102 int privsize;
103 void *priv;
104};
105
106struct videobuf_queue_ops {
107 int (*buf_setup)(struct videobuf_queue *q,
108 unsigned int *count, unsigned int *size);
109 int (*buf_prepare)(struct videobuf_queue *q,
110 struct videobuf_buffer *vb,
111 enum v4l2_field field);
112 void (*buf_queue)(struct videobuf_queue *q,
113 struct videobuf_buffer *vb);
114 void (*buf_release)(struct videobuf_queue *q,
115 struct videobuf_buffer *vb);
116};
117
118#define MAGIC_QTYPE_OPS 0x12261003
119
120/* Helper operations - device type dependent */
121struct videobuf_qtype_ops {
122 u32 magic;
123
Pawel Osciak33c38282010-05-11 10:36:28 -0300124 struct videobuf_buffer *(*alloc_vb)(size_t size);
Hans Verkuil037c75e2010-03-28 08:18:37 -0300125 void *(*vaddr) (struct videobuf_buffer *buf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300126 int (*iolock) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300127 struct videobuf_buffer *vb,
128 struct v4l2_framebuffer *fbuf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300129 int (*sync) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130 struct videobuf_buffer *buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300131 int (*mmap_mapper) (struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300132 struct videobuf_buffer *buf,
133 struct vm_area_struct *vma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300134};
135
136struct videobuf_queue {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300137 struct mutex vb_lock;
Hans Verkuil97397682010-09-20 17:24:30 -0300138 struct mutex *ext_lock;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300139 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300140 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300141
Brandon Philips137d1cb2008-04-02 18:10:59 -0300142 wait_queue_head_t wait; /* wait if queue is empty */
143
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300144 enum v4l2_buf_type type;
145 unsigned int inputs; /* for V4L2_BUF_FLAG_INPUT */
146 unsigned int msize;
147 enum v4l2_field field;
148 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
149 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300150 const struct videobuf_queue_ops *ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300151 struct videobuf_qtype_ops *int_ops;
152
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300153 unsigned int streaming:1;
154 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300155
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300156 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300157 struct list_head stream;
158
159 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160 unsigned int read_off;
161 struct videobuf_buffer *read_buf;
162
163 /* driver private data */
164 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300165};
166
Hans Verkuil97397682010-09-20 17:24:30 -0300167static inline void videobuf_queue_lock(struct videobuf_queue *q)
168{
169 if (!q->ext_lock)
170 mutex_lock(&q->vb_lock);
171}
172
173static inline void videobuf_queue_unlock(struct videobuf_queue *q)
174{
175 if (!q->ext_lock)
176 mutex_unlock(&q->vb_lock);
177}
178
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300179int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
180 int non_blocking, int intr);
Pawel Osciak7a022642010-03-17 04:01:04 -0300181int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300182 struct v4l2_framebuffer *fbuf);
183
Pawel Osciak33c38282010-05-11 10:36:28 -0300184struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300185
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300186/* Used on videobuf-dvb */
Hans Verkuilf4fce602010-03-28 08:33:23 -0300187void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
188 struct videobuf_buffer *buf);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300189
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300190void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300191 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300192 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300193 spinlock_t *irqlock,
194 enum v4l2_buf_type type,
195 enum v4l2_field field,
196 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300197 void *priv,
Hans Verkuil08bff032010-09-20 17:39:46 -0300198 struct videobuf_qtype_ops *int_ops,
199 struct mutex *ext_lock);
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);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300211int videobuf_streamon(struct videobuf_queue *q);
212int videobuf_streamoff(struct videobuf_queue *q);
213
Brandon Philips19bc5132007-11-13 20:05:38 -0300214void videobuf_stop(struct videobuf_queue *q);
215
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300216int videobuf_read_start(struct videobuf_queue *q);
217void videobuf_read_stop(struct videobuf_queue *q);
218ssize_t videobuf_read_stream(struct videobuf_queue *q,
219 char __user *data, size_t count, loff_t *ppos,
220 int vbihack, int nonblocking);
221ssize_t videobuf_read_one(struct videobuf_queue *q,
222 char __user *data, size_t count, loff_t *ppos,
223 int nonblocking);
224unsigned int videobuf_poll_stream(struct file *file,
225 struct videobuf_queue *q,
226 poll_table *wait);
227
228int videobuf_mmap_setup(struct videobuf_queue *q,
229 unsigned int bcount, unsigned int bsize,
230 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700231int __videobuf_mmap_setup(struct videobuf_queue *q,
232 unsigned int bcount, unsigned int bsize,
233 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300234int videobuf_mmap_free(struct videobuf_queue *q);
235int videobuf_mmap_mapper(struct videobuf_queue *q,
236 struct vm_area_struct *vma);
237
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300238#endif