blob: 8c8e92b20c7b99c46cbea93a2f93018e3f4d3db0 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 init/start/stop/exit stream functions
3 *
4 * Derived from ivtv-streams.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
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.
13 *
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.
18 *
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
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030026#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-fileops.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-ioctl.h"
32#include "cx18-streams.h"
33#include "cx18-cards.h"
34#include "cx18-scb.h"
35#include "cx18-av-core.h"
36#include "cx18-dvb.h"
37
38#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
39
40static struct file_operations cx18_v4l2_enc_fops = {
Hans Verkuildaf20d92008-05-12 11:21:58 -030041 .owner = THIS_MODULE,
42 .read = cx18_v4l2_read,
43 .open = cx18_v4l2_open,
Andy Walls3b6fe582008-06-21 08:36:31 -030044 /* FIXME change to video_ioctl2 if serialization lock can be removed */
Hans Verkuildaf20d92008-05-12 11:21:58 -030045 .ioctl = cx18_v4l2_ioctl,
46 .compat_ioctl = v4l_compat_ioctl32,
47 .release = cx18_v4l2_close,
48 .poll = cx18_v4l2_enc_poll,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030049};
50
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
57
58static struct {
59 const char *name;
60 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030061 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030062 int dma;
63 enum v4l2_buf_type buf_type;
64 struct file_operations *fops;
65} cx18_stream_info[] = {
66 { /* CX18_ENC_STREAM_TYPE_MPG */
67 "encoder MPEG",
68 VFL_TYPE_GRABBER, 0,
69 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
70 &cx18_v4l2_enc_fops
71 },
72 { /* CX18_ENC_STREAM_TYPE_TS */
73 "TS",
74 VFL_TYPE_GRABBER, -1,
75 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
76 &cx18_v4l2_enc_fops
77 },
78 { /* CX18_ENC_STREAM_TYPE_YUV */
79 "encoder YUV",
80 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
81 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
82 &cx18_v4l2_enc_fops
83 },
84 { /* CX18_ENC_STREAM_TYPE_VBI */
85 "encoder VBI",
86 VFL_TYPE_VBI, 0,
87 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
88 &cx18_v4l2_enc_fops
89 },
90 { /* CX18_ENC_STREAM_TYPE_PCM */
91 "encoder PCM audio",
92 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
93 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
94 &cx18_v4l2_enc_fops
95 },
96 { /* CX18_ENC_STREAM_TYPE_IDX */
97 "encoder IDX",
98 VFL_TYPE_GRABBER, -1,
99 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
100 &cx18_v4l2_enc_fops
101 },
102 { /* CX18_ENC_STREAM_TYPE_RAD */
103 "encoder radio",
104 VFL_TYPE_RADIO, 0,
105 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
106 &cx18_v4l2_enc_fops
107 },
108};
109
110static void cx18_stream_init(struct cx18 *cx, int type)
111{
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;
115
116 /* we need to keep v4l2dev, so restore it afterwards */
117 memset(s, 0, sizeof(*s));
118 s->v4l2dev = dev;
119
120 /* initialize cx18_stream fields */
121 s->cx = cx;
122 s->type = type;
123 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300124 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300125
126 s->dma = cx18_stream_info[type].dma;
127 s->buf_size = cx->stream_buf_size[type];
128 if (s->buf_size)
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. */
133 s->buffers = 63;
134 s->buf_size = (max_size / s->buffers) & ~0xfff;
135 }
Andy Wallsf576cee2008-11-16 20:18:05 -0300136 mutex_init(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137 init_waitqueue_head(&s->waitq);
138 s->id = -1;
139 cx18_queue_init(&s->q_free);
140 cx18_queue_init(&s->q_full);
141 cx18_queue_init(&s->q_io);
142}
143
144static int cx18_prep_dev(struct cx18 *cx, int type)
145{
146 struct cx18_stream *s = &cx->streams[type];
147 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300148 int num_offset = cx18_stream_info[type].num_offset;
149 int num = cx->num + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300150
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
153 four can be used. */
154 s->v4l2dev = NULL;
155 s->cx = cx;
156 s->type = type;
157 s->name = cx18_stream_info[type].name;
158
159 /* Check whether the radio is supported */
160 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
161 return 0;
162
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)))
166 return 0;
167
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300168 /* User explicitly selected 0 buffers for these streams, so don't
169 create them. */
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);
173 return 0;
174 }
175
176 cx18_stream_init(cx, type);
177
Hans Verkuildd896012008-10-04 08:36:54 -0300178 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300179 return 0;
180
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",
185 s->name);
186 return -ENOMEM;
187 }
188
Hans Verkuil37f89f92008-06-22 11:57:31 -0300189 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
190 cx->num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300191
Hans Verkuildd896012008-10-04 08:36:54 -0300192 s->v4l2dev->num = num;
Hans Verkuil5e85e732008-07-20 06:31:39 -0300193 s->v4l2dev->parent = &cx->dev->dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300194 s->v4l2dev->fops = cx18_stream_info[type].fops;
195 s->v4l2dev->release = video_device_release;
Andy Walls3b6fe582008-06-21 08:36:31 -0300196 s->v4l2dev->tvnorms = V4L2_STD_ALL;
197 cx18_set_funcs(s->v4l2dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300198 return 0;
199}
200
201/* Initialize v4l2 variables and register v4l2 devices */
202int cx18_streams_setup(struct cx18 *cx)
203{
Andy Walls9b4a7c82008-10-18 10:20:25 -0300204 int type, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300205
206 /* Setup V4L2 Devices */
207 for (type = 0; type < CX18_MAX_STREAMS; type++) {
208 /* Prepare device */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300209 ret = cx18_prep_dev(cx, type);
210 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300211 break;
212
213 /* Allocate Stream */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300214 ret = cx18_stream_alloc(&cx->streams[type]);
215 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300216 break;
217 }
218 if (type == CX18_MAX_STREAMS)
219 return 0;
220
221 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300222 cx18_streams_cleanup(cx, 0);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300223 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300224}
225
226static int cx18_reg_dev(struct cx18 *cx, int type)
227{
228 struct cx18_stream *s = &cx->streams[type];
229 int vfl_type = cx18_stream_info[type].vfl_type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300230 int num, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300231
232 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
233 * We need a VFL_TYPE_TS defined.
234 */
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)
238 return 0;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300239 ret = cx18_dvb_register(s);
240 if (ret < 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300241 CX18_ERR("DVB failed to register\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300242 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300243 }
244 }
245
246 if (s->v4l2dev == NULL)
247 return 0;
248
Hans Verkuildd896012008-10-04 08:36:54 -0300249 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];
253
254 if (s_mpg->v4l2dev)
255 num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
256 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300257
258 /* Register device. First try the desired minor, then any free one. */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300259 ret = video_register_device(s->v4l2dev, vfl_type, num);
260 if (ret < 0) {
Hans Verkuildd896012008-10-04 08:36:54 -0300261 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
262 s->name, num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300263 video_device_release(s->v4l2dev);
264 s->v4l2dev = NULL;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300265 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300266 }
Hans Verkuildd896012008-10-04 08:36:54 -0300267 num = s->v4l2dev->num;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300268
269 switch (vfl_type) {
270 case VFL_TYPE_GRABBER:
271 CX18_INFO("Registered device video%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300272 num, s->name, cx->options.megabytes[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300273 break;
274
275 case VFL_TYPE_RADIO:
276 CX18_INFO("Registered device radio%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300277 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300278 break;
279
280 case VFL_TYPE_VBI:
281 if (cx->options.megabytes[type])
282 CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300283 num,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300284 s->name, cx->options.megabytes[type]);
285 else
286 CX18_INFO("Registered device vbi%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300287 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300288 break;
289 }
290
291 return 0;
292}
293
294/* Register v4l2 devices */
295int cx18_streams_register(struct cx18 *cx)
296{
297 int type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300298 int err;
299 int ret = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300300
301 /* Register V4L2 devices */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300302 for (type = 0; type < CX18_MAX_STREAMS; type++) {
303 err = cx18_reg_dev(cx, type);
304 if (err && ret == 0)
305 ret = err;
306 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300307
Andy Walls9b4a7c82008-10-18 10:20:25 -0300308 if (ret == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300309 return 0;
310
311 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300312 cx18_streams_cleanup(cx, 1);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300313 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300314}
315
316/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300317void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300318{
319 struct video_device *vdev;
320 int type;
321
322 /* Teardown all streams */
323 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Hans Verkuilfac36392008-07-18 10:07:10 -0300324 if (cx->streams[type].dvb.enabled) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300325 cx18_dvb_unregister(&cx->streams[type]);
Hans Verkuilfac36392008-07-18 10:07:10 -0300326 cx->streams[type].dvb.enabled = false;
327 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300328
329 vdev = cx->streams[type].v4l2dev;
330
331 cx->streams[type].v4l2dev = NULL;
332 if (vdev == NULL)
333 continue;
334
335 cx18_stream_free(&cx->streams[type]);
336
Hans Verkuil3f983872008-05-01 10:31:12 -0300337 /* Unregister or release device */
338 if (unregister)
339 video_unregister_device(vdev);
340 else
341 video_device_release(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300342 }
343}
344
345static void cx18_vbi_setup(struct cx18_stream *s)
346{
347 struct cx18 *cx = s->cx;
348 int raw = cx->vbi.sliced_in->service_set == 0;
349 u32 data[CX2341X_MBOX_MAX_DATA];
350 int lines;
351
352 if (cx->is_60hz) {
353 cx->vbi.count = 12;
354 cx->vbi.start[0] = 10;
355 cx->vbi.start[1] = 273;
356 } else { /* PAL/SECAM */
357 cx->vbi.count = 18;
358 cx->vbi.start[0] = 6;
359 cx->vbi.start[1] = 318;
360 }
361
362 /* setup VBI registers */
363 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
364
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) */
370 if (raw) {
371 lines = cx->vbi.count * 2;
372 } else {
373 lines = cx->is_60hz ? 24 : 38;
374 if (cx->is_60hz)
375 lines += 2;
376 }
377
378 cx->vbi.enc_size = lines *
379 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
380
381 data[0] = s->handle;
382 /* Lines per field */
383 data[1] = (lines / 2) | ((lines / 2) << 16);
384 /* bytes per line */
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) */
388 data[3] = 1;
389 /* Setup VBI for the cx25840 digitizer */
390 if (raw) {
391 data[4] = 0x20602060;
392 data[5] = 0x30703070;
393 } else {
394 data[4] = 0xB0F0B0F0;
395 data[5] = 0xA0E0A0E0;
396 }
397
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]);
400
401 if (s->type == CX18_ENC_STREAM_TYPE_VBI)
402 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
403}
404
405int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
406{
407 u32 data[MAX_MB_ARGUMENTS];
408 struct cx18 *cx = s->cx;
409 struct list_head *p;
410 int ts = 0;
411 int captype = 0;
412
413 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
414 return -EINVAL;
415
416 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
417
418 switch (s->type) {
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;
425 break;
426
427 case CX18_ENC_STREAM_TYPE_TS:
428 captype = CAPTURE_CHANNEL_TYPE_TS;
429 ts = 1;
430 break;
431 case CX18_ENC_STREAM_TYPE_YUV:
432 captype = CAPTURE_CHANNEL_TYPE_YUV;
433 break;
434 case CX18_ENC_STREAM_TYPE_PCM:
435 captype = CAPTURE_CHANNEL_TYPE_PCM;
436 break;
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;
440 cx->vbi.frame = 0;
441 cx->vbi.inserted_frame = 0;
442 memset(cx->vbi.sliced_mpeg_size,
443 0, sizeof(cx->vbi.sliced_mpeg_size));
444 break;
445 default:
446 return -EINVAL;
447 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300448
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));
452
453 /* Clear Streamoff flags in case left from last capture */
454 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
455
456 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
457 s->handle = data[0];
458 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
459
Hans Verkuil31554ae2008-05-25 11:21:27 -0300460 if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300461 /* 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);
467
468 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
469 s->handle, cx->digitizer, cx->digitizer);
470
471 /* Setup VBI */
472 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
473 cx18_vbi_setup(s);
474
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);
478
479 /* Setup API for Stream */
480 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
481 }
482
Hans Verkuil31554ae2008-05-25 11:21:27 -0300483 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300484 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300485 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300486 }
487
488 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300489 (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);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300491
492 list_for_each(p, &s->q_free.list) {
493 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
494
Andy Wallsb1526422008-08-30 16:03:44 -0300495 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);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300498 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300499 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
500 1, buf->id, s->buf_size);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300501 }
502 /* begin_capture */
503 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
504 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300505 /* 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);
508 else
509 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
510 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300511 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls3b5df8e2008-08-23 18:36:50 -0300512 /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300513 return -EINVAL;
514 }
515
516 /* you're live! sit back and await interrupts :) */
Hans Verkuil31554ae2008-05-25 11:21:27 -0300517 if (!ts)
518 atomic_inc(&cx->ana_capturing);
519 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300520 return 0;
521}
522
523void cx18_stop_all_captures(struct cx18 *cx)
524{
525 int i;
526
527 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
528 struct cx18_stream *s = &cx->streams[i];
529
530 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
531 continue;
532 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
533 cx18_stop_v4l2_encode_stream(s, 0);
534 }
535}
536
537int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
538{
539 struct cx18 *cx = s->cx;
540 unsigned long then;
541
542 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
543 return -EINVAL;
544
545 /* This function assumes that you are allowed to stop the capture
546 and that we are actually capturing */
547
548 CX18_DEBUG_INFO("Stop Capture\n");
549
Hans Verkuil31554ae2008-05-25 11:21:27 -0300550 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300551 return 0;
552
553 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
554 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
555 else
556 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
557
558 then = jiffies;
559
560 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
561 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
562 }
563
Hans Verkuil31554ae2008-05-25 11:21:27 -0300564 if (s->type != CX18_ENC_STREAM_TYPE_TS)
565 atomic_dec(&cx->ana_capturing);
566 atomic_dec(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300567
568 /* Clear capture and no-read bits */
569 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
570
Andy Wallsf68d0cf2008-11-05 21:19:15 -0300571 /* Tell the CX23418 it can't use our buffers anymore */
572 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
573
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300574 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300575 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300576
Hans Verkuil31554ae2008-05-25 11:21:27 -0300577 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300578 return 0;
579
Andy Wallsb1526422008-08-30 16:03:44 -0300580 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300581 wake_up(&s->waitq);
582
583 return 0;
584}
585
586u32 cx18_find_handle(struct cx18 *cx)
587{
588 int i;
589
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];
593
Andy Wallsd3c5e702008-08-23 16:42:29 -0300594 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300595 return s->handle;
596 }
Andy Wallsd3c5e702008-08-23 16:42:29 -0300597 return CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300598}
Andy Wallsee2d64f2008-11-16 01:38:19 -0300599
600struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
601{
602 int i;
603 struct cx18_stream *s;
604
605 if (handle == CX18_INVALID_TASK_HANDLE)
606 return NULL;
607
608 for (i = 0; i < CX18_MAX_STREAMS; i++) {
609 s = &cx->streams[i];
610 if (s->handle != handle)
611 continue;
612 if (s->v4l2dev || s->dvb.enabled)
613 return s;
614 }
615 return NULL;
616}