1 // SPDX-License-Identifier: GPL-2.0-or-later
4 bttv - Bt848 frame grabber driver
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>
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/kdev_t.h>
22 #include <media/v4l2-ioctl.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
35 #define VBI_OFFSET 244
37 static unsigned int vbibufs = 4;
38 static unsigned int vbi_debug;
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)");
48 #define dprintk(fmt, ...) \
51 pr_debug("%d: " fmt, btv->c.nr, ##__VA_ARGS__); \
54 #define IMAGE_SIZE(fmt) \
55 (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
57 /* ----------------------------------------------------------------------- */
58 /* vbi risc code + mm */
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[])
64 struct bttv *btv = vb2_get_drv_priv(q);
65 unsigned int size = IMAGE_SIZE(&btv->vbi_fmt.fmt);
68 return sizes[0] < size ? -EINVAL : 0;
75 static void buf_queue_vbi(struct vb2_buffer *vb)
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);
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);
91 list_add_tail(&buf->list, &btv->vcapture);
92 spin_unlock_irqrestore(&btv->s_lock, flags);
95 static int buf_prepare_vbi(struct vb2_buffer *vb)
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);
104 if (vb2_plane_size(vb, 0) < size)
106 vb2_set_plane_payload(vb, 0, size);
107 buf->vbuf.field = V4L2_FIELD_NONE;
108 ret = bttv_buffer_risc_vbi(btv, buf);
113 static void buf_cleanup_vbi(struct vb2_buffer *vb)
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);
120 btcx_riscmem_free(btv->c.pci, &buf->top);
121 btcx_riscmem_free(btv->c.pci, &buf->bottom);
124 static int start_streaming_vbi(struct vb2_queue *q, unsigned int count)
128 struct bttv_buffer *buf;
129 struct bttv *btv = vb2_get_drv_priv(q);
132 ret = check_alloc_btres_lock(btv, RESOURCE_VBI);
134 if (btv->field_count)
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);
146 if (!vb2_is_streaming(&btv->capq)) {
148 btv->field_count = 0;
153 static void stop_streaming_vbi(struct vb2_queue *q)
155 struct bttv *btv = vb2_get_drv_priv(q);
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);
165 spin_unlock_irqrestore(&btv->s_lock, flags);
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,
179 /* ----------------------------------------------------------------------- */
181 static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
184 __s32 min_start, max_start, max_end, f2_offset;
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;
198 if (min_start > max_start)
201 WARN_ON(max_start >= max_end);
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;
208 f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
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. */
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;
223 f->count[i] = clamp(count, (s64) 1,
227 min_start += f2_offset;
228 max_start += f2_offset;
229 max_end += f2_offset;
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];
248 int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
250 struct bttv *btv = video_drvdata(file);
251 const struct bttv_tvnorm *tvnorm;
254 mutex_lock(&btv->lock);
256 tvnorm = &bttv_tvnorms[btv->tvnorm];
257 crop_start = btv->crop_start;
259 mutex_unlock(&btv->lock);
261 return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
265 int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
267 struct bttv *btv = video_drvdata(file);
268 const struct bttv_tvnorm *tvnorm;
272 mutex_lock(&btv->lock);
275 if (btv->resources & RESOURCE_VBI)
278 tvnorm = &bttv_tvnorms[btv->tvnorm];
280 rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
284 start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
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;
295 btv->vbi_fmt.fmt = frt->fmt.vbi;
296 btv->vbi_fmt.tvnorm = tvnorm;
297 btv->vbi_fmt.end = end;
302 mutex_unlock(&btv->lock);
308 int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
310 const struct bttv_tvnorm *tvnorm;
311 struct bttv *btv = video_drvdata(file);
313 frt->fmt.vbi = btv->vbi_fmt.fmt;
315 tvnorm = &bttv_tvnorms[btv->tvnorm];
317 if (tvnorm != btv->vbi_fmt.tvnorm) {
321 /* As in vbi_buffer_prepare() this imitates the
322 behaviour of earlier driver versions after video
323 standard changes, with default parameters anyway. */
325 max_end = (tvnorm->cropcap.bounds.top
326 + tvnorm->cropcap.bounds.height) >> 1;
328 frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
330 for (i = 0; i < 2; ++i) {
333 new_start = frt->fmt.vbi.start[i] + tvnorm->vbistart[i]
334 - btv->vbi_fmt.tvnorm->vbistart[i];
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]);
341 max_end += tvnorm->vbistart[1]
342 - tvnorm->vbistart[0];
348 void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
350 const struct bttv_tvnorm *tvnorm;
351 unsigned int real_samples_per_line;
352 unsigned int real_count;
354 tvnorm = &bttv_tvnorms[norm];
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;
365 f->fmt.reserved[0] = 0;
366 f->fmt.reserved[1] = 0;
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]);
374 WARN_ON(real_samples_per_line > VBI_BPL);
375 WARN_ON(real_count > VBI_DEFLINES);
379 /* See bttv_vbi_fmt_set(). */
380 f->end = tvnorm->vbistart[0] * 2 + 2;