blob: 1f0cc79e2a33e8e75c969f7e003d62d7abd0e793 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 bttv - Bt848 frame grabber driver
4 vbi interface
5
6 (c) 2002 Gerd Knorr <kraxel@bytesex.org>
7
Michael Schimeke5bd0262007-01-18 16:17:39 -03008 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
9 Sponsored by OPQ Systems AB
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 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 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/interrupt.h>
31#include <linux/kdev_t.h>
32#include <asm/io.h>
33#include "bttvp.h"
34
Trent Piephoddecbe12006-07-26 17:08:29 -030035/* Offset from line sync pulse leading edge (0H) to start of VBI capture,
36 in fCLKx2 pixels. According to the datasheet, VBI capture starts
37 VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
38 is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
39 (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
40 Just Plain Wrong. The real value appears to be different for
41 different revisions of the bt8x8 chips, and to be affected by the
42 horizontal scaling factor. Experimentally, the value is measured
43 to be about 244. */
44#define VBI_OFFSET 244
Michael H. Schimek67f15702006-01-09 15:25:27 -020045
Michael Schimeke5bd0262007-01-18 16:17:39 -030046/* 2048 for compatibility with earlier driver versions. The driver
47 really stores 1024 + tvnorm->vbipack * 4 samples per line in the
48 buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
49 is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
50 four bytes of the VBI image. */
51#define VBI_BPL 2048
52
53/* Compatibility. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define VBI_DEFLINES 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static unsigned int vbibufs = 4;
57static unsigned int vbi_debug = 0;
58
59module_param(vbibufs, int, 0444);
60module_param(vbi_debug, int, 0644);
61MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
62MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
63
64#ifdef dprintk
65# undef dprintk
66#endif
67#define dprintk(fmt, arg...) if (vbi_debug) \
68 printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
69
Michael Schimeke5bd0262007-01-18 16:17:39 -030070#define IMAGE_SIZE(fmt) \
71 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* ----------------------------------------------------------------------- */
74/* vbi risc code + mm */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int vbi_buffer_setup(struct videobuf_queue *q,
77 unsigned int *count, unsigned int *size)
78{
79 struct bttv_fh *fh = q->priv_data;
80 struct bttv *btv = fh->btv;
81
82 if (0 == *count)
83 *count = vbibufs;
Michael Schimeke5bd0262007-01-18 16:17:39 -030084
85 *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
86
87 dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
88 fh->vbi_fmt.fmt.samples_per_line,
89 fh->vbi_fmt.fmt.start[0],
90 fh->vbi_fmt.fmt.start[1],
91 fh->vbi_fmt.fmt.count[0],
92 fh->vbi_fmt.fmt.count[1]);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95}
96
97static int vbi_buffer_prepare(struct videobuf_queue *q,
98 struct videobuf_buffer *vb,
99 enum v4l2_field field)
100{
101 struct bttv_fh *fh = q->priv_data;
102 struct bttv *btv = fh->btv;
103 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300104 const struct bttv_tvnorm *tvnorm;
105 unsigned int skip_lines0, skip_lines1, min_vdelay;
106 int redo_dma_risc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int rc;
108
Michael Schimeke5bd0262007-01-18 16:17:39 -0300109 buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
111 return -EINVAL;
112
Michael Schimeke5bd0262007-01-18 16:17:39 -0300113 tvnorm = fh->vbi_fmt.tvnorm;
114
115 /* There's no VBI_VDELAY register, RISC must skip the lines
116 we don't want. With default parameters we skip zero lines
117 as earlier driver versions did. The driver permits video
118 standard changes while capturing, so we use vbi_fmt.tvnorm
119 instead of btv->tvnorm to skip zero lines after video
120 standard changes as well. */
121
122 skip_lines0 = 0;
123 skip_lines1 = 0;
124
125 if (fh->vbi_fmt.fmt.count[0] > 0)
126 skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
127 - tvnorm->vbistart[0]));
128 if (fh->vbi_fmt.fmt.count[1] > 0)
129 skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
130 - tvnorm->vbistart[1]));
131
132 redo_dma_risc = 0;
133
134 if (buf->vbi_skip[0] != skip_lines0 ||
135 buf->vbi_skip[1] != skip_lines1 ||
136 buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
137 buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
138 buf->vbi_skip[0] = skip_lines0;
139 buf->vbi_skip[1] = skip_lines1;
140 buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
141 buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
142 redo_dma_risc = 1;
143 }
144
Brandon Philips0fc06862007-11-06 20:02:36 -0300145 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Michael Schimeke5bd0262007-01-18 16:17:39 -0300146 redo_dma_risc = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300147 if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
Michael Schimeke5bd0262007-01-18 16:17:39 -0300150
151 if (redo_dma_risc) {
152 unsigned int bpl, padding, offset;
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300153 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300154
155 bpl = 2044; /* max. vbipack */
156 padding = VBI_BPL - bpl;
157
158 if (fh->vbi_fmt.fmt.count[0] > 0) {
159 rc = bttv_risc_packed(btv, &buf->top,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300160 dma->sglist,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300161 /* offset */ 0, bpl,
162 padding, skip_lines0,
163 fh->vbi_fmt.fmt.count[0]);
164 if (0 != rc)
165 goto fail;
166 }
167
168 if (fh->vbi_fmt.fmt.count[1] > 0) {
169 offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
170
171 rc = bttv_risc_packed(btv, &buf->bottom,
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300172 dma->sglist,
Michael Schimeke5bd0262007-01-18 16:17:39 -0300173 offset, bpl,
174 padding, skip_lines1,
175 fh->vbi_fmt.fmt.count[1]);
176 if (0 != rc)
177 goto fail;
178 }
179 }
180
181 /* VBI capturing ends at VDELAY, start of video capturing,
182 no matter where the RISC program ends. VDELAY minimum is 2,
183 bounds.top is the corresponding first field line number
184 times two. VDELAY counts half field lines. */
185 min_vdelay = MIN_VDELAY;
186 if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
187 min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
188
189 /* For bttv_buffer_activate_vbi(). */
190 buf->geo.vdelay = min_vdelay;
191
Brandon Philips0fc06862007-11-06 20:02:36 -0300192 buf->vb.state = VIDEOBUF_PREPARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 buf->vb.field = field;
194 dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
195 vb, &buf->top, &buf->bottom,
196 v4l2_field_names[buf->vb.field]);
197 return 0;
198
199 fail:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300200 bttv_dma_free(q,btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return rc;
202}
203
204static void
205vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
206{
207 struct bttv_fh *fh = q->priv_data;
208 struct bttv *btv = fh->btv;
209 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
210
211 dprintk("queue %p\n",vb);
Brandon Philips0fc06862007-11-06 20:02:36 -0300212 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 list_add_tail(&buf->vb.queue,&btv->vcapture);
214 if (NULL == btv->cvbi) {
215 fh->btv->loop_irq |= 4;
216 bttv_set_dma(btv,0x0c);
217 }
218}
219
220static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
221{
222 struct bttv_fh *fh = q->priv_data;
223 struct bttv *btv = fh->btv;
224 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
225
226 dprintk("free %p\n",vb);
Michael Schimekfeaba7a2007-01-26 08:30:05 -0300227 bttv_dma_free(q,fh->btv,buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230struct videobuf_queue_ops bttv_vbi_qops = {
231 .buf_setup = vbi_buffer_setup,
232 .buf_prepare = vbi_buffer_prepare,
233 .buf_queue = vbi_buffer_queue,
234 .buf_release = vbi_buffer_release,
235};
236
237/* ----------------------------------------------------------------------- */
238
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300239static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
240 __s32 crop_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Michael Schimeke5bd0262007-01-18 16:17:39 -0300242 __s32 min_start, max_start, max_end, f2_offset;
243 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Michael Schimeke5bd0262007-01-18 16:17:39 -0300245 /* For compatibility with earlier driver versions we must pretend
246 the VBI and video capture window may overlap. In reality RISC
247 magic aborts VBI capturing at the first line of video capturing,
248 leaving the rest of the buffer unchanged, usually all zero.
249 VBI capturing must always start before video capturing. >> 1
250 because cropping counts field lines times two. */
251 min_start = tvnorm->vbistart[0];
252 max_start = (crop_start >> 1) - 1;
253 max_end = (tvnorm->cropcap.bounds.top
254 + tvnorm->cropcap.bounds.height) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Michael Schimeke5bd0262007-01-18 16:17:39 -0300256 if (min_start > max_start)
257 return -EBUSY;
258
259 BUG_ON(max_start >= max_end);
260
261 f->sampling_rate = tvnorm->Fsc;
262 f->samples_per_line = VBI_BPL;
263 f->sample_format = V4L2_PIX_FMT_GREY;
264 f->offset = VBI_OFFSET;
265
266 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
267
268 for (i = 0; i < 2; ++i) {
269 if (0 == f->count[i]) {
270 /* No data from this field. We leave f->start[i]
271 alone because VIDIOCSVBIFMT is w/o and EINVALs
272 when a driver does not support exactly the
273 requested parameters. */
274 } else {
275 s64 start, count;
276
277 start = clamp(f->start[i], min_start, max_start);
278 /* s64 to prevent overflow. */
279 count = (s64) f->start[i] + f->count[i] - start;
280 f->start[i] = start;
281 f->count[i] = clamp(count, (s64) 1,
282 max_end - start);
283 }
284
285 min_start += f2_offset;
286 max_start += f2_offset;
287 max_end += f2_offset;
288 }
289
290 if (0 == (f->count[0] | f->count[1])) {
291 /* As in earlier driver versions. */
292 f->start[0] = tvnorm->vbistart[0];
293 f->start[1] = tvnorm->vbistart[1];
294 f->count[0] = 1;
295 f->count[1] = 1;
296 }
297
298 f->flags = 0;
299
300 f->reserved[0] = 0;
301 f->reserved[1] = 0;
302
303 return 0;
304}
305
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -0300306int bttv_try_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
Michael Schimeke5bd0262007-01-18 16:17:39 -0300307{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300308 struct bttv_fh *fh = f;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300309 struct bttv *btv = fh->btv;
310 const struct bttv_tvnorm *tvnorm;
311 __s32 crop_start;
312
313 mutex_lock(&btv->lock);
314
315 tvnorm = &bttv_tvnorms[btv->tvnorm];
316 crop_start = btv->crop_start;
317
318 mutex_unlock(&btv->lock);
319
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300320 return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300321}
322
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300323
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -0300324int bttv_s_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
Michael Schimeke5bd0262007-01-18 16:17:39 -0300325{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300326 struct bttv_fh *fh = f;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300327 struct bttv *btv = fh->btv;
328 const struct bttv_tvnorm *tvnorm;
329 __s32 start1, end;
330 int rc;
331
332 mutex_lock(&btv->lock);
333
334 rc = -EBUSY;
335 if (fh->resources & RESOURCE_VBI)
336 goto fail;
337
338 tvnorm = &bttv_tvnorms[btv->tvnorm];
339
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300340 rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300341 if (0 != rc)
342 goto fail;
343
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300344 start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
345 tvnorm->vbistart[0];
Michael Schimeke5bd0262007-01-18 16:17:39 -0300346
347 /* First possible line of video capturing. Should be
348 max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
349 when capturing both fields. But for compatibility we must
350 pretend the VBI and video capture window may overlap,
351 so end = start + 1, the lowest possible value, times two
352 because vbi_fmt.end counts field lines times two. */
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300353 end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300354
355 mutex_lock(&fh->vbi.lock);
356
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300357 fh->vbi_fmt.fmt = frt->fmt.vbi;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300358 fh->vbi_fmt.tvnorm = tvnorm;
359 fh->vbi_fmt.end = end;
360
361 mutex_unlock(&fh->vbi.lock);
362
363 rc = 0;
364
365 fail:
366 mutex_unlock(&btv->lock);
367
368 return rc;
369}
370
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300371
Mauro Carvalho Chehabe5ae3db2007-12-27 22:22:59 -0300372int bttv_g_fmt_vbi(struct file *file, void *f, struct v4l2_format *frt)
Michael Schimeke5bd0262007-01-18 16:17:39 -0300373{
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300374 struct bttv_fh *fh = f;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300375 const struct bttv_tvnorm *tvnorm;
376
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300377 frt->fmt.vbi = fh->vbi_fmt.fmt;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300378
379 tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
380
381 if (tvnorm != fh->vbi_fmt.tvnorm) {
382 __s32 max_end;
383 unsigned int i;
384
385 /* As in vbi_buffer_prepare() this imitates the
386 behaviour of earlier driver versions after video
387 standard changes, with default parameters anyway. */
388
389 max_end = (tvnorm->cropcap.bounds.top
390 + tvnorm->cropcap.bounds.height) >> 1;
391
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300392 frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300393
394 for (i = 0; i < 2; ++i) {
395 __s32 new_start;
396
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300397 new_start = frt->fmt.vbi.start[i]
Michael Schimeke5bd0262007-01-18 16:17:39 -0300398 + tvnorm->vbistart[i]
399 - fh->vbi_fmt.tvnorm->vbistart[i];
400
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300401 frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
402 frt->fmt.vbi.count[i] =
403 min((__s32) frt->fmt.vbi.count[i],
404 max_end - frt->fmt.vbi.start[i]);
Michael Schimeke5bd0262007-01-18 16:17:39 -0300405
406 max_end += tvnorm->vbistart[1]
407 - tvnorm->vbistart[0];
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Douglas Schilling Landgraf402aa762007-12-27 22:20:58 -0300413void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, int norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 const struct bttv_tvnorm *tvnorm;
Michael Schimeke5bd0262007-01-18 16:17:39 -0300416 unsigned int real_samples_per_line;
417 unsigned int real_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Michael Schimeke5bd0262007-01-18 16:17:39 -0300419 tvnorm = &bttv_tvnorms[norm];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Michael Schimeke5bd0262007-01-18 16:17:39 -0300421 f->fmt.sampling_rate = tvnorm->Fsc;
422 f->fmt.samples_per_line = VBI_BPL;
423 f->fmt.sample_format = V4L2_PIX_FMT_GREY;
424 f->fmt.offset = VBI_OFFSET;
425 f->fmt.start[0] = tvnorm->vbistart[0];
426 f->fmt.start[1] = tvnorm->vbistart[1];
427 f->fmt.count[0] = VBI_DEFLINES;
428 f->fmt.count[1] = VBI_DEFLINES;
429 f->fmt.flags = 0;
430 f->fmt.reserved[0] = 0;
431 f->fmt.reserved[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Michael Schimeke5bd0262007-01-18 16:17:39 -0300433 /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
434 VBI_BPL regardless of the current video standard. */
435 real_samples_per_line = 1024 + tvnorm->vbipack * 4;
436 real_count = ((tvnorm->cropcap.defrect.top >> 1)
437 - tvnorm->vbistart[0]);
Michael H. Schimek67f15702006-01-09 15:25:27 -0200438
Michael Schimeke5bd0262007-01-18 16:17:39 -0300439 BUG_ON(real_samples_per_line > VBI_BPL);
440 BUG_ON(real_count > VBI_DEFLINES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Michael Schimeke5bd0262007-01-18 16:17:39 -0300442 f->tvnorm = tvnorm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Michael Schimeke5bd0262007-01-18 16:17:39 -0300444 /* See bttv_vbi_fmt_set(). */
445 f->end = tvnorm->vbistart[0] * 2 + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/* ----------------------------------------------------------------------- */
449/*
450 * Local variables:
451 * c-basic-offset: 8
452 * End:
453 */