xref: /openbmc/linux/drivers/media/pci/bt8xx/bttv-vbi.c (revision 479965a2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 
4     bttv - Bt848 frame grabber driver
5     vbi interface
6 
7     (c) 2002 Gerd Knorr <kraxel@bytesex.org>
8 
9     Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
10     Sponsored by OPQ Systems AB
11 
12 */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/kdev_t.h>
22 #include <media/v4l2-ioctl.h>
23 #include <asm/io.h>
24 #include "bttvp.h"
25 
26 /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
27    in fCLKx2 pixels.  According to the datasheet, VBI capture starts
28    VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
29    is 64 fCLKx1 pixels wide.  VBI_HDELAY is set to 0, so this should be
30    (64 + 0) * 2 = 128 fCLKx2 pixels.  But it's not!  The datasheet is
31    Just Plain Wrong.  The real value appears to be different for
32    different revisions of the bt8x8 chips, and to be affected by the
33    horizontal scaling factor.  Experimentally, the value is measured
34    to be about 244.  */
35 #define VBI_OFFSET 244
36 
37 static unsigned int vbibufs = 4;
38 static unsigned int vbi_debug;
39 
40 module_param(vbibufs,   int, 0444);
41 module_param(vbi_debug, int, 0644);
42 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
43 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
44 
45 #ifdef dprintk
46 # undef dprintk
47 #endif
48 #define dprintk(fmt, ...)						\
49 do {									\
50 	if (vbi_debug)							\
51 		pr_debug("%d: " fmt, btv->c.nr, ##__VA_ARGS__);		\
52 } while (0)
53 
54 #define IMAGE_SIZE(fmt) \
55 	(((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
56 
57 /* ----------------------------------------------------------------------- */
58 /* vbi risc code + mm                                                      */
59 
60 static int queue_setup_vbi(struct vb2_queue *q, unsigned int *num_buffers,
61 			   unsigned int *num_planes, unsigned int sizes[],
62 			   struct device *alloc_devs[])
63 {
64 	struct bttv *btv = vb2_get_drv_priv(q);
65 	unsigned int size = IMAGE_SIZE(&btv->vbi_fmt.fmt);
66 
67 	if (*num_planes)
68 		return sizes[0] < size ? -EINVAL : 0;
69 	*num_planes = 1;
70 	sizes[0] = size;
71 
72 	return 0;
73 }
74 
75 static void buf_queue_vbi(struct vb2_buffer *vb)
76 {
77 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
78 	struct vb2_queue *vq = vb->vb2_queue;
79 	struct bttv *btv = vb2_get_drv_priv(vq);
80 	struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
81 	unsigned long flags;
82 
83 	spin_lock_irqsave(&btv->s_lock, flags);
84 	if (list_empty(&btv->vcapture)) {
85 		btv->loop_irq = BT848_RISC_VBI;
86 		if (vb2_is_streaming(&btv->capq))
87 			btv->loop_irq |= BT848_RISC_VIDEO;
88 		bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_VBI_ODD |
89 			     BT848_CAP_CTL_CAPTURE_VBI_EVEN);
90 	}
91 	list_add_tail(&buf->list, &btv->vcapture);
92 	spin_unlock_irqrestore(&btv->s_lock, flags);
93 }
94 
95 static int buf_prepare_vbi(struct vb2_buffer *vb)
96 {
97 	int ret = 0;
98 	struct vb2_queue *vq = vb->vb2_queue;
99 	struct bttv *btv = vb2_get_drv_priv(vq);
100 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
101 	struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
102 	unsigned int size = IMAGE_SIZE(&btv->vbi_fmt.fmt);
103 
104 	if (vb2_plane_size(vb, 0) < size)
105 		return -EINVAL;
106 	vb2_set_plane_payload(vb, 0, size);
107 	buf->vbuf.field = V4L2_FIELD_NONE;
108 	ret = bttv_buffer_risc_vbi(btv, buf);
109 
110 	return ret;
111 }
112 
113 static void buf_cleanup_vbi(struct vb2_buffer *vb)
114 {
115 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
116 	struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
117 	struct vb2_queue *vq = vb->vb2_queue;
118 	struct bttv *btv = vb2_get_drv_priv(vq);
119 
120 	btcx_riscmem_free(btv->c.pci, &buf->top);
121 	btcx_riscmem_free(btv->c.pci, &buf->bottom);
122 }
123 
124 static int start_streaming_vbi(struct vb2_queue *q, unsigned int count)
125 {
126 	int ret;
127 	int seqnr = 0;
128 	struct bttv_buffer *buf;
129 	struct bttv *btv = vb2_get_drv_priv(q);
130 
131 	btv->framedrop = 0;
132 	ret = check_alloc_btres_lock(btv, RESOURCE_VBI);
133 	if (ret == 0) {
134 		if (btv->field_count)
135 			seqnr++;
136 		while (!list_empty(&btv->vcapture)) {
137 			buf = list_entry(btv->vcapture.next,
138 					 struct bttv_buffer, list);
139 			list_del(&buf->list);
140 			buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
141 			vb2_buffer_done(&buf->vbuf.vb2_buf,
142 					VB2_BUF_STATE_QUEUED);
143 		}
144 		return !ret;
145 	}
146 	if (!vb2_is_streaming(&btv->capq)) {
147 		init_irqreg(btv);
148 		btv->field_count = 0;
149 	}
150 	return !ret;
151 }
152 
153 static void stop_streaming_vbi(struct vb2_queue *q)
154 {
155 	struct bttv *btv = vb2_get_drv_priv(q);
156 	unsigned long flags;
157 
158 	vb2_wait_for_all_buffers(q);
159 	spin_lock_irqsave(&btv->s_lock, flags);
160 	free_btres_lock(btv, RESOURCE_VBI);
161 	if (!vb2_is_streaming(&btv->capq)) {
162 		/* stop field counter */
163 		btand(~BT848_INT_VSYNC, BT848_INT_MASK);
164 	}
165 	spin_unlock_irqrestore(&btv->s_lock, flags);
166 }
167 
168 const struct vb2_ops bttv_vbi_qops = {
169 	.queue_setup    = queue_setup_vbi,
170 	.buf_queue      = buf_queue_vbi,
171 	.buf_prepare    = buf_prepare_vbi,
172 	.buf_cleanup	= buf_cleanup_vbi,
173 	.start_streaming = start_streaming_vbi,
174 	.stop_streaming = stop_streaming_vbi,
175 	.wait_prepare   = vb2_ops_wait_prepare,
176 	.wait_finish    = vb2_ops_wait_finish,
177 };
178 
179 /* ----------------------------------------------------------------------- */
180 
181 static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
182 			__s32 crop_start)
183 {
184 	__s32 min_start, max_start, max_end, f2_offset;
185 	unsigned int i;
186 
187 	/* For compatibility with earlier driver versions we must pretend
188 	   the VBI and video capture window may overlap. In reality RISC
189 	   magic aborts VBI capturing at the first line of video capturing,
190 	   leaving the rest of the buffer unchanged, usually all zero.
191 	   VBI capturing must always start before video capturing. >> 1
192 	   because cropping counts field lines times two. */
193 	min_start = tvnorm->vbistart[0];
194 	max_start = (crop_start >> 1) - 1;
195 	max_end = (tvnorm->cropcap.bounds.top
196 		   + tvnorm->cropcap.bounds.height) >> 1;
197 
198 	if (min_start > max_start)
199 		return -EBUSY;
200 
201 	WARN_ON(max_start >= max_end);
202 
203 	f->sampling_rate    = tvnorm->Fsc;
204 	f->samples_per_line = VBI_BPL;
205 	f->sample_format    = V4L2_PIX_FMT_GREY;
206 	f->offset           = VBI_OFFSET;
207 
208 	f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
209 
210 	for (i = 0; i < 2; ++i) {
211 		if (0 == f->count[i]) {
212 			/* No data from this field. We leave f->start[i]
213 			   alone because VIDIOCSVBIFMT is w/o and EINVALs
214 			   when a driver does not support exactly the
215 			   requested parameters. */
216 		} else {
217 			s64 start, count;
218 
219 			start = clamp(f->start[i], min_start, max_start);
220 			/* s64 to prevent overflow. */
221 			count = (s64) f->start[i] + f->count[i] - start;
222 			f->start[i] = start;
223 			f->count[i] = clamp(count, (s64) 1,
224 					    max_end - start);
225 		}
226 
227 		min_start += f2_offset;
228 		max_start += f2_offset;
229 		max_end += f2_offset;
230 	}
231 
232 	if (0 == (f->count[0] | f->count[1])) {
233 		/* As in earlier driver versions. */
234 		f->start[0] = tvnorm->vbistart[0];
235 		f->start[1] = tvnorm->vbistart[1];
236 		f->count[0] = 1;
237 		f->count[1] = 1;
238 	}
239 
240 	f->flags = 0;
241 
242 	f->reserved[0] = 0;
243 	f->reserved[1] = 0;
244 
245 	return 0;
246 }
247 
248 int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
249 {
250 	struct bttv *btv = video_drvdata(file);
251 	const struct bttv_tvnorm *tvnorm;
252 	__s32 crop_start;
253 
254 	mutex_lock(&btv->lock);
255 
256 	tvnorm = &bttv_tvnorms[btv->tvnorm];
257 	crop_start = btv->crop_start;
258 
259 	mutex_unlock(&btv->lock);
260 
261 	return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
262 }
263 
264 
265 int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
266 {
267 	struct bttv *btv = video_drvdata(file);
268 	const struct bttv_tvnorm *tvnorm;
269 	__s32 start1, end;
270 	int rc;
271 
272 	mutex_lock(&btv->lock);
273 
274 	rc = -EBUSY;
275 	if (btv->resources & RESOURCE_VBI)
276 		goto fail;
277 
278 	tvnorm = &bttv_tvnorms[btv->tvnorm];
279 
280 	rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
281 	if (0 != rc)
282 		goto fail;
283 
284 	start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
285 		tvnorm->vbistart[0];
286 
287 	/* First possible line of video capturing. Should be
288 	   max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
289 	   when capturing both fields. But for compatibility we must
290 	   pretend the VBI and video capture window may overlap,
291 	   so end = start + 1, the lowest possible value, times two
292 	   because vbi_fmt.end counts field lines times two. */
293 	end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
294 
295 	btv->vbi_fmt.fmt = frt->fmt.vbi;
296 	btv->vbi_fmt.tvnorm = tvnorm;
297 	btv->vbi_fmt.end = end;
298 
299 	rc = 0;
300 
301  fail:
302 	mutex_unlock(&btv->lock);
303 
304 	return rc;
305 }
306 
307 
308 int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
309 {
310 	const struct bttv_tvnorm *tvnorm;
311 	struct bttv *btv = video_drvdata(file);
312 
313 	frt->fmt.vbi = btv->vbi_fmt.fmt;
314 
315 	tvnorm = &bttv_tvnorms[btv->tvnorm];
316 
317 	if (tvnorm != btv->vbi_fmt.tvnorm) {
318 		__s32 max_end;
319 		unsigned int i;
320 
321 		/* As in vbi_buffer_prepare() this imitates the
322 		   behaviour of earlier driver versions after video
323 		   standard changes, with default parameters anyway. */
324 
325 		max_end = (tvnorm->cropcap.bounds.top
326 			   + tvnorm->cropcap.bounds.height) >> 1;
327 
328 		frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
329 
330 		for (i = 0; i < 2; ++i) {
331 			__s32 new_start;
332 
333 			new_start = frt->fmt.vbi.start[i] + tvnorm->vbistart[i]
334 				- btv->vbi_fmt.tvnorm->vbistart[i];
335 
336 			frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
337 			frt->fmt.vbi.count[i] =
338 				min((__s32) frt->fmt.vbi.count[i],
339 					  max_end - frt->fmt.vbi.start[i]);
340 
341 			max_end += tvnorm->vbistart[1]
342 				- tvnorm->vbistart[0];
343 		}
344 	}
345 	return 0;
346 }
347 
348 void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
349 {
350 	const struct bttv_tvnorm *tvnorm;
351 	unsigned int real_samples_per_line;
352 	unsigned int real_count;
353 
354 	tvnorm = &bttv_tvnorms[norm];
355 
356 	f->fmt.sampling_rate    = tvnorm->Fsc;
357 	f->fmt.samples_per_line = VBI_BPL;
358 	f->fmt.sample_format    = V4L2_PIX_FMT_GREY;
359 	f->fmt.offset           = VBI_OFFSET;
360 	f->fmt.start[0]		= tvnorm->vbistart[0];
361 	f->fmt.start[1]		= tvnorm->vbistart[1];
362 	f->fmt.count[0]		= VBI_DEFLINES;
363 	f->fmt.count[1]		= VBI_DEFLINES;
364 	f->fmt.flags            = 0;
365 	f->fmt.reserved[0]      = 0;
366 	f->fmt.reserved[1]      = 0;
367 
368 	/* For compatibility the buffer size must be 2 * VBI_DEFLINES *
369 	   VBI_BPL regardless of the current video standard. */
370 	real_samples_per_line   = 1024 + tvnorm->vbipack * 4;
371 	real_count              = ((tvnorm->cropcap.defrect.top >> 1)
372 				   - tvnorm->vbistart[0]);
373 
374 	WARN_ON(real_samples_per_line > VBI_BPL);
375 	WARN_ON(real_count > VBI_DEFLINES);
376 
377 	f->tvnorm               = tvnorm;
378 
379 	/* See bttv_vbi_fmt_set(). */
380 	f->end                  = tvnorm->vbistart[0] * 2 + 2;
381 }
382