Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[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  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  *  02111-1307  USA
22  */
23
24 #include "cx18-driver.h"
25 #include "cx18-fileops.h"
26 #include "cx18-i2c.h"
27 #include "cx18-queue.h"
28 #include "cx18-vbi.h"
29 #include "cx18-audio.h"
30 #include "cx18-mailbox.h"
31 #include "cx18-scb.h"
32 #include "cx18-streams.h"
33 #include "cx18-controls.h"
34 #include "cx18-ioctl.h"
35 #include "cx18-cards.h"
36
37 /* This function tries to claim the stream for a specific file descriptor.
38    If no one else is using this stream then the stream is claimed and
39    associated VBI streams are also automatically claimed.
40    Possible error returns: -EBUSY if someone else has claimed
41    the stream or 0 on success. */
42 static int cx18_claim_stream(struct cx18_open_id *id, int type)
43 {
44         struct cx18 *cx = id->cx;
45         struct cx18_stream *s = &cx->streams[type];
46         struct cx18_stream *s_vbi;
47         int vbi_type;
48
49         if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
50                 /* someone already claimed this stream */
51                 if (s->id == id->open_id) {
52                         /* yes, this file descriptor did. So that's OK. */
53                         return 0;
54                 }
55                 if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
56                         /* VBI is handled already internally, now also assign
57                            the file descriptor to this stream for external
58                            reading of the stream. */
59                         s->id = id->open_id;
60                         CX18_DEBUG_INFO("Start Read VBI\n");
61                         return 0;
62                 }
63                 /* someone else is using this stream already */
64                 CX18_DEBUG_INFO("Stream %d is busy\n", type);
65                 return -EBUSY;
66         }
67         s->id = id->open_id;
68
69         /* CX18_DEC_STREAM_TYPE_MPG needs to claim CX18_DEC_STREAM_TYPE_VBI,
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 && cx->vbi.sliced_in->service_set) {
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_enqueue(s_vbi, buf, &s_vbi->q_free);
190                                 }
191                         }
192                         buf = &cx->vbi.sliced_mpeg_buf;
193                         if (buf->readpos != buf->bytesused)
194                                 return buf;
195                 }
196
197                 /* do we have leftover data? */
198                 buf = cx18_dequeue(s, &s->q_io);
199                 if (buf)
200                         return buf;
201
202                 /* do we have new data? */
203                 buf = cx18_dequeue(s, &s->q_full);
204                 if (buf) {
205                         if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
206                                                 &buf->b_flags))
207                                 return buf;
208                         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
209                                 /* byteswap MPG data */
210                                 cx18_buf_swap(buf);
211                         else {
212                                 /* byteswap and process VBI data */
213                                 cx18_process_vbi_data(cx, buf,
214                                                 s->dma_pts, s->type);
215                         }
216                         return buf;
217                 }
218
219                 /* return if end of stream */
220                 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
221                         CX18_DEBUG_INFO("EOS %s\n", s->name);
222                         return NULL;
223                 }
224
225                 /* return if file was opened with O_NONBLOCK */
226                 if (non_block) {
227                         *err = -EAGAIN;
228                         return NULL;
229                 }
230
231                 /* wait for more data to arrive */
232                 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
233                 /* New buffers might have become available before we were added
234                    to the waitqueue */
235                 if (!atomic_read(&s->q_full.buffers))
236                         schedule();
237                 finish_wait(&s->waitq, &wait);
238                 if (signal_pending(current)) {
239                         /* return if a signal was received */
240                         CX18_DEBUG_INFO("User stopped %s\n", s->name);
241                         *err = -EINTR;
242                         return NULL;
243                 }
244         }
245 }
246
247 static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
248 {
249         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
250
251         cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
252         cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
253         cx->vbi.sliced_mpeg_buf.readpos = 0;
254 }
255
256 static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
257                 struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
258 {
259         struct cx18 *cx = s->cx;
260         size_t len = buf->bytesused - buf->readpos;
261
262         if (len > ucount)
263                 len = ucount;
264         if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
265             cx->vbi.sliced_in->service_set && buf != &cx->vbi.sliced_mpeg_buf) {
266                 const char *start = buf->buf + buf->readpos;
267                 const char *p = start + 1;
268                 const u8 *q;
269                 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
270                 int stuffing, i;
271
272                 while (start + len > p) {
273                         q = memchr(p, 0, start + len - p);
274                         if (q == NULL)
275                                 break;
276                         p = q + 1;
277                         if ((char *)q + 15 >= buf->buf + buf->bytesused ||
278                             q[1] != 0 || q[2] != 1 || q[3] != ch)
279                                 continue;
280                         if (!cx->search_pack_header) {
281                                 if ((q[6] & 0xc0) != 0x80)
282                                         continue;
283                                 if (((q[7] & 0xc0) == 0x80 &&
284                                      (q[9] & 0xf0) == 0x20) ||
285                                     ((q[7] & 0xc0) == 0xc0 &&
286                                      (q[9] & 0xf0) == 0x30)) {
287                                         ch = 0xba;
288                                         cx->search_pack_header = 1;
289                                         p = q + 9;
290                                 }
291                                 continue;
292                         }
293                         stuffing = q[13] & 7;
294                         /* all stuffing bytes must be 0xff */
295                         for (i = 0; i < stuffing; i++)
296                                 if (q[14 + i] != 0xff)
297                                         break;
298                         if (i == stuffing &&
299                             (q[4] & 0xc4) == 0x44 &&
300                             (q[12] & 3) == 3 &&
301                             q[14 + stuffing] == 0 &&
302                             q[15 + stuffing] == 0 &&
303                             q[16 + stuffing] == 1) {
304                                 cx->search_pack_header = 0;
305                                 len = (char *)q - start;
306                                 cx18_setup_sliced_vbi_buf(cx);
307                                 break;
308                         }
309                 }
310         }
311         if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
312                 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
313                                 len, s->name);
314                 return -EFAULT;
315         }
316         buf->readpos += len;
317         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
318             buf != &cx->vbi.sliced_mpeg_buf)
319                 cx->mpg_data_received += len;
320         return len;
321 }
322
323 static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
324                 size_t tot_count, int non_block)
325 {
326         struct cx18 *cx = s->cx;
327         size_t tot_written = 0;
328         int single_frame = 0;
329
330         if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
331                 /* shouldn't happen */
332                 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
333                                 s->name);
334                 return -EIO;
335         }
336
337         /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
338            frames should arrive one-by-one, so make sure we never output more
339            than one VBI frame at a time */
340         if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
341             cx->vbi.sliced_in->service_set)
342                 single_frame = 1;
343
344         for (;;) {
345                 struct cx18_buffer *buf;
346                 int rc;
347
348                 buf = cx18_get_buffer(s, non_block, &rc);
349                 /* if there is no data available... */
350                 if (buf == NULL) {
351                         /* if we got data, then return that regardless */
352                         if (tot_written)
353                                 break;
354                         /* EOS condition */
355                         if (rc == 0) {
356                                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
357                                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
358                                 cx18_release_stream(s);
359                         }
360                         /* set errno */
361                         return rc;
362                 }
363
364                 rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
365                                 tot_count - tot_written);
366
367                 if (buf != &cx->vbi.sliced_mpeg_buf) {
368                         if (buf->readpos == buf->bytesused) {
369                                 cx18_buf_sync_for_device(s, buf);
370                                 cx18_enqueue(s, buf, &s->q_free);
371                                 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5,
372                                         s->handle,
373                                         (void __iomem *)&cx->scb->cpu_mdl[buf->id] -
374                                           cx->enc_mem,
375                                         1, buf->id, s->buf_size);
376                         } else
377                                 cx18_enqueue(s, buf, &s->q_io);
378                 } else if (buf->readpos == buf->bytesused) {
379                         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
380
381                         cx->vbi.sliced_mpeg_size[idx] = 0;
382                         cx->vbi.inserted_frame++;
383                         cx->vbi_data_inserted += buf->bytesused;
384                 }
385                 if (rc < 0)
386                         return rc;
387                 tot_written += rc;
388
389                 if (tot_written == tot_count || single_frame)
390                         break;
391         }
392         return tot_written;
393 }
394
395 static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
396                 size_t count, loff_t *pos, int non_block)
397 {
398         ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
399         struct cx18 *cx = s->cx;
400
401         CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
402         if (rc > 0)
403                 pos += rc;
404         return rc;
405 }
406
407 int cx18_start_capture(struct cx18_open_id *id)
408 {
409         struct cx18 *cx = id->cx;
410         struct cx18_stream *s = &cx->streams[id->type];
411         struct cx18_stream *s_vbi;
412
413         if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
414                 /* you cannot read from these stream types. */
415                 return -EPERM;
416         }
417
418         /* Try to claim this stream. */
419         if (cx18_claim_stream(id, s->type))
420                 return -EBUSY;
421
422         /* If capture is already in progress, then we also have to
423            do nothing extra. */
424         if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
425             test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
426                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
427                 return 0;
428         }
429
430         /* Start VBI capture if required */
431         s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
432         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
433             test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
434             !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
435                 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
436                    automatically when the MPG stream is claimed.
437                    We only need to start the VBI capturing. */
438                 if (cx18_start_v4l2_encode_stream(s_vbi)) {
439                         CX18_DEBUG_WARN("VBI capture start failed\n");
440
441                         /* Failure, clean up and return an error */
442                         clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
443                         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
444                         /* also releases the associated VBI stream */
445                         cx18_release_stream(s);
446                         return -EIO;
447                 }
448                 CX18_DEBUG_INFO("VBI insertion started\n");
449         }
450
451         /* Tell the card to start capturing */
452         if (!cx18_start_v4l2_encode_stream(s)) {
453                 /* We're done */
454                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
455                 /* Resume a possibly paused encoder */
456                 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
457                         cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
458                 return 0;
459         }
460
461         /* failure, clean up */
462         CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
463
464         /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
465            automatically when the MPG stream is released.
466            We only need to stop the VBI capturing. */
467         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
468             test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
469                 cx18_stop_v4l2_encode_stream(s_vbi, 0);
470                 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
471         }
472         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
473         cx18_release_stream(s);
474         return -EIO;
475 }
476
477 ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
478                 loff_t *pos)
479 {
480         struct cx18_open_id *id = filp->private_data;
481         struct cx18 *cx = id->cx;
482         struct cx18_stream *s = &cx->streams[id->type];
483         int rc;
484
485         CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
486
487         mutex_lock(&cx->serialize_lock);
488         rc = cx18_start_capture(id);
489         mutex_unlock(&cx->serialize_lock);
490         if (rc)
491                 return rc;
492         return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
493 }
494
495 unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
496 {
497         struct cx18_open_id *id = filp->private_data;
498         struct cx18 *cx = id->cx;
499         struct cx18_stream *s = &cx->streams[id->type];
500         int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
501
502         /* Start a capture if there is none */
503         if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
504                 int rc;
505
506                 mutex_lock(&cx->serialize_lock);
507                 rc = cx18_start_capture(id);
508                 mutex_unlock(&cx->serialize_lock);
509                 if (rc) {
510                         CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
511                                         s->name, rc);
512                         return POLLERR;
513                 }
514                 CX18_DEBUG_FILE("Encoder poll started capture\n");
515         }
516
517         /* add stream's waitq to the poll list */
518         CX18_DEBUG_HI_FILE("Encoder poll\n");
519         poll_wait(filp, &s->waitq, wait);
520
521         if (atomic_read(&s->q_full.buffers) || atomic_read(&s->q_io.buffers))
522                 return POLLIN | POLLRDNORM;
523         if (eof)
524                 return POLLHUP;
525         return 0;
526 }
527
528 void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
529 {
530         struct cx18 *cx = id->cx;
531         struct cx18_stream *s = &cx->streams[id->type];
532
533         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
534
535         /* 'Unclaim' this stream */
536
537         /* Stop capturing */
538         if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
539                 struct cx18_stream *s_vbi =
540                         &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
541
542                 CX18_DEBUG_INFO("close stopping capture\n");
543                 /* Special case: a running VBI capture for VBI insertion
544                    in the mpeg stream. Need to stop that too. */
545                 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
546                     test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
547                     !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
548                         CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
549                         cx18_stop_v4l2_encode_stream(s_vbi, 0);
550                 }
551                 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
552                     test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
553                         /* Also used internally, don't stop capturing */
554                         s->id = -1;
555                 else
556                         cx18_stop_v4l2_encode_stream(s, gop_end);
557         }
558         if (!gop_end) {
559                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
560                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
561                 cx18_release_stream(s);
562         }
563 }
564
565 int cx18_v4l2_close(struct inode *inode, struct file *filp)
566 {
567         struct cx18_open_id *id = filp->private_data;
568         struct cx18 *cx = id->cx;
569         struct cx18_stream *s = &cx->streams[id->type];
570
571         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
572
573         v4l2_prio_close(&cx->prio, &id->prio);
574
575         /* Easy case first: this stream was never claimed by us */
576         if (s->id != id->open_id) {
577                 kfree(id);
578                 return 0;
579         }
580
581         /* 'Unclaim' this stream */
582
583         /* Stop radio */
584         mutex_lock(&cx->serialize_lock);
585         if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
586                 /* Closing radio device, return to TV mode */
587                 cx18_mute(cx);
588                 /* Mark that the radio is no longer in use */
589                 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
590                 /* Switch tuner to TV */
591                 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
592                 /* Select correct audio input (i.e. TV tuner or Line in) */
593                 cx18_audio_set_io(cx);
594                 if (atomic_read(&cx->ana_capturing) > 0) {
595                         /* Undo video mute */
596                         cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
597                                 cx->params.video_mute |
598                                         (cx->params.video_mute_yuv << 8));
599                 }
600                 /* Done! Unmute and continue. */
601                 cx18_unmute(cx);
602                 cx18_release_stream(s);
603         } else {
604                 cx18_stop_capture(id, 0);
605         }
606         kfree(id);
607         mutex_unlock(&cx->serialize_lock);
608         return 0;
609 }
610
611 static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
612 {
613         struct cx18 *cx = s->cx;
614         struct cx18_open_id *item;
615
616         CX18_DEBUG_FILE("open %s\n", s->name);
617
618         /* Allocate memory */
619         item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
620         if (NULL == item) {
621                 CX18_DEBUG_WARN("nomem on v4l2 open\n");
622                 return -ENOMEM;
623         }
624         item->cx = cx;
625         item->type = s->type;
626         v4l2_prio_open(&cx->prio, &item->prio);
627
628         item->open_id = cx->open_id++;
629         filp->private_data = item;
630
631         if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
632                 /* Try to claim this stream */
633                 if (cx18_claim_stream(item, item->type)) {
634                         /* No, it's already in use */
635                         kfree(item);
636                         return -EBUSY;
637                 }
638
639                 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
640                         if (atomic_read(&cx->ana_capturing) > 0) {
641                                 /* switching to radio while capture is
642                                    in progress is not polite */
643                                 cx18_release_stream(s);
644                                 kfree(item);
645                                 return -EBUSY;
646                         }
647                 }
648
649                 /* Mark that the radio is being used. */
650                 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
651                 /* We have the radio */
652                 cx18_mute(cx);
653                 /* Switch tuner to radio */
654                 cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
655                 /* Select the correct audio input (i.e. radio tuner) */
656                 cx18_audio_set_io(cx);
657                 /* Done! Unmute and continue. */
658                 cx18_unmute(cx);
659         }
660         return 0;
661 }
662
663 int cx18_v4l2_open(struct inode *inode, struct file *filp)
664 {
665         int res, x, y = 0;
666         struct cx18 *cx = NULL;
667         struct cx18_stream *s = NULL;
668         int minor = iminor(inode);
669
670         /* Find which card this open was on */
671         spin_lock(&cx18_cards_lock);
672         for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
673                 /* find out which stream this open was on */
674                 for (y = 0; y < CX18_MAX_STREAMS; y++) {
675                         if (cx18_cards[x] == NULL)
676                                 continue;
677                         s = &cx18_cards[x]->streams[y];
678                         if (s->v4l2dev && s->v4l2dev->minor == minor) {
679                                 cx = cx18_cards[x];
680                                 break;
681                         }
682                 }
683         }
684         spin_unlock(&cx18_cards_lock);
685
686         if (cx == NULL) {
687                 /* Couldn't find a device registered
688                    on that minor, shouldn't happen! */
689                 printk(KERN_WARNING "No cx18 device found on minor %d\n",
690                                 minor);
691                 return -ENXIO;
692         }
693
694         mutex_lock(&cx->serialize_lock);
695         if (cx18_init_on_first_open(cx)) {
696                 CX18_ERR("Failed to initialize on minor %d\n", minor);
697                 mutex_unlock(&cx->serialize_lock);
698                 return -ENXIO;
699         }
700         res = cx18_serialized_open(s, filp);
701         mutex_unlock(&cx->serialize_lock);
702         return res;
703 }
704
705 void cx18_mute(struct cx18 *cx)
706 {
707         u32 h;
708         if (atomic_read(&cx->ana_capturing)) {
709                 h = cx18_find_handle(cx);
710                 if (h != CX18_INVALID_TASK_HANDLE)
711                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
712                 else
713                         CX18_ERR("Can't find valid task handle for mute\n");
714         }
715         CX18_DEBUG_INFO("Mute\n");
716 }
717
718 void cx18_unmute(struct cx18 *cx)
719 {
720         u32 h;
721         if (atomic_read(&cx->ana_capturing)) {
722                 h = cx18_find_handle(cx);
723                 if (h != CX18_INVALID_TASK_HANDLE) {
724                         cx18_msleep_timeout(100, 0);
725                         cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
726                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
727                 } else
728                         CX18_ERR("Can't find valid task handle for unmute\n");
729         }
730         CX18_DEBUG_INFO("Unmute\n");
731 }