]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/media/video/cx18/cx18-fileops.c
Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
[mv-sheeva.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_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         u32 new_bitmap;
132         u32 new_stereo_mode;
133         const u32 stereo_mask = 0x0300;
134         const u32 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_all(cx, tuner, 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_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
170                                      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_mdl *mdl;
175         DEFINE_WAIT(wait);
176
177         *err = 0;
178         while (1) {
179                 if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
180                         /* Process pending program info updates and pending
181                            VBI data */
182
183                         if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
184                                 cx->dualwatch_jiffies = jiffies;
185                                 cx18_dualwatch(cx);
186                         }
187                         if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
188                             !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
189                                 while ((mdl = cx18_dequeue(s_vbi,
190                                                            &s_vbi->q_full))) {
191                                         /* byteswap and process VBI data */
192                                         cx18_process_vbi_data(cx, mdl,
193                                                               s_vbi->type);
194                                         cx18_stream_put_mdl_fw(s_vbi, mdl);
195                                 }
196                         }
197                         mdl = &cx->vbi.sliced_mpeg_mdl;
198                         if (mdl->readpos != mdl->bytesused)
199                                 return mdl;
200                 }
201
202                 /* do we have new data? */
203                 mdl = cx18_dequeue(s, &s->q_full);
204                 if (mdl) {
205                         if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
206                                                 &mdl->m_flags))
207                                 return mdl;
208                         if (s->type == CX18_ENC_STREAM_TYPE_MPG)
209                                 /* byteswap MPG data */
210                                 cx18_mdl_swap(mdl);
211                         else {
212                                 /* byteswap and process VBI data */
213                                 cx18_process_vbi_data(cx, mdl, s->type);
214                         }
215                         return mdl;
216                 }
217
218                 /* return if end of stream */
219                 if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
220                         CX18_DEBUG_INFO("EOS %s\n", s->name);
221                         return NULL;
222                 }
223
224                 /* return if file was opened with O_NONBLOCK */
225                 if (non_block) {
226                         *err = -EAGAIN;
227                         return NULL;
228                 }
229
230                 /* wait for more data to arrive */
231                 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
232                 /* New buffers might have become available before we were added
233                    to the waitqueue */
234                 if (!atomic_read(&s->q_full.depth))
235                         schedule();
236                 finish_wait(&s->waitq, &wait);
237                 if (signal_pending(current)) {
238                         /* return if a signal was received */
239                         CX18_DEBUG_INFO("User stopped %s\n", s->name);
240                         *err = -EINTR;
241                         return NULL;
242                 }
243         }
244 }
245
246 static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
247 {
248         struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
249         struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
250         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
251
252         buf->buf = cx->vbi.sliced_mpeg_data[idx];
253         buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
254         buf->readpos = 0;
255
256         mdl->curr_buf = NULL;
257         mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
258         mdl->readpos = 0;
259 }
260
261 static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
262         struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
263 {
264         struct cx18 *cx = s->cx;
265         size_t len = buf->bytesused - buf->readpos;
266
267         *stop = false;
268         if (len > ucount)
269                 len = ucount;
270         if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
271             !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
272                 /*
273                  * Try to find a good splice point in the PS, just before
274                  * an MPEG-2 Program Pack start code, and provide only
275                  * up to that point to the user, so it's easy to insert VBI data
276                  * the next time around.
277                  *
278                  * This will not work for an MPEG-2 TS and has only been
279                  * verified by analysis to work for an MPEG-2 PS.  Helen Buus
280                  * pointed out this works for the CX23416 MPEG-2 DVD compatible
281                  * stream, and research indicates both the MPEG 2 SVCD and DVD
282                  * stream types use an MPEG-2 PS container.
283                  */
284                 /*
285                  * An MPEG-2 Program Stream (PS) is a series of
286                  * MPEG-2 Program Packs terminated by an
287                  * MPEG Program End Code after the last Program Pack.
288                  * A Program Pack may hold a PS System Header packet and any
289                  * number of Program Elementary Stream (PES) Packets
290                  */
291                 const char *start = buf->buf + buf->readpos;
292                 const char *p = start + 1;
293                 const u8 *q;
294                 u8 ch = cx->search_pack_header ? 0xba : 0xe0;
295                 int stuffing, i;
296
297                 while (start + len > p) {
298                         /* Scan for a 0 to find a potential MPEG-2 start code */
299                         q = memchr(p, 0, start + len - p);
300                         if (q == NULL)
301                                 break;
302                         p = q + 1;
303                         /*
304                          * Keep looking if not a
305                          * MPEG-2 Pack header start code:  0x00 0x00 0x01 0xba
306                          * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
307                          */
308                         if ((char *)q + 15 >= buf->buf + buf->bytesused ||
309                             q[1] != 0 || q[2] != 1 || q[3] != ch)
310                                 continue;
311
312                         /* If expecting the primary video PES */
313                         if (!cx->search_pack_header) {
314                                 /* Continue if it couldn't be a PES packet */
315                                 if ((q[6] & 0xc0) != 0x80)
316                                         continue;
317                                 /* Check if a PTS or PTS & DTS follow */
318                                 if (((q[7] & 0xc0) == 0x80 &&  /* PTS only */
319                                      (q[9] & 0xf0) == 0x20) || /* PTS only */
320                                     ((q[7] & 0xc0) == 0xc0 &&  /* PTS & DTS */
321                                      (q[9] & 0xf0) == 0x30)) { /* DTS follows */
322                                         /* Assume we found the video PES hdr */
323                                         ch = 0xba; /* next want a Program Pack*/
324                                         cx->search_pack_header = 1;
325                                         p = q + 9; /* Skip this video PES hdr */
326                                 }
327                                 continue;
328                         }
329
330                         /* We may have found a Program Pack start code */
331
332                         /* Get the count of stuffing bytes & verify them */
333                         stuffing = q[13] & 7;
334                         /* all stuffing bytes must be 0xff */
335                         for (i = 0; i < stuffing; i++)
336                                 if (q[14 + i] != 0xff)
337                                         break;
338                         if (i == stuffing && /* right number of stuffing bytes*/
339                             (q[4] & 0xc4) == 0x44 && /* marker check */
340                             (q[12] & 3) == 3 &&  /* marker check */
341                             q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
342                             q[15 + stuffing] == 0 &&
343                             q[16 + stuffing] == 1) {
344                                 /* We declare we actually found a Program Pack*/
345                                 cx->search_pack_header = 0; /* expect vid PES */
346                                 len = (char *)q - start;
347                                 cx18_setup_sliced_vbi_mdl(cx);
348                                 *stop = true;
349                                 break;
350                         }
351                 }
352         }
353         if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
354                 CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
355                                 len, s->name);
356                 return -EFAULT;
357         }
358         buf->readpos += len;
359         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
360             buf != &cx->vbi.sliced_mpeg_buf)
361                 cx->mpg_data_received += len;
362         return len;
363 }
364
365 /**
366  * list_entry_is_past_end - check if a previous loop cursor is off list end
367  * @pos:        the type * previously used as a loop cursor.
368  * @head:       the head for your list.
369  * @member:     the name of the list_struct within the struct.
370  *
371  * Check if the entry's list_head is the head of the list, thus it's not a
372  * real entry but was the loop cursor that walked past the end
373  */
374 #define list_entry_is_past_end(pos, head, member) \
375         (&pos->member == (head))
376
377 static size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
378                 struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
379 {
380         size_t tot_written = 0;
381         int rc;
382         bool stop = false;
383
384         if (mdl->curr_buf == NULL)
385                 mdl->curr_buf = list_first_entry(&mdl->buf_list,
386                                                  struct cx18_buffer, list);
387
388         if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
389                 /*
390                  * For some reason we've exhausted the buffers, but the MDL
391                  * object still said some data was unread.
392                  * Fix that and bail out.
393                  */
394                 mdl->readpos = mdl->bytesused;
395                 return 0;
396         }
397
398         list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
399
400                 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
401                         continue;
402
403                 rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
404                                            ucount - tot_written, &stop);
405                 if (rc < 0)
406                         return rc;
407                 mdl->readpos += rc;
408                 tot_written += rc;
409
410                 if (stop ||     /* Forced stopping point for VBI insertion */
411                     tot_written >= ucount ||    /* Reader request statisfied */
412                     mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
413                     mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
414                         break;
415         }
416         return tot_written;
417 }
418
419 static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
420                 size_t tot_count, int non_block)
421 {
422         struct cx18 *cx = s->cx;
423         size_t tot_written = 0;
424         int single_frame = 0;
425
426         if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
427                 /* shouldn't happen */
428                 CX18_DEBUG_WARN("Stream %s not initialized before read\n",
429                                 s->name);
430                 return -EIO;
431         }
432
433         /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
434            frames should arrive one-by-one, so make sure we never output more
435            than one VBI frame at a time */
436         if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
437                 single_frame = 1;
438
439         for (;;) {
440                 struct cx18_mdl *mdl;
441                 int rc;
442
443                 mdl = cx18_get_mdl(s, non_block, &rc);
444                 /* if there is no data available... */
445                 if (mdl == NULL) {
446                         /* if we got data, then return that regardless */
447                         if (tot_written)
448                                 break;
449                         /* EOS condition */
450                         if (rc == 0) {
451                                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
452                                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
453                                 cx18_release_stream(s);
454                         }
455                         /* set errno */
456                         return rc;
457                 }
458
459                 rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
460                                 tot_count - tot_written);
461
462                 if (mdl != &cx->vbi.sliced_mpeg_mdl) {
463                         if (mdl->readpos == mdl->bytesused)
464                                 cx18_stream_put_mdl_fw(s, mdl);
465                         else
466                                 cx18_push(s, mdl, &s->q_full);
467                 } else if (mdl->readpos == mdl->bytesused) {
468                         int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
469
470                         cx->vbi.sliced_mpeg_size[idx] = 0;
471                         cx->vbi.inserted_frame++;
472                         cx->vbi_data_inserted += mdl->bytesused;
473                 }
474                 if (rc < 0)
475                         return rc;
476                 tot_written += rc;
477
478                 if (tot_written == tot_count || single_frame)
479                         break;
480         }
481         return tot_written;
482 }
483
484 static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
485                 size_t count, loff_t *pos, int non_block)
486 {
487         ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
488         struct cx18 *cx = s->cx;
489
490         CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
491         if (rc > 0)
492                 pos += rc;
493         return rc;
494 }
495
496 int cx18_start_capture(struct cx18_open_id *id)
497 {
498         struct cx18 *cx = id->cx;
499         struct cx18_stream *s = &cx->streams[id->type];
500         struct cx18_stream *s_vbi;
501
502         if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
503                 /* you cannot read from these stream types. */
504                 return -EPERM;
505         }
506
507         /* Try to claim this stream. */
508         if (cx18_claim_stream(id, s->type))
509                 return -EBUSY;
510
511         /* If capture is already in progress, then we also have to
512            do nothing extra. */
513         if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
514             test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
515                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
516                 return 0;
517         }
518
519         /* Start VBI capture if required */
520         s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
521         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
522             test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
523             !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
524                 /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
525                    automatically when the MPG stream is claimed.
526                    We only need to start the VBI capturing. */
527                 if (cx18_start_v4l2_encode_stream(s_vbi)) {
528                         CX18_DEBUG_WARN("VBI capture start failed\n");
529
530                         /* Failure, clean up and return an error */
531                         clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
532                         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
533                         /* also releases the associated VBI stream */
534                         cx18_release_stream(s);
535                         return -EIO;
536                 }
537                 CX18_DEBUG_INFO("VBI insertion started\n");
538         }
539
540         /* Tell the card to start capturing */
541         if (!cx18_start_v4l2_encode_stream(s)) {
542                 /* We're done */
543                 set_bit(CX18_F_S_APPL_IO, &s->s_flags);
544                 /* Resume a possibly paused encoder */
545                 if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
546                         cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
547                 return 0;
548         }
549
550         /* failure, clean up */
551         CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
552
553         /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
554            automatically when the MPG stream is released.
555            We only need to stop the VBI capturing. */
556         if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
557             test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
558                 cx18_stop_v4l2_encode_stream(s_vbi, 0);
559                 clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
560         }
561         clear_bit(CX18_F_S_STREAMING, &s->s_flags);
562         cx18_release_stream(s);
563         return -EIO;
564 }
565
566 ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
567                 loff_t *pos)
568 {
569         struct cx18_open_id *id = filp->private_data;
570         struct cx18 *cx = id->cx;
571         struct cx18_stream *s = &cx->streams[id->type];
572         int rc;
573
574         CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
575
576         mutex_lock(&cx->serialize_lock);
577         rc = cx18_start_capture(id);
578         mutex_unlock(&cx->serialize_lock);
579         if (rc)
580                 return rc;
581         return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
582 }
583
584 unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
585 {
586         struct cx18_open_id *id = filp->private_data;
587         struct cx18 *cx = id->cx;
588         struct cx18_stream *s = &cx->streams[id->type];
589         int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
590
591         /* Start a capture if there is none */
592         if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
593                 int rc;
594
595                 mutex_lock(&cx->serialize_lock);
596                 rc = cx18_start_capture(id);
597                 mutex_unlock(&cx->serialize_lock);
598                 if (rc) {
599                         CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
600                                         s->name, rc);
601                         return POLLERR;
602                 }
603                 CX18_DEBUG_FILE("Encoder poll started capture\n");
604         }
605
606         /* add stream's waitq to the poll list */
607         CX18_DEBUG_HI_FILE("Encoder poll\n");
608         poll_wait(filp, &s->waitq, wait);
609
610         if (atomic_read(&s->q_full.depth))
611                 return POLLIN | POLLRDNORM;
612         if (eof)
613                 return POLLHUP;
614         return 0;
615 }
616
617 void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
618 {
619         struct cx18 *cx = id->cx;
620         struct cx18_stream *s = &cx->streams[id->type];
621
622         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
623
624         /* 'Unclaim' this stream */
625
626         /* Stop capturing */
627         if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
628                 struct cx18_stream *s_vbi =
629                         &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
630
631                 CX18_DEBUG_INFO("close stopping capture\n");
632                 /* Special case: a running VBI capture for VBI insertion
633                    in the mpeg stream. Need to stop that too. */
634                 if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
635                     test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
636                     !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
637                         CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
638                         cx18_stop_v4l2_encode_stream(s_vbi, 0);
639                 }
640                 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
641                     test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
642                         /* Also used internally, don't stop capturing */
643                         s->id = -1;
644                 else
645                         cx18_stop_v4l2_encode_stream(s, gop_end);
646         }
647         if (!gop_end) {
648                 clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
649                 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
650                 cx18_release_stream(s);
651         }
652 }
653
654 int cx18_v4l2_close(struct file *filp)
655 {
656         struct cx18_open_id *id = filp->private_data;
657         struct cx18 *cx = id->cx;
658         struct cx18_stream *s = &cx->streams[id->type];
659
660         CX18_DEBUG_IOCTL("close() of %s\n", s->name);
661
662         v4l2_prio_close(&cx->prio, &id->prio);
663
664         /* Easy case first: this stream was never claimed by us */
665         if (s->id != id->open_id) {
666                 kfree(id);
667                 return 0;
668         }
669
670         /* 'Unclaim' this stream */
671
672         /* Stop radio */
673         mutex_lock(&cx->serialize_lock);
674         if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
675                 /* Closing radio device, return to TV mode */
676                 cx18_mute(cx);
677                 /* Mark that the radio is no longer in use */
678                 clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
679                 /* Switch tuner to TV */
680                 cx18_call_all(cx, core, s_std, cx->std);
681                 /* Select correct audio input (i.e. TV tuner or Line in) */
682                 cx18_audio_set_io(cx);
683                 if (atomic_read(&cx->ana_capturing) > 0) {
684                         /* Undo video mute */
685                         cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
686                                 cx->params.video_mute |
687                                         (cx->params.video_mute_yuv << 8));
688                 }
689                 /* Done! Unmute and continue. */
690                 cx18_unmute(cx);
691                 cx18_release_stream(s);
692         } else {
693                 cx18_stop_capture(id, 0);
694         }
695         kfree(id);
696         mutex_unlock(&cx->serialize_lock);
697         return 0;
698 }
699
700 static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
701 {
702         struct cx18 *cx = s->cx;
703         struct cx18_open_id *item;
704
705         CX18_DEBUG_FILE("open %s\n", s->name);
706
707         /* Allocate memory */
708         item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
709         if (NULL == item) {
710                 CX18_DEBUG_WARN("nomem on v4l2 open\n");
711                 return -ENOMEM;
712         }
713         item->cx = cx;
714         item->type = s->type;
715         v4l2_prio_open(&cx->prio, &item->prio);
716
717         item->open_id = cx->open_id++;
718         filp->private_data = item;
719
720         if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
721                 /* Try to claim this stream */
722                 if (cx18_claim_stream(item, item->type)) {
723                         /* No, it's already in use */
724                         kfree(item);
725                         return -EBUSY;
726                 }
727
728                 if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
729                         if (atomic_read(&cx->ana_capturing) > 0) {
730                                 /* switching to radio while capture is
731                                    in progress is not polite */
732                                 cx18_release_stream(s);
733                                 kfree(item);
734                                 return -EBUSY;
735                         }
736                 }
737
738                 /* Mark that the radio is being used. */
739                 set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
740                 /* We have the radio */
741                 cx18_mute(cx);
742                 /* Switch tuner to radio */
743                 cx18_call_all(cx, tuner, s_radio);
744                 /* Select the correct audio input (i.e. radio tuner) */
745                 cx18_audio_set_io(cx);
746                 /* Done! Unmute and continue. */
747                 cx18_unmute(cx);
748         }
749         return 0;
750 }
751
752 int cx18_v4l2_open(struct file *filp)
753 {
754         int res;
755         struct video_device *video_dev = video_devdata(filp);
756         struct cx18_stream *s = video_get_drvdata(video_dev);
757         struct cx18 *cx = s->cx;
758
759         mutex_lock(&cx->serialize_lock);
760         if (cx18_init_on_first_open(cx)) {
761                 CX18_ERR("Failed to initialize on %s\n",
762                          video_device_node_name(video_dev));
763                 mutex_unlock(&cx->serialize_lock);
764                 return -ENXIO;
765         }
766         res = cx18_serialized_open(s, filp);
767         mutex_unlock(&cx->serialize_lock);
768         return res;
769 }
770
771 void cx18_mute(struct cx18 *cx)
772 {
773         u32 h;
774         if (atomic_read(&cx->ana_capturing)) {
775                 h = cx18_find_handle(cx);
776                 if (h != CX18_INVALID_TASK_HANDLE)
777                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
778                 else
779                         CX18_ERR("Can't find valid task handle for mute\n");
780         }
781         CX18_DEBUG_INFO("Mute\n");
782 }
783
784 void cx18_unmute(struct cx18 *cx)
785 {
786         u32 h;
787         if (atomic_read(&cx->ana_capturing)) {
788                 h = cx18_find_handle(cx);
789                 if (h != CX18_INVALID_TASK_HANDLE) {
790                         cx18_msleep_timeout(100, 0);
791                         cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
792                         cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
793                 } else
794                         CX18_ERR("Can't find valid task handle for unmute\n");
795         }
796         CX18_DEBUG_INFO("Unmute\n");
797 }