V4L/DVB (9730): cx18: Quiet a sometimes common warning that often has benign consequences
[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         cx18_queue_init(&s->q_io);
142 }
143
144 static int cx18_prep_dev(struct cx18 *cx, int type)
145 {
146         struct cx18_stream *s = &cx->streams[type];
147         u32 cap = cx->v4l2_cap;
148         int num_offset = cx18_stream_info[type].num_offset;
149         int num = cx->num + cx18_first_minor + num_offset;
150
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
168         /* 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
178         if (num_offset == -1)
179                 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
189         snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
190                         cx->num);
191
192         s->v4l2dev->num = num;
193         s->v4l2dev->parent = &cx->dev->dev;
194         s->v4l2dev->fops = cx18_stream_info[type].fops;
195         s->v4l2dev->release = video_device_release;
196         s->v4l2dev->tvnorms = V4L2_STD_ALL;
197         cx18_set_funcs(s->v4l2dev);
198         return 0;
199 }
200
201 /* Initialize v4l2 variables and register v4l2 devices */
202 int cx18_streams_setup(struct cx18 *cx)
203 {
204         int type, ret;
205
206         /* Setup V4L2 Devices */
207         for (type = 0; type < CX18_MAX_STREAMS; type++) {
208                 /* Prepare device */
209                 ret = cx18_prep_dev(cx, type);
210                 if (ret < 0)
211                         break;
212
213                 /* Allocate Stream */
214                 ret = cx18_stream_alloc(&cx->streams[type]);
215                 if (ret < 0)
216                         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. */
222         cx18_streams_cleanup(cx, 0);
223         return ret;
224 }
225
226 static 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;
230         int num, ret;
231
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;
239                 ret = cx18_dvb_register(s);
240                 if (ret < 0) {
241                         CX18_ERR("DVB failed to register\n");
242                         return ret;
243                 }
244         }
245
246         if (s->v4l2dev == NULL)
247                 return 0;
248
249         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         }
257
258         /* Register device. First try the desired minor, then any free one. */
259         ret = video_register_device(s->v4l2dev, vfl_type, num);
260         if (ret < 0) {
261                 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
262                         s->name, num);
263                 video_device_release(s->v4l2dev);
264                 s->v4l2dev = NULL;
265                 return ret;
266         }
267         num = s->v4l2dev->num;
268
269         switch (vfl_type) {
270         case VFL_TYPE_GRABBER:
271                 CX18_INFO("Registered device video%d for %s (%d MB)\n",
272                         num, s->name, cx->options.megabytes[type]);
273                 break;
274
275         case VFL_TYPE_RADIO:
276                 CX18_INFO("Registered device radio%d for %s\n",
277                         num, s->name);
278                 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",
283                                 num,
284                                 s->name, cx->options.megabytes[type]);
285                 else
286                         CX18_INFO("Registered device vbi%d for %s\n",
287                                 num, s->name);
288                 break;
289         }
290
291         return 0;
292 }
293
294 /* Register v4l2 devices */
295 int cx18_streams_register(struct cx18 *cx)
296 {
297         int type;
298         int err;
299         int ret = 0;
300
301         /* Register V4L2 devices */
302         for (type = 0; type < CX18_MAX_STREAMS; type++) {
303                 err = cx18_reg_dev(cx, type);
304                 if (err && ret == 0)
305                         ret = err;
306         }
307
308         if (ret == 0)
309                 return 0;
310
311         /* One or more streams could not be initialized. Clean 'em all up. */
312         cx18_streams_cleanup(cx, 1);
313         return ret;
314 }
315
316 /* Unregister v4l2 devices */
317 void cx18_streams_cleanup(struct cx18 *cx, int unregister)
318 {
319         struct video_device *vdev;
320         int type;
321
322         /* Teardown all streams */
323         for (type = 0; type < CX18_MAX_STREAMS; type++) {
324                 if (cx->streams[type].dvb.enabled) {
325                         cx18_dvb_unregister(&cx->streams[type]);
326                         cx->streams[type].dvb.enabled = false;
327                 }
328
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
337                 /* Unregister or release device */
338                 if (unregister)
339                         video_unregister_device(vdev);
340                 else
341                         video_device_release(vdev);
342         }
343 }
344
345 static 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
405 int 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         }
448
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
460         if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
461                 /* 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
483         if (atomic_read(&cx->tot_capturing) == 0) {
484                 clear_bit(CX18_F_I_EOS, &cx->i_flags);
485                 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
486         }
487
488         cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
489                 (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);
491
492         list_for_each(p, &s->q_free.list) {
493                 struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
494
495                 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);
498                 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
499                         (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
500                         1, buf->id, s->buf_size);
501         }
502         /* begin_capture */
503         if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
504                 CX18_DEBUG_WARN("Error starting capture!\n");
505                 /* 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);
511                 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
512                 /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
513                 return -EINVAL;
514         }
515
516         /* you're live! sit back and await interrupts :) */
517         if (!ts)
518                 atomic_inc(&cx->ana_capturing);
519         atomic_inc(&cx->tot_capturing);
520         return 0;
521 }
522
523 void 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
537 int 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
550         if (atomic_read(&cx->tot_capturing) == 0)
551                 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
564         if (s->type != CX18_ENC_STREAM_TYPE_TS)
565                 atomic_dec(&cx->ana_capturing);
566         atomic_dec(&cx->tot_capturing);
567
568         /* Clear capture and no-read bits */
569         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
570
571         /* Tell the CX23418 it can't use our buffers anymore */
572         cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
573
574         cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
575         s->handle = CX18_INVALID_TASK_HANDLE;
576
577         if (atomic_read(&cx->tot_capturing) > 0)
578                 return 0;
579
580         cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
581         wake_up(&s->waitq);
582
583         return 0;
584 }
585
586 u32 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
594                 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
595                         return s->handle;
596         }
597         return CX18_INVALID_TASK_HANDLE;
598 }
599
600 struct 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 }