]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/media/video/ivtv/ivtv-fileops.c
Pull fujitsu-v3 into release branch
[linux-2.6.git] / drivers / media / video / ivtv / ivtv-fileops.c
1 /*
2     file operation functions
3     Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
4     Copyright (C) 2004  Chris Kennedy <c@groovy.org>
5     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "ivtv-driver.h"
23 #include "ivtv-fileops.h"
24 #include "ivtv-i2c.h"
25 #include "ivtv-queue.h"
26 #include "ivtv-udma.h"
27 #include "ivtv-irq.h"
28 #include "ivtv-vbi.h"
29 #include "ivtv-mailbox.h"
30 #include "ivtv-audio.h"
31 #include "ivtv-streams.h"
32 #include "ivtv-yuv.h"
33 #include "ivtv-controls.h"
34 #include "ivtv-ioctl.h"
35 #include "ivtv-cards.h"
36 #include <media/saa7115.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 int ivtv_claim_stream(struct ivtv_open_id *id, int type)
44 {
45         struct ivtv *itv = id->itv;
46         struct ivtv_stream *s = &itv->streams[type];
47         struct ivtv_stream *s_vbi;
48         int vbi_type;
49
50         if (test_and_set_bit(IVTV_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 == IVTV_DEC_STREAM_TYPE_VBI ||
57                                          type == IVTV_ENC_STREAM_TYPE_VBI)) {
58                         /* VBI is handled already internally, now also assign
59                            the file descriptor to this stream for external
60                            reading of the stream. */
61                         s->id = id->open_id;
62                         IVTV_DEBUG_INFO("Start Read VBI\n");
63                         return 0;
64                 }
65                 /* someone else is using this stream already */
66                 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
67                 return -EBUSY;
68         }
69         s->id = id->open_id;
70         if (type == IVTV_DEC_STREAM_TYPE_VBI) {
71                 /* Enable reinsertion interrupt */
72                 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
73         }
74
75         /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
76            IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
77            (provided VBI insertion is on and sliced VBI is selected), for all
78            other streams we're done */
79         if (type == IVTV_DEC_STREAM_TYPE_MPG) {
80                 vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
81         } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
82                    itv->vbi.insert_mpeg && itv->vbi.sliced_in->service_set) {
83                 vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
84         } else {
85                 return 0;
86         }
87         s_vbi = &itv->streams[vbi_type];
88
89         if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
90                 /* Enable reinsertion interrupt */
91                 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
92                         ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
93         }
94         /* mark that it is used internally */
95         set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
96         return 0;
97 }
98
99 /* This function releases a previously claimed stream. It will take into
100    account associated VBI streams. */
101 void ivtv_release_stream(struct ivtv_stream *s)
102 {
103         struct ivtv *itv = s->itv;
104         struct ivtv_stream *s_vbi;
105
106         s->id = -1;
107         if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
108                 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
109                 /* this stream is still in use internally */
110                 return;
111         }
112         if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
113                 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
114                 return;
115         }
116
117         ivtv_flush_queues(s);
118
119         /* disable reinsertion interrupt */
120         if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
121                 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
122
123         /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
124            IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
125            for all other streams we're done */
126         if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
127                 s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
128         else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
129                 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
130         else
131                 return;
132
133         /* clear internal use flag */
134         if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
135                 /* was already cleared */
136                 return;
137         }
138         if (s_vbi->id != -1) {
139                 /* VBI stream still claimed by a file descriptor */
140                 return;
141         }
142         /* disable reinsertion interrupt */
143         if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
144                 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
145         clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
146         ivtv_flush_queues(s_vbi);
147 }
148
149 static void ivtv_dualwatch(struct ivtv *itv)
150 {
151         struct v4l2_tuner vt;
152         u16 new_bitmap;
153         u16 new_stereo_mode;
154         const u16 stereo_mask = 0x0300;
155         const u16 dual = 0x0200;
156
157         new_stereo_mode = itv->params.audio_properties & stereo_mask;
158         memset(&vt, 0, sizeof(vt));
159         ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, &vt);
160         if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
161                 new_stereo_mode = dual;
162
163         if (new_stereo_mode == itv->dualwatch_stereo_mode)
164                 return;
165
166         new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask);
167
168         IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
169                            itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
170
171         if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) {
172                 itv->dualwatch_stereo_mode = new_stereo_mode;
173                 return;
174         }
175         IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
176 }
177
178 static void ivtv_update_pgm_info(struct ivtv *itv)
179 {
180         u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
181         int cnt;
182         int i = 0;
183
184         if (wr_idx >= itv->pgm_info_num) {
185                 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
186                 return;
187         }
188         cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
189         while (i < cnt) {
190                 int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
191                 struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
192                 u32 addr = itv->pgm_info_offset + 4 + idx * 24;
193                 const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
194                         V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
195                                         // 1=I, 2=P, 4=B
196
197                 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
198                 if (e->offset > itv->mpg_data_received) {
199                         break;
200                 }
201                 e->offset += itv->vbi_data_inserted;
202                 e->length = read_enc(addr);
203                 e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
204                 e->flags = mapping[read_enc(addr + 12) & 7];
205                 i++;
206         }
207         itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
208 }
209
210 static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
211 {
212         struct ivtv *itv = s->itv;
213         struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
214         struct ivtv_buffer *buf;
215         DEFINE_WAIT(wait);
216
217         *err = 0;
218         while (1) {
219                 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
220                         /* Process pending program info updates and pending VBI data */
221                         ivtv_update_pgm_info(itv);
222
223                         if (jiffies - itv->dualwatch_jiffies > msecs_to_jiffies(1000)) {
224                                 itv->dualwatch_jiffies = jiffies;
225                                 ivtv_dualwatch(itv);
226                         }
227
228                         if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
229                             !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
230                                 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
231                                         /* byteswap and process VBI data */
232                                         ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
233                                         ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
234                                 }
235                         }
236                         buf = &itv->vbi.sliced_mpeg_buf;
237                         if (buf->readpos != buf->bytesused) {
238                                 return buf;
239                         }
240                 }
241
242                 /* do we have leftover data? */
243                 buf = ivtv_dequeue(s, &s->q_io);
244                 if (buf)
245                         return buf;
246
247                 /* do we have new data? */
248                 buf = ivtv_dequeue(s, &s->q_full);
249                 if (buf) {
250                         if (!test_and_clear_bit(IVTV_F_B_NEED_BUF_SWAP, &buf->b_flags))
251                                 return buf;
252                         if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
253                                 /* byteswap MPG data */
254                                 ivtv_buf_swap(buf);
255                         else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
256                                 /* byteswap and process VBI data */
257                                 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
258                         }
259                         return buf;
260                 }
261                 /* return if file was opened with O_NONBLOCK */
262                 if (non_block) {
263                         *err = -EAGAIN;
264                         return NULL;
265                 }
266
267                 /* return if end of stream */
268                 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
269                         clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
270                         IVTV_DEBUG_INFO("EOS %s\n", s->name);
271                         return NULL;
272                 }
273
274                 /* wait for more data to arrive */
275                 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
276                 /* New buffers might have become available before we were added to the waitqueue */
277                 if (!s->q_full.buffers)
278                         schedule();
279                 finish_wait(&s->waitq, &wait);
280                 if (signal_pending(current)) {
281                         /* return if a signal was received */
282                         IVTV_DEBUG_INFO("User stopped %s\n", s->name);
283                         *err = -EINTR;
284                         return NULL;
285                 }
286         }
287 }
288
289 static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
290 {
291         int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
292
293         itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
294         itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
295         itv->vbi.sliced_mpeg_buf.readpos = 0;
296 }
297
298 static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
299                 char __user *ubuf, size_t ucount)
300 {
301         struct ivtv *itv = s->itv;
302         size_t len = buf->bytesused - buf->readpos;
303
304         if (len > ucount) len = ucount;
305         if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
306             itv->vbi.sliced_in->service_set && buf != &itv->vbi.sliced_mpeg_buf) {
307                 const char *start = buf->buf + buf->readpos;
308                 const char *p = start + 1;
309                 const u8 *q;
310                 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
311                 int stuffing, i;
312
313                 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
314                         p = q + 1;
315                         if ((char *)q + 15 >= buf->buf + buf->bytesused ||
316                             q[1] != 0 || q[2] != 1 || q[3] != ch) {
317                                 continue;
318                         }
319                         if (!itv->search_pack_header) {
320                                 if ((q[6] & 0xc0) != 0x80)
321                                         continue;
322                                 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
323                                     ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
324                                         ch = 0xba;
325                                         itv->search_pack_header = 1;
326                                         p = q + 9;
327                                 }
328                                 continue;
329                         }
330                         stuffing = q[13] & 7;
331                         /* all stuffing bytes must be 0xff */
332                         for (i = 0; i < stuffing; i++)
333                                 if (q[14 + i] != 0xff)
334                                         break;
335                         if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
336                                         q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
337                                         q[16 + stuffing] == 1) {
338                                 itv->search_pack_header = 0;
339                                 len = (char *)q - start;
340                                 ivtv_setup_sliced_vbi_buf(itv);
341                                 break;
342                         }
343                 }
344         }
345         if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
346                 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
347                 return -EFAULT;
348         }
349         /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
350                         buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
351                         buf == &itv->vbi.sliced_mpeg_buf); */
352         buf->readpos += len;
353         if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
354                 itv->mpg_data_received += len;
355         return len;
356 }
357
358 static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
359 {
360         struct ivtv *itv = s->itv;
361         size_t tot_written = 0;
362         int single_frame = 0;
363
364         if (atomic_read(&itv->capturing) == 0 && s->id == -1) {
365                 /* shouldn't happen */
366                 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
367                 return -EIO;
368         }
369
370         /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
371            arrive one-by-one, so make sure we never output more than one VBI frame at a time */
372         if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
373                         (s->type == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set))
374                 single_frame = 1;
375
376         for (;;) {
377                 struct ivtv_buffer *buf;
378                 int rc;
379
380                 buf = ivtv_get_buffer(s, non_block, &rc);
381                 if (buf == NULL && rc == -EAGAIN && tot_written)
382                         break;
383                 if (buf == NULL)
384                         return rc;
385                 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
386                 if (buf != &itv->vbi.sliced_mpeg_buf) {
387                         ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
388                 }
389                 else if (buf->readpos == buf->bytesused) {
390                         int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
391                         itv->vbi.sliced_mpeg_size[idx] = 0;
392                         itv->vbi.inserted_frame++;
393                         itv->vbi_data_inserted += buf->bytesused;
394                 }
395                 if (rc < 0)
396                         return rc;
397                 tot_written += rc;
398
399                 if (tot_written == tot_count || single_frame)
400                         break;
401         }
402         return tot_written;
403 }
404
405 static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
406                         loff_t *pos, int non_block)
407 {
408         ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
409         struct ivtv *itv = s->itv;
410
411         IVTV_DEBUG_HI_INFO("read %zd from %s, got %zd\n", count, s->name, rc);
412         if (rc > 0)
413                 pos += rc;
414         return rc;
415 }
416
417 int ivtv_start_capture(struct ivtv_open_id *id)
418 {
419         struct ivtv *itv = id->itv;
420         struct ivtv_stream *s = &itv->streams[id->type];
421         struct ivtv_stream *s_vbi;
422
423         if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
424             s->type == IVTV_DEC_STREAM_TYPE_MPG ||
425             s->type == IVTV_DEC_STREAM_TYPE_YUV ||
426             s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
427                 /* you cannot read from these stream types. */
428                 return -EPERM;
429         }
430
431         /* Try to claim this stream. */
432         if (ivtv_claim_stream(id, s->type))
433                 return -EBUSY;
434
435         /* This stream does not need to start capturing */
436         if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
437                 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
438                 return 0;
439         }
440
441         /* If capture is already in progress, then we also have to
442            do nothing extra. */
443         if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
444                 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
445                 return 0;
446         }
447
448         /* Start VBI capture if required */
449         s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
450         if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
451             test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
452             !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
453                 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
454                    automatically when the MPG stream is claimed.
455                    We only need to start the VBI capturing. */
456                 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
457                         IVTV_DEBUG_WARN("VBI capture start failed\n");
458
459                         /* Failure, clean up and return an error */
460                         clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
461                         clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
462                         /* also releases the associated VBI stream */
463                         ivtv_release_stream(s);
464                         return -EIO;
465                 }
466                 IVTV_DEBUG_INFO("VBI insertion started\n");
467         }
468
469         /* Tell the card to start capturing */
470         if (!ivtv_start_v4l2_encode_stream(s)) {
471                 /* We're done */
472                 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
473                 /* Resume a possibly paused encoder */
474                 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
475                         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
476                 return 0;
477         }
478
479         /* failure, clean up */
480         IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
481
482         /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
483            automatically when the MPG stream is released.
484            We only need to stop the VBI capturing. */
485         if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
486             test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
487                 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
488                 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
489         }
490         clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
491         ivtv_release_stream(s);
492         return -EIO;
493 }
494
495 ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
496 {
497         struct ivtv_open_id *id = filp->private_data;
498         struct ivtv *itv = id->itv;
499         struct ivtv_stream *s = &itv->streams[id->type];
500         int rc;
501
502         IVTV_DEBUG_HI_IOCTL("read %zd bytes from %s\n", count, s->name);
503
504         rc = ivtv_start_capture(id);
505         if (rc)
506                 return rc;
507         return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
508 }
509
510 int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
511 {
512         struct ivtv *itv = id->itv;
513         struct ivtv_stream *s = &itv->streams[id->type];
514
515         if (atomic_read(&itv->decoding) == 0) {
516                 if (ivtv_claim_stream(id, s->type)) {
517                         /* someone else is using this stream already */
518                         IVTV_DEBUG_WARN("start decode, stream already claimed\n");
519                         return -EBUSY;
520                 }
521                 ivtv_start_v4l2_decode_stream(s, 0);
522         }
523         if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
524                 return ivtv_set_speed(itv, speed);
525         return 0;
526 }
527
528 ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
529 {
530         struct ivtv_open_id *id = filp->private_data;
531         struct ivtv *itv = id->itv;
532         struct ivtv_stream *s = &itv->streams[id->type];
533         struct ivtv_buffer *buf;
534         struct ivtv_queue q;
535         int bytes_written = 0;
536         int mode;
537         int rc;
538         DEFINE_WAIT(wait);
539
540         IVTV_DEBUG_HI_IOCTL("write %zd bytes to %s\n", count, s->name);
541
542         if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
543             s->type != IVTV_DEC_STREAM_TYPE_YUV &&
544             s->type != IVTV_DEC_STREAM_TYPE_VOUT)
545                 /* not decoder streams */
546                 return -EPERM;
547
548         /* Try to claim this stream */
549         if (ivtv_claim_stream(id, s->type))
550                 return -EBUSY;
551
552         /* This stream does not need to start any decoding */
553         if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
554                 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
555                 return ivtv_write_vbi(itv, user_buf, count);
556         }
557
558         mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
559
560         if (ivtv_set_output_mode(itv, mode) != mode) {
561             ivtv_release_stream(s);
562             return -EBUSY;
563         }
564         ivtv_queue_init(&q);
565         set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
566
567 retry:
568         for (;;) {
569                 /* Gather buffers */
570                 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
571                         ivtv_enqueue(s, buf, &q);
572                 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
573                         ivtv_enqueue(s, buf, &q);
574                 }
575                 if (q.buffers)
576                         break;
577                 if (filp->f_flags & O_NONBLOCK)
578                         return -EAGAIN;
579                 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
580                 /* New buffers might have become free before we were added to the waitqueue */
581                 if (!s->q_free.buffers)
582                         schedule();
583                 finish_wait(&s->waitq, &wait);
584                 if (signal_pending(current)) {
585                         IVTV_DEBUG_INFO("User stopped %s\n", s->name);
586                         return -EINTR;
587                 }
588         }
589
590         /* copy user data into buffers */
591         while ((buf = ivtv_dequeue(s, &q))) {
592                 /* Make sure we really got all the user data */
593                 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
594
595                 if (rc < 0) {
596                         ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
597                         return rc;
598                 }
599                 user_buf += rc;
600                 count -= rc;
601                 bytes_written += rc;
602
603                 if (buf->bytesused != s->buf_size) {
604                         /* incomplete, leave in q_io for next time */
605                         ivtv_enqueue(s, buf, &s->q_io);
606                         break;
607                 }
608                 /* Byteswap MPEG buffer */
609                 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
610                         ivtv_buf_swap(buf);
611                 ivtv_enqueue(s, buf, &s->q_full);
612         }
613
614         /* Start decoder (returns 0 if already started) */
615         rc = ivtv_start_decoding(id, itv->speed);
616         if (rc) {
617                 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
618
619                 /* failure, clean up */
620                 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
621                 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
622                 return rc;
623         }
624         if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
625                 if (s->q_full.length >= itv->dma_data_req_size) {
626                         int got_sig;
627
628                         prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
629                         while (!(got_sig = signal_pending(current)) &&
630                                         test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
631                                 schedule();
632                         }
633                         finish_wait(&itv->dma_waitq, &wait);
634                         if (got_sig) {
635                                 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
636                                 return -EINTR;
637                         }
638
639                         clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
640                         ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
641                         ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
642                 }
643         }
644         /* more user data is available, wait until buffers become free
645            to transfer the rest. */
646         if (count && !(filp->f_flags & O_NONBLOCK))
647                 goto retry;
648         IVTV_DEBUG_HI_INFO("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
649         return bytes_written;
650 }
651
652 unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
653 {
654         struct ivtv_open_id *id = filp->private_data;
655         struct ivtv *itv = id->itv;
656         struct ivtv_stream *s = &itv->streams[id->type];
657         int res = 0;
658
659         /* add stream's waitq to the poll list */
660         poll_wait(filp, &s->waitq, wait);
661
662         set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
663         if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
664             test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
665                 res = POLLPRI;
666
667         /* Allow write if buffers are available for writing */
668         if (s->q_free.buffers)
669                 res |= POLLOUT | POLLWRNORM;
670         return res;
671 }
672
673 unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
674 {
675         struct ivtv_open_id *id = filp->private_data;
676         struct ivtv *itv = id->itv;
677         struct ivtv_stream *s = &itv->streams[id->type];
678         int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
679
680         /* Start a capture if there is none */
681         if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
682                 int rc = ivtv_start_capture(id);
683
684                 if (rc) {
685                         IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
686                                         s->name, rc);
687                         return POLLERR;
688                 }
689         }
690
691         /* add stream's waitq to the poll list */
692         poll_wait(filp, &s->waitq, wait);
693
694         if (eof || s->q_full.length)
695                 return POLLIN | POLLRDNORM;
696         return 0;
697 }
698
699 void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
700 {
701         struct ivtv *itv = id->itv;
702         struct ivtv_stream *s = &itv->streams[id->type];
703
704         IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
705
706         /* 'Unclaim' this stream */
707
708         /* Stop capturing */
709         if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
710                 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
711
712                 IVTV_DEBUG_INFO("close stopping capture\n");
713                 /* Special case: a running VBI capture for VBI insertion
714                    in the mpeg stream. Need to stop that too. */
715                 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
716                     test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
717                     !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
718                         IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
719                         ivtv_stop_v4l2_encode_stream(s_vbi, 0);
720                 }
721                 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
722                      id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
723                     test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
724                         /* Also used internally, don't stop capturing */
725                         s->id = -1;
726                 }
727                 else {
728                         ivtv_stop_v4l2_encode_stream(s, gop_end);
729                 }
730         }
731         clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
732         clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
733
734         ivtv_release_stream(s);
735 }
736
737 static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
738 {
739         struct ivtv *itv = id->itv;
740         struct ivtv_stream *s = &itv->streams[id->type];
741
742         IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
743
744         /* Stop decoding */
745         if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
746                 IVTV_DEBUG_INFO("close stopping decode\n");
747
748                 ivtv_stop_v4l2_decode_stream(s, flags, pts);
749         }
750         clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
751         clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
752         if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
753                 /* Restore registers we've changed & clean up any mess we've made */
754                 ivtv_yuv_close(itv);
755         }
756         if (s->type == IVTV_DEC_STREAM_TYPE_YUV && itv->output_mode == OUT_YUV)
757                 itv->output_mode = OUT_NONE;
758         else if (s->type == IVTV_DEC_STREAM_TYPE_YUV && itv->output_mode == OUT_UDMA_YUV)
759                 itv->output_mode = OUT_NONE;
760         else if (s->type == IVTV_DEC_STREAM_TYPE_MPG && itv->output_mode == OUT_MPG)
761                 itv->output_mode = OUT_NONE;
762
763         itv->speed = 0;
764         clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
765         ivtv_release_stream(s);
766 }
767
768 int ivtv_v4l2_close(struct inode *inode, struct file *filp)
769 {
770         struct ivtv_open_id *id = filp->private_data;
771         struct ivtv *itv = id->itv;
772         struct ivtv_stream *s = &itv->streams[id->type];
773
774         IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
775
776         v4l2_prio_close(&itv->prio, &id->prio);
777
778         /* Easy case first: this stream was never claimed by us */
779         if (s->id != id->open_id) {
780                 kfree(id);
781                 return 0;
782         }
783
784         /* 'Unclaim' this stream */
785
786         /* Stop radio */
787         if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
788                 /* Closing radio device, return to TV mode */
789                 ivtv_mute(itv);
790                 /* Mark that the radio is no longer in use */
791                 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
792                 /* Switch tuner to TV */
793                 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
794                 /* Select correct audio input (i.e. TV tuner or Line in) */
795                 ivtv_audio_set_io(itv);
796                 if (itv->hw_flags & IVTV_HW_SAA711X)
797                 {
798                         struct v4l2_crystal_freq crystal_freq;
799                         crystal_freq.freq = SAA7115_FREQ_32_11_MHZ;
800                         crystal_freq.flags = 0;
801                         ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq);
802                 }
803                 /* Done! Unmute and continue. */
804                 ivtv_unmute(itv);
805                 ivtv_release_stream(s);
806         } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
807                 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
808
809                 ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
810
811                 /* If all output streams are closed, and if the user doesn't have
812                    IVTV_DEC_STREAM_TYPE_VOUT open, then disable VBI on TV-out. */
813                 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
814                         /* disable VBI on TV-out */
815                         ivtv_disable_vbi(itv);
816                 }
817         } else {
818                 ivtv_stop_capture(id, 0);
819         }
820         kfree(id);
821         return 0;
822 }
823
824 int ivtv_v4l2_open(struct inode *inode, struct file *filp)
825 {
826         int x, y = 0;
827         struct ivtv_open_id *item;
828         struct ivtv *itv = NULL;
829         struct ivtv_stream *s = NULL;
830         int minor = iminor(inode);
831
832         /* Find which card this open was on */
833         spin_lock(&ivtv_cards_lock);
834         for (x = 0; itv == NULL && x < ivtv_cards_active; x++) {
835                 /* find out which stream this open was on */
836                 for (y = 0; y < IVTV_MAX_STREAMS; y++) {
837                         s = &ivtv_cards[x]->streams[y];
838                         if (s->v4l2dev && s->v4l2dev->minor == minor) {
839                                 itv = ivtv_cards[x];
840                                 break;
841                         }
842                 }
843         }
844         spin_unlock(&ivtv_cards_lock);
845
846         if (itv == NULL) {
847                 /* Couldn't find a device registered
848                    on that minor, shouldn't happen! */
849                 printk(KERN_WARNING "ivtv:  No ivtv device found on minor %d\n", minor);
850                 return -ENXIO;
851         }
852
853         if (y == IVTV_DEC_STREAM_TYPE_MPG &&
854                 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
855                 return -EBUSY;
856
857         if (y == IVTV_DEC_STREAM_TYPE_YUV &&
858                 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
859                 return -EBUSY;
860
861         if (y == IVTV_DEC_STREAM_TYPE_YUV) {
862                 if (read_reg(0x82c) == 0) {
863                         IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
864                         /* return -ENODEV; */
865                 }
866                 ivtv_udma_alloc(itv);
867         }
868
869         /* Allocate memory */
870         item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
871         if (NULL == item) {
872                 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
873                 return -ENOMEM;
874         }
875         item->itv = itv;
876         item->type = y;
877         v4l2_prio_open(&itv->prio, &item->prio);
878
879         item->open_id = itv->open_id++;
880         filp->private_data = item;
881
882         if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
883                 /* Try to claim this stream */
884                 if (ivtv_claim_stream(item, item->type)) {
885                         /* No, it's already in use */
886                         kfree(item);
887                         return -EBUSY;
888                 }
889
890                 /* We have the radio */
891                 ivtv_mute(itv);
892                 /* Switch tuner to radio */
893                 ivtv_call_i2c_clients(itv, AUDC_SET_RADIO, NULL);
894                 /* Mark that the radio is being used. */
895                 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
896                 /* Select the correct audio input (i.e. radio tuner) */
897                 ivtv_audio_set_io(itv);
898                 if (itv->hw_flags & IVTV_HW_SAA711X)
899                 {
900                         struct v4l2_crystal_freq crystal_freq;
901                         crystal_freq.freq = SAA7115_FREQ_32_11_MHZ;
902                         crystal_freq.flags = SAA7115_FREQ_FL_APLL;
903                         ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq);
904                 }
905                 /* Done! Unmute and continue. */
906                 ivtv_unmute(itv);
907         }
908
909         /* YUV or MPG Decoding Mode? */
910         if (y == IVTV_DEC_STREAM_TYPE_MPG)
911                 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
912         else if (y == IVTV_DEC_STREAM_TYPE_YUV)
913         {
914                 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
915         }
916
917         return 0;
918 }
919
920 void ivtv_mute(struct ivtv *itv)
921 {
922         struct v4l2_control ctrl = { V4L2_CID_AUDIO_MUTE, 1 };
923
924         /* Mute sound to avoid pop */
925         ivtv_control_ioctls(itv, VIDIOC_S_CTRL, &ctrl);
926
927         if (atomic_read(&itv->capturing))
928                 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
929
930         IVTV_DEBUG_INFO("Mute\n");
931 }
932
933 void ivtv_unmute(struct ivtv *itv)
934 {
935         struct v4l2_control ctrl = { V4L2_CID_AUDIO_MUTE, 0 };
936
937         /* initialize or refresh input */
938         if (atomic_read(&itv->capturing) == 0)
939                 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
940
941         ivtv_msleep_timeout(100, 0);
942
943         if (atomic_read(&itv->capturing)) {
944                 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
945                 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
946         }
947
948         /* Unmute */
949         ivtv_control_ioctls(itv, VIDIOC_S_CTRL, &ctrl);
950         IVTV_DEBUG_INFO("Unmute\n");
951 }