cx88-vbi.c 6.0 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 27 28

	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	f->fmt.vbi.offset = 244;
	f->fmt.vbi.count[0] = VBI_LINE_COUNT;
	f->fmt.vbi.count[1] = VBI_LINE_COUNT;

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

35
	} else if (dev->core->tvnorm & V4L2_STD_625_50) {
L
Linus Torvalds 已提交
36 37 38 39 40
		/* pal */
		f->fmt.vbi.sampling_rate = 35468950;
		f->fmt.vbi.start[0] = 7 -1;
		f->fmt.vbi.start[1] = 319 -1;
	}
41
	return 0;
L
Linus Torvalds 已提交
42 43
}

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

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

	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);
	q->count = 1;

	/* enable irqs */
63
	cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75
	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 已提交
76
void cx8800_stop_vbi_dma(struct cx8800_dev *dev)
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86
{
	struct cx88_core *core = dev->core;

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

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

	/* disable irqs */
87
	cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98
	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 已提交
99
	buf = list_entry(q->active.next, struct cx88_buffer, list);
L
Linus Torvalds 已提交
100
	dprintk(2,"restart_queue [%p/%d]: restart dma\n",
H
Hans Verkuil 已提交
101
		buf, buf->vb.v4l2_buf.index);
L
Linus Torvalds 已提交
102
	cx8800_start_vbi_dma(dev, q, buf);
H
Hans Verkuil 已提交
103
	list_for_each_entry(buf, &q->active, list)
L
Linus Torvalds 已提交
104 105 106 107 108 109
		buf->count = q->count++;
	return 0;
}

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

H
Hans Verkuil 已提交
110 111 112
static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
			   unsigned int *num_buffers, unsigned int *num_planes,
			   unsigned int sizes[], void *alloc_ctxs[])
L
Linus Torvalds 已提交
113
{
H
Hans Verkuil 已提交
114 115
	*num_planes = 1;
	sizes[0] = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
L
Linus Torvalds 已提交
116 117 118
	return 0;
}

H
Hans Verkuil 已提交
119 120

static int buffer_prepare(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
121
{
H
Hans Verkuil 已提交
122 123 124
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
	struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
L
Linus Torvalds 已提交
125 126 127 128
	unsigned int size;
	int rc;

	size = VBI_LINE_COUNT * VBI_LINE_LENGTH * 2;
H
Hans Verkuil 已提交
129
	if (vb2_plane_size(vb, 0) < size)
L
Linus Torvalds 已提交
130
		return -EINVAL;
H
Hans Verkuil 已提交
131
	vb2_set_plane_payload(vb, 0, size);
L
Linus Torvalds 已提交
132

H
Hans Verkuil 已提交
133 134 135 136 137 138 139 140
	rc = dma_map_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
	if (!rc)
		return -EIO;

	cx88_risc_buffer(dev->pci, &buf->risc, sgt->sgl,
			 0, VBI_LINE_LENGTH * VBI_LINE_COUNT,
			 VBI_LINE_LENGTH, 0,
			 VBI_LINE_COUNT);
L
Linus Torvalds 已提交
141
	return 0;
H
Hans Verkuil 已提交
142
}
L
Linus Torvalds 已提交
143

H
Hans Verkuil 已提交
144 145 146 147 148 149
static void buffer_finish(struct vb2_buffer *vb)
{
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
	struct cx88_buffer *buf = container_of(vb, struct cx88_buffer, vb);
	struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);

H
Hans Verkuil 已提交
150
	btcx_riscmem_free(dev->pci, &buf->risc);
H
Hans Verkuil 已提交
151 152

	dma_unmap_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
L
Linus Torvalds 已提交
153 154
}

H
Hans Verkuil 已提交
155
static void buffer_queue(struct vb2_buffer *vb)
L
Linus Torvalds 已提交
156
{
H
Hans Verkuil 已提交
157 158
	struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
	struct cx88_buffer    *buf = container_of(vb, struct cx88_buffer, vb);
L
Linus Torvalds 已提交
159 160 161
	struct cx88_buffer    *prev;
	struct cx88_dmaqueue  *q    = &dev->vbiq;

H
Hans Verkuil 已提交
162 163 164 165
	/* 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 已提交
166 167

	if (list_empty(&q->active)) {
H
Hans Verkuil 已提交
168
		list_add_tail(&buf->list, &q->active);
L
Linus Torvalds 已提交
169 170 171
		cx8800_start_vbi_dma(dev, q, buf);
		buf->count    = q->count++;
		dprintk(2,"[%p/%d] vbi_queue - first active\n",
H
Hans Verkuil 已提交
172
			buf, buf->vb.v4l2_buf.index);
L
Linus Torvalds 已提交
173 174

	} else {
H
Hans Verkuil 已提交
175 176 177
		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 已提交
178 179 180
		buf->count    = q->count++;
		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
		dprintk(2,"[%p/%d] buffer_queue - append to active\n",
H
Hans Verkuil 已提交
181
			buf, buf->vb.v4l2_buf.index);
L
Linus Torvalds 已提交
182 183 184
	}
}

H
Hans Verkuil 已提交
185
static int start_streaming(struct vb2_queue *q, unsigned int count)
L
Linus Torvalds 已提交
186
{
H
Hans Verkuil 已提交
187 188 189 190
	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 已提交
191

H
Hans Verkuil 已提交
192 193
	cx8800_start_vbi_dma(dev, dmaq, buf);
	return 0;
L
Linus Torvalds 已提交
194 195
}

H
Hans Verkuil 已提交
196 197 198 199 200 201
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 已提交
202

H
Hans Verkuil 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);

	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);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}
	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,
};