blob: 0c8e7542cf6044ec86fbe8e0c6eb8931240c8e25 [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>
7 *
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.
12 *
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.
17 *
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
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030025#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030026#include "cx18-fileops.h"
27#include "cx18-mailbox.h"
28#include "cx18-i2c.h"
29#include "cx18-queue.h"
30#include "cx18-ioctl.h"
31#include "cx18-streams.h"
32#include "cx18-cards.h"
33#include "cx18-scb.h"
34#include "cx18-av-core.h"
35#include "cx18-dvb.h"
36
37#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
38
39static struct file_operations cx18_v4l2_enc_fops = {
Hans Verkuildaf20d92008-05-12 11:21:58 -030040 .owner = THIS_MODULE,
41 .read = cx18_v4l2_read,
42 .open = cx18_v4l2_open,
Andy Walls3b6fe582008-06-21 08:36:31 -030043 /* FIXME change to video_ioctl2 if serialization lock can be removed */
Hans Verkuildaf20d92008-05-12 11:21:58 -030044 .ioctl = cx18_v4l2_ioctl,
45 .compat_ioctl = v4l_compat_ioctl32,
46 .release = cx18_v4l2_close,
47 .poll = cx18_v4l2_enc_poll,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030048};
49
50/* offset from 0 to register ts v4l2 minors on */
51#define CX18_V4L2_ENC_TS_OFFSET 16
52/* offset from 0 to register pcm v4l2 minors on */
53#define CX18_V4L2_ENC_PCM_OFFSET 24
54/* offset from 0 to register yuv v4l2 minors on */
55#define CX18_V4L2_ENC_YUV_OFFSET 32
56
57static struct {
58 const char *name;
59 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030060 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030061 int dma;
62 enum v4l2_buf_type buf_type;
63 struct file_operations *fops;
64} cx18_stream_info[] = {
65 { /* CX18_ENC_STREAM_TYPE_MPG */
66 "encoder MPEG",
67 VFL_TYPE_GRABBER, 0,
68 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
69 &cx18_v4l2_enc_fops
70 },
71 { /* CX18_ENC_STREAM_TYPE_TS */
72 "TS",
73 VFL_TYPE_GRABBER, -1,
74 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
75 &cx18_v4l2_enc_fops
76 },
77 { /* CX18_ENC_STREAM_TYPE_YUV */
78 "encoder YUV",
79 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
80 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
81 &cx18_v4l2_enc_fops
82 },
83 { /* CX18_ENC_STREAM_TYPE_VBI */
84 "encoder VBI",
85 VFL_TYPE_VBI, 0,
86 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
87 &cx18_v4l2_enc_fops
88 },
89 { /* CX18_ENC_STREAM_TYPE_PCM */
90 "encoder PCM audio",
91 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
92 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
93 &cx18_v4l2_enc_fops
94 },
95 { /* CX18_ENC_STREAM_TYPE_IDX */
96 "encoder IDX",
97 VFL_TYPE_GRABBER, -1,
98 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
99 &cx18_v4l2_enc_fops
100 },
101 { /* CX18_ENC_STREAM_TYPE_RAD */
102 "encoder radio",
103 VFL_TYPE_RADIO, 0,
104 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
105 &cx18_v4l2_enc_fops
106 },
107};
108
109static void cx18_stream_init(struct cx18 *cx, int type)
110{
111 struct cx18_stream *s = &cx->streams[type];
112 struct video_device *dev = s->v4l2dev;
113 u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
114
115 /* we need to keep v4l2dev, so restore it afterwards */
116 memset(s, 0, sizeof(*s));
117 s->v4l2dev = dev;
118
119 /* initialize cx18_stream fields */
120 s->cx = cx;
121 s->type = type;
122 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300123 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124
125 s->dma = cx18_stream_info[type].dma;
126 s->buf_size = cx->stream_buf_size[type];
127 if (s->buf_size)
128 s->buffers = max_size / s->buf_size;
129 if (s->buffers > 63) {
130 /* Each stream has a maximum of 63 buffers,
131 ensure we do not exceed that. */
132 s->buffers = 63;
133 s->buf_size = (max_size / s->buffers) & ~0xfff;
134 }
135 spin_lock_init(&s->qlock);
136 init_waitqueue_head(&s->waitq);
137 s->id = -1;
138 cx18_queue_init(&s->q_free);
139 cx18_queue_init(&s->q_full);
140 cx18_queue_init(&s->q_io);
141}
142
143static int cx18_prep_dev(struct cx18 *cx, int type)
144{
145 struct cx18_stream *s = &cx->streams[type];
146 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300147 int num_offset = cx18_stream_info[type].num_offset;
148 int num = cx->num + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149
150 /* These four fields are always initialized. If v4l2dev == NULL, then
151 this stream is not in use. In that case no other fields but these
152 four can be used. */
153 s->v4l2dev = NULL;
154 s->cx = cx;
155 s->type = type;
156 s->name = cx18_stream_info[type].name;
157
158 /* Check whether the radio is supported */
159 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
160 return 0;
161
162 /* Check whether VBI is supported */
163 if (type == CX18_ENC_STREAM_TYPE_VBI &&
164 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
165 return 0;
166
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167 /* User explicitly selected 0 buffers for these streams, so don't
168 create them. */
169 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
170 cx->options.megabytes[type] == 0) {
171 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
172 return 0;
173 }
174
175 cx18_stream_init(cx, type);
176
Hans Verkuildd896012008-10-04 08:36:54 -0300177 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300178 return 0;
179
180 /* allocate and initialize the v4l2 video device structure */
181 s->v4l2dev = video_device_alloc();
182 if (s->v4l2dev == NULL) {
183 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
184 s->name);
185 return -ENOMEM;
186 }
187
Hans Verkuil37f89f92008-06-22 11:57:31 -0300188 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
189 cx->num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190
Hans Verkuildd896012008-10-04 08:36:54 -0300191 s->v4l2dev->num = num;
Hans Verkuil5e85e732008-07-20 06:31:39 -0300192 s->v4l2dev->parent = &cx->dev->dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300193 s->v4l2dev->fops = cx18_stream_info[type].fops;
194 s->v4l2dev->release = video_device_release;
Andy Walls3b6fe582008-06-21 08:36:31 -0300195 s->v4l2dev->tvnorms = V4L2_STD_ALL;
196 cx18_set_funcs(s->v4l2dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300197 return 0;
198}
199
200/* Initialize v4l2 variables and register v4l2 devices */
201int cx18_streams_setup(struct cx18 *cx)
202{
203 int type;
204
205 /* Setup V4L2 Devices */
206 for (type = 0; type < CX18_MAX_STREAMS; type++) {
207 /* Prepare device */
208 if (cx18_prep_dev(cx, type))
209 break;
210
211 /* Allocate Stream */
212 if (cx18_stream_alloc(&cx->streams[type]))
213 break;
214 }
215 if (type == CX18_MAX_STREAMS)
216 return 0;
217
218 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300219 cx18_streams_cleanup(cx, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300220 return -ENOMEM;
221}
222
223static int cx18_reg_dev(struct cx18 *cx, int type)
224{
225 struct cx18_stream *s = &cx->streams[type];
226 int vfl_type = cx18_stream_info[type].vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -0300227 int num;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228
229 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
230 * We need a VFL_TYPE_TS defined.
231 */
232 if (strcmp("TS", s->name) == 0) {
233 /* just return if no DVB is supported */
234 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
235 return 0;
236 if (cx18_dvb_register(s) < 0) {
237 CX18_ERR("DVB failed to register\n");
238 return -EINVAL;
239 }
240 }
241
242 if (s->v4l2dev == NULL)
243 return 0;
244
Hans Verkuildd896012008-10-04 08:36:54 -0300245 num = s->v4l2dev->num;
246 /* card number + user defined offset + device offset */
247 if (type != CX18_ENC_STREAM_TYPE_MPG) {
248 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
249
250 if (s_mpg->v4l2dev)
251 num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
252 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300253
254 /* Register device. First try the desired minor, then any free one. */
Hans Verkuildd896012008-10-04 08:36:54 -0300255 if (video_register_device(s->v4l2dev, vfl_type, num)) {
256 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
257 s->name, num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300258 video_device_release(s->v4l2dev);
259 s->v4l2dev = NULL;
260 return -ENOMEM;
261 }
Hans Verkuildd896012008-10-04 08:36:54 -0300262 num = s->v4l2dev->num;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300263
264 switch (vfl_type) {
265 case VFL_TYPE_GRABBER:
266 CX18_INFO("Registered device video%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300267 num, s->name, cx->options.megabytes[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300268 break;
269
270 case VFL_TYPE_RADIO:
271 CX18_INFO("Registered device radio%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300272 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300273 break;
274
275 case VFL_TYPE_VBI:
276 if (cx->options.megabytes[type])
277 CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300278 num,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300279 s->name, cx->options.megabytes[type]);
280 else
281 CX18_INFO("Registered device vbi%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300282 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300283 break;
284 }
285
286 return 0;
287}
288
289/* Register v4l2 devices */
290int cx18_streams_register(struct cx18 *cx)
291{
292 int type;
293 int err = 0;
294
295 /* Register V4L2 devices */
296 for (type = 0; type < CX18_MAX_STREAMS; type++)
297 err |= cx18_reg_dev(cx, type);
298
299 if (err == 0)
300 return 0;
301
302 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300303 cx18_streams_cleanup(cx, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300304 return -ENOMEM;
305}
306
307/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300308void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300309{
310 struct video_device *vdev;
311 int type;
312
313 /* Teardown all streams */
314 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Hans Verkuilfac36392008-07-18 10:07:10 -0300315 if (cx->streams[type].dvb.enabled) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300316 cx18_dvb_unregister(&cx->streams[type]);
Hans Verkuilfac36392008-07-18 10:07:10 -0300317 cx->streams[type].dvb.enabled = false;
318 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300319
320 vdev = cx->streams[type].v4l2dev;
321
322 cx->streams[type].v4l2dev = NULL;
323 if (vdev == NULL)
324 continue;
325
326 cx18_stream_free(&cx->streams[type]);
327
Hans Verkuil3f983872008-05-01 10:31:12 -0300328 /* Unregister or release device */
329 if (unregister)
330 video_unregister_device(vdev);
331 else
332 video_device_release(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300333 }
334}
335
336static void cx18_vbi_setup(struct cx18_stream *s)
337{
338 struct cx18 *cx = s->cx;
339 int raw = cx->vbi.sliced_in->service_set == 0;
340 u32 data[CX2341X_MBOX_MAX_DATA];
341 int lines;
342
343 if (cx->is_60hz) {
344 cx->vbi.count = 12;
345 cx->vbi.start[0] = 10;
346 cx->vbi.start[1] = 273;
347 } else { /* PAL/SECAM */
348 cx->vbi.count = 18;
349 cx->vbi.start[0] = 6;
350 cx->vbi.start[1] = 318;
351 }
352
353 /* setup VBI registers */
354 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
355
356 /* determine number of lines and total number of VBI bytes.
357 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
358 The '- 1' byte is probably an unused U or V byte. Or something...
359 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
360 header, 42 data bytes + checksum (to be confirmed) */
361 if (raw) {
362 lines = cx->vbi.count * 2;
363 } else {
364 lines = cx->is_60hz ? 24 : 38;
365 if (cx->is_60hz)
366 lines += 2;
367 }
368
369 cx->vbi.enc_size = lines *
370 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
371
372 data[0] = s->handle;
373 /* Lines per field */
374 data[1] = (lines / 2) | ((lines / 2) << 16);
375 /* bytes per line */
376 data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
377 /* Every X number of frames a VBI interrupt arrives
378 (frames as in 25 or 30 fps) */
379 data[3] = 1;
380 /* Setup VBI for the cx25840 digitizer */
381 if (raw) {
382 data[4] = 0x20602060;
383 data[5] = 0x30703070;
384 } else {
385 data[4] = 0xB0F0B0F0;
386 data[5] = 0xA0E0A0E0;
387 }
388
389 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
390 data[0], data[1], data[2], data[3], data[4], data[5]);
391
392 if (s->type == CX18_ENC_STREAM_TYPE_VBI)
393 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
394}
395
396int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
397{
398 u32 data[MAX_MB_ARGUMENTS];
399 struct cx18 *cx = s->cx;
400 struct list_head *p;
401 int ts = 0;
402 int captype = 0;
403
404 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
405 return -EINVAL;
406
407 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
408
409 switch (s->type) {
410 case CX18_ENC_STREAM_TYPE_MPG:
411 captype = CAPTURE_CHANNEL_TYPE_MPEG;
412 cx->mpg_data_received = cx->vbi_data_inserted = 0;
413 cx->dualwatch_jiffies = jiffies;
414 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
415 cx->search_pack_header = 0;
416 break;
417
418 case CX18_ENC_STREAM_TYPE_TS:
419 captype = CAPTURE_CHANNEL_TYPE_TS;
420 ts = 1;
421 break;
422 case CX18_ENC_STREAM_TYPE_YUV:
423 captype = CAPTURE_CHANNEL_TYPE_YUV;
424 break;
425 case CX18_ENC_STREAM_TYPE_PCM:
426 captype = CAPTURE_CHANNEL_TYPE_PCM;
427 break;
428 case CX18_ENC_STREAM_TYPE_VBI:
429 captype = cx->vbi.sliced_in->service_set ?
430 CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
431 cx->vbi.frame = 0;
432 cx->vbi.inserted_frame = 0;
433 memset(cx->vbi.sliced_mpeg_size,
434 0, sizeof(cx->vbi.sliced_mpeg_size));
435 break;
436 default:
437 return -EINVAL;
438 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300439
440 /* mute/unmute video */
441 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
442 s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
443
444 /* Clear Streamoff flags in case left from last capture */
445 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
446
447 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
448 s->handle = data[0];
449 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
450
Hans Verkuil31554ae2008-05-25 11:21:27 -0300451 if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300452 /* Stuff from Windows, we don't know what it is */
453 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
454 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
455 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
456 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
457 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
458
459 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
460 s->handle, cx->digitizer, cx->digitizer);
461
462 /* Setup VBI */
463 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
464 cx18_vbi_setup(s);
465
466 /* assign program index info.
467 Mask 7: select I/P/B, Num_req: 400 max */
468 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
469
470 /* Setup API for Stream */
471 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
472 }
473
Hans Verkuil31554ae2008-05-25 11:21:27 -0300474 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300475 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300476 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300477 }
478
479 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300480 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
481 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300482
483 list_for_each(p, &s->q_free.list) {
484 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
485
Andy Wallsb1526422008-08-30 16:03:44 -0300486 cx18_writel(cx, buf->dma_handle,
487 &cx->scb->cpu_mdl[buf->id].paddr);
488 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300489 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300490 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
491 1, buf->id, s->buf_size);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300492 }
493 /* begin_capture */
494 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
495 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300496 /* Ensure we're really not capturing before releasing MDLs */
497 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
498 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
499 else
500 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
501 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300502 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls3b5df8e2008-08-23 18:36:50 -0300503 /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300504 return -EINVAL;
505 }
506
507 /* you're live! sit back and await interrupts :) */
Hans Verkuil31554ae2008-05-25 11:21:27 -0300508 if (!ts)
509 atomic_inc(&cx->ana_capturing);
510 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300511 return 0;
512}
513
514void cx18_stop_all_captures(struct cx18 *cx)
515{
516 int i;
517
518 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
519 struct cx18_stream *s = &cx->streams[i];
520
521 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
522 continue;
523 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
524 cx18_stop_v4l2_encode_stream(s, 0);
525 }
526}
527
528int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
529{
530 struct cx18 *cx = s->cx;
531 unsigned long then;
532
533 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
534 return -EINVAL;
535
536 /* This function assumes that you are allowed to stop the capture
537 and that we are actually capturing */
538
539 CX18_DEBUG_INFO("Stop Capture\n");
540
Hans Verkuil31554ae2008-05-25 11:21:27 -0300541 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300542 return 0;
543
544 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
545 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
546 else
547 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
548
549 then = jiffies;
550
551 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
552 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
553 }
554
Andy Walls3b5df8e2008-08-23 18:36:50 -0300555 /* Tell the CX23418 it can't use our buffers anymore */
556 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
557
Hans Verkuil31554ae2008-05-25 11:21:27 -0300558 if (s->type != CX18_ENC_STREAM_TYPE_TS)
559 atomic_dec(&cx->ana_capturing);
560 atomic_dec(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300561
562 /* Clear capture and no-read bits */
563 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
564
565 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300566 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300567
Hans Verkuil31554ae2008-05-25 11:21:27 -0300568 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300569 return 0;
570
Andy Wallsb1526422008-08-30 16:03:44 -0300571 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300572 wake_up(&s->waitq);
573
574 return 0;
575}
576
577u32 cx18_find_handle(struct cx18 *cx)
578{
579 int i;
580
581 /* find first available handle to be used for global settings */
582 for (i = 0; i < CX18_MAX_STREAMS; i++) {
583 struct cx18_stream *s = &cx->streams[i];
584
Andy Wallsd3c5e702008-08-23 16:42:29 -0300585 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300586 return s->handle;
587 }
Andy Wallsd3c5e702008-08-23 16:42:29 -0300588 return CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300589}