V4L/DVB (9805): cx18: Port fix for raw/sliced VBI mixup from ivtv and cx25840
[linux-2.6] / drivers / media / video / cx18 / cx18-fileops.c
1 /*
2  *  cx18 file operation functions
3  *
4  *  Derived from ivtv-fileops.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-fileops.h"
27 #include "cx18-i2c.h"
28 #include "cx18-queue.h"
29 #include "cx18-vbi.h"
30 #include "cx18-audio.h"
31 #include "cx18-mailbox.h"
32 #include "cx18-scb.h"
33 #include "cx18-streams.h"
34 #include "cx18-controls.h"
35 #include "cx18-ioctl.h"
36 #include "cx18-cards.h"
37
38 /* This function tries to claim the stream for a specific file descriptor.
39    If no one else is using this stream then the stream is claimed and
40    associated VBI streams are also automatically claimed.
41    Possible error returns: -EBUSY if someone else has claimed
42    the stream or 0 on success. */
43 static int cx18_claim_stream(struct cx18_open_id *id, int type)
44 {
45         struct cx18 *cx = id->cx;
46         struct cx18_stream *s = &cx->streams[type];
47         struct cx18_stream *s_vbi;
48         int vbi_type;
49
50         if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
51                 /* someone already claimed this stream */
52                 if (s->id == id->open_id) {
53                         /* yes, this file descriptor did. So that's OK. */
54                         return 0;
55                 }
56                 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
57                         /* VBI is handled already internally, now also assign
58                            the file descriptor to this stream for external
59                            reading of the stream. */
60                         s->id = id->open_id;
61                         CX18_DEBUG_INFO("Start Read VBI\n");
62                         return 0;
63                 }
64                 /* someone else is using this stream already */
65                 CX18_DEBUG_INFO("Stream %d is busy\n", type);
66                 return -EBUSY;
67         }
68         s->id = id->open_id;
69
70         /* CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
71            (provided VBI insertion is on and sliced VBI is selected), for all
72            other streams we're done */
73         if (type == CX18_ENC_STREAM_TYPE_MPG &&
74             cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) {
75                 vbi_type = CX18_ENC_STREAM_TYPE_VBI;
76         } else {
77                 return 0;
78         }
79         s_vbi = &cx->streams[vbi_type];
80
81         set_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
82
83         /* mark that it is used internally */
84         set_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags);
85         return 0;
86 }
87
88 /* This function releases a previously claimed stream. It will take into
89    account associated VBI streams. */
90 static void cx18_release_stream(struct cx18_stream *s)
91 {
92         struct cx18 *cx = s->cx;
93         struct cx18_stream *s_vbi;
94
95         s->id = -1;
96         if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
97                 test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
98                 /* this stream is still in use internally */
99                 return;
100         }
101         if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
102                 CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
103                 return;
104         }
105
106         cx18_flush_queues(s);
107
108         /* CX18_ENC_STREAM_TYPE_MPG needs to release CX18_ENC_STREAM_TYPE_VBI,
109            for all other streams we're done */
110         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
111                 s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
112         else
113                 return;
114
115         /* clear internal use flag */
116         if (!test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
117                 /* was already cleared */
118                 return;
119         }
120         if (s_vbi->id != -1) {
121                 /* VBI stream still claimed by a file descriptor */
122                 return;
123         }
124         clear_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
125         cx18_flush_queues(s_vbi);
126 }
127
128 static void cx18_dualwatch(struct cx18 *cx)
129 {
130         struct v4l2_tuner vt;
131         u16 new_bitmap;
132         u16 new_stereo_mode;
133         const u16 stereo_mask = 0x0300;
134         const u16 dual = 0x0200;
135         u32 h;
136
137         new_stereo_mode = cx->params.audio_properties & stereo_mask;
138         memset(&vt, 0, sizeof(vt));
139         cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, &vt);
140         if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
141                         (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
142                 new_stereo_mode = dual;
143
144         if (new_stereo_mode == cx->dualwatch_stereo_mode)
145                 return;
146
147         new_bitmap = new_stereo_mode
148                         | (cx->params.audio_properties & ~stereo_mask);
149
150         CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. "
151                         "new audio_bitmask=0x%ux\n",
152                         cx->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
153
154         h = cx18_find_handle(cx);
155         if (h == CX18_INVALID_TASK_HANDLE) {
156                 CX18_DEBUG_INFO("dualwatch: can't find valid task handle\n");
157                 return;
158         }
159
160         if (cx18_vapi(cx,
161                       CX18_CPU_SET_AUDIO_PARAMETERS, 2, h, new_bitmap) == 0) {
162                 cx->dualwatch_stereo_mode = new_stereo_mode;
163                 return;
164         }
165         CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
166 }
167
168
169 static struct cx18_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
170 {
171         struct cx18 *cx = s->cx;
172         struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
173         struct cx18_buffer *buf;
174         DEFINE_WAIT(wait);
175
176         *err = 0;
177         while (1) {
178                 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
179
180                         if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
181                                 cx->dualwatch_jiffies = jiffies;
182                                 cx18_dualwatch(cx);
183                         }
184                         if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
185                             !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
186                                 while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
187                                         /* byteswap and process VBI data */
188 /*                                      cx18_process_vbi_data(cx, buf, s_vbi->dma_pts, s_vbi->type); */
189                                         cx18_stream_put_buf_fw(s_vbi, buf);
190                                 }
191                         }
192                         buf = &cx->vbi.sliced_mpeg_buf;
193                         if (buf->readpos != buf->bytesused)
194                                 return buf;
195                 }
196
197                 /* do we have new data? */
198                 buf = cx18_dequeue(s, &s->q_full);
199                 if (buf) {
200                         if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
201                                                 &buf->b_flags))
202                                 return buf;
203                         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
204                                 /* byteswap MPG data */
205                                 cx18_buf_swap(buf);
206                         else {
207                                 /* byteswap and process VBI data */
208                                 cx18_process_vbi_data(cx, buf,
209                                                 s->dma_pts, s->type);
210                         }
211                         return buf;
212                 }
213
214                 /* return if end of stream */
215                 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
216                         CX18_DEBUG_INFO("EOS %s\n", s->name);
217                         return NULL;
218                 }
219
220                 /* return if file was opened with O_NONBLOCK */
221                 if (non_block) {
222                         *err = -EAGAIN;
223                         return NULL;
224                 }
225
226                 /* wait for more data to arrive */
227                 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
228                 /* New buffers might have become available before we were added
229                    to the waitqueue */
230                 if (!atomic_read(&s->q_full.buffers))
231                         schedule();
232                 finish_wait(&s->waitq, &wait);
233                 if (signal_pending(current)) {
234                         /* return if a signal was received */
235                         CX18_DEBUG_INFO("User stopped %s\n", s->name);
236                         *err = -EINTR;
237                         return NULL;
238                 }
239         }
240 }
241
242 static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
243 {
244         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
245
246         cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
247         cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
248         cx->vbi.sliced_mpeg_buf.readpos = 0;
249 }
250
251 static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
252                 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
253 {
254         struct cx18 *cx = s->cx;
255         size_t len = buf->bytesused - buf->readpos;
256
257         if (len > ucount)
258                 len = ucount;
259         if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
260             !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
261                 const char *start = buf->buf + buf->readpos;
262                 const char *p = start + 1;
263                 const u8 *q;
264                 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
265                 int stuffing, i;
266
267                 while (start + len > p) {
268                         q = memchr(p, 0, start + len - p);
269                         if (q == NULL)
270                                 break;
271                         p = q + 1;
272                         if ((char *)q + 15 >= buf->buf + buf->bytesused ||
273                             q[1] != 0 || q[2] != 1 || q[3] != ch)
274                                 continue;
275                         if (!cx->search_pack_header) {
276                                 if ((q[6] & 0xc0) != 0x80)
277                                         continue;
278                                 if (((q[7] & 0xc0) == 0x80 &&
279                                      (q[9] & 0xf0) == 0x20) ||
280                                     ((q[7] & 0xc0) == 0xc0 &&
281                                      (q[9] & 0xf0) == 0x30)) {
282                                         ch = 0xba;
283                                         cx->search_pack_header = 1;
284                                         p = q + 9;
285                                 }
286                                 continue;
287                         }
288                         stuffing = q[13] & 7;
289                         /* all stuffing bytes must be 0xff */
290                         for (i = 0; i < stuffing; i++)
291                                 if (q[14 + i] != 0xff)
292                                         break;
293                         if (i == stuffing &&
294                             (q[4] & 0xc4) == 0x44 &&
295                             (q[12] & 3) == 3 &&
296                             q[14 + stuffing] == 0 &&
297                             q[15 + stuffing] == 0 &&
298                             q[16 + stuffing] == 1) {
299                                 cx->search_pack_header = 0;
300                                 len = (char *)q - start;
301                                 cx18_setup_sliced_vbi_buf(cx);
302                                 break;
303                         }
304                 }
305         }
306         if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
307                 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
308                                 len, s->name);
309                 return -EFAULT;
310         }
311         buf->readpos += len;
312         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
313             buf != &cx->vbi.sliced_mpeg_buf)
314                 cx->mpg_data_received += len;
315         return len;
316 }
317
318 static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
319                 size_t tot_count, int non_block)
320 {
321         struct cx18 *cx = s->cx;
322         size_t tot_written = 0;
323         int single_frame = 0;
324
325         if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
326                 /* shouldn't happen */
327                 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
328                                 s->name);
329                 return -EIO;
330         }
331
332         /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
333            frames should arrive one-by-one, so make sure we never output more
334            than one VBI frame at a time */
335         if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
336                 single_frame = 1;
337
338         for (;;) {
339                 struct cx18_buffer *buf;
340                 int rc;
341
342                 buf = cx18_get_buffer(s, non_block, &rc);
343                 /* if there is no data available... */
344                 if (buf == NULL) {
345                         /* if we got data, then return that regardless */
346                         if (tot_written)
347                                 break;
348                         /* EOS condition */
349                         if (rc == 0) {
350                                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
351                                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
352                                 cx18_release_stream(s);
353                         }
354                         /* set errno */
355                         return rc;
356                 }
357
358                 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
359                                 tot_count - tot_written);
360
361                 if (buf != &cx->vbi.sliced_mpeg_buf) {
362                         if (buf->readpos == buf->bytesused)
363                                 cx18_stream_put_buf_fw(s, buf);
364                         else
365                                 cx18_push(s, buf, &s->q_full);
366                 } else if (buf->readpos == buf->bytesused) {
367                         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
368
369                         cx->vbi.sliced_mpeg_size[idx] = 0;
370                         cx->vbi.inserted_frame++;
371                         cx->vbi_data_inserted += buf->bytesused;
372                 }
373                 if (rc < 0)
374                         return rc;
375                 tot_written += rc;
376
377                 if (tot_written == tot_count || single_frame)
378                         break;
379         }
380         return tot_written;
381 }
382
383 static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
384                 size_t count, loff_t *pos, int non_block)
385 {
386         ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
387         struct cx18 *cx = s->cx;
388
389         CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
390         if (rc > 0)
391                 pos += rc;
392         return rc;
393 }
394
395 int cx18_start_capture(struct cx18_open_id *id)
396 {
397         struct cx18 *cx = id->cx;
398         struct cx18_stream *s = &cx->streams[id->type];
399         struct cx18_stream *s_vbi;
400
401         if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
402                 /* you cannot read from these stream types. */
403                 return -EPERM;
404         }
405
406         /* Try to claim this stream. */
407         if (cx18_claim_stream(id, s->type))
408                 return -EBUSY;
409
410         /* If capture is already in progress, then we also have to
411            do nothing extra. */
412         if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
413             test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
414                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
415                 return 0;
416         }
417
418         /* Start VBI capture if required */
419         s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
420         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
421             test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
422             !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
423                 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
424                    automatically when the MPG stream is claimed.
425                    We only need to start the VBI capturing. */
426                 if (cx18_start_v4l2_encode_stream(s_vbi)) {
427                         CX18_DEBUG_WARN("VBI capture start failed\n");
428
429                         /* Failure, clean up and return an error */
430                         clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
431                         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
432                         /* also releases the associated VBI stream */
433                         cx18_release_stream(s);
434                         return -EIO;
435                 }
436                 CX18_DEBUG_INFO("VBI insertion started\n");
437         }
438
439         /* Tell the card to start capturing */
440         if (!cx18_start_v4l2_encode_stream(s)) {
441                 /* We're done */
442                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
443                 /* Resume a possibly paused encoder */
444                 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
445                         cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
446                 return 0;
447         }
448
449         /* failure, clean up */
450         CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
451
452         /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
453            automatically when the MPG stream is released.
454            We only need to stop the VBI capturing. */
455         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
456             test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
457                 cx18_stop_v4l2_encode_stream(s_vbi, 0);
458                 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
459         }
460         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
461         cx18_release_stream(s);
462         return -EIO;
463 }
464
465 ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
466                 loff_t *pos)
467 {
468         struct cx18_open_id *id = filp->private_data;
469         struct cx18 *cx = id->cx;
470         struct cx18_stream *s = &cx->streams[id->type];
471         int rc;
472
473         CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
474
475         mutex_lock(&cx->serialize_lock);
476         rc = cx18_start_capture(id);
477         mutex_unlock(&cx->serialize_lock);
478         if (rc)
479                 return rc;
480         return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
481 }
482
483 unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
484 {
485         struct cx18_open_id *id = filp->private_data;
486         struct cx18 *cx = id->cx;
487         struct cx18_stream *s = &cx->streams[id->type];
488         int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
489
490         /* Start a capture if there is none */
491         if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
492                 int rc;
493
494                 mutex_lock(&cx->serialize_lock);
495                 rc = cx18_start_capture(id);
496                 mutex_unlock(&cx->serialize_lock);
497                 if (rc) {
498                         CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
499                                         s->name, rc);
500                         return POLLERR;
501                 }
502                 CX18_DEBUG_FILE("Encoder poll started capture\n");
503         }
504
505         /* add stream's waitq to the poll list */
506         CX18_DEBUG_HI_FILE("Encoder poll\n");
507         poll_wait(filp, &s->waitq, wait);
508
509         if (atomic_read(&s->q_full.buffers))
510                 return POLLIN | POLLRDNORM;
511         if (eof)
512                 return POLLHUP;
513         return 0;
514 }
515
516 void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
517 {
518         struct cx18 *cx = id->cx;
519         struct cx18_stream *s = &cx->streams[id->type];
520
521         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
522
523         /* 'Unclaim' this stream */
524
525         /* Stop capturing */
526         if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
527                 struct cx18_stream *s_vbi =
528                         &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
529
530                 CX18_DEBUG_INFO("close stopping capture\n");
531                 /* Special case: a running VBI capture for VBI insertion
532                    in the mpeg stream. Need to stop that too. */
533                 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
534                     test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
535                     !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
536                         CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
537                         cx18_stop_v4l2_encode_stream(s_vbi, 0);
538                 }
539                 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
540                     test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
541                         /* Also used internally, don't stop capturing */
542                         s->id = -1;
543                 else
544                         cx18_stop_v4l2_encode_stream(s, gop_end);
545         }
546         if (!gop_end) {
547                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
548                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
549                 cx18_release_stream(s);
550         }
551 }
552
553 int cx18_v4l2_close(struct inode *inode, struct file *filp)
554 {
555         struct cx18_open_id *id = filp->private_data;
556         struct cx18 *cx = id->cx;
557         struct cx18_stream *s = &cx->streams[id->type];
558
559         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
560
561         v4l2_prio_close(&cx->prio, &id->prio);
562
563         /* Easy case first: this stream was never claimed by us */
564         if (s->id != id->open_id) {
565                 kfree(id);
566                 return 0;
567         }
568
569         /* 'Unclaim' this stream */
570
571         /* Stop radio */
572         mutex_lock(&cx->serialize_lock);
573         if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
574                 /* Closing radio device, return to TV mode */
575                 cx18_mute(cx);
576                 /* Mark that the radio is no longer in use */
577                 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
578                 /* Switch tuner to TV */
579                 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
580                 /* Select correct audio input (i.e. TV tuner or Line in) */
581                 cx18_audio_set_io(cx);
582                 if (atomic_read(&cx->ana_capturing) > 0) {
583                         /* Undo video mute */
584                         cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
585                                 cx->params.video_mute |
586                                         (cx->params.video_mute_yuv << 8));
587                 }
588                 /* Done! Unmute and continue. */
589                 cx18_unmute(cx);
590                 cx18_release_stream(s);
591         } else {
592                 cx18_stop_capture(id, 0);
593         }
594         kfree(id);
595         mutex_unlock(&cx->serialize_lock);
596         return 0;
597 }
598
599 static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
600 {
601         struct cx18 *cx = s->cx;
602         struct cx18_open_id *item;
603
604         CX18_DEBUG_FILE("open %s\n", s->name);
605
606         /* Allocate memory */
607         item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
608         if (NULL == item) {
609                 CX18_DEBUG_WARN("nomem on v4l2 open\n");
610                 return -ENOMEM;
611         }
612         item->cx = cx;
613         item->type = s->type;
614         v4l2_prio_open(&cx->prio, &item->prio);
615
616         item->open_id = cx->open_id++;
617         filp->private_data = item;
618
619         if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
620                 /* Try to claim this stream */
621                 if (cx18_claim_stream(item, item->type)) {
622                         /* No, it's already in use */
623                         kfree(item);
624                         return -EBUSY;
625                 }
626
627                 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
628                         if (atomic_read(&cx->ana_capturing) > 0) {
629                                 /* switching to radio while capture is
630                                    in progress is not polite */
631                                 cx18_release_stream(s);
632                                 kfree(item);
633                                 return -EBUSY;
634                         }
635                 }
636
637                 /* Mark that the radio is being used. */
638                 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
639                 /* We have the radio */
640                 cx18_mute(cx);
641                 /* Switch tuner to radio */
642                 cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
643                 /* Select the correct audio input (i.e. radio tuner) */
644                 cx18_audio_set_io(cx);
645                 /* Done! Unmute and continue. */
646                 cx18_unmute(cx);
647         }
648         return 0;
649 }
650
651 int cx18_v4l2_open(struct inode *inode, struct file *filp)
652 {
653         int res, x, y = 0;
654         struct cx18 *cx = NULL;
655         struct cx18_stream *s = NULL;
656         int minor = iminor(inode);
657
658         /* Find which card this open was on */
659         spin_lock(&cx18_cards_lock);
660         for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
661                 /* find out which stream this open was on */
662                 for (y = 0; y < CX18_MAX_STREAMS; y++) {
663                         if (cx18_cards[x] == NULL)
664                                 continue;
665                         s = &cx18_cards[x]->streams[y];
666                         if (s->v4l2dev && s->v4l2dev->minor == minor) {
667                                 cx = cx18_cards[x];
668                                 break;
669                         }
670                 }
671         }
672         spin_unlock(&cx18_cards_lock);
673
674         if (cx == NULL) {
675                 /* Couldn't find a device registered
676                    on that minor, shouldn't happen! */
677                 printk(KERN_WARNING "No cx18 device found on minor %d\n",
678                                 minor);
679                 return -ENXIO;
680         }
681
682         mutex_lock(&cx->serialize_lock);
683         if (cx18_init_on_first_open(cx)) {
684                 CX18_ERR("Failed to initialize on minor %d\n", minor);
685                 mutex_unlock(&cx->serialize_lock);
686                 return -ENXIO;
687         }
688         res = cx18_serialized_open(s, filp);
689         mutex_unlock(&cx->serialize_lock);
690         return res;
691 }
692
693 void cx18_mute(struct cx18 *cx)
694 {
695         u32 h;
696         if (atomic_read(&cx->ana_capturing)) {
697                 h = cx18_find_handle(cx);
698                 if (h != CX18_INVALID_TASK_HANDLE)
699                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
700                 else
701                         CX18_ERR("Can't find valid task handle for mute\n");
702         }
703         CX18_DEBUG_INFO("Mute\n");
704 }
705
706 void cx18_unmute(struct cx18 *cx)
707 {
708         u32 h;
709         if (atomic_read(&cx->ana_capturing)) {
710                 h = cx18_find_handle(cx);
711                 if (h != CX18_INVALID_TASK_HANDLE) {
712                         cx18_msleep_timeout(100, 0);
713                         cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
714                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
715                 } else
716                         CX18_ERR("Can't find valid task handle for unmute\n");
717         }
718         CX18_DEBUG_INFO("Unmute\n");
719 }