V4L/DVB (9800): cx18: Eliminate q_io from stream buffer handling
[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-av-core.h"
36 #include "cx18-dvb.h"
37
38 #define CX18_DSP0_INTERRUPT_MASK        0xd0004C
39
40 static struct file_operations cx18_v4l2_enc_fops = {
41         .owner = THIS_MODULE,
42         .read = cx18_v4l2_read,
43         .open = cx18_v4l2_open,
44         /* FIXME change to video_ioctl2 if serialization lock can be removed */
45         .ioctl = cx18_v4l2_ioctl,
46         .compat_ioctl = v4l_compat_ioctl32,
47         .release = cx18_v4l2_close,
48         .poll = cx18_v4l2_enc_poll,
49 };
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
58 static struct {
59         const char *name;
60         int vfl_type;
61         int num_offset;
62         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
110 static 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;
124         s->handle = CX18_INVALID_TASK_HANDLE;
125
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         }
136         mutex_init(&s->qlock);
137         init_waitqueue_head(&s->waitq);
138         s->id = -1;
139         cx18_queue_init(&s->q_free);
140         cx18_queue_init(&s->q_full);
141 }
142
143 static int cx18_prep_dev(struct cx18 *cx, int type)
144 {
145         struct cx18_stream *s = &cx->streams[type];
146         u32 cap = cx->v4l2_cap;
147         int num_offset = cx18_stream_info[type].num_offset;
148         int num = cx->num + cx18_first_minor + num_offset;
149
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
167         /* 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
177         if (num_offset == -1)
178                 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
188         snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
189                         cx->num);
190
191         s->v4l2dev->num = num;
192         s->v4l2dev->parent = &cx->dev->dev;
193         s->v4l2dev->fops = cx18_stream_info[type].fops;
194         s->v4l2dev->release = video_device_release;
195         s->v4l2dev->tvnorms = V4L2_STD_ALL;
196         cx18_set_funcs(s->v4l2dev);
197         return 0;
198 }
199
200 /* Initialize v4l2 variables and register v4l2 devices */
201 int cx18_streams_setup(struct cx18 *cx)
202 {
203         int type, ret;
204
205         /* Setup V4L2 Devices */
206         for (type = 0; type < CX18_MAX_STREAMS; type++) {
207                 /* Prepare device */
208                 ret = cx18_prep_dev(cx, type);
209                 if (ret < 0)
210                         break;
211
212                 /* Allocate Stream */
213                 ret = cx18_stream_alloc(&cx->streams[type]);
214                 if (ret < 0)
215                         break;
216         }
217         if (type == CX18_MAX_STREAMS)
218                 return 0;
219
220         /* One or more streams could not be initialized. Clean 'em all up. */
221         cx18_streams_cleanup(cx, 0);
222         return ret;
223 }
224
225 static int cx18_reg_dev(struct cx18 *cx, int type)
226 {
227         struct cx18_stream *s = &cx->streams[type];
228         int vfl_type = cx18_stream_info[type].vfl_type;
229         int num, ret;
230
231         /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
232          * We need a VFL_TYPE_TS defined.
233          */
234         if (strcmp("TS", s->name) == 0) {
235                 /* just return if no DVB is supported */
236                 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
237                         return 0;
238                 ret = cx18_dvb_register(s);
239                 if (ret < 0) {
240                         CX18_ERR("DVB failed to register\n");
241                         return ret;
242                 }
243         }
244
245         if (s->v4l2dev == NULL)
246                 return 0;
247
248         num = s->v4l2dev->num;
249         /* card number + user defined offset + device offset */
250         if (type != CX18_ENC_STREAM_TYPE_MPG) {
251                 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
252
253                 if (s_mpg->v4l2dev)
254                         num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
255         }
256
257         /* Register device. First try the desired minor, then any free one. */
258         ret = video_register_device(s->v4l2dev, vfl_type, num);
259         if (ret < 0) {
260                 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
261                         s->name, num);
262                 video_device_release(s->v4l2dev);
263                 s->v4l2dev = NULL;
264                 return ret;
265         }
266         num = s->v4l2dev->num;
267
268         switch (vfl_type) {
269         case VFL_TYPE_GRABBER:
270                 CX18_INFO("Registered device video%d for %s (%d MB)\n",
271                         num, s->name, cx->options.megabytes[type]);
272                 break;
273
274         case VFL_TYPE_RADIO:
275                 CX18_INFO("Registered device radio%d for %s\n",
276                         num, s->name);
277                 break;
278
279         case VFL_TYPE_VBI:
280                 if (cx->options.megabytes[type])
281                         CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
282                                 num,
283                                 s->name, cx->options.megabytes[type]);
284                 else
285                         CX18_INFO("Registered device vbi%d for %s\n",
286                                 num, s->name);
287                 break;
288         }
289
290         return 0;
291 }
292
293 /* Register v4l2 devices */
294 int cx18_streams_register(struct cx18 *cx)
295 {
296         int type;
297         int err;
298         int ret = 0;
299
300         /* Register V4L2 devices */
301         for (type = 0; type < CX18_MAX_STREAMS; type++) {
302                 err = cx18_reg_dev(cx, type);
303                 if (err && ret == 0)
304                         ret = err;
305         }
306
307         if (ret == 0)
308                 return 0;
309
310         /* One or more streams could not be initialized. Clean 'em all up. */
311         cx18_streams_cleanup(cx, 1);
312         return ret;
313 }
314
315 /* Unregister v4l2 devices */
316 void cx18_streams_cleanup(struct cx18 *cx, int unregister)
317 {
318         struct video_device *vdev;
319         int type;
320
321         /* Teardown all streams */
322         for (type = 0; type < CX18_MAX_STREAMS; type++) {
323                 if (cx->streams[type].dvb.enabled) {
324                         cx18_dvb_unregister(&cx->streams[type]);
325                         cx->streams[type].dvb.enabled = false;
326                 }
327
328                 vdev = cx->streams[type].v4l2dev;
329
330                 cx->streams[type].v4l2dev = NULL;
331                 if (vdev == NULL)
332                         continue;
333
334                 cx18_stream_free(&cx->streams[type]);
335
336                 /* Unregister or release device */
337                 if (unregister)
338                         video_unregister_device(vdev);
339                 else
340                         video_device_release(vdev);
341         }
342 }
343
344 static void cx18_vbi_setup(struct cx18_stream *s)
345 {
346         struct cx18 *cx = s->cx;
347         int raw = cx->vbi.sliced_in->service_set == 0;
348         u32 data[CX2341X_MBOX_MAX_DATA];
349         int lines;
350
351         if (cx->is_60hz) {
352                 cx->vbi.count = 12;
353                 cx->vbi.start[0] = 10;
354                 cx->vbi.start[1] = 273;
355         } else {        /* PAL/SECAM */
356                 cx->vbi.count = 18;
357                 cx->vbi.start[0] = 6;
358                 cx->vbi.start[1] = 318;
359         }
360
361         /* setup VBI registers */
362         cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
363
364         /* determine number of lines and total number of VBI bytes.
365            A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
366            The '- 1' byte is probably an unused U or V byte. Or something...
367            A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
368            header, 42 data bytes + checksum (to be confirmed) */
369         if (raw) {
370                 lines = cx->vbi.count * 2;
371         } else {
372                 lines = cx->is_60hz ? 24 : 38;
373                 if (cx->is_60hz)
374                         lines += 2;
375         }
376
377         cx->vbi.enc_size = lines *
378                 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
379
380         data[0] = s->handle;
381         /* Lines per field */
382         data[1] = (lines / 2) | ((lines / 2) << 16);
383         /* bytes per line */
384         data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
385         /* Every X number of frames a VBI interrupt arrives
386            (frames as in 25 or 30 fps) */
387         data[3] = 1;
388         /* Setup VBI for the cx25840 digitizer */
389         if (raw) {
390                 data[4] = 0x20602060;
391                 data[5] = 0x30703070;
392         } else {
393                 data[4] = 0xB0F0B0F0;
394                 data[5] = 0xA0E0A0E0;
395         }
396
397         CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
398                         data[0], data[1], data[2], data[3], data[4], data[5]);
399
400         if (s->type == CX18_ENC_STREAM_TYPE_VBI)
401                 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
402 }
403
404 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
405 {
406         u32 data[MAX_MB_ARGUMENTS];
407         struct cx18 *cx = s->cx;
408         struct list_head *p;
409         int ts = 0;
410         int captype = 0;
411
412         if (s->v4l2dev == NULL && s->dvb.enabled == 0)
413                 return -EINVAL;
414
415         CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
416
417         switch (s->type) {
418         case CX18_ENC_STREAM_TYPE_MPG:
419                 captype = CAPTURE_CHANNEL_TYPE_MPEG;
420                 cx->mpg_data_received = cx->vbi_data_inserted = 0;
421                 cx->dualwatch_jiffies = jiffies;
422                 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
423                 cx->search_pack_header = 0;
424                 break;
425
426         case CX18_ENC_STREAM_TYPE_TS:
427                 captype = CAPTURE_CHANNEL_TYPE_TS;
428                 ts = 1;
429                 break;
430         case CX18_ENC_STREAM_TYPE_YUV:
431                 captype = CAPTURE_CHANNEL_TYPE_YUV;
432                 break;
433         case CX18_ENC_STREAM_TYPE_PCM:
434                 captype = CAPTURE_CHANNEL_TYPE_PCM;
435                 break;
436         case CX18_ENC_STREAM_TYPE_VBI:
437                 captype = cx->vbi.sliced_in->service_set ?
438                     CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
439                 cx->vbi.frame = 0;
440                 cx->vbi.inserted_frame = 0;
441                 memset(cx->vbi.sliced_mpeg_size,
442                         0, sizeof(cx->vbi.sliced_mpeg_size));
443                 break;
444         default:
445                 return -EINVAL;
446         }
447
448         /* mute/unmute video */
449         cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
450                   s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
451
452         /* Clear Streamoff flags in case left from last capture */
453         clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
454
455         cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
456         s->handle = data[0];
457         cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
458
459         if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
460                 /* Stuff from Windows, we don't know what it is */
461                 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
462                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
463                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
464                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
465                 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
466
467                 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
468                                s->handle, cx->digitizer, cx->digitizer);
469
470                 /* Setup VBI */
471                 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
472                         cx18_vbi_setup(s);
473
474                 /* assign program index info.
475                    Mask 7: select I/P/B, Num_req: 400 max */
476                 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
477
478                 /* Setup API for Stream */
479                 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
480         }
481
482         if (atomic_read(&cx->tot_capturing) == 0) {
483                 clear_bit(CX18_F_I_EOS, &cx->i_flags);
484                 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
485         }
486
487         cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
488                 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
489                 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
490
491         list_for_each(p, &s->q_free.list) {
492                 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
493
494                 cx18_writel(cx, buf->dma_handle,
495                                         &cx->scb->cpu_mdl[buf->id].paddr);
496                 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
497                 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
498                         (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
499                         1, buf->id, s->buf_size);
500         }
501         /* begin_capture */
502         if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
503                 CX18_DEBUG_WARN("Error starting capture!\n");
504                 /* Ensure we're really not capturing before releasing MDLs */
505                 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
506                         cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
507                 else
508                         cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
509                 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
510                 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
511                 /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
512                 return -EINVAL;
513         }
514
515         /* you're live! sit back and await interrupts :) */
516         if (!ts)
517                 atomic_inc(&cx->ana_capturing);
518         atomic_inc(&cx->tot_capturing);
519         return 0;
520 }
521
522 void cx18_stop_all_captures(struct cx18 *cx)
523 {
524         int i;
525
526         for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
527                 struct cx18_stream *s = &cx->streams[i];
528
529                 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
530                         continue;
531                 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
532                         cx18_stop_v4l2_encode_stream(s, 0);
533         }
534 }
535
536 int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
537 {
538         struct cx18 *cx = s->cx;
539         unsigned long then;
540
541         if (s->v4l2dev == NULL && s->dvb.enabled == 0)
542                 return -EINVAL;
543
544         /* This function assumes that you are allowed to stop the capture
545            and that we are actually capturing */
546
547         CX18_DEBUG_INFO("Stop Capture\n");
548
549         if (atomic_read(&cx->tot_capturing) == 0)
550                 return 0;
551
552         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
553                 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
554         else
555                 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
556
557         then = jiffies;
558
559         if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
560                 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
561         }
562
563         if (s->type != CX18_ENC_STREAM_TYPE_TS)
564                 atomic_dec(&cx->ana_capturing);
565         atomic_dec(&cx->tot_capturing);
566
567         /* Clear capture and no-read bits */
568         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
569
570         /* Tell the CX23418 it can't use our buffers anymore */
571         cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
572
573         cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
574         s->handle = CX18_INVALID_TASK_HANDLE;
575
576         if (atomic_read(&cx->tot_capturing) > 0)
577                 return 0;
578
579         cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
580         wake_up(&s->waitq);
581
582         return 0;
583 }
584
585 u32 cx18_find_handle(struct cx18 *cx)
586 {
587         int i;
588
589         /* find first available handle to be used for global settings */
590         for (i = 0; i < CX18_MAX_STREAMS; i++) {
591                 struct cx18_stream *s = &cx->streams[i];
592
593                 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
594                         return s->handle;
595         }
596         return CX18_INVALID_TASK_HANDLE;
597 }
598
599 struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
600 {
601         int i;
602         struct cx18_stream *s;
603
604         if (handle == CX18_INVALID_TASK_HANDLE)
605                 return NULL;
606
607         for (i = 0; i < CX18_MAX_STREAMS; i++) {
608                 s = &cx->streams[i];
609                 if (s->handle != handle)
610                         continue;
611                 if (s->v4l2dev || s->dvb.enabled)
612                         return s;
613         }
614         return NULL;
615 }