2 * cx18 init/start/stop/exit stream functions
4 * Derived from ivtv-streams.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include "cx18-driver.h"
25 #include "cx18-fileops.h"
26 #include "cx18-mailbox.h"
28 #include "cx18-queue.h"
29 #include "cx18-ioctl.h"
30 #include "cx18-streams.h"
31 #include "cx18-cards.h"
33 #include "cx18-av-core.h"
36 #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
38 static struct file_operations cx18_v4l2_enc_fops = {
40 .read = cx18_v4l2_read,
41 .open = cx18_v4l2_open,
42 /* FIXME change to video_ioctl2 if serialization lock can be removed */
43 .ioctl = cx18_v4l2_ioctl,
44 .compat_ioctl = v4l_compat_ioctl32,
45 .release = cx18_v4l2_close,
46 .poll = cx18_v4l2_enc_poll,
49 /* offset from 0 to register ts v4l2 minors on */
50 #define CX18_V4L2_ENC_TS_OFFSET 16
51 /* offset from 0 to register pcm v4l2 minors on */
52 #define CX18_V4L2_ENC_PCM_OFFSET 24
53 /* offset from 0 to register yuv v4l2 minors on */
54 #define CX18_V4L2_ENC_YUV_OFFSET 32
61 enum v4l2_buf_type buf_type;
62 struct file_operations *fops;
63 } cx18_stream_info[] = {
64 { /* CX18_ENC_STREAM_TYPE_MPG */
67 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
70 { /* CX18_ENC_STREAM_TYPE_TS */
73 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
76 { /* CX18_ENC_STREAM_TYPE_YUV */
78 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
79 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
82 { /* CX18_ENC_STREAM_TYPE_VBI */
85 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
88 { /* CX18_ENC_STREAM_TYPE_PCM */
90 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
91 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
94 { /* CX18_ENC_STREAM_TYPE_IDX */
97 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
100 { /* CX18_ENC_STREAM_TYPE_RAD */
103 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
108 static void cx18_stream_init(struct cx18 *cx, int type)
110 struct cx18_stream *s = &cx->streams[type];
111 struct video_device *dev = s->v4l2dev;
112 u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
114 /* we need to keep v4l2dev, so restore it afterwards */
115 memset(s, 0, sizeof(*s));
118 /* initialize cx18_stream fields */
121 s->name = cx18_stream_info[type].name;
122 s->handle = 0xffffffff;
124 s->dma = cx18_stream_info[type].dma;
125 s->buf_size = cx->stream_buf_size[type];
127 s->buffers = max_size / s->buf_size;
128 if (s->buffers > 63) {
129 /* Each stream has a maximum of 63 buffers,
130 ensure we do not exceed that. */
132 s->buf_size = (max_size / s->buffers) & ~0xfff;
134 spin_lock_init(&s->qlock);
135 init_waitqueue_head(&s->waitq);
137 cx18_queue_init(&s->q_free);
138 cx18_queue_init(&s->q_full);
139 cx18_queue_init(&s->q_io);
142 static int cx18_prep_dev(struct cx18 *cx, int type)
144 struct cx18_stream *s = &cx->streams[type];
145 u32 cap = cx->v4l2_cap;
146 int minor_offset = cx18_stream_info[type].minor_offset;
149 /* These four fields are always initialized. If v4l2dev == NULL, then
150 this stream is not in use. In that case no other fields but these
155 s->name = cx18_stream_info[type].name;
157 /* Check whether the radio is supported */
158 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
161 /* Check whether VBI is supported */
162 if (type == CX18_ENC_STREAM_TYPE_VBI &&
163 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
166 /* card number + user defined offset + device offset */
167 minor = cx->num + cx18_first_minor + minor_offset;
169 /* User explicitly selected 0 buffers for these streams, so don't
171 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
172 cx->options.megabytes[type] == 0) {
173 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
177 cx18_stream_init(cx, type);
179 if (minor_offset == -1)
182 /* allocate and initialize the v4l2 video device structure */
183 s->v4l2dev = video_device_alloc();
184 if (s->v4l2dev == NULL) {
185 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
191 VID_TYPE_CAPTURE | VID_TYPE_TUNER | VID_TYPE_TELETEXT |
192 VID_TYPE_CLIPPING | VID_TYPE_SCALES | VID_TYPE_MPEG_ENCODER;
193 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
196 s->v4l2dev->minor = minor;
197 s->v4l2dev->dev = &cx->dev->dev;
198 s->v4l2dev->fops = cx18_stream_info[type].fops;
199 s->v4l2dev->release = video_device_release;
200 s->v4l2dev->tvnorms = V4L2_STD_ALL;
201 cx18_set_funcs(s->v4l2dev);
205 /* Initialize v4l2 variables and register v4l2 devices */
206 int cx18_streams_setup(struct cx18 *cx)
210 /* Setup V4L2 Devices */
211 for (type = 0; type < CX18_MAX_STREAMS; type++) {
213 if (cx18_prep_dev(cx, type))
216 /* Allocate Stream */
217 if (cx18_stream_alloc(&cx->streams[type]))
220 if (type == CX18_MAX_STREAMS)
223 /* One or more streams could not be initialized. Clean 'em all up. */
224 cx18_streams_cleanup(cx, 0);
228 static int cx18_reg_dev(struct cx18 *cx, int type)
230 struct cx18_stream *s = &cx->streams[type];
231 int vfl_type = cx18_stream_info[type].vfl_type;
234 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
235 * We need a VFL_TYPE_TS defined.
237 if (strcmp("TS", s->name) == 0) {
238 /* just return if no DVB is supported */
239 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
241 if (cx18_dvb_register(s) < 0) {
242 CX18_ERR("DVB failed to register\n");
247 if (s->v4l2dev == NULL)
250 minor = s->v4l2dev->minor;
252 /* Register device. First try the desired minor, then any free one. */
253 if (video_register_device(s->v4l2dev, vfl_type, minor) &&
254 video_register_device(s->v4l2dev, vfl_type, -1)) {
255 CX18_ERR("Couldn't register v4l2 device for %s minor %d\n",
257 video_device_release(s->v4l2dev);
261 minor = s->v4l2dev->minor;
264 case VFL_TYPE_GRABBER:
265 CX18_INFO("Registered device video%d for %s (%d MB)\n",
266 minor, s->name, cx->options.megabytes[type]);
270 CX18_INFO("Registered device radio%d for %s\n",
271 minor - MINOR_VFL_TYPE_RADIO_MIN, s->name);
275 if (cx->options.megabytes[type])
276 CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
277 minor - MINOR_VFL_TYPE_VBI_MIN,
278 s->name, cx->options.megabytes[type]);
280 CX18_INFO("Registered device vbi%d for %s\n",
281 minor - MINOR_VFL_TYPE_VBI_MIN, s->name);
288 /* Register v4l2 devices */
289 int cx18_streams_register(struct cx18 *cx)
294 /* Register V4L2 devices */
295 for (type = 0; type < CX18_MAX_STREAMS; type++)
296 err |= cx18_reg_dev(cx, type);
301 /* One or more streams could not be initialized. Clean 'em all up. */
302 cx18_streams_cleanup(cx, 1);
306 /* Unregister v4l2 devices */
307 void cx18_streams_cleanup(struct cx18 *cx, int unregister)
309 struct video_device *vdev;
312 /* Teardown all streams */
313 for (type = 0; type < CX18_MAX_STREAMS; type++) {
314 if (cx->streams[type].dvb.enabled)
315 cx18_dvb_unregister(&cx->streams[type]);
317 vdev = cx->streams[type].v4l2dev;
319 cx->streams[type].v4l2dev = NULL;
323 cx18_stream_free(&cx->streams[type]);
325 /* Unregister or release device */
327 video_unregister_device(vdev);
329 video_device_release(vdev);
333 static void cx18_vbi_setup(struct cx18_stream *s)
335 struct cx18 *cx = s->cx;
336 int raw = cx->vbi.sliced_in->service_set == 0;
337 u32 data[CX2341X_MBOX_MAX_DATA];
342 cx->vbi.start[0] = 10;
343 cx->vbi.start[1] = 273;
344 } else { /* PAL/SECAM */
346 cx->vbi.start[0] = 6;
347 cx->vbi.start[1] = 318;
350 /* setup VBI registers */
351 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
353 /* determine number of lines and total number of VBI bytes.
354 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
355 The '- 1' byte is probably an unused U or V byte. Or something...
356 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
357 header, 42 data bytes + checksum (to be confirmed) */
359 lines = cx->vbi.count * 2;
361 lines = cx->is_60hz ? 24 : 38;
366 cx->vbi.enc_size = lines *
367 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
370 /* Lines per field */
371 data[1] = (lines / 2) | ((lines / 2) << 16);
373 data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
374 /* Every X number of frames a VBI interrupt arrives
375 (frames as in 25 or 30 fps) */
377 /* Setup VBI for the cx25840 digitizer */
379 data[4] = 0x20602060;
380 data[5] = 0x30703070;
382 data[4] = 0xB0F0B0F0;
383 data[5] = 0xA0E0A0E0;
386 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
387 data[0], data[1], data[2], data[3], data[4], data[5]);
389 if (s->type == CX18_ENC_STREAM_TYPE_VBI)
390 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
393 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
395 u32 data[MAX_MB_ARGUMENTS];
396 struct cx18 *cx = s->cx;
401 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
404 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
407 case CX18_ENC_STREAM_TYPE_MPG:
408 captype = CAPTURE_CHANNEL_TYPE_MPEG;
409 cx->mpg_data_received = cx->vbi_data_inserted = 0;
410 cx->dualwatch_jiffies = jiffies;
411 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
412 cx->search_pack_header = 0;
415 case CX18_ENC_STREAM_TYPE_TS:
416 captype = CAPTURE_CHANNEL_TYPE_TS;
419 case CX18_ENC_STREAM_TYPE_YUV:
420 captype = CAPTURE_CHANNEL_TYPE_YUV;
422 case CX18_ENC_STREAM_TYPE_PCM:
423 captype = CAPTURE_CHANNEL_TYPE_PCM;
425 case CX18_ENC_STREAM_TYPE_VBI:
426 captype = cx->vbi.sliced_in->service_set ?
427 CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
429 cx->vbi.inserted_frame = 0;
430 memset(cx->vbi.sliced_mpeg_size,
431 0, sizeof(cx->vbi.sliced_mpeg_size));
436 s->buffers_stolen = 0;
438 /* mute/unmute video */
439 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
440 s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
442 /* Clear Streamoff flags in case left from last capture */
443 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
445 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
447 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
449 if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
450 /* Stuff from Windows, we don't know what it is */
451 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
452 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
453 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
454 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
455 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
457 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
458 s->handle, cx->digitizer, cx->digitizer);
461 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
464 /* assign program index info.
465 Mask 7: select I/P/B, Num_req: 400 max */
466 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
468 /* Setup API for Stream */
469 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
472 if (atomic_read(&cx->tot_capturing) == 0) {
473 clear_bit(CX18_F_I_EOS, &cx->i_flags);
474 write_reg(7, CX18_DSP0_INTERRUPT_MASK);
477 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
478 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
479 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
481 list_for_each(p, &s->q_free.list) {
482 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
484 writel(buf->dma_handle, &cx->scb->cpu_mdl[buf->id].paddr);
485 writel(s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
486 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
487 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
488 1, buf->id, s->buf_size);
491 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
492 CX18_DEBUG_WARN("Error starting capture!\n");
493 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
497 /* you're live! sit back and await interrupts :) */
499 atomic_inc(&cx->ana_capturing);
500 atomic_inc(&cx->tot_capturing);
504 void cx18_stop_all_captures(struct cx18 *cx)
508 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
509 struct cx18_stream *s = &cx->streams[i];
511 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
513 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
514 cx18_stop_v4l2_encode_stream(s, 0);
518 int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
520 struct cx18 *cx = s->cx;
523 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
526 /* This function assumes that you are allowed to stop the capture
527 and that we are actually capturing */
529 CX18_DEBUG_INFO("Stop Capture\n");
531 if (atomic_read(&cx->tot_capturing) == 0)
534 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
535 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
537 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
541 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
542 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
545 if (s->type != CX18_ENC_STREAM_TYPE_TS)
546 atomic_dec(&cx->ana_capturing);
547 atomic_dec(&cx->tot_capturing);
549 /* Clear capture and no-read bits */
550 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
552 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
553 s->handle = 0xffffffff;
555 if (atomic_read(&cx->tot_capturing) > 0)
558 write_reg(5, CX18_DSP0_INTERRUPT_MASK);
564 u32 cx18_find_handle(struct cx18 *cx)
568 /* find first available handle to be used for global settings */
569 for (i = 0; i < CX18_MAX_STREAMS; i++) {
570 struct cx18_stream *s = &cx->streams[i];
572 if (s->v4l2dev && s->handle)