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@md.metrocast.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.
20 #include "cx18-driver.h"
22 #include "cx18-fileops.h"
23 #include "cx18-mailbox.h"
25 #include "cx18-queue.h"
26 #include "cx18-ioctl.h"
27 #include "cx18-streams.h"
28 #include "cx18-cards.h"
32 #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
34 static struct v4l2_file_operations cx18_v4l2_enc_fops
= {
36 .read
= cx18_v4l2_read
,
37 .open
= cx18_v4l2_open
,
38 .unlocked_ioctl
= video_ioctl2
,
39 .release
= cx18_v4l2_close
,
40 .poll
= cx18_v4l2_enc_poll
,
41 .mmap
= cx18_v4l2_mmap
,
44 /* offset from 0 to register ts v4l2 minors on */
45 #define CX18_V4L2_ENC_TS_OFFSET 16
46 /* offset from 0 to register pcm v4l2 minors on */
47 #define CX18_V4L2_ENC_PCM_OFFSET 24
48 /* offset from 0 to register yuv v4l2 minors on */
49 #define CX18_V4L2_ENC_YUV_OFFSET 32
57 } cx18_stream_info
[] = {
58 { /* CX18_ENC_STREAM_TYPE_MPG */
62 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
63 V4L2_CAP_AUDIO
| V4L2_CAP_TUNER
65 { /* CX18_ENC_STREAM_TYPE_TS */
70 { /* CX18_ENC_STREAM_TYPE_YUV */
72 VFL_TYPE_GRABBER
, CX18_V4L2_ENC_YUV_OFFSET
,
74 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
75 V4L2_CAP_STREAMING
| V4L2_CAP_AUDIO
| V4L2_CAP_TUNER
77 { /* CX18_ENC_STREAM_TYPE_VBI */
81 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_SLICED_VBI_CAPTURE
|
82 V4L2_CAP_READWRITE
| V4L2_CAP_TUNER
84 { /* CX18_ENC_STREAM_TYPE_PCM */
86 VFL_TYPE_GRABBER
, CX18_V4L2_ENC_PCM_OFFSET
,
88 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_READWRITE
,
90 { /* CX18_ENC_STREAM_TYPE_IDX */
95 { /* CX18_ENC_STREAM_TYPE_RAD */
99 V4L2_CAP_RADIO
| V4L2_CAP_TUNER
104 static void cx18_dma_free(struct videobuf_queue
*q
,
105 struct cx18_stream
*s
, struct cx18_videobuf_buffer
*buf
)
107 videobuf_waiton(q
, &buf
->vb
, 0, 0);
108 videobuf_vmalloc_free(&buf
->vb
);
109 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
112 static int cx18_prepare_buffer(struct videobuf_queue
*q
,
113 struct cx18_stream
*s
,
114 struct cx18_videobuf_buffer
*buf
,
116 unsigned int width
, unsigned int height
,
117 enum v4l2_field field
)
119 struct cx18
*cx
= s
->cx
;
125 if ((width
< 48) || (height
< 32))
128 buf
->vb
.size
= (width
* height
* 2);
129 if ((buf
->vb
.baddr
!= 0) && (buf
->vb
.bsize
< buf
->vb
.size
))
132 /* alloc + fill struct (if changed) */
133 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
134 buf
->vb
.field
!= field
|| s
->pixelformat
!= pixelformat
||
135 buf
->tvnorm
!= cx
->std
) {
137 buf
->vb
.width
= width
;
138 buf
->vb
.height
= height
;
139 buf
->vb
.field
= field
;
140 buf
->tvnorm
= cx
->std
;
141 s
->pixelformat
= pixelformat
;
143 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
144 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
145 if (s
->pixelformat
== V4L2_PIX_FMT_HM12
)
146 s
->vb_bytes_per_frame
= height
* 720 * 3 / 2;
148 s
->vb_bytes_per_frame
= height
* 720 * 2;
149 cx18_dma_free(q
, s
, buf
);
152 if ((buf
->vb
.baddr
!= 0) && (buf
->vb
.bsize
< buf
->vb
.size
))
155 if (buf
->vb
.field
== 0)
156 buf
->vb
.field
= V4L2_FIELD_INTERLACED
;
158 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
159 buf
->vb
.width
= width
;
160 buf
->vb
.height
= height
;
161 buf
->vb
.field
= field
;
162 buf
->tvnorm
= cx
->std
;
163 s
->pixelformat
= pixelformat
;
165 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
166 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
167 if (s
->pixelformat
== V4L2_PIX_FMT_HM12
)
168 s
->vb_bytes_per_frame
= height
* 720 * 3 / 2;
170 s
->vb_bytes_per_frame
= height
* 720 * 2;
171 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
175 buf
->vb
.state
= VIDEOBUF_PREPARED
;
179 cx18_dma_free(q
, s
, buf
);
184 /* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
185 1440 is a single line of 4:2:2 YUV at 720 luma samples wide
187 #define VB_MIN_BUFFERS 32
188 #define VB_MIN_BUFSIZE 4147200
190 static int buffer_setup(struct videobuf_queue
*q
,
191 unsigned int *count
, unsigned int *size
)
193 struct cx18_stream
*s
= q
->priv_data
;
194 struct cx18
*cx
= s
->cx
;
196 *size
= 2 * cx
->cxhdl
.width
* cx
->cxhdl
.height
;
198 *count
= VB_MIN_BUFFERS
;
200 while (*size
* *count
> VB_MIN_BUFFERS
* VB_MIN_BUFSIZE
)
203 q
->field
= V4L2_FIELD_INTERLACED
;
204 q
->last
= V4L2_FIELD_INTERLACED
;
209 static int buffer_prepare(struct videobuf_queue
*q
,
210 struct videobuf_buffer
*vb
,
211 enum v4l2_field field
)
213 struct cx18_videobuf_buffer
*buf
=
214 container_of(vb
, struct cx18_videobuf_buffer
, vb
);
215 struct cx18_stream
*s
= q
->priv_data
;
216 struct cx18
*cx
= s
->cx
;
218 return cx18_prepare_buffer(q
, s
, buf
, s
->pixelformat
,
219 cx
->cxhdl
.width
, cx
->cxhdl
.height
, field
);
222 static void buffer_release(struct videobuf_queue
*q
,
223 struct videobuf_buffer
*vb
)
225 struct cx18_videobuf_buffer
*buf
=
226 container_of(vb
, struct cx18_videobuf_buffer
, vb
);
227 struct cx18_stream
*s
= q
->priv_data
;
229 cx18_dma_free(q
, s
, buf
);
232 static void buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
234 struct cx18_videobuf_buffer
*buf
=
235 container_of(vb
, struct cx18_videobuf_buffer
, vb
);
236 struct cx18_stream
*s
= q
->priv_data
;
238 buf
->vb
.state
= VIDEOBUF_QUEUED
;
240 list_add_tail(&buf
->vb
.queue
, &s
->vb_capture
);
243 static struct videobuf_queue_ops cx18_videobuf_qops
= {
244 .buf_setup
= buffer_setup
,
245 .buf_prepare
= buffer_prepare
,
246 .buf_queue
= buffer_queue
,
247 .buf_release
= buffer_release
,
250 static void cx18_stream_init(struct cx18
*cx
, int type
)
252 struct cx18_stream
*s
= &cx
->streams
[type
];
254 memset(s
, 0, sizeof(*s
));
256 /* initialize cx18_stream fields */
260 s
->name
= cx18_stream_info
[type
].name
;
261 s
->handle
= CX18_INVALID_TASK_HANDLE
;
263 s
->dma
= cx18_stream_info
[type
].dma
;
264 s
->v4l2_dev_caps
= cx18_stream_info
[type
].caps
;
265 s
->buffers
= cx
->stream_buffers
[type
];
266 s
->buf_size
= cx
->stream_buf_size
[type
];
267 INIT_LIST_HEAD(&s
->buf_pool
);
269 s
->mdl_size
= s
->buf_size
* s
->bufs_per_mdl
;
271 init_waitqueue_head(&s
->waitq
);
273 spin_lock_init(&s
->q_free
.lock
);
274 cx18_queue_init(&s
->q_free
);
275 spin_lock_init(&s
->q_busy
.lock
);
276 cx18_queue_init(&s
->q_busy
);
277 spin_lock_init(&s
->q_full
.lock
);
278 cx18_queue_init(&s
->q_full
);
279 spin_lock_init(&s
->q_idle
.lock
);
280 cx18_queue_init(&s
->q_idle
);
282 INIT_WORK(&s
->out_work_order
, cx18_out_work_handler
);
284 INIT_LIST_HEAD(&s
->vb_capture
);
285 s
->vb_timeout
.function
= cx18_vb_timeout
;
286 s
->vb_timeout
.data
= (unsigned long)s
;
287 init_timer(&s
->vb_timeout
);
288 spin_lock_init(&s
->vb_lock
);
289 if (type
== CX18_ENC_STREAM_TYPE_YUV
) {
290 spin_lock_init(&s
->vbuf_q_lock
);
292 s
->vb_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
293 videobuf_queue_vmalloc_init(&s
->vbuf_q
, &cx18_videobuf_qops
,
294 &cx
->pci_dev
->dev
, &s
->vbuf_q_lock
,
295 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
296 V4L2_FIELD_INTERLACED
,
297 sizeof(struct cx18_videobuf_buffer
),
298 s
, &cx
->serialize_lock
);
300 /* Assume the previous pixel default */
301 s
->pixelformat
= V4L2_PIX_FMT_HM12
;
302 s
->vb_bytes_per_frame
= cx
->cxhdl
.height
* 720 * 3 / 2;
303 s
->vb_bytes_per_line
= 720;
307 static int cx18_prep_dev(struct cx18
*cx
, int type
)
309 struct cx18_stream
*s
= &cx
->streams
[type
];
310 u32 cap
= cx
->v4l2_cap
;
311 int num_offset
= cx18_stream_info
[type
].num_offset
;
312 int num
= cx
->instance
+ cx18_first_minor
+ num_offset
;
315 * These five fields are always initialized.
316 * For analog capture related streams, if video_dev.v4l2_dev == NULL then the
317 * stream is not in use.
318 * For the TS stream, if dvb == NULL then the stream is not in use.
319 * In those cases no other fields but these four can be used.
321 s
->video_dev
.v4l2_dev
= NULL
;
325 s
->name
= cx18_stream_info
[type
].name
;
327 /* Check whether the radio is supported */
328 if (type
== CX18_ENC_STREAM_TYPE_RAD
&& !(cap
& V4L2_CAP_RADIO
))
331 /* Check whether VBI is supported */
332 if (type
== CX18_ENC_STREAM_TYPE_VBI
&&
333 !(cap
& (V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_SLICED_VBI_CAPTURE
)))
336 /* User explicitly selected 0 buffers for these streams, so don't
338 if (cx18_stream_info
[type
].dma
!= PCI_DMA_NONE
&&
339 cx
->stream_buffers
[type
] == 0) {
340 CX18_INFO("Disabled %s device\n", cx18_stream_info
[type
].name
);
344 cx18_stream_init(cx
, type
);
346 /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
347 if (type
== CX18_ENC_STREAM_TYPE_TS
) {
348 if (cx
->card
->hw_all
& CX18_HW_DVB
) {
349 s
->dvb
= kzalloc(sizeof(struct cx18_dvb
), GFP_KERNEL
);
350 if (s
->dvb
== NULL
) {
351 CX18_ERR("Couldn't allocate cx18_dvb structure for %s\n",
356 /* Don't need buffers for the TS, if there is no DVB */
361 if (num_offset
== -1)
364 /* initialize the v4l2 video device structure */
365 snprintf(s
->video_dev
.name
, sizeof(s
->video_dev
.name
), "%s %s",
366 cx
->v4l2_dev
.name
, s
->name
);
368 s
->video_dev
.num
= num
;
369 s
->video_dev
.v4l2_dev
= &cx
->v4l2_dev
;
370 s
->video_dev
.fops
= &cx18_v4l2_enc_fops
;
371 s
->video_dev
.release
= video_device_release_empty
;
372 if (cx
->card
->video_inputs
->video_type
== CX18_CARD_INPUT_VID_TUNER
)
373 s
->video_dev
.tvnorms
= cx
->tuner_std
;
375 s
->video_dev
.tvnorms
= V4L2_STD_ALL
;
376 s
->video_dev
.lock
= &cx
->serialize_lock
;
377 cx18_set_funcs(&s
->video_dev
);
381 /* Initialize v4l2 variables and register v4l2 devices */
382 int cx18_streams_setup(struct cx18
*cx
)
386 /* Setup V4L2 Devices */
387 for (type
= 0; type
< CX18_MAX_STREAMS
; type
++) {
389 ret
= cx18_prep_dev(cx
, type
);
393 /* Allocate Stream */
394 ret
= cx18_stream_alloc(&cx
->streams
[type
]);
398 if (type
== CX18_MAX_STREAMS
)
401 /* One or more streams could not be initialized. Clean 'em all up. */
402 cx18_streams_cleanup(cx
, 0);
406 static int cx18_reg_dev(struct cx18
*cx
, int type
)
408 struct cx18_stream
*s
= &cx
->streams
[type
];
409 int vfl_type
= cx18_stream_info
[type
].vfl_type
;
413 if (type
== CX18_ENC_STREAM_TYPE_TS
&& s
->dvb
!= NULL
) {
414 ret
= cx18_dvb_register(s
);
416 CX18_ERR("DVB failed to register\n");
421 if (s
->video_dev
.v4l2_dev
== NULL
)
424 num
= s
->video_dev
.num
;
425 /* card number + user defined offset + device offset */
426 if (type
!= CX18_ENC_STREAM_TYPE_MPG
) {
427 struct cx18_stream
*s_mpg
= &cx
->streams
[CX18_ENC_STREAM_TYPE_MPG
];
429 if (s_mpg
->video_dev
.v4l2_dev
)
430 num
= s_mpg
->video_dev
.num
431 + cx18_stream_info
[type
].num_offset
;
433 video_set_drvdata(&s
->video_dev
, s
);
435 /* Register device. First try the desired minor, then any free one. */
436 ret
= video_register_device_no_warn(&s
->video_dev
, vfl_type
, num
);
438 CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
440 s
->video_dev
.v4l2_dev
= NULL
;
444 name
= video_device_node_name(&s
->video_dev
);
447 case VFL_TYPE_GRABBER
:
448 CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
449 name
, s
->name
, cx
->stream_buffers
[type
],
450 cx
->stream_buf_size
[type
] / 1024,
451 (cx
->stream_buf_size
[type
] * 100 / 1024) % 100);
455 CX18_INFO("Registered device %s for %s\n", name
, s
->name
);
459 if (cx
->stream_buffers
[type
])
460 CX18_INFO("Registered device %s for %s (%d x %d bytes)\n",
461 name
, s
->name
, cx
->stream_buffers
[type
],
462 cx
->stream_buf_size
[type
]);
464 CX18_INFO("Registered device %s for %s\n",
472 /* Register v4l2 devices */
473 int cx18_streams_register(struct cx18
*cx
)
479 /* Register V4L2 devices */
480 for (type
= 0; type
< CX18_MAX_STREAMS
; type
++) {
481 err
= cx18_reg_dev(cx
, type
);
489 /* One or more streams could not be initialized. Clean 'em all up. */
490 cx18_streams_cleanup(cx
, 1);
494 /* Unregister v4l2 devices */
495 void cx18_streams_cleanup(struct cx18
*cx
, int unregister
)
497 struct video_device
*vdev
;
500 /* Teardown all streams */
501 for (type
= 0; type
< CX18_MAX_STREAMS
; type
++) {
503 /* The TS has a cx18_dvb structure, not a video_device */
504 if (type
== CX18_ENC_STREAM_TYPE_TS
) {
505 if (cx
->streams
[type
].dvb
!= NULL
) {
507 cx18_dvb_unregister(&cx
->streams
[type
]);
508 kfree(cx
->streams
[type
].dvb
);
509 cx
->streams
[type
].dvb
= NULL
;
510 cx18_stream_free(&cx
->streams
[type
]);
515 /* No struct video_device, but can have buffers allocated */
516 if (type
== CX18_ENC_STREAM_TYPE_IDX
) {
517 /* If the module params didn't inhibit IDX ... */
518 if (cx
->stream_buffers
[type
] != 0) {
519 cx
->stream_buffers
[type
] = 0;
521 * Before calling cx18_stream_free(),
522 * check if the IDX stream was actually set up.
523 * Needed, since the cx18_probe() error path
524 * exits through here as well as normal clean up
526 if (cx
->streams
[type
].buffers
!= 0)
527 cx18_stream_free(&cx
->streams
[type
]);
532 /* If struct video_device exists, can have buffers allocated */
533 vdev
= &cx
->streams
[type
].video_dev
;
535 if (vdev
->v4l2_dev
== NULL
)
538 if (type
== CX18_ENC_STREAM_TYPE_YUV
)
539 videobuf_mmap_free(&cx
->streams
[type
].vbuf_q
);
541 cx18_stream_free(&cx
->streams
[type
]);
543 video_unregister_device(vdev
);
547 static void cx18_vbi_setup(struct cx18_stream
*s
)
549 struct cx18
*cx
= s
->cx
;
550 int raw
= cx18_raw_vbi(cx
);
551 u32 data
[CX2341X_MBOX_MAX_DATA
];
556 cx
->vbi
.start
[0] = 10;
557 cx
->vbi
.start
[1] = 273;
558 } else { /* PAL/SECAM */
560 cx
->vbi
.start
[0] = 6;
561 cx
->vbi
.start
[1] = 318;
564 /* setup VBI registers */
566 v4l2_subdev_call(cx
->sd_av
, vbi
, s_raw_fmt
, &cx
->vbi
.in
.fmt
.vbi
);
568 v4l2_subdev_call(cx
->sd_av
, vbi
, s_sliced_fmt
, &cx
->vbi
.in
.fmt
.sliced
);
571 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
572 * VBI when the first analog capture channel starts, as once it starts
573 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
574 * (i.e. for the VBI capture channels). We also send it for each
575 * analog capture channel anyway just to make sure we get the proper
579 lines
= cx
->vbi
.count
* 2;
582 * For 525/60 systems, according to the VIP 2 & BT.656 std:
583 * The EAV RP code's Field bit toggles on line 4, a few lines
584 * after the Vertcal Blank bit has already toggled.
585 * Tell the encoder to capture 21-4+1=18 lines per field,
586 * since we want lines 10 through 21.
588 * For 625/50 systems, according to the VIP 2 & BT.656 std:
589 * The EAV RP code's Field bit toggles on line 1, a few lines
590 * after the Vertcal Blank bit has already toggled.
591 * (We've actually set the digitizer so that the Field bit
592 * toggles on line 2.) Tell the encoder to capture 23-2+1=22
593 * lines per field, since we want lines 6 through 23.
595 lines
= cx
->is_60hz
? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
599 /* Lines per field */
600 data
[1] = (lines
/ 2) | ((lines
/ 2) << 16);
602 data
[2] = (raw
? VBI_ACTIVE_SAMPLES
603 : (cx
->is_60hz
? VBI_HBLANK_SAMPLES_60HZ
604 : VBI_HBLANK_SAMPLES_50HZ
));
605 /* Every X number of frames a VBI interrupt arrives
606 (frames as in 25 or 30 fps) */
609 * Set the SAV/EAV RP codes to look for as start/stop points
610 * when in VIP-1.1 mode
614 * Start codes for beginning of "active" line in vertical blank
615 * 0x20 ( VerticalBlank )
616 * 0x60 ( EvenField VerticalBlank )
618 data
[4] = 0x20602060;
620 * End codes for end of "active" raw lines and regular lines
621 * 0x30 ( VerticalBlank HorizontalBlank)
622 * 0x70 ( EvenField VerticalBlank HorizontalBlank)
623 * 0x90 (Task HorizontalBlank)
624 * 0xd0 (Task EvenField HorizontalBlank)
626 data
[5] = 0x307090d0;
629 * End codes for active video, we want data in the hblank region
630 * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
631 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
633 * Since the V bit is only allowed to toggle in the EAV RP code,
634 * just before the first active region line, these two
636 * 0x90 (Task HorizontalBlank)
637 * 0xd0 (Task EvenField HorizontalBlank)
639 * We have set the digitzer such that we don't have to worry
640 * about these problem codes.
642 data
[4] = 0xB0F0B0F0;
644 * Start codes for beginning of active line in vertical blank
645 * 0xa0 (Task VerticalBlank )
646 * 0xe0 (Task EvenField VerticalBlank )
648 data
[5] = 0xA0E0A0E0;
651 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
652 data
[0], data
[1], data
[2], data
[3], data
[4], data
[5]);
654 cx18_api(cx
, CX18_CPU_SET_RAW_VBI_PARAM
, 6, data
);
657 void cx18_stream_rotate_idx_mdls(struct cx18
*cx
)
659 struct cx18_stream
*s
= &cx
->streams
[CX18_ENC_STREAM_TYPE_IDX
];
660 struct cx18_mdl
*mdl
;
662 if (!cx18_stream_enabled(s
))
665 /* Return if the firmware is not running low on MDLs */
666 if ((atomic_read(&s
->q_free
.depth
) + atomic_read(&s
->q_busy
.depth
)) >=
667 CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN
)
670 /* Return if there are no MDLs to rotate back to the firmware */
671 if (atomic_read(&s
->q_full
.depth
) < 2)
675 * Take the oldest IDX MDL still holding data, and discard its index
676 * entries by scheduling the MDL to go back to the firmware
678 mdl
= cx18_dequeue(s
, &s
->q_full
);
680 cx18_enqueue(s
, mdl
, &s
->q_free
);
684 struct cx18_queue
*_cx18_stream_put_mdl_fw(struct cx18_stream
*s
,
685 struct cx18_mdl
*mdl
)
687 struct cx18
*cx
= s
->cx
;
688 struct cx18_queue
*q
;
690 /* Don't give it to the firmware, if we're not running a capture */
691 if (s
->handle
== CX18_INVALID_TASK_HANDLE
||
692 test_bit(CX18_F_S_STOPPING
, &s
->s_flags
) ||
693 !test_bit(CX18_F_S_STREAMING
, &s
->s_flags
))
694 return cx18_enqueue(s
, mdl
, &s
->q_free
);
696 q
= cx18_enqueue(s
, mdl
, &s
->q_busy
);
698 return q
; /* The firmware has the max MDLs it can handle */
700 cx18_mdl_sync_for_device(s
, mdl
);
701 cx18_vapi(cx
, CX18_CPU_DE_SET_MDL
, 5, s
->handle
,
702 (void __iomem
*) &cx
->scb
->cpu_mdl
[mdl
->id
] - cx
->enc_mem
,
703 s
->bufs_per_mdl
, mdl
->id
, s
->mdl_size
);
708 void _cx18_stream_load_fw_queue(struct cx18_stream
*s
)
710 struct cx18_queue
*q
;
711 struct cx18_mdl
*mdl
;
713 if (atomic_read(&s
->q_free
.depth
) == 0 ||
714 atomic_read(&s
->q_busy
.depth
) >= CX18_MAX_FW_MDLS_PER_STREAM
)
717 /* Move from q_free to q_busy notifying the firmware, until the limit */
719 mdl
= cx18_dequeue(s
, &s
->q_free
);
722 q
= _cx18_stream_put_mdl_fw(s
, mdl
);
723 } while (atomic_read(&s
->q_busy
.depth
) < CX18_MAX_FW_MDLS_PER_STREAM
727 void cx18_out_work_handler(struct work_struct
*work
)
729 struct cx18_stream
*s
=
730 container_of(work
, struct cx18_stream
, out_work_order
);
732 _cx18_stream_load_fw_queue(s
);
735 static void cx18_stream_configure_mdls(struct cx18_stream
*s
)
737 cx18_unload_queues(s
);
740 case CX18_ENC_STREAM_TYPE_YUV
:
742 * Height should be a multiple of 32 lines.
743 * Set the MDL size to the exact size needed for one frame.
744 * Use enough buffers per MDL to cover the MDL size
746 if (s
->pixelformat
== V4L2_PIX_FMT_HM12
)
747 s
->mdl_size
= 720 * s
->cx
->cxhdl
.height
* 3 / 2;
749 s
->mdl_size
= 720 * s
->cx
->cxhdl
.height
* 2;
750 s
->bufs_per_mdl
= s
->mdl_size
/ s
->buf_size
;
751 if (s
->mdl_size
% s
->buf_size
)
754 case CX18_ENC_STREAM_TYPE_VBI
:
756 if (cx18_raw_vbi(s
->cx
)) {
757 s
->mdl_size
= (s
->cx
->is_60hz
? 12 : 18)
758 * 2 * VBI_ACTIVE_SAMPLES
;
761 * See comment in cx18_vbi_setup() below about the
762 * extra lines we capture in sliced VBI mode due to
763 * the lines on which EAV RP codes toggle.
765 s
->mdl_size
= s
->cx
->is_60hz
766 ? (21 - 4 + 1) * 2 * VBI_HBLANK_SAMPLES_60HZ
767 : (23 - 2 + 1) * 2 * VBI_HBLANK_SAMPLES_50HZ
;
772 s
->mdl_size
= s
->buf_size
* s
->bufs_per_mdl
;
779 int cx18_start_v4l2_encode_stream(struct cx18_stream
*s
)
781 u32 data
[MAX_MB_ARGUMENTS
];
782 struct cx18
*cx
= s
->cx
;
784 struct cx18_stream
*s_idx
;
786 if (!cx18_stream_enabled(s
))
789 CX18_DEBUG_INFO("Start encoder stream %s\n", s
->name
);
792 case CX18_ENC_STREAM_TYPE_MPG
:
793 captype
= CAPTURE_CHANNEL_TYPE_MPEG
;
794 cx
->mpg_data_received
= cx
->vbi_data_inserted
= 0;
795 cx
->dualwatch_jiffies
= jiffies
;
796 cx
->dualwatch_stereo_mode
= v4l2_ctrl_g_ctrl(cx
->cxhdl
.audio_mode
);
797 cx
->search_pack_header
= 0;
800 case CX18_ENC_STREAM_TYPE_IDX
:
801 captype
= CAPTURE_CHANNEL_TYPE_INDEX
;
803 case CX18_ENC_STREAM_TYPE_TS
:
804 captype
= CAPTURE_CHANNEL_TYPE_TS
;
806 case CX18_ENC_STREAM_TYPE_YUV
:
807 captype
= CAPTURE_CHANNEL_TYPE_YUV
;
809 case CX18_ENC_STREAM_TYPE_PCM
:
810 captype
= CAPTURE_CHANNEL_TYPE_PCM
;
812 case CX18_ENC_STREAM_TYPE_VBI
:
813 #ifdef CX18_ENCODER_PARSES_SLICED
814 captype
= cx18_raw_vbi(cx
) ?
815 CAPTURE_CHANNEL_TYPE_VBI
: CAPTURE_CHANNEL_TYPE_SLICED_VBI
;
818 * Currently we set things up so that Sliced VBI from the
819 * digitizer is handled as Raw VBI by the encoder
821 captype
= CAPTURE_CHANNEL_TYPE_VBI
;
824 cx
->vbi
.inserted_frame
= 0;
825 memset(cx
->vbi
.sliced_mpeg_size
,
826 0, sizeof(cx
->vbi
.sliced_mpeg_size
));
832 /* Clear Streamoff flags in case left from last capture */
833 clear_bit(CX18_F_S_STREAMOFF
, &s
->s_flags
);
835 cx18_vapi_result(cx
, data
, CX18_CREATE_TASK
, 1, CPU_CMD_MASK_CAPTURE
);
837 cx18_vapi(cx
, CX18_CPU_SET_CHANNEL_TYPE
, 2, s
->handle
, captype
);
840 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
841 * set up all the parameters, as it is not obvious which parameters the
842 * firmware shares across capture channel types and which it does not.
844 * Some of the cx18_vapi() calls below apply to only certain capture
845 * channel types. We're hoping there's no harm in calling most of them
846 * anyway, as long as the values are all consistent. Setting some
847 * shared parameters will have no effect once an analog capture channel
848 * has started streaming.
850 if (captype
!= CAPTURE_CHANNEL_TYPE_TS
) {
851 cx18_vapi(cx
, CX18_CPU_SET_VER_CROP_LINE
, 2, s
->handle
, 0);
852 cx18_vapi(cx
, CX18_CPU_SET_MISC_PARAMETERS
, 3, s
->handle
, 3, 1);
853 cx18_vapi(cx
, CX18_CPU_SET_MISC_PARAMETERS
, 3, s
->handle
, 8, 0);
854 cx18_vapi(cx
, CX18_CPU_SET_MISC_PARAMETERS
, 3, s
->handle
, 4, 1);
857 * Audio related reset according to
858 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
860 if (atomic_read(&cx
->ana_capturing
) == 0)
861 cx18_vapi(cx
, CX18_CPU_SET_MISC_PARAMETERS
, 2,
865 * Number of lines for Field 1 & Field 2 according to
866 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
867 * Field 1 is 312 for 625 line systems in BT.656
868 * Field 2 is 313 for 625 line systems in BT.656
870 cx18_vapi(cx
, CX18_CPU_SET_CAPTURE_LINE_NO
, 3,
871 s
->handle
, 312, 313);
873 if (cx
->v4l2_cap
& V4L2_CAP_VBI_CAPTURE
)
877 * Select to receive I, P, and B frame index entries, if the
878 * index stream is enabled. Otherwise disable index entry
881 s_idx
= &cx
->streams
[CX18_ENC_STREAM_TYPE_IDX
];
882 cx18_vapi_result(cx
, data
, CX18_CPU_SET_INDEXTABLE
, 2,
883 s
->handle
, cx18_stream_enabled(s_idx
) ? 7 : 0);
885 /* Call out to the common CX2341x API setup for user controls */
887 cx2341x_handler_setup(&cx
->cxhdl
);
890 * When starting a capture and we're set for radio,
891 * ensure the video is muted, despite the user control.
893 if (!cx
->cxhdl
.video_mute
&&
894 test_bit(CX18_F_I_RADIO_USER
, &cx
->i_flags
))
895 cx18_vapi(cx
, CX18_CPU_SET_VIDEO_MUTE
, 2, s
->handle
,
896 (v4l2_ctrl_g_ctrl(cx
->cxhdl
.video_mute_yuv
) << 8) | 1);
898 /* Enable the Video Format Converter for UYVY 4:2:2 support,
899 * rather than the default HM12 Macroblovk 4:2:0 support.
901 if (captype
== CAPTURE_CHANNEL_TYPE_YUV
) {
902 if (s
->pixelformat
== V4L2_PIX_FMT_UYVY
)
903 cx18_vapi(cx
, CX18_CPU_SET_VFC_PARAM
, 2,
906 /* If in doubt, default to HM12 */
907 cx18_vapi(cx
, CX18_CPU_SET_VFC_PARAM
, 2,
912 if (atomic_read(&cx
->tot_capturing
) == 0) {
913 cx2341x_handler_set_busy(&cx
->cxhdl
, 1);
914 clear_bit(CX18_F_I_EOS
, &cx
->i_flags
);
915 cx18_write_reg(cx
, 7, CX18_DSP0_INTERRUPT_MASK
);
918 cx18_vapi(cx
, CX18_CPU_DE_SET_MDL_ACK
, 3, s
->handle
,
919 (void __iomem
*)&cx
->scb
->cpu_mdl_ack
[s
->type
][0] - cx
->enc_mem
,
920 (void __iomem
*)&cx
->scb
->cpu_mdl_ack
[s
->type
][1] - cx
->enc_mem
);
922 /* Init all the cpu_mdls for this stream */
923 cx18_stream_configure_mdls(s
);
924 _cx18_stream_load_fw_queue(s
);
927 if (cx18_vapi(cx
, CX18_CPU_CAPTURE_START
, 1, s
->handle
)) {
928 CX18_DEBUG_WARN("Error starting capture!\n");
929 /* Ensure we're really not capturing before releasing MDLs */
930 set_bit(CX18_F_S_STOPPING
, &s
->s_flags
);
931 if (s
->type
== CX18_ENC_STREAM_TYPE_MPG
)
932 cx18_vapi(cx
, CX18_CPU_CAPTURE_STOP
, 2, s
->handle
, 1);
934 cx18_vapi(cx
, CX18_CPU_CAPTURE_STOP
, 1, s
->handle
);
935 clear_bit(CX18_F_S_STREAMING
, &s
->s_flags
);
936 /* FIXME - CX18_F_S_STREAMOFF as well? */
937 cx18_vapi(cx
, CX18_CPU_DE_RELEASE_MDL
, 1, s
->handle
);
938 cx18_vapi(cx
, CX18_DESTROY_TASK
, 1, s
->handle
);
939 s
->handle
= CX18_INVALID_TASK_HANDLE
;
940 clear_bit(CX18_F_S_STOPPING
, &s
->s_flags
);
941 if (atomic_read(&cx
->tot_capturing
) == 0) {
942 set_bit(CX18_F_I_EOS
, &cx
->i_flags
);
943 cx18_write_reg(cx
, 5, CX18_DSP0_INTERRUPT_MASK
);
948 /* you're live! sit back and await interrupts :) */
949 if (captype
!= CAPTURE_CHANNEL_TYPE_TS
)
950 atomic_inc(&cx
->ana_capturing
);
951 atomic_inc(&cx
->tot_capturing
);
954 EXPORT_SYMBOL(cx18_start_v4l2_encode_stream
);
956 void cx18_stop_all_captures(struct cx18
*cx
)
960 for (i
= CX18_MAX_STREAMS
- 1; i
>= 0; i
--) {
961 struct cx18_stream
*s
= &cx
->streams
[i
];
963 if (!cx18_stream_enabled(s
))
965 if (test_bit(CX18_F_S_STREAMING
, &s
->s_flags
))
966 cx18_stop_v4l2_encode_stream(s
, 0);
970 int cx18_stop_v4l2_encode_stream(struct cx18_stream
*s
, int gop_end
)
972 struct cx18
*cx
= s
->cx
;
974 if (!cx18_stream_enabled(s
))
977 /* This function assumes that you are allowed to stop the capture
978 and that we are actually capturing */
980 CX18_DEBUG_INFO("Stop Capture\n");
982 if (atomic_read(&cx
->tot_capturing
) == 0)
985 set_bit(CX18_F_S_STOPPING
, &s
->s_flags
);
986 if (s
->type
== CX18_ENC_STREAM_TYPE_MPG
)
987 cx18_vapi(cx
, CX18_CPU_CAPTURE_STOP
, 2, s
->handle
, !gop_end
);
989 cx18_vapi(cx
, CX18_CPU_CAPTURE_STOP
, 1, s
->handle
);
991 if (s
->type
== CX18_ENC_STREAM_TYPE_MPG
&& gop_end
) {
992 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
995 if (s
->type
!= CX18_ENC_STREAM_TYPE_TS
)
996 atomic_dec(&cx
->ana_capturing
);
997 atomic_dec(&cx
->tot_capturing
);
999 /* Clear capture and no-read bits */
1000 clear_bit(CX18_F_S_STREAMING
, &s
->s_flags
);
1002 /* Tell the CX23418 it can't use our buffers anymore */
1003 cx18_vapi(cx
, CX18_CPU_DE_RELEASE_MDL
, 1, s
->handle
);
1005 cx18_vapi(cx
, CX18_DESTROY_TASK
, 1, s
->handle
);
1006 s
->handle
= CX18_INVALID_TASK_HANDLE
;
1007 clear_bit(CX18_F_S_STOPPING
, &s
->s_flags
);
1009 if (atomic_read(&cx
->tot_capturing
) > 0)
1012 cx2341x_handler_set_busy(&cx
->cxhdl
, 0);
1013 cx18_write_reg(cx
, 5, CX18_DSP0_INTERRUPT_MASK
);
1018 EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream
);
1020 u32
cx18_find_handle(struct cx18
*cx
)
1024 /* find first available handle to be used for global settings */
1025 for (i
= 0; i
< CX18_MAX_STREAMS
; i
++) {
1026 struct cx18_stream
*s
= &cx
->streams
[i
];
1028 if (s
->video_dev
.v4l2_dev
&& (s
->handle
!= CX18_INVALID_TASK_HANDLE
))
1031 return CX18_INVALID_TASK_HANDLE
;
1034 struct cx18_stream
*cx18_handle_to_stream(struct cx18
*cx
, u32 handle
)
1037 struct cx18_stream
*s
;
1039 if (handle
== CX18_INVALID_TASK_HANDLE
)
1042 for (i
= 0; i
< CX18_MAX_STREAMS
; i
++) {
1043 s
= &cx
->streams
[i
];
1044 if (s
->handle
!= handle
)
1046 if (cx18_stream_enabled(s
))