blob: 2e01b2e9a1c0b9e34223a8fcca932785187605dd [file] [log] [blame]
Thomas Gleixner77512ba2019-06-03 07:44:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03002/*
3 * generic helper functions for handling video4linux capture buffers
4 *
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04005 * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -03006 *
7 * Highly based on video-buf written originally by:
8 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04009 * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030010 * (c) 2006 Ted Walther and John Sokol
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030011 */
12
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -030013#ifndef _VIDEOBUF_CORE_H
14#define _VIDEOBUF_CORE_H
15
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030016#include <linux/poll.h>
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030017#include <linux/videodev2.h>
18
19#define UNSET (-1U)
20
21
22struct videobuf_buffer;
23struct videobuf_queue;
24
25/* --------------------------------------------------------------------- */
26
27/*
28 * A small set of helper functions to manage video4linux buffers.
29 *
30 * struct videobuf_buffer holds the data structures used by the helper
31 * functions, additionally some commonly used fields for v4l buffers
32 * (width, height, lists, waitqueue) are in there. That struct should
33 * be used as first element in the drivers buffer struct.
34 *
35 * about the mmap helpers (videobuf_mmap_*):
36 *
Masahiro Yamadae1c05062015-07-07 10:14:59 +090037 * The mmaper function allows to map any subset of contiguous buffers.
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030038 * This includes one mmap() call for all buffers (which the original
39 * video4linux API uses) as well as one mmap() for every single buffer
40 * (which v4l2 uses).
41 *
42 * If there is a valid mapping for a buffer, buffer->baddr/bsize holds
Mauro Carvalho Chehabe907bf32019-02-18 14:29:06 -050043 * userspace address + size which can be fed into the
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030044 * videobuf_dma_init_user function listed above.
45 *
46 */
47
48struct videobuf_mapping {
49 unsigned int count;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030050 struct videobuf_queue *q;
51};
52
53enum videobuf_state {
Brandon Philips0fc06862007-11-06 20:02:36 -030054 VIDEOBUF_NEEDS_INIT = 0,
55 VIDEOBUF_PREPARED = 1,
56 VIDEOBUF_QUEUED = 2,
57 VIDEOBUF_ACTIVE = 3,
58 VIDEOBUF_DONE = 4,
59 VIDEOBUF_ERROR = 5,
60 VIDEOBUF_IDLE = 6,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030061};
62
63struct videobuf_buffer {
64 unsigned int i;
65 u32 magic;
66
67 /* info about the buffer */
68 unsigned int width;
69 unsigned int height;
70 unsigned int bytesperline; /* use only if != 0 */
71 unsigned long size;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030072 enum v4l2_field field;
73 enum videobuf_state state;
74 struct list_head stream; /* QBUF/DQBUF list */
75
76 /* touched by irq handler */
77 struct list_head queue;
78 wait_queue_head_t done;
79 unsigned int field_count;
Hans Verkuil15a40b22019-01-21 08:32:23 -050080 u64 ts;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030081
82 /* Memory type */
83 enum v4l2_memory memory;
84
85 /* buffer size */
86 size_t bsize;
87
88 /* buffer offset (mmap + overlay) */
89 size_t boff;
90
91 /* buffer addr (userland ptr!) */
92 unsigned long baddr;
93
Mauro Carvalho Chehab851c0c92007-09-27 18:25:44 -030094 /* for mmap'ed buffers */
95 struct videobuf_mapping *map;
96
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -030097 /* Private pointer to allow specific methods to store their data */
98 int privsize;
99 void *priv;
100};
101
102struct videobuf_queue_ops {
103 int (*buf_setup)(struct videobuf_queue *q,
104 unsigned int *count, unsigned int *size);
105 int (*buf_prepare)(struct videobuf_queue *q,
106 struct videobuf_buffer *vb,
107 enum v4l2_field field);
108 void (*buf_queue)(struct videobuf_queue *q,
109 struct videobuf_buffer *vb);
110 void (*buf_release)(struct videobuf_queue *q,
111 struct videobuf_buffer *vb);
112};
113
114#define MAGIC_QTYPE_OPS 0x12261003
115
116/* Helper operations - device type dependent */
117struct videobuf_qtype_ops {
118 u32 magic;
119
Pawel Osciak33c38282010-05-11 10:36:28 -0300120 struct videobuf_buffer *(*alloc_vb)(size_t size);
Hans Verkuil037c75e2010-03-28 08:18:37 -0300121 void *(*vaddr) (struct videobuf_buffer *buf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300122 int (*iolock) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300123 struct videobuf_buffer *vb,
124 struct v4l2_framebuffer *fbuf);
Pawel Osciak7a022642010-03-17 04:01:04 -0300125 int (*sync) (struct videobuf_queue *q,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300126 struct videobuf_buffer *buf);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300127 int (*mmap_mapper) (struct videobuf_queue *q,
Hans Verkuil0b62b732010-03-28 09:09:05 -0300128 struct videobuf_buffer *buf,
129 struct vm_area_struct *vma);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300130};
131
132struct videobuf_queue {
Mauro Carvalho Chehab64f94772008-01-31 13:57:53 -0300133 struct mutex vb_lock;
Hans Verkuil97397682010-09-20 17:24:30 -0300134 struct mutex *ext_lock;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300135 spinlock_t *irqlock;
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300136 struct device *dev;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300137
Brandon Philips137d1cb2008-04-02 18:10:59 -0300138 wait_queue_head_t wait; /* wait if queue is empty */
139
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300140 enum v4l2_buf_type type;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300141 unsigned int msize;
142 enum v4l2_field field;
143 enum v4l2_field last; /* for field=V4L2_FIELD_ALTERNATE */
144 struct videobuf_buffer *bufs[VIDEO_MAX_FRAME];
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300145 const struct videobuf_queue_ops *ops;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300146 struct videobuf_qtype_ops *int_ops;
147
Brandon Philipsd6964aa2007-11-06 20:23:08 -0300148 unsigned int streaming:1;
149 unsigned int reading:1;
Mauro Carvalho Chehabd05051c2008-01-10 07:33:03 -0300150
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300151 /* capture via mmap() + ioctl(QBUF/DQBUF) */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300152 struct list_head stream;
153
154 /* capture via read() */
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300155 unsigned int read_off;
156 struct videobuf_buffer *read_buf;
157
158 /* driver private data */
159 void *priv_data;
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300160};
161
Hans Verkuil97397682010-09-20 17:24:30 -0300162static inline void videobuf_queue_lock(struct videobuf_queue *q)
163{
164 if (!q->ext_lock)
165 mutex_lock(&q->vb_lock);
166}
167
168static inline void videobuf_queue_unlock(struct videobuf_queue *q)
169{
170 if (!q->ext_lock)
171 mutex_unlock(&q->vb_lock);
172}
173
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300174int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
175 int non_blocking, int intr);
Pawel Osciak7a022642010-03-17 04:01:04 -0300176int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300177 struct v4l2_framebuffer *fbuf);
178
Pawel Osciak33c38282010-05-11 10:36:28 -0300179struct videobuf_buffer *videobuf_alloc_vb(struct videobuf_queue *q);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300180
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300181/* Used on videobuf-dvb */
Hans Verkuilf4fce602010-03-28 08:33:23 -0300182void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
183 struct videobuf_buffer *buf);
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300184
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300185void videobuf_queue_core_init(struct videobuf_queue *q,
Jonathan Corbet38a54f32009-11-17 19:43:41 -0300186 const struct videobuf_queue_ops *ops,
Guennadi Liakhovetskie9bcf662008-04-22 14:46:02 -0300187 struct device *dev,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300188 spinlock_t *irqlock,
189 enum v4l2_buf_type type,
190 enum v4l2_field field,
191 unsigned int msize,
Mauro Carvalho Chehabd4cae5a2007-10-08 12:20:02 -0300192 void *priv,
Hans Verkuil08bff032010-09-20 17:39:46 -0300193 struct videobuf_qtype_ops *int_ops,
194 struct mutex *ext_lock);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300195int videobuf_queue_is_busy(struct videobuf_queue *q);
196void videobuf_queue_cancel(struct videobuf_queue *q);
197
198enum v4l2_field videobuf_next_field(struct videobuf_queue *q);
199int videobuf_reqbufs(struct videobuf_queue *q,
200 struct v4l2_requestbuffers *req);
201int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b);
202int videobuf_qbuf(struct videobuf_queue *q,
203 struct v4l2_buffer *b);
204int videobuf_dqbuf(struct videobuf_queue *q,
205 struct v4l2_buffer *b, int nonblocking);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300206int videobuf_streamon(struct videobuf_queue *q);
207int videobuf_streamoff(struct videobuf_queue *q);
208
Brandon Philips19bc5132007-11-13 20:05:38 -0300209void videobuf_stop(struct videobuf_queue *q);
210
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300211int videobuf_read_start(struct videobuf_queue *q);
212void videobuf_read_stop(struct videobuf_queue *q);
213ssize_t videobuf_read_stream(struct videobuf_queue *q,
214 char __user *data, size_t count, loff_t *ppos,
215 int vbihack, int nonblocking);
216ssize_t videobuf_read_one(struct videobuf_queue *q,
217 char __user *data, size_t count, loff_t *ppos,
218 int nonblocking);
Al Viroc23e0cb2017-07-03 03:02:56 -0400219__poll_t videobuf_poll_stream(struct file *file,
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300220 struct videobuf_queue *q,
221 poll_table *wait);
222
223int videobuf_mmap_setup(struct videobuf_queue *q,
224 unsigned int bcount, unsigned int bsize,
225 enum v4l2_memory memory);
Arjan van de Ven81b2dbc2008-05-20 09:53:52 -0700226int __videobuf_mmap_setup(struct videobuf_queue *q,
227 unsigned int bcount, unsigned int bsize,
228 enum v4l2_memory memory);
Mauro Carvalho Chehab7a7d9a82007-08-23 16:26:14 -0300229int videobuf_mmap_free(struct videobuf_queue *q);
230int videobuf_mmap_mapper(struct videobuf_queue *q,
231 struct vm_area_struct *vma);
232
Mauro Carvalho Chehab59d34482008-04-13 15:10:00 -0300233#endif