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