V4L/DVB (11619): cx18: Simplify the work handler for outgoing mailbox commands
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-streams.c
1 /*
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  *  Copyright (C) 2008  Andy Walls <awalls@radix.net>
8  *
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"
26 #include "cx18-io.h"
27 #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-dvb.h"
36
37 #define CX18_DSP0_INTERRUPT_MASK        0xd0004C
38
39 static struct v4l2_file_operations cx18_v4l2_enc_fops = {
40         .owner = THIS_MODULE,
41         .read = cx18_v4l2_read,
42         .open = cx18_v4l2_open,
43         /* FIXME change to video_ioctl2 if serialization lock can be removed */
44         .ioctl = cx18_v4l2_ioctl,
45         .release = cx18_v4l2_close,
46         .poll = cx18_v4l2_enc_poll,
47 };
48
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
55
56 static struct {
57         const char *name;
58         int vfl_type;
59         int num_offset;
60         int dma;
61         enum v4l2_buf_type buf_type;
62 } cx18_stream_info[] = {
63         {       /* CX18_ENC_STREAM_TYPE_MPG */
64                 "encoder MPEG",
65                 VFL_TYPE_GRABBER, 0,
66                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
67         },
68         {       /* CX18_ENC_STREAM_TYPE_TS */
69                 "TS",
70                 VFL_TYPE_GRABBER, -1,
71                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
72         },
73         {       /* CX18_ENC_STREAM_TYPE_YUV */
74                 "encoder YUV",
75                 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
76                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
77         },
78         {       /* CX18_ENC_STREAM_TYPE_VBI */
79                 "encoder VBI",
80                 VFL_TYPE_VBI, 0,
81                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
82         },
83         {       /* CX18_ENC_STREAM_TYPE_PCM */
84                 "encoder PCM audio",
85                 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
86                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
87         },
88         {       /* CX18_ENC_STREAM_TYPE_IDX */
89                 "encoder IDX",
90                 VFL_TYPE_GRABBER, -1,
91                 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
92         },
93         {       /* CX18_ENC_STREAM_TYPE_RAD */
94                 "encoder radio",
95                 VFL_TYPE_RADIO, 0,
96                 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
97         },
98 };
99
100 static void cx18_stream_init(struct cx18 *cx, int type)
101 {
102         struct cx18_stream *s = &cx->streams[type];
103         struct video_device *video_dev = s->video_dev;
104
105         /* we need to keep video_dev, so restore it afterwards */
106         memset(s, 0, sizeof(*s));
107         s->video_dev = video_dev;
108
109         /* initialize cx18_stream fields */
110         s->cx = cx;
111         s->type = type;
112         s->name = cx18_stream_info[type].name;
113         s->handle = CX18_INVALID_TASK_HANDLE;
114
115         s->dma = cx18_stream_info[type].dma;
116         s->buffers = cx->stream_buffers[type];
117         s->buf_size = cx->stream_buf_size[type];
118
119         init_waitqueue_head(&s->waitq);
120         s->id = -1;
121         spin_lock_init(&s->q_free.lock);
122         cx18_queue_init(&s->q_free);
123         spin_lock_init(&s->q_busy.lock);
124         cx18_queue_init(&s->q_busy);
125         spin_lock_init(&s->q_full.lock);
126         cx18_queue_init(&s->q_full);
127
128         INIT_WORK(&s->out_work_order, cx18_out_work_handler);
129 }
130
131 static int cx18_prep_dev(struct cx18 *cx, int type)
132 {
133         struct cx18_stream *s = &cx->streams[type];
134         u32 cap = cx->v4l2_cap;
135         int num_offset = cx18_stream_info[type].num_offset;
136         int num = cx->instance + cx18_first_minor + num_offset;
137
138         /* These four fields are always initialized. If video_dev == NULL, then
139            this stream is not in use. In that case no other fields but these
140            four can be used. */
141         s->video_dev = NULL;
142         s->cx = cx;
143         s->type = type;
144         s->name = cx18_stream_info[type].name;
145
146         /* Check whether the radio is supported */
147         if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
148                 return 0;
149
150         /* Check whether VBI is supported */
151         if (type == CX18_ENC_STREAM_TYPE_VBI &&
152             !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
153                 return 0;
154
155         /* User explicitly selected 0 buffers for these streams, so don't
156            create them. */
157         if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
158             cx->stream_buffers[type] == 0) {
159                 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
160                 return 0;
161         }
162
163         cx18_stream_init(cx, type);
164
165         if (num_offset == -1)
166                 return 0;
167
168         /* allocate and initialize the v4l2 video device structure */
169         s->video_dev = video_device_alloc();
170         if (s->video_dev == NULL) {
171                 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
172                                 s->name);
173                 return -ENOMEM;
174         }
175
176         snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
177                  cx->v4l2_dev.name, s->name);
178
179         s->video_dev->num = num;
180         s->video_dev->v4l2_dev = &cx->v4l2_dev;
181         s->video_dev->fops = &cx18_v4l2_enc_fops;
182         s->video_dev->release = video_device_release;
183         s->video_dev->tvnorms = V4L2_STD_ALL;
184         cx18_set_funcs(s->video_dev);
185         return 0;
186 }
187
188 /* Initialize v4l2 variables and register v4l2 devices */
189 int cx18_streams_setup(struct cx18 *cx)
190 {
191         int type, ret;
192
193         /* Setup V4L2 Devices */
194         for (type = 0; type < CX18_MAX_STREAMS; type++) {
195                 /* Prepare device */
196                 ret = cx18_prep_dev(cx, type);
197                 if (ret < 0)
198                         break;
199
200                 /* Allocate Stream */
201                 ret = cx18_stream_alloc(&cx->streams[type]);
202                 if (ret < 0)
203                         break;
204         }
205         if (type == CX18_MAX_STREAMS)
206                 return 0;
207
208         /* One or more streams could not be initialized. Clean 'em all up. */
209         cx18_streams_cleanup(cx, 0);
210         return ret;
211 }
212
213 static int cx18_reg_dev(struct cx18 *cx, int type)
214 {
215         struct cx18_stream *s = &cx->streams[type];
216         int vfl_type = cx18_stream_info[type].vfl_type;
217         int num, ret;
218
219         /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
220          * We need a VFL_TYPE_TS defined.
221          */
222         if (strcmp("TS", s->name) == 0) {
223                 /* just return if no DVB is supported */
224                 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
225                         return 0;
226                 ret = cx18_dvb_register(s);
227                 if (ret < 0) {
228                         CX18_ERR("DVB failed to register\n");
229                         return ret;
230                 }
231         }
232
233         if (s->video_dev == NULL)
234                 return 0;
235
236         num = s->video_dev->num;
237         /* card number + user defined offset + device offset */
238         if (type != CX18_ENC_STREAM_TYPE_MPG) {
239                 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
240
241                 if (s_mpg->video_dev)
242                         num = s_mpg->video_dev->num
243                             + cx18_stream_info[type].num_offset;
244         }
245         video_set_drvdata(s->video_dev, s);
246
247         /* Register device. First try the desired minor, then any free one. */
248         ret = video_register_device(s->video_dev, vfl_type, num);
249         if (ret < 0) {
250                 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
251                         s->name, num);
252                 video_device_release(s->video_dev);
253                 s->video_dev = NULL;
254                 return ret;
255         }
256         num = s->video_dev->num;
257
258         switch (vfl_type) {
259         case VFL_TYPE_GRABBER:
260                 CX18_INFO("Registered device video%d for %s (%d x %d kB)\n",
261                           num, s->name, cx->stream_buffers[type],
262                           cx->stream_buf_size[type]/1024);
263                 break;
264
265         case VFL_TYPE_RADIO:
266                 CX18_INFO("Registered device radio%d for %s\n",
267                         num, s->name);
268                 break;
269
270         case VFL_TYPE_VBI:
271                 if (cx->stream_buffers[type])
272                         CX18_INFO("Registered device vbi%d for %s "
273                                   "(%d x %d bytes)\n",
274                                   num, s->name, cx->stream_buffers[type],
275                                   cx->stream_buf_size[type]);
276                 else
277                         CX18_INFO("Registered device vbi%d for %s\n",
278                                 num, s->name);
279                 break;
280         }
281
282         return 0;
283 }
284
285 /* Register v4l2 devices */
286 int cx18_streams_register(struct cx18 *cx)
287 {
288         int type;
289         int err;
290         int ret = 0;
291
292         /* Register V4L2 devices */
293         for (type = 0; type < CX18_MAX_STREAMS; type++) {
294                 err = cx18_reg_dev(cx, type);
295                 if (err && ret == 0)
296                         ret = err;
297         }
298
299         if (ret == 0)
300                 return 0;
301
302         /* One or more streams could not be initialized. Clean 'em all up. */
303         cx18_streams_cleanup(cx, 1);
304         return ret;
305 }
306
307 /* Unregister v4l2 devices */
308 void cx18_streams_cleanup(struct cx18 *cx, int unregister)
309 {
310         struct video_device *vdev;
311         int type;
312
313         /* Teardown all streams */
314         for (type = 0; type < CX18_MAX_STREAMS; type++) {
315                 if (cx->streams[type].dvb.enabled) {
316                         cx18_dvb_unregister(&cx->streams[type]);
317                         cx->streams[type].dvb.enabled = false;
318                 }
319
320                 vdev = cx->streams[type].video_dev;
321
322                 cx->streams[type].video_dev = NULL;
323                 if (vdev == NULL)
324                         continue;
325
326                 cx18_stream_free(&cx->streams[type]);
327
328                 /* Unregister or release device */
329                 if (unregister)
330                         video_unregister_device(vdev);
331                 else
332                         video_device_release(vdev);
333         }
334 }
335
336 static void cx18_vbi_setup(struct cx18_stream *s)
337 {
338         struct cx18 *cx = s->cx;
339         int raw = cx18_raw_vbi(cx);
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         v4l2_subdev_call(cx->sd_av, video, s_fmt, &cx->vbi.in);
355
356         /*
357          * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
358          * VBI when the first analog capture channel starts, as once it starts
359          * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
360          * (i.e. for the VBI capture channels).  We also send it for each
361          * analog capture channel anyway just to make sure we get the proper
362          * behavior
363          */
364         if (raw) {
365                 lines = cx->vbi.count * 2;
366         } else {
367                 /*
368                  * For 525/60 systems, according to the VIP 2 & BT.656 std:
369                  * The EAV RP code's Field bit toggles on line 4, a few lines
370                  * after the Vertcal Blank bit has already toggled.
371                  * Tell the encoder to capture 21-4+1=18 lines per field,
372                  * since we want lines 10 through 21.
373                  *
374                  * FIXME - revisit for 625/50 systems
375                  */
376                 lines = cx->is_60hz ? (21 - 4 + 1) * 2 : 38;
377         }
378
379         data[0] = s->handle;
380         /* Lines per field */
381         data[1] = (lines / 2) | ((lines / 2) << 16);
382         /* bytes per line */
383         data[2] = (raw ? vbi_active_samples
384                        : (cx->is_60hz ? vbi_hblank_samples_60Hz
385                                       : vbi_hblank_samples_50Hz));
386         /* Every X number of frames a VBI interrupt arrives
387            (frames as in 25 or 30 fps) */
388         data[3] = 1;
389         /*
390          * Set the SAV/EAV RP codes to look for as start/stop points
391          * when in VIP-1.1 mode
392          */
393         if (raw) {
394                 /*
395                  * Start codes for beginning of "active" line in vertical blank
396                  * 0x20 (               VerticalBlank                )
397                  * 0x60 (     EvenField VerticalBlank                )
398                  */
399                 data[4] = 0x20602060;
400                 /*
401                  * End codes for end of "active" raw lines and regular lines
402                  * 0x30 (               VerticalBlank HorizontalBlank)
403                  * 0x70 (     EvenField VerticalBlank HorizontalBlank)
404                  * 0x90 (Task                         HorizontalBlank)
405                  * 0xd0 (Task EvenField               HorizontalBlank)
406                  */
407                 data[5] = 0x307090d0;
408         } else {
409                 /*
410                  * End codes for active video, we want data in the hblank region
411                  * 0xb0 (Task         0 VerticalBlank HorizontalBlank)
412                  * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
413                  *
414                  * Since the V bit is only allowed to toggle in the EAV RP code,
415                  * just before the first active region line, these two
416                  * are problematic:
417                  * 0x90 (Task                         HorizontalBlank)
418                  * 0xd0 (Task EvenField               HorizontalBlank)
419                  *
420                  * We have set the digitzer such that we don't have to worry
421                  * about these problem codes.
422                  */
423                 data[4] = 0xB0F0B0F0;
424                 /*
425                  * Start codes for beginning of active line in vertical blank
426                  * 0xa0 (Task           VerticalBlank                )
427                  * 0xe0 (Task EvenField VerticalBlank                )
428                  */
429                 data[5] = 0xA0E0A0E0;
430         }
431
432         CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
433                         data[0], data[1], data[2], data[3], data[4], data[5]);
434
435         cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
436 }
437
438 static
439 struct cx18_queue *_cx18_stream_put_buf_fw(struct cx18_stream *s,
440                                            struct cx18_buffer *buf)
441 {
442         struct cx18 *cx = s->cx;
443         struct cx18_queue *q;
444
445         /* Don't give it to the firmware, if we're not running a capture */
446         if (s->handle == CX18_INVALID_TASK_HANDLE ||
447             test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
448             !test_bit(CX18_F_S_STREAMING, &s->s_flags))
449                 return cx18_enqueue(s, buf, &s->q_free);
450
451         q = cx18_enqueue(s, buf, &s->q_busy);
452         if (q != &s->q_busy)
453                 return q; /* The firmware has the max buffers it can handle */
454
455         cx18_buf_sync_for_device(s, buf);
456         cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
457                   (void __iomem *) &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
458                   1, buf->id, s->buf_size);
459         return q;
460 }
461
462 static
463 void _cx18_stream_load_fw_queue(struct cx18_stream *s)
464 {
465         struct cx18_queue *q;
466         struct cx18_buffer *buf;
467
468         if (atomic_read(&s->q_free.buffers) == 0 ||
469             atomic_read(&s->q_busy.buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
470                 return;
471
472         /* Move from q_free to q_busy notifying the firmware, until the limit */
473         do {
474                 buf = cx18_dequeue(s, &s->q_free);
475                 if (buf == NULL)
476                         break;
477                 q = _cx18_stream_put_buf_fw(s, buf);
478         } while (atomic_read(&s->q_busy.buffers) < CX18_MAX_FW_MDLS_PER_STREAM
479                  && q == &s->q_busy);
480 }
481
482 void cx18_out_work_handler(struct work_struct *work)
483 {
484         struct cx18_stream *s =
485                          container_of(work, struct cx18_stream, out_work_order);
486
487         _cx18_stream_load_fw_queue(s);
488 }
489
490 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
491 {
492         u32 data[MAX_MB_ARGUMENTS];
493         struct cx18 *cx = s->cx;
494         struct cx18_buffer *buf;
495         int captype = 0;
496         struct cx18_api_func_private priv;
497
498         if (s->video_dev == NULL && s->dvb.enabled == 0)
499                 return -EINVAL;
500
501         CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
502
503         switch (s->type) {
504         case CX18_ENC_STREAM_TYPE_MPG:
505                 captype = CAPTURE_CHANNEL_TYPE_MPEG;
506                 cx->mpg_data_received = cx->vbi_data_inserted = 0;
507                 cx->dualwatch_jiffies = jiffies;
508                 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
509                 cx->search_pack_header = 0;
510                 break;
511
512         case CX18_ENC_STREAM_TYPE_TS:
513                 captype = CAPTURE_CHANNEL_TYPE_TS;
514                 break;
515         case CX18_ENC_STREAM_TYPE_YUV:
516                 captype = CAPTURE_CHANNEL_TYPE_YUV;
517                 break;
518         case CX18_ENC_STREAM_TYPE_PCM:
519                 captype = CAPTURE_CHANNEL_TYPE_PCM;
520                 break;
521         case CX18_ENC_STREAM_TYPE_VBI:
522 #ifdef CX18_ENCODER_PARSES_SLICED
523                 captype = cx18_raw_vbi(cx) ?
524                      CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
525 #else
526                 /*
527                  * Currently we set things up so that Sliced VBI from the
528                  * digitizer is handled as Raw VBI by the encoder
529                  */
530                 captype = CAPTURE_CHANNEL_TYPE_VBI;
531 #endif
532                 cx->vbi.frame = 0;
533                 cx->vbi.inserted_frame = 0;
534                 memset(cx->vbi.sliced_mpeg_size,
535                         0, sizeof(cx->vbi.sliced_mpeg_size));
536                 break;
537         default:
538                 return -EINVAL;
539         }
540
541         /* Clear Streamoff flags in case left from last capture */
542         clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
543
544         cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
545         s->handle = data[0];
546         cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
547
548         /*
549          * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
550          * set up all the parameters, as it is not obvious which parameters the
551          * firmware shares across capture channel types and which it does not.
552          *
553          * Some of the cx18_vapi() calls below apply to only certain capture
554          * channel types.  We're hoping there's no harm in calling most of them
555          * anyway, as long as the values are all consistent.  Setting some
556          * shared parameters will have no effect once an analog capture channel
557          * has started streaming.
558          */
559         if (captype != CAPTURE_CHANNEL_TYPE_TS) {
560                 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
561                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
562                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
563                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
564
565                 /*
566                  * Audio related reset according to
567                  * Documentation/video4linux/cx2341x/fw-encoder-api.txt
568                  */
569                 if (atomic_read(&cx->ana_capturing) == 0)
570                         cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
571                                   s->handle, 12);
572
573                 /*
574                  * Number of lines for Field 1 & Field 2 according to
575                  * Documentation/video4linux/cx2341x/fw-encoder-api.txt
576                  * Field 1 is 312 for 625 line systems in BT.656
577                  * Field 2 is 313 for 625 line systems in BT.656
578                  */
579                 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
580                           s->handle, 312, 313);
581
582                 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
583                         cx18_vbi_setup(s);
584
585                 /*
586                  * assign program index info.
587                  * Mask 7: select I/P/B, Num_req: 400 max
588                  * FIXME - currently we have this hardcoded as disabled
589                  */
590                 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
591
592                 /* Call out to the common CX2341x API setup for user controls */
593                 priv.cx = cx;
594                 priv.s = s;
595                 cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
596
597                 /*
598                  * When starting a capture and we're set for radio,
599                  * ensure the video is muted, despite the user control.
600                  */
601                 if (!cx->params.video_mute &&
602                     test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
603                         cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
604                                   (cx->params.video_mute_yuv << 8) | 1);
605         }
606
607         if (atomic_read(&cx->tot_capturing) == 0) {
608                 clear_bit(CX18_F_I_EOS, &cx->i_flags);
609                 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
610         }
611
612         cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
613                 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
614                 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
615
616         /* Init all the cpu_mdls for this stream */
617         cx18_flush_queues(s);
618         spin_lock(&s->q_free.lock);
619         list_for_each_entry(buf, &s->q_free.list, list) {
620                 cx18_writel(cx, buf->dma_handle,
621                                         &cx->scb->cpu_mdl[buf->id].paddr);
622                 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
623         }
624         spin_unlock(&s->q_free.lock);
625         _cx18_stream_load_fw_queue(s);
626
627         /* begin_capture */
628         if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
629                 CX18_DEBUG_WARN("Error starting capture!\n");
630                 /* Ensure we're really not capturing before releasing MDLs */
631                 set_bit(CX18_F_S_STOPPING, &s->s_flags);
632                 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
633                         cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
634                 else
635                         cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
636                 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
637                 /* FIXME - CX18_F_S_STREAMOFF as well? */
638                 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
639                 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
640                 s->handle = CX18_INVALID_TASK_HANDLE;
641                 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
642                 if (atomic_read(&cx->tot_capturing) == 0) {
643                         set_bit(CX18_F_I_EOS, &cx->i_flags);
644                         cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
645                 }
646                 return -EINVAL;
647         }
648
649         /* you're live! sit back and await interrupts :) */
650         if (captype != CAPTURE_CHANNEL_TYPE_TS)
651                 atomic_inc(&cx->ana_capturing);
652         atomic_inc(&cx->tot_capturing);
653         return 0;
654 }
655
656 void cx18_stop_all_captures(struct cx18 *cx)
657 {
658         int i;
659
660         for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
661                 struct cx18_stream *s = &cx->streams[i];
662
663                 if (s->video_dev == NULL && s->dvb.enabled == 0)
664                         continue;
665                 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
666                         cx18_stop_v4l2_encode_stream(s, 0);
667         }
668 }
669
670 int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
671 {
672         struct cx18 *cx = s->cx;
673         unsigned long then;
674
675         if (s->video_dev == NULL && s->dvb.enabled == 0)
676                 return -EINVAL;
677
678         /* This function assumes that you are allowed to stop the capture
679            and that we are actually capturing */
680
681         CX18_DEBUG_INFO("Stop Capture\n");
682
683         if (atomic_read(&cx->tot_capturing) == 0)
684                 return 0;
685
686         set_bit(CX18_F_S_STOPPING, &s->s_flags);
687         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
688                 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
689         else
690                 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
691
692         then = jiffies;
693
694         if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
695                 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
696         }
697
698         if (s->type != CX18_ENC_STREAM_TYPE_TS)
699                 atomic_dec(&cx->ana_capturing);
700         atomic_dec(&cx->tot_capturing);
701
702         /* Clear capture and no-read bits */
703         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
704
705         /* Tell the CX23418 it can't use our buffers anymore */
706         cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
707
708         cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
709         s->handle = CX18_INVALID_TASK_HANDLE;
710         clear_bit(CX18_F_S_STOPPING, &s->s_flags);
711
712         if (atomic_read(&cx->tot_capturing) > 0)
713                 return 0;
714
715         cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
716         wake_up(&s->waitq);
717
718         return 0;
719 }
720
721 u32 cx18_find_handle(struct cx18 *cx)
722 {
723         int i;
724
725         /* find first available handle to be used for global settings */
726         for (i = 0; i < CX18_MAX_STREAMS; i++) {
727                 struct cx18_stream *s = &cx->streams[i];
728
729                 if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
730                         return s->handle;
731         }
732         return CX18_INVALID_TASK_HANDLE;
733 }
734
735 struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
736 {
737         int i;
738         struct cx18_stream *s;
739
740         if (handle == CX18_INVALID_TASK_HANDLE)
741                 return NULL;
742
743         for (i = 0; i < CX18_MAX_STREAMS; i++) {
744                 s = &cx->streams[i];
745                 if (s->handle != handle)
746                         continue;
747                 if (s->video_dev || s->dvb.enabled)
748                         return s;
749         }
750         return NULL;
751 }