treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / pci / bt8xx / bttv-vbi.c
blobce36a2c0f60b39b9c61e97d5112785f66e43641a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 bttv - Bt848 frame grabber driver
5 vbi interface
7 (c) 2002 Gerd Knorr <kraxel@bytesex.org>
9 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
10 Sponsored by OPQ Systems AB
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
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
37 /* 2048 for compatibility with earlier driver versions. The driver
38 really stores 1024 + tvnorm->vbipack * 4 samples per line in the
39 buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
40 is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
41 four bytes of the VBI image. */
42 #define VBI_BPL 2048
44 /* Compatibility. */
45 #define VBI_DEFLINES 16
47 static unsigned int vbibufs = 4;
48 static unsigned int vbi_debug;
50 module_param(vbibufs, int, 0444);
51 module_param(vbi_debug, int, 0644);
52 MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
53 MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
55 #ifdef dprintk
56 # undef dprintk
57 #endif
58 #define dprintk(fmt, ...) \
59 do { \
60 if (vbi_debug) \
61 pr_debug("%d: " fmt, btv->c.nr, ##__VA_ARGS__); \
62 } while (0)
64 #define IMAGE_SIZE(fmt) \
65 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
67 /* ----------------------------------------------------------------------- */
68 /* vbi risc code + mm */
70 static int vbi_buffer_setup(struct videobuf_queue *q,
71 unsigned int *count, unsigned int *size)
73 struct bttv_fh *fh = q->priv_data;
74 struct bttv *btv = fh->btv;
76 if (0 == *count)
77 *count = vbibufs;
79 *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
81 dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
82 fh->vbi_fmt.fmt.samples_per_line,
83 fh->vbi_fmt.fmt.start[0],
84 fh->vbi_fmt.fmt.start[1],
85 fh->vbi_fmt.fmt.count[0],
86 fh->vbi_fmt.fmt.count[1]);
88 return 0;
91 static int vbi_buffer_prepare(struct videobuf_queue *q,
92 struct videobuf_buffer *vb,
93 enum v4l2_field field)
95 struct bttv_fh *fh = q->priv_data;
96 struct bttv *btv = fh->btv;
97 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
98 const struct bttv_tvnorm *tvnorm;
99 unsigned int skip_lines0, skip_lines1, min_vdelay;
100 int redo_dma_risc;
101 int rc;
103 buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
104 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
105 return -EINVAL;
107 tvnorm = fh->vbi_fmt.tvnorm;
109 /* There's no VBI_VDELAY register, RISC must skip the lines
110 we don't want. With default parameters we skip zero lines
111 as earlier driver versions did. The driver permits video
112 standard changes while capturing, so we use vbi_fmt.tvnorm
113 instead of btv->tvnorm to skip zero lines after video
114 standard changes as well. */
116 skip_lines0 = 0;
117 skip_lines1 = 0;
119 if (fh->vbi_fmt.fmt.count[0] > 0)
120 skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
121 - tvnorm->vbistart[0]));
122 if (fh->vbi_fmt.fmt.count[1] > 0)
123 skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
124 - tvnorm->vbistart[1]));
126 redo_dma_risc = 0;
128 if (buf->vbi_skip[0] != skip_lines0 ||
129 buf->vbi_skip[1] != skip_lines1 ||
130 buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
131 buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
132 buf->vbi_skip[0] = skip_lines0;
133 buf->vbi_skip[1] = skip_lines1;
134 buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
135 buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
136 redo_dma_risc = 1;
139 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
140 redo_dma_risc = 1;
141 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
142 goto fail;
145 if (redo_dma_risc) {
146 unsigned int bpl, padding, offset;
147 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
149 bpl = 2044; /* max. vbipack */
150 padding = VBI_BPL - bpl;
152 if (fh->vbi_fmt.fmt.count[0] > 0) {
153 rc = bttv_risc_packed(btv, &buf->top,
154 dma->sglist,
155 /* offset */ 0, bpl,
156 padding, skip_lines0,
157 fh->vbi_fmt.fmt.count[0]);
158 if (0 != rc)
159 goto fail;
162 if (fh->vbi_fmt.fmt.count[1] > 0) {
163 offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
165 rc = bttv_risc_packed(btv, &buf->bottom,
166 dma->sglist,
167 offset, bpl,
168 padding, skip_lines1,
169 fh->vbi_fmt.fmt.count[1]);
170 if (0 != rc)
171 goto fail;
175 /* VBI capturing ends at VDELAY, start of video capturing,
176 no matter where the RISC program ends. VDELAY minimum is 2,
177 bounds.top is the corresponding first field line number
178 times two. VDELAY counts half field lines. */
179 min_vdelay = MIN_VDELAY;
180 if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
181 min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
183 /* For bttv_buffer_activate_vbi(). */
184 buf->geo.vdelay = min_vdelay;
186 buf->vb.state = VIDEOBUF_PREPARED;
187 buf->vb.field = field;
188 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
189 vb, &buf->top, &buf->bottom,
190 v4l2_field_names[buf->vb.field]);
191 return 0;
193 fail:
194 bttv_dma_free(q,btv,buf);
195 return rc;
198 static void
199 vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
201 struct bttv_fh *fh = q->priv_data;
202 struct bttv *btv = fh->btv;
203 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
205 dprintk("queue %p\n",vb);
206 buf->vb.state = VIDEOBUF_QUEUED;
207 list_add_tail(&buf->vb.queue,&btv->vcapture);
208 if (NULL == btv->cvbi) {
209 fh->btv->loop_irq |= 4;
210 bttv_set_dma(btv,0x0c);
214 static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
216 struct bttv_fh *fh = q->priv_data;
217 struct bttv *btv = fh->btv;
218 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
220 dprintk("free %p\n",vb);
221 bttv_dma_free(q,fh->btv,buf);
224 const struct videobuf_queue_ops bttv_vbi_qops = {
225 .buf_setup = vbi_buffer_setup,
226 .buf_prepare = vbi_buffer_prepare,
227 .buf_queue = vbi_buffer_queue,
228 .buf_release = vbi_buffer_release,
231 /* ----------------------------------------------------------------------- */
233 static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
234 __s32 crop_start)
236 __s32 min_start, max_start, max_end, f2_offset;
237 unsigned int i;
239 /* For compatibility with earlier driver versions we must pretend
240 the VBI and video capture window may overlap. In reality RISC
241 magic aborts VBI capturing at the first line of video capturing,
242 leaving the rest of the buffer unchanged, usually all zero.
243 VBI capturing must always start before video capturing. >> 1
244 because cropping counts field lines times two. */
245 min_start = tvnorm->vbistart[0];
246 max_start = (crop_start >> 1) - 1;
247 max_end = (tvnorm->cropcap.bounds.top
248 + tvnorm->cropcap.bounds.height) >> 1;
250 if (min_start > max_start)
251 return -EBUSY;
253 BUG_ON(max_start >= max_end);
255 f->sampling_rate = tvnorm->Fsc;
256 f->samples_per_line = VBI_BPL;
257 f->sample_format = V4L2_PIX_FMT_GREY;
258 f->offset = VBI_OFFSET;
260 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
262 for (i = 0; i < 2; ++i) {
263 if (0 == f->count[i]) {
264 /* No data from this field. We leave f->start[i]
265 alone because VIDIOCSVBIFMT is w/o and EINVALs
266 when a driver does not support exactly the
267 requested parameters. */
268 } else {
269 s64 start, count;
271 start = clamp(f->start[i], min_start, max_start);
272 /* s64 to prevent overflow. */
273 count = (s64) f->start[i] + f->count[i] - start;
274 f->start[i] = start;
275 f->count[i] = clamp(count, (s64) 1,
276 max_end - start);
279 min_start += f2_offset;
280 max_start += f2_offset;
281 max_end += f2_offset;
284 if (0 == (f->count[0] | f->count[1])) {
285 /* As in earlier driver versions. */
286 f->start[0] = tvnorm->vbistart[0];
287 f->start[1] = tvnorm->vbistart[1];
288 f->count[0] = 1;
289 f->count[1] = 1;
292 f->flags = 0;
294 f->reserved[0] = 0;
295 f->reserved[1] = 0;
297 return 0;
300 int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
302 struct bttv_fh *fh = f;
303 struct bttv *btv = fh->btv;
304 const struct bttv_tvnorm *tvnorm;
305 __s32 crop_start;
307 mutex_lock(&btv->lock);
309 tvnorm = &bttv_tvnorms[btv->tvnorm];
310 crop_start = btv->crop_start;
312 mutex_unlock(&btv->lock);
314 return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
318 int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
320 struct bttv_fh *fh = f;
321 struct bttv *btv = fh->btv;
322 const struct bttv_tvnorm *tvnorm;
323 __s32 start1, end;
324 int rc;
326 mutex_lock(&btv->lock);
328 rc = -EBUSY;
329 if (fh->resources & RESOURCE_VBI)
330 goto fail;
332 tvnorm = &bttv_tvnorms[btv->tvnorm];
334 rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
335 if (0 != rc)
336 goto fail;
338 start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
339 tvnorm->vbistart[0];
341 /* First possible line of video capturing. Should be
342 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
343 when capturing both fields. But for compatibility we must
344 pretend the VBI and video capture window may overlap,
345 so end = start + 1, the lowest possible value, times two
346 because vbi_fmt.end counts field lines times two. */
347 end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
349 mutex_lock(&fh->vbi.vb_lock);
351 fh->vbi_fmt.fmt = frt->fmt.vbi;
352 fh->vbi_fmt.tvnorm = tvnorm;
353 fh->vbi_fmt.end = end;
355 mutex_unlock(&fh->vbi.vb_lock);
357 rc = 0;
359 fail:
360 mutex_unlock(&btv->lock);
362 return rc;
366 int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
368 struct bttv_fh *fh = f;
369 const struct bttv_tvnorm *tvnorm;
371 frt->fmt.vbi = fh->vbi_fmt.fmt;
373 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
375 if (tvnorm != fh->vbi_fmt.tvnorm) {
376 __s32 max_end;
377 unsigned int i;
379 /* As in vbi_buffer_prepare() this imitates the
380 behaviour of earlier driver versions after video
381 standard changes, with default parameters anyway. */
383 max_end = (tvnorm->cropcap.bounds.top
384 + tvnorm->cropcap.bounds.height) >> 1;
386 frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
388 for (i = 0; i < 2; ++i) {
389 __s32 new_start;
391 new_start = frt->fmt.vbi.start[i]
392 + tvnorm->vbistart[i]
393 - fh->vbi_fmt.tvnorm->vbistart[i];
395 frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
396 frt->fmt.vbi.count[i] =
397 min((__s32) frt->fmt.vbi.count[i],
398 max_end - frt->fmt.vbi.start[i]);
400 max_end += tvnorm->vbistart[1]
401 - tvnorm->vbistart[0];
404 return 0;
407 void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
409 const struct bttv_tvnorm *tvnorm;
410 unsigned int real_samples_per_line;
411 unsigned int real_count;
413 tvnorm = &bttv_tvnorms[norm];
415 f->fmt.sampling_rate = tvnorm->Fsc;
416 f->fmt.samples_per_line = VBI_BPL;
417 f->fmt.sample_format = V4L2_PIX_FMT_GREY;
418 f->fmt.offset = VBI_OFFSET;
419 f->fmt.start[0] = tvnorm->vbistart[0];
420 f->fmt.start[1] = tvnorm->vbistart[1];
421 f->fmt.count[0] = VBI_DEFLINES;
422 f->fmt.count[1] = VBI_DEFLINES;
423 f->fmt.flags = 0;
424 f->fmt.reserved[0] = 0;
425 f->fmt.reserved[1] = 0;
427 /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
428 VBI_BPL regardless of the current video standard. */
429 real_samples_per_line = 1024 + tvnorm->vbipack * 4;
430 real_count = ((tvnorm->cropcap.defrect.top >> 1)
431 - tvnorm->vbistart[0]);
433 BUG_ON(real_samples_per_line > VBI_BPL);
434 BUG_ON(real_count > VBI_DEFLINES);
436 f->tvnorm = tvnorm;
438 /* See bttv_vbi_fmt_set(). */
439 f->end = tvnorm->vbistart[0] * 2 + 2;