cx88-vbi.c 6.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

#include "cx88.h"

9
static unsigned int vbi_debug;
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17
module_param(vbi_debug,int,0644);
MODULE_PARM_DESC(vbi_debug,"enable debug messages [vbi]");

#define dprintk(level,fmt, arg...)	if (vbi_debug >= level) \
	printk(KERN_DEBUG "%s: " fmt, dev->core->name , ## arg)

/* ------------------------------------------------------------------ */

18 19
int cx8800_vbi_fmt (struct file *file, void *priv,
					struct v4l2_format *f)
L
Linus Torvalds 已提交
20
{
H
Hans Verkuil 已提交
21
	struct cx8800_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
22 23 24 25 26

	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	f->fmt.vbi.offset = 244;

27
	if (dev->core->tvnorm & V4L2_STD_525_60) {
L
Linus Torvalds 已提交
28 29
		/* ntsc */
		f->fmt.vbi.sampling_rate = 28636363;
30 31
		f->fmt.vbi.start[0] = 10;
		f->fmt.vbi.start[1] = 273;
H
Hans Verkuil 已提交
32 33
		f->fmt.vbi.count[0] = VBI_LINE_NTSC_COUNT;
		f->fmt.vbi.count[1] = VBI_LINE_NTSC_COUNT;
L
Linus Torvalds 已提交
34

35
	} else if (dev->core->tvnorm & V4L2_STD_625_50) {
L
Linus Torvalds 已提交
36 37
		/* pal */
		f->fmt.vbi.sampling_rate = 35468950;
H
Hans Verkuil 已提交
38 39 40 41
		f->fmt.vbi.start[0] = V4L2_VBI_ITU_625_F1_START + 5;
		f->fmt.vbi.start[1] = V4L2_VBI_ITU_625_F2_START + 5;
		f->fmt.vbi.count[0] = VBI_LINE_PAL_COUNT;
		f->fmt.vbi.count[1] = VBI_LINE_PAL_COUNT;
L
Linus Torvalds 已提交
42
	}
43
	return 0;
L
Linus Torvalds 已提交
44 45
}

A
Adrian Bunk 已提交
46
static int cx8800_start_vbi_dma(struct cx8800_dev    *dev,
47 48
			 struct cx88_dmaqueue *q,
			 struct cx88_buffer   *buf)
L
Linus Torvalds 已提交
49 50 51 52 53
{
	struct cx88_core *core = dev->core;

	/* setup fifo + format */
	cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH24],
H
Hans Verkuil 已提交
54
				VBI_LINE_LENGTH, buf->risc.dma);
L
Linus Torvalds 已提交
55 56 57 58 59 60 61

	cx_write(MO_VBOS_CONTROL, ( (1 << 18) |  // comb filter delay fixup
				    (1 << 15) |  // enable vbi capture
				    (1 << 11) ));

	/* reset counter */
	cx_write(MO_VBI_GPCNTRL, GP_COUNT_CONTROL_RESET);
62
	q->count = 0;
L
Linus Torvalds 已提交
63 64

	/* enable irqs */
65
	cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77
	cx_set(MO_VID_INTMSK, 0x0f0088);

	/* enable capture */
	cx_set(VID_CAPTURE_CONTROL,0x18);

	/* start dma */
	cx_set(MO_DEV_CNTRL2, (1<<5));
	cx_set(MO_VID_DMACNTRL, 0x88);

	return 0;
}

H
Hans Verkuil 已提交
78
void cx8800_stop_vbi_dma(struct cx8800_dev *dev)
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88
{
	struct cx88_core *core = dev->core;

	/* stop dma */
	cx_clear(MO_VID_DMACNTRL, 0x88);

	/* disable capture */
	cx_clear(VID_CAPTURE_CONTROL,0x18);

	/* disable irqs */
89
	cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99 100
	cx_clear(MO_VID_INTMSK, 0x0f0088);
}

int cx8800_restart_vbi_queue(struct cx8800_dev    *dev,
			     struct cx88_dmaqueue *q)
{
	struct cx88_buffer *buf;

	if (list_empty(&q->active))
		return 0;

H
Hans Verkuil 已提交
101
	buf = list_entry(q->active.next, struct cx88_buffer, list);
L
Linus Torvalds 已提交
102
	dprintk(2,"restart_queue [%p/%d]: restart dma\n",
103
		buf, buf->vb.vb2_buf.index);
L
Linus Torvalds 已提交
104 105 106 107 108 109
	cx8800_start_vbi_dma(dev, q, buf);
	return 0;
}

/* ------------------------------------------------------------------ */

110
static int queue_setup(struct vb2_queue *q,
H
Hans Verkuil 已提交
111 112
			   unsigned int *num_buffers, unsigned int *num_planes,
			   unsigned int sizes[], void *alloc_ctxs[])
L
Linus Torvalds 已提交
113
{
H
Hans Verkuil 已提交
114 115
	struct cx8800_dev *dev = q->drv_priv;

H
Hans Verkuil 已提交
116
	*num_planes = 1;
H
Hans Verkuil 已提交
117 118 119 120
	if (dev->core->tvnorm & V4L2_STD_525_60)
		sizes[0] = VBI_LINE_NTSC_COUNT * VBI_LINE_LENGTH * 2;
	else
		sizes[0] = VBI_LINE_PAL_COUNT * VBI_LINE_LENGTH * 2;
L
Linus Torvalds 已提交
121 122 123
	return 0;
}

