2 * cx18 init/start/stop/exit stream functions
4 * Derived from ivtv-streams.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "cx18-driver.h"
27 #include "cx18-fileops.h"
28 #include "cx18-mailbox.h"
30 #include "cx18-queue.h"
31 #include "cx18-ioctl.h"
32 #include "cx18-streams.h"
33 #include "cx18-cards.h"
35 #include "cx18-av-core.h"
38 #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
40 static struct file_operations cx18_v4l2_enc_fops = {
42 .read = cx18_v4l2_read,
43 .open = cx18_v4l2_open,
44 /* FIXME change to video_ioctl2 if serialization lock can be removed */
45 .ioctl = cx18_v4l2_ioctl,
46 .compat_ioctl = v4l_compat_ioctl32,
47 .release = cx18_v4l2_close,
48 .poll = cx18_v4l2_enc_poll,
51 /* offset from 0 to register ts v4l2 minors on */
52 #define CX18_V4L2_ENC_TS_OFFSET 16
53 /* offset from 0 to register pcm v4l2 minors on */
54 #define CX18_V4L2_ENC_PCM_OFFSET 24
55 /* offset from 0 to register yuv v4l2 minors on */
56 #define CX18_V4L2_ENC_YUV_OFFSET 32
63 enum v4l2_buf_type buf_type;
64 struct file_operations *fops;
65 } cx18_stream_info[] = {
66 { /* CX18_ENC_STREAM_TYPE_MPG */
69 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
72 { /* CX18_ENC_STREAM_TYPE_TS */
75 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
78 { /* CX18_ENC_STREAM_TYPE_YUV */
80 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
81 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
84 { /* CX18_ENC_STREAM_TYPE_VBI */
87 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
90 { /* CX18_ENC_STREAM_TYPE_PCM */
92 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
93 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
96 { /* CX18_ENC_STREAM_TYPE_IDX */
99 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
102 { /* CX18_ENC_STREAM_TYPE_RAD */
105 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
110 static void cx18_stream_init(struct cx18 *cx, int type)
112 struct cx18_stream *s = &cx->streams[type];
113 struct video_device *dev = s->v4l2dev;
114 u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
116 /* we need to keep v4l2dev, so restore it afterwards */
117 memset(s, 0, sizeof(*s));
120 /* initialize cx18_stream fields */
123 s->name = cx18_stream_info[type].name;
124 s->handle = CX18_INVALID_TASK_HANDLE;
126 s->dma = cx18_stream_info[type].dma;
127 s->buf_size = cx->stream_buf_size[type];
129 s->buffers = max_size / s->buf_size;
130 if (s->buffers > 63) {
131 /* Each stream has a maximum of 63 buffers,
132 ensure we do not exceed that. */
134 s->buf_size = (max_size / s->buffers) & ~0xfff;
136 mutex_init(&s->qlock);
137 init_waitqueue_head(&s->waitq);
139 cx18_queue_init(&s->q_free);
140 cx18_queue_init(&s->q_full);
141 cx18_queue_init(&s->q_io);
144 static int cx18_prep_dev(struct cx18 *cx, int type)
146 struct cx18_stream *s = &cx->streams[type];
147 u32 cap = cx->v4l2_cap;
148 int num_offset = cx18_stream_info[type].num_offset;
149 int num = cx->num + cx18_first_minor + num_offset;
151 /* These four fields are always initialized. If v4l2dev == NULL, then
152 this stream is not in use. In that case no other fields but these
157 s->name = cx18_stream_info[type].name;
159 /* Check whether the radio is supported */
160 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
163 /* Check whether VBI is supported */
164 if (type == CX18_ENC_STREAM_TYPE_VBI &&
165 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
168 /* User explicitly selected 0 buffers for these streams, so don't
170 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
171 cx->options.megabytes[type] == 0) {
172 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
176 cx18_stream_init(cx, type);
178 if (num_offset == -1)
181 /* allocate and initialize the v4l2 video device structure */
182 s->v4l2dev = video_device_alloc();
183 if (s->v4l2dev == NULL) {
184 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
189 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
192 s->v4l2dev->num = num;
193 s->v4l2dev->parent = &cx->dev->dev;
194 s->v4l2dev->fops = cx18_stream_info[type].fops;
195 s->v4l2dev->release = video_device_release;
196 s->v4l2dev->tvnorms = V4L2_STD_ALL;
197 cx18_set_funcs(s->v4l2dev);
201 /* Initialize v4l2 variables and register v4l2 devices */
202 int cx18_streams_setup(struct cx18 *cx)
206 /* Setup V4L2 Devices */
207 for (type = 0; type < CX18_MAX_STREAMS; type++) {
209 ret = cx18_prep_dev(cx, type);
213 /* Allocate Stream */
214 ret = cx18_stream_alloc(&cx->streams[type]);
218 if (type == CX18_MAX_STREAMS)
221 /* One or more streams could not be initialized. Clean 'em all up. */
222 cx18_streams_cleanup(cx, 0);
226 static int cx18_reg_dev(struct cx18 *cx, int type)
228 struct cx18_stream *s = &cx->streams[type];
229 int vfl_type = cx18_stream_info[type].vfl_type;
232 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
233 * We need a VFL_TYPE_TS defined.
235 if (strcmp("TS", s->name) == 0) {
236 /* just return if no DVB is supported */
237 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
239 ret = cx18_dvb_register(s);
241 CX18_ERR("DVB failed to register\n");
246 if (s->v4l2dev == NULL)
249 num = s->v4l2dev->num;
250 /* card number + user defined offset + device offset */
251 if (type != CX18_ENC_STREAM_TYPE_MPG) {
252 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
255 num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
258 /* Register device. First try the desired minor, then any free one. */
259 ret = video_register_device(s->v4l2dev, vfl_type, num);
261 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
263 video_device_release(s->v4l2dev);
267 num = s->v4l2dev->num;
270 case VFL_TYPE_GRABBER:
271 CX18_INFO("Registered device video%d for %s (%d MB)\n",
272 num, s->name, cx->options.megabytes[type]);
276 CX18_INFO("Registered device radio%d for %s\n",
281 if (cx->options.megabytes[type])
282 CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
284 s->name, cx->options.megabytes[type]);
286 CX18_INFO("Registered device vbi%d for %s\n",
294 /* Register v4l2 devices */
295 int cx18_streams_register(struct cx18 *cx)
301 /* Register V4L2 devices */
302 for (type = 0; type < CX18_MAX_STREAMS; type++) {
303 err = cx18_reg_dev(cx, type);
311 /* One or more streams could not be initialized. Clean 'em all up. */
312 cx18_streams_cleanup(cx, 1);
316 /* Unregister v4l2 devices */
317 void cx18_streams_cleanup(struct cx18 *cx, int unregister)
319 struct video_device *vdev;
322 /* Teardown all streams */
323 for (type = 0; type < CX18_MAX_STREAMS; type++) {
324 if (cx->streams[type].dvb.enabled) {
325 cx18_dvb_unregister(&cx->streams[type]);
326 cx->streams[type].dvb.enabled = false;
329 vdev = cx->streams[type].v4l2dev;
331 cx->streams[type].v4l2dev = NULL;
335 cx18_stream_free(&cx->streams[type]);
337 /* Unregister or release device */
339 video_unregister_device(vdev);
341 video_device_release(vdev);
345 static void cx18_vbi_setup(struct cx18_stream *s)
347 struct cx18 *cx = s->cx;
348 int raw = cx->vbi.sliced_in->service_set == 0;
349 u32 data[CX2341X_MBOX_MAX_DATA];
354 cx->vbi.start[0] = 10;
355 cx->vbi.start[1] = 273;
356 } else { /* PAL/SECAM */
358 cx->vbi.start[0] = 6;
359 cx->vbi.start[1] = 318;
362 /* setup VBI registers */
363 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
365 /* determine number of lines and total number of VBI bytes.
366 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
367 The '- 1' byte is probably an unused U or V byte. Or something...
368 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
369 header, 42 data bytes + checksum (to be confirmed) */
371 lines = cx->vbi.count * 2;
373 lines = cx->is_60hz ? 24 : 38;
378 cx->vbi.enc_size = lines *
379 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
382 /* Lines per field */
383 data[1] = (lines / 2) | ((lines / 2) << 16);
385 data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
386 /* Every X number of frames a VBI interrupt arrives
387 (frames as in 25 or 30 fps) */
389 /* Setup VBI for the cx25840 digitizer */
391 data[4] = 0x20602060;
392 data[5] = 0x30703070;
394 data[4] = 0xB0F0B0F0;
395 data[5] = 0xA0E0A0E0;
398 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
399 data[0], data[1], data[2], data[3], data[4], data[5]);
401 if (s->type == CX18_ENC_STREAM_TYPE_VBI)
402 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
405 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
407 u32 data[MAX_MB_ARGUMENTS];
408 struct cx18 *cx = s->cx;
413 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
416 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
419 case CX18_ENC_STREAM_TYPE_MPG:
420 captype = CAPTURE_CHANNEL_TYPE_MPEG;
421 cx->mpg_data_received = cx->vbi_data_inserted = 0;
422 cx->dualwatch_jiffies = jiffies;
423 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
424 cx->search_pack_header = 0;
427 case CX18_ENC_STREAM_TYPE_TS:
428 captype = CAPTURE_CHANNEL_TYPE_TS;
431 case CX18_ENC_STREAM_TYPE_YUV:
432 captype = CAPTURE_CHANNEL_TYPE_YUV;
434 case CX18_ENC_STREAM_TYPE_PCM:
435 captype = CAPTURE_CHANNEL_TYPE_PCM;
437 case CX18_ENC_STREAM_TYPE_VBI:
438 captype = cx->vbi.sliced_in->service_set ?
439 CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
441 cx->vbi.inserted_frame = 0;
442 memset(cx->vbi.sliced_mpeg_size,
443 0, sizeof(cx->vbi.sliced_mpeg_size));
449 /* mute/unmute video */
450 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
451 s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
453 /* Clear Streamoff flags in case left from last capture */
454 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
456 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
458 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
460 if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
461 /* Stuff from Windows, we don't know what it is */
462 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
463 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
464 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
465 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
466 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
468 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
469 s->handle, cx->digitizer, cx->digitizer);
472 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
475 /* assign program index info.
476 Mask 7: select I/P/B, Num_req: 400 max */
477 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
479 /* Setup API for Stream */
480 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
483 if (atomic_read(&cx->tot_capturing) == 0) {
484 clear_bit(CX18_F_I_EOS, &cx->i_flags);
485 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
488 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
489 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
490 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
492 list_for_each(p, &s->q_free.list) {
493 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
495 cx18_writel(cx, buf->dma_handle,
496 &cx->scb->cpu_mdl[buf->id].paddr);
497 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
498 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
499 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
500 1, buf->id, s->buf_size);
503 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
504 CX18_DEBUG_WARN("Error starting capture!\n");
505 /* Ensure we're really not capturing before releasing MDLs */
506 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
507 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
509 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
510 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
511 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
512 /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
516 /* you're live! sit back and await interrupts :) */
518 atomic_inc(&cx->ana_capturing);
519 atomic_inc(&cx->tot_capturing);
523 void cx18_stop_all_captures(struct cx18 *cx)
527 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
528 struct cx18_stream *s = &cx->streams[i];
530 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
532 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
533 cx18_stop_v4l2_encode_stream(s, 0);
537 int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
539 struct cx18 *cx = s->cx;
542 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
545 /* This function assumes that you are allowed to stop the capture
546 and that we are actually capturing */
548 CX18_DEBUG_INFO("Stop Capture\n");
550 if (atomic_read(&cx->tot_capturing) == 0)
553 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
554 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
556 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
560 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
561 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
564 if (s->type != CX18_ENC_STREAM_TYPE_TS)
565 atomic_dec(&cx->ana_capturing);
566 atomic_dec(&cx->tot_capturing);
568 /* Clear capture and no-read bits */
569 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
571 /* Tell the CX23418 it can't use our buffers anymore */
572 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
574 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
575 s->handle = CX18_INVALID_TASK_HANDLE;
577 if (atomic_read(&cx->tot_capturing) > 0)
580 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
586 u32 cx18_find_handle(struct cx18 *cx)
590 /* find first available handle to be used for global settings */
591 for (i = 0; i < CX18_MAX_STREAMS; i++) {
592 struct cx18_stream *s = &cx->streams[i];
594 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
597 return CX18_INVALID_TASK_HANDLE;
600 struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
603 struct cx18_stream *s;
605 if (handle == CX18_INVALID_TASK_HANDLE)
608 for (i = 0; i < CX18_MAX_STREAMS; i++) {
610 if (s->handle != handle)
612 if (s->v4l2dev || s->dvb.enabled)