H
Hans Verkuil 已提交
124 125

static int buffer_prepare(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
126
{
127
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
H
Hans Verkuil 已提交
128
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
129
	struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
H
Hans Verkuil 已提交
130
	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
H
Hans Verkuil 已提交
131
	unsigned int lines;
L
Linus Torvalds 已提交
132 133
	unsigned int size;

H
Hans Verkuil 已提交
134 135 136 137 138
	if (dev->core->tvnorm & V4L2_STD_525_60)
		lines = VBI_LINE_NTSC_COUNT;
	else
		lines = VBI_LINE_PAL_COUNT;
	size = lines * VBI_LINE_LENGTH * 2;
H
Hans Verkuil 已提交
139
	if (vb2_plane_size(vb, 0) < size)
L
Linus Torvalds 已提交
140
		return -EINVAL;
H
Hans Verkuil 已提交
141
	vb2_set_plane_payload(vb, 0, size);
L
Linus Torvalds 已提交
142

H
Hans Verkuil 已提交
143
	cx88_risc_buffer(dev->pci, &buf->risc, sgt->sgl,
H
Hans Verkuil 已提交
144
			 0, VBI_LINE_LENGTH * lines,
H
Hans Verkuil 已提交
145
			 VBI_LINE_LENGTH, 0,
H
Hans Verkuil 已提交
146
			 lines);
L
Linus Torvalds 已提交
147
	return 0;
H
Hans Verkuil 已提交
148
}
L
Linus Torvalds 已提交
149

H
Hans Verkuil 已提交
150 151
static void buffer_finish(struct vb2_buffer *vb)
{
152
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
H
Hans Verkuil 已提交
153
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
154
	struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
155
	struct cx88_riscmem *risc = &buf->risc;
H
Hans Verkuil 已提交
156

157 158 159
	if (risc->cpu)
		pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
	memset(risc, 0, sizeof(*risc));
L
Linus Torvalds 已提交
160 161
}

H
Hans Verkuil 已提交
162
static void buffer_queue(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
163
{
164
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
H
Hans Verkuil 已提交
165
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
166
	struct cx88_buffer    *buf = container_of(vbuf, struct cx88_buffer, vb);
L
Linus Torvalds 已提交
167 168 169
	struct cx88_buffer    *prev;
	struct cx88_dmaqueue  *q    = &dev->vbiq;

H
Hans Verkuil 已提交
170 171 172 173
	/* add jump to start */
	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
L
Linus Torvalds 已提交
174 175

	if (list_empty(&q->active)) {
H
Hans Verkuil 已提交
176
		list_add_tail(&buf->list, &q->active);
L
Linus Torvalds 已提交
177 178
		cx8800_start_vbi_dma(dev, q, buf);
		dprintk(2,"[%p/%d] vbi_queue - first active\n",
179
			buf, buf->vb.vb2_buf.index);
L
Linus Torvalds 已提交
180 181

	} else {
H
Hans Verkuil 已提交
182 183 184
		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
		prev = list_entry(q->active.prev, struct cx88_buffer, list);
		list_add_tail(&buf->list, &q->active);
L
Linus Torvalds 已提交
185 186
		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
		dprintk(2,"[%p/%d] buffer_queue - append to active\n",
187
			buf, buf->vb.vb2_buf.index);
L
Linus Torvalds 已提交
188 189 190
	}
}

H
Hans Verkuil 已提交
191
static int start_streaming(struct vb2_queue *q, unsigned int count)
L
Linus Torvalds 已提交
192
{
H
Hans Verkuil 已提交
193 194 195 196
	struct cx8800_dev *dev = q->drv_priv;
	struct cx88_dmaqueue *dmaq = &dev->vbiq;
	struct cx88_buffer *buf = list_entry(dmaq->active.next,
			struct cx88_buffer, list);
L
Linus Torvalds 已提交
197

H
Hans Verkuil 已提交
198 199
	cx8800_start_vbi_dma(dev, dmaq, buf);
	return 0;
L
Linus Torvalds 已提交
200 201
}

H
Hans Verkuil 已提交
202 203 204 205 206 207
static void stop_streaming(struct vb2_queue *q)
{
	struct cx8800_dev *dev = q->drv_priv;
	struct cx88_core *core = dev->core;
	struct cx88_dmaqueue *dmaq = &dev->vbiq;
	unsigned long flags;
L
Linus Torvalds 已提交
208

H
Hans Verkuil 已提交
209 210 211 212 213 214 215 216 217
	cx_clear(MO_VID_DMACNTRL, 0x11);
	cx_clear(VID_CAPTURE_CONTROL, 0x06);
	cx8800_stop_vbi_dma(dev);
	spin_lock_irqsave(&dev->slock, flags);
	while (!list_empty(&dmaq->active)) {
		struct cx88_buffer *buf = list_entry(dmaq->active.next,
			struct cx88_buffer, list);

		list_del(&buf->list);
218
		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
H
Hans Verkuil 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232
	}
	spin_unlock_irqrestore(&dev->slock, flags);
}

const struct vb2_ops cx8800_vbi_qops = {
	.queue_setup    = queue_setup,
	.buf_prepare  = buffer_prepare,
	.buf_finish = buffer_finish,
	.buf_queue    = buffer_queue,
	.wait_prepare = vb2_ops_wait_prepare,
	.wait_finish = vb2_ops_wait_finish,
	.start_streaming = start_streaming,
	.stop_streaming = stop_streaming,
};