blob: 3fb21e1406a1cf390f5ce81e64604c1cc3d57f31 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
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"
Hans Verkuil33c0fca2007-08-23 06:32:46 -030030#include "ivtv-routing.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030031#include "ivtv-streams.h"
32#include "ivtv-yuv.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030033#include "ivtv-ioctl.h"
Hans Verkuilf2100d82007-05-17 08:08:45 -030034#include "ivtv-cards.h"
Ian Armstrong914610e2010-06-12 13:33:21 -030035#include "ivtv-firmware.h"
Hans Verkuil09250192010-03-27 14:10:13 -030036#include <media/v4l2-event.h>
Hans Verkuilf2100d82007-05-17 08:08:45 -030037#include <media/saa7115.h>
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030038
39/* This function tries to claim the stream for a specific file descriptor.
40 If no one else is using this stream then the stream is claimed and
41 associated VBI streams are also automatically claimed.
42 Possible error returns: -EBUSY if someone else has claimed
43 the stream or 0 on success. */
Adrian Bunk95f73c52008-07-22 14:20:12 -030044static int ivtv_claim_stream(struct ivtv_open_id *id, int type)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030045{
46 struct ivtv *itv = id->itv;
47 struct ivtv_stream *s = &itv->streams[type];
48 struct ivtv_stream *s_vbi;
49 int vbi_type;
50
51 if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
52 /* someone already claimed this stream */
53 if (s->id == id->open_id) {
54 /* yes, this file descriptor did. So that's OK. */
55 return 0;
56 }
57 if (s->id == -1 && (type == IVTV_DEC_STREAM_TYPE_VBI ||
58 type == IVTV_ENC_STREAM_TYPE_VBI)) {
59 /* VBI is handled already internally, now also assign
60 the file descriptor to this stream for external
61 reading of the stream. */
62 s->id = id->open_id;
63 IVTV_DEBUG_INFO("Start Read VBI\n");
64 return 0;
65 }
66 /* someone else is using this stream already */
67 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
68 return -EBUSY;
69 }
70 s->id = id->open_id;
71 if (type == IVTV_DEC_STREAM_TYPE_VBI) {
72 /* Enable reinsertion interrupt */
73 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
74 }
75
76 /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
77 IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
78 (provided VBI insertion is on and sliced VBI is selected), for all
79 other streams we're done */
80 if (type == IVTV_DEC_STREAM_TYPE_MPG) {
81 vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
82 } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
Hans Verkuila8b86432008-10-04 08:05:30 -030083 itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030084 vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
85 } else {
86 return 0;
87 }
88 s_vbi = &itv->streams[vbi_type];
89
90 if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
91 /* Enable reinsertion interrupt */
92 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
93 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
94 }
95 /* mark that it is used internally */
96 set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
97 return 0;
98}
99
100/* This function releases a previously claimed stream. It will take into
101 account associated VBI streams. */
102void ivtv_release_stream(struct ivtv_stream *s)
103{
104 struct ivtv *itv = s->itv;
105 struct ivtv_stream *s_vbi;
106
107 s->id = -1;
108 if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
109 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
110 /* this stream is still in use internally */
111 return;
112 }
113 if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
114 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
115 return;
116 }
117
118 ivtv_flush_queues(s);
119
120 /* disable reinsertion interrupt */
121 if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
122 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
123
124 /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
125 IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
126 for all other streams we're done */
127 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
128 s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
129 else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
130 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
131 else
132 return;
133
134 /* clear internal use flag */
135 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
136 /* was already cleared */
137 return;
138 }
139 if (s_vbi->id != -1) {
140 /* VBI stream still claimed by a file descriptor */
141 return;
142 }
143 /* disable reinsertion interrupt */
144 if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
145 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
146 clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
147 ivtv_flush_queues(s_vbi);
148}
149
150static void ivtv_dualwatch(struct ivtv *itv)
151{
152 struct v4l2_tuner vt;
Andy Walls0d82fe82009-01-01 19:02:31 -0300153 u32 new_bitmap;
154 u32 new_stereo_mode;
155 const u32 stereo_mask = 0x0300;
156 const u32 dual = 0x0200;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300157
158 new_stereo_mode = itv->params.audio_properties & stereo_mask;
159 memset(&vt, 0, sizeof(vt));
Hans Verkuil67ec09f2008-11-29 19:38:23 -0300160 ivtv_call_all(itv, tuner, g_tuner, &vt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300161 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
162 new_stereo_mode = dual;
163
164 if (new_stereo_mode == itv->dualwatch_stereo_mode)
165 return;
166
167 new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask);
168
169 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
170 itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
171
172 if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) {
173 itv->dualwatch_stereo_mode = new_stereo_mode;
174 return;
175 }
176 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
177}
178
179static void ivtv_update_pgm_info(struct ivtv *itv)
180{
181 u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
182 int cnt;
183 int i = 0;
184
185 if (wr_idx >= itv->pgm_info_num) {
186 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
187 return;
188 }
189 cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
190 while (i < cnt) {
191 int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
192 struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
193 u32 addr = itv->pgm_info_offset + 4 + idx * 24;
Hans Verkuil5614b022007-08-23 17:48:41 -0300194 const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
195 V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
196 // 1=I, 2=P, 4=B
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300197
198 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
199 if (e->offset > itv->mpg_data_received) {
200 break;
201 }
202 e->offset += itv->vbi_data_inserted;
203 e->length = read_enc(addr);
204 e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
Hans Verkuil5614b022007-08-23 17:48:41 -0300205 e->flags = mapping[read_enc(addr + 12) & 7];
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300206 i++;
207 }
208 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
209}
210
211static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
212{
213 struct ivtv *itv = s->itv;
214 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
215 struct ivtv_buffer *buf;
216 DEFINE_WAIT(wait);
217
218 *err = 0;
219 while (1) {
220 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
221 /* Process pending program info updates and pending VBI data */
222 ivtv_update_pgm_info(itv);
223
Julia Lawall168c6262008-04-16 16:13:15 -0300224 if (time_after(jiffies,
225 itv->dualwatch_jiffies +
226 msecs_to_jiffies(1000))) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300227 itv->dualwatch_jiffies = jiffies;
228 ivtv_dualwatch(itv);
229 }
230
231 if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
232 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
233 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
234 /* byteswap and process VBI data */
235 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
236 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
237 }
238 }
239 buf = &itv->vbi.sliced_mpeg_buf;
240 if (buf->readpos != buf->bytesused) {
241 return buf;
242 }
243 }
244
245 /* do we have leftover data? */
246 buf = ivtv_dequeue(s, &s->q_io);
247 if (buf)
248 return buf;
249
250 /* do we have new data? */
251 buf = ivtv_dequeue(s, &s->q_full);
252 if (buf) {
Hans Verkuilf4071b82007-07-28 12:07:12 -0300253 if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300254 return buf;
Hans Verkuilf4071b82007-07-28 12:07:12 -0300255 buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300256 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
257 /* byteswap MPG data */
258 ivtv_buf_swap(buf);
259 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
260 /* byteswap and process VBI data */
261 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
262 }
263 return buf;
264 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300265
266 /* return if end of stream */
267 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300268 IVTV_DEBUG_INFO("EOS %s\n", s->name);
269 return NULL;
270 }
271
Hans Verkuil559e1962007-08-23 17:51:07 -0300272 /* return if file was opened with O_NONBLOCK */
273 if (non_block) {
274 *err = -EAGAIN;
275 return NULL;
276 }
277
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300278 /* wait for more data to arrive */
279 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
280 /* New buffers might have become available before we were added to the waitqueue */
281 if (!s->q_full.buffers)
282 schedule();
283 finish_wait(&s->waitq, &wait);
284 if (signal_pending(current)) {
285 /* return if a signal was received */
286 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
287 *err = -EINTR;
288 return NULL;
289 }
290 }
291}
292
293static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
294{
295 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
296
297 itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
298 itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
299 itv->vbi.sliced_mpeg_buf.readpos = 0;
300}
301
302static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
303 char __user *ubuf, size_t ucount)
304{
305 struct ivtv *itv = s->itv;
306 size_t len = buf->bytesused - buf->readpos;
307
308 if (len > ucount) len = ucount;
309 if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
Hans Verkuila8b86432008-10-04 08:05:30 -0300310 !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300311 const char *start = buf->buf + buf->readpos;
312 const char *p = start + 1;
313 const u8 *q;
314 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
315 int stuffing, i;
316
317 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
318 p = q + 1;
319 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
320 q[1] != 0 || q[2] != 1 || q[3] != ch) {
321 continue;
322 }
323 if (!itv->search_pack_header) {
324 if ((q[6] & 0xc0) != 0x80)
325 continue;
326 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
327 ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
328 ch = 0xba;
329 itv->search_pack_header = 1;
330 p = q + 9;
331 }
332 continue;
333 }
334 stuffing = q[13] & 7;
335 /* all stuffing bytes must be 0xff */
336 for (i = 0; i < stuffing; i++)
337 if (q[14 + i] != 0xff)
338 break;
339 if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
340 q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
341 q[16 + stuffing] == 1) {
342 itv->search_pack_header = 0;
343 len = (char *)q - start;
344 ivtv_setup_sliced_vbi_buf(itv);
345 break;
346 }
347 }
348 }
349 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
350 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
351 return -EFAULT;
352 }
353 /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
354 buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
355 buf == &itv->vbi.sliced_mpeg_buf); */
356 buf->readpos += len;
357 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
358 itv->mpg_data_received += len;
359 return len;
360}
361
362static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
363{
364 struct ivtv *itv = s->itv;
365 size_t tot_written = 0;
366 int single_frame = 0;
367
368 if (atomic_read(&itv->capturing) == 0 && s->id == -1) {
369 /* shouldn't happen */
370 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
371 return -EIO;
372 }
373
374 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
375 arrive one-by-one, so make sure we never output more than one VBI frame at a time */
376 if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
Hans Verkuila8b86432008-10-04 08:05:30 -0300377 (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300378 single_frame = 1;
379
380 for (;;) {
381 struct ivtv_buffer *buf;
382 int rc;
383
384 buf = ivtv_get_buffer(s, non_block, &rc);
Hans Verkuil559e1962007-08-23 17:51:07 -0300385 /* if there is no data available... */
386 if (buf == NULL) {
387 /* if we got data, then return that regardless */
388 if (tot_written)
389 break;
390 /* EOS condition */
391 if (rc == 0) {
392 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
393 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
394 ivtv_release_stream(s);
395 }
396 /* set errno */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300397 return rc;
Hans Verkuil559e1962007-08-23 17:51:07 -0300398 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300399 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
400 if (buf != &itv->vbi.sliced_mpeg_buf) {
401 ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
402 }
403 else if (buf->readpos == buf->bytesused) {
404 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
405 itv->vbi.sliced_mpeg_size[idx] = 0;
406 itv->vbi.inserted_frame++;
407 itv->vbi_data_inserted += buf->bytesused;
408 }
409 if (rc < 0)
410 return rc;
411 tot_written += rc;
412
413 if (tot_written == tot_count || single_frame)
414 break;
415 }
416 return tot_written;
417}
418
419static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
420 loff_t *pos, int non_block)
421{
422 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
423 struct ivtv *itv = s->itv;
424
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300425 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300426 if (rc > 0)
427 pos += rc;
428 return rc;
429}
430
431int ivtv_start_capture(struct ivtv_open_id *id)
432{
433 struct ivtv *itv = id->itv;
434 struct ivtv_stream *s = &itv->streams[id->type];
435 struct ivtv_stream *s_vbi;
436
437 if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
438 s->type == IVTV_DEC_STREAM_TYPE_MPG ||
439 s->type == IVTV_DEC_STREAM_TYPE_YUV ||
440 s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
441 /* you cannot read from these stream types. */
442 return -EPERM;
443 }
444
445 /* Try to claim this stream. */
446 if (ivtv_claim_stream(id, s->type))
447 return -EBUSY;
448
449 /* This stream does not need to start capturing */
450 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
451 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
452 return 0;
453 }
454
455 /* If capture is already in progress, then we also have to
456 do nothing extra. */
457 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
458 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
459 return 0;
460 }
461
462 /* Start VBI capture if required */
463 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
464 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
465 test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
466 !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
467 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
468 automatically when the MPG stream is claimed.
469 We only need to start the VBI capturing. */
470 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
471 IVTV_DEBUG_WARN("VBI capture start failed\n");
472
473 /* Failure, clean up and return an error */
474 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
475 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
476 /* also releases the associated VBI stream */
477 ivtv_release_stream(s);
478 return -EIO;
479 }
480 IVTV_DEBUG_INFO("VBI insertion started\n");
481 }
482
483 /* Tell the card to start capturing */
484 if (!ivtv_start_v4l2_encode_stream(s)) {
485 /* We're done */
486 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
487 /* Resume a possibly paused encoder */
488 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
489 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
490 return 0;
491 }
492
493 /* failure, clean up */
494 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
495
496 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
497 automatically when the MPG stream is released.
498 We only need to stop the VBI capturing. */
499 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
500 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
501 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
502 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
503 }
504 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
505 ivtv_release_stream(s);
506 return -EIO;
507}
508
509ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
510{
Hans Verkuil09250192010-03-27 14:10:13 -0300511 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300512 struct ivtv *itv = id->itv;
513 struct ivtv_stream *s = &itv->streams[id->type];
514 int rc;
515
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300516 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300517
Hans Verkuilbaa40722007-08-19 07:10:55 -0300518 mutex_lock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300519 rc = ivtv_start_capture(id);
Hans Verkuilbaa40722007-08-19 07:10:55 -0300520 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300521 if (rc)
522 return rc;
523 return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
524}
525
526int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
527{
528 struct ivtv *itv = id->itv;
529 struct ivtv_stream *s = &itv->streams[id->type];
Ian Armstrong914610e2010-06-12 13:33:21 -0300530 int rc;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300531
532 if (atomic_read(&itv->decoding) == 0) {
533 if (ivtv_claim_stream(id, s->type)) {
534 /* someone else is using this stream already */
535 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
536 return -EBUSY;
537 }
Ian Armstrong914610e2010-06-12 13:33:21 -0300538 rc = ivtv_start_v4l2_decode_stream(s, 0);
539 if (rc < 0)
540 return rc;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300541 }
542 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
543 return ivtv_set_speed(itv, speed);
544 return 0;
545}
546
547ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
548{
Hans Verkuil09250192010-03-27 14:10:13 -0300549 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300550 struct ivtv *itv = id->itv;
551 struct ivtv_stream *s = &itv->streams[id->type];
Ian Armstrongc240ad02007-10-16 03:21:46 -0300552 struct yuv_playback_info *yi = &itv->yuv_info;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300553 struct ivtv_buffer *buf;
554 struct ivtv_queue q;
555 int bytes_written = 0;
556 int mode;
557 int rc;
558 DEFINE_WAIT(wait);
559
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300560 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300561
562 if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
563 s->type != IVTV_DEC_STREAM_TYPE_YUV &&
564 s->type != IVTV_DEC_STREAM_TYPE_VOUT)
565 /* not decoder streams */
566 return -EPERM;
567
568 /* Try to claim this stream */
569 if (ivtv_claim_stream(id, s->type))
570 return -EBUSY;
571
572 /* This stream does not need to start any decoding */
573 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300574 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
575
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300576 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300577 ivtv_write_vbi(itv, (const struct v4l2_sliced_vbi_data *)user_buf, elems);
578 return elems * sizeof(struct v4l2_sliced_vbi_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300579 }
580
581 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
582
583 if (ivtv_set_output_mode(itv, mode) != mode) {
584 ivtv_release_stream(s);
585 return -EBUSY;
586 }
587 ivtv_queue_init(&q);
588 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
589
Ian Armstrong464e9f32008-06-21 09:25:23 -0300590 /* Start decoder (returns 0 if already started) */
591 mutex_lock(&itv->serialize_lock);
592 rc = ivtv_start_decoding(id, itv->speed);
593 mutex_unlock(&itv->serialize_lock);
594 if (rc) {
595 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
596
597 /* failure, clean up */
598 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
599 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
600 return rc;
601 }
602
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300603retry:
Ian Armstrong77aded62007-11-05 14:27:09 -0300604 /* If possible, just DMA the entire frame - Check the data transfer size
605 since we may get here before the stream has been fully set-up */
606 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
607 while (count >= itv->dma_data_req_size) {
Ian Armstrong2e668962008-10-11 06:39:05 -0300608 rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
609
610 if (rc < 0)
611 return rc;
612
613 bytes_written += itv->dma_data_req_size;
614 user_buf += itv->dma_data_req_size;
615 count -= itv->dma_data_req_size;
Ian Armstrong77aded62007-11-05 14:27:09 -0300616 }
617 if (count == 0) {
618 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
619 return bytes_written;
620 }
621 }
622
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300623 for (;;) {
624 /* Gather buffers */
625 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
626 ivtv_enqueue(s, buf, &q);
627 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
628 ivtv_enqueue(s, buf, &q);
629 }
630 if (q.buffers)
631 break;
632 if (filp->f_flags & O_NONBLOCK)
633 return -EAGAIN;
634 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
635 /* New buffers might have become free before we were added to the waitqueue */
636 if (!s->q_free.buffers)
637 schedule();
638 finish_wait(&s->waitq, &wait);
639 if (signal_pending(current)) {
640 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
641 return -EINTR;
642 }
643 }
644
645 /* copy user data into buffers */
646 while ((buf = ivtv_dequeue(s, &q))) {
Ian Armstrongc240ad02007-10-16 03:21:46 -0300647 /* yuv is a pain. Don't copy more data than needed for a single
648 frame, otherwise we lose sync with the incoming stream */
649 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
650 yi->stream_size + count > itv->dma_data_req_size)
651 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
652 itv->dma_data_req_size - yi->stream_size);
653 else
654 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300655
Ian Armstrongc240ad02007-10-16 03:21:46 -0300656 /* Make sure we really got all the user data */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300657 if (rc < 0) {
658 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
659 return rc;
660 }
661 user_buf += rc;
662 count -= rc;
663 bytes_written += rc;
664
Ian Armstrongc240ad02007-10-16 03:21:46 -0300665 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
666 yi->stream_size += rc;
667 /* If we have a complete yuv frame, break loop now */
668 if (yi->stream_size == itv->dma_data_req_size) {
669 ivtv_enqueue(s, buf, &s->q_full);
670 yi->stream_size = 0;
671 break;
672 }
673 }
674
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300675 if (buf->bytesused != s->buf_size) {
676 /* incomplete, leave in q_io for next time */
677 ivtv_enqueue(s, buf, &s->q_io);
678 break;
679 }
680 /* Byteswap MPEG buffer */
681 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
682 ivtv_buf_swap(buf);
683 ivtv_enqueue(s, buf, &s->q_full);
684 }
685
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300686 if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
687 if (s->q_full.length >= itv->dma_data_req_size) {
688 int got_sig;
689
Ian Armstrong77aded62007-11-05 14:27:09 -0300690 if (mode == OUT_YUV)
691 ivtv_yuv_setup_stream_frame(itv);
692
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300693 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
694 while (!(got_sig = signal_pending(current)) &&
695 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
696 schedule();
697 }
698 finish_wait(&itv->dma_waitq, &wait);
699 if (got_sig) {
700 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
701 return -EINTR;
702 }
703
704 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
705 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
706 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
707 }
708 }
709 /* more user data is available, wait until buffers become free
710 to transfer the rest. */
711 if (count && !(filp->f_flags & O_NONBLOCK))
712 goto retry;
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300713 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300714 return bytes_written;
715}
716
717unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
718{
Hans Verkuil09250192010-03-27 14:10:13 -0300719 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300720 struct ivtv *itv = id->itv;
721 struct ivtv_stream *s = &itv->streams[id->type];
722 int res = 0;
723
724 /* add stream's waitq to the poll list */
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300725 IVTV_DEBUG_HI_FILE("Decoder poll\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300726
Hans Verkuil09250192010-03-27 14:10:13 -0300727 /* If there are subscribed events, then only use the new event
728 API instead of the old video.h based API. */
729 if (!list_empty(&id->fh.events->subscribed)) {
730 poll_wait(filp, &id->fh.events->wait, wait);
731 /* Turn off the old-style vsync events */
732 clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
733 if (v4l2_event_pending(&id->fh))
734 res = POLLPRI;
735 } else {
736 /* This is the old-style API which is here only for backwards
737 compatibility. */
738 poll_wait(filp, &s->waitq, wait);
739 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
740 if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
741 test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
742 res = POLLPRI;
743 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300744
745 /* Allow write if buffers are available for writing */
746 if (s->q_free.buffers)
747 res |= POLLOUT | POLLWRNORM;
748 return res;
749}
750
751unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
752{
Hans Verkuil09250192010-03-27 14:10:13 -0300753 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300754 struct ivtv *itv = id->itv;
755 struct ivtv_stream *s = &itv->streams[id->type];
756 int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
757
758 /* Start a capture if there is none */
759 if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
Hans Verkuilbaa40722007-08-19 07:10:55 -0300760 int rc;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300761
Hans Verkuilbaa40722007-08-19 07:10:55 -0300762 mutex_lock(&itv->serialize_lock);
763 rc = ivtv_start_capture(id);
764 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300765 if (rc) {
766 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
767 s->name, rc);
768 return POLLERR;
769 }
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300770 IVTV_DEBUG_FILE("Encoder poll started capture\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300771 }
772
773 /* add stream's waitq to the poll list */
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300774 IVTV_DEBUG_HI_FILE("Encoder poll\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300775 poll_wait(filp, &s->waitq, wait);
776
Hans Verkuil16928be2008-04-28 12:18:00 -0300777 if (s->q_full.length || s->q_io.length)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300778 return POLLIN | POLLRDNORM;
Hans Verkuil16928be2008-04-28 12:18:00 -0300779 if (eof)
780 return POLLHUP;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300781 return 0;
782}
783
784void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
785{
786 struct ivtv *itv = id->itv;
787 struct ivtv_stream *s = &itv->streams[id->type];
788
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300789 IVTV_DEBUG_FILE("close() of %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300790
791 /* 'Unclaim' this stream */
792
793 /* Stop capturing */
794 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
795 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
796
797 IVTV_DEBUG_INFO("close stopping capture\n");
798 /* Special case: a running VBI capture for VBI insertion
799 in the mpeg stream. Need to stop that too. */
800 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
801 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
802 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
803 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
804 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
805 }
806 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
807 id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
808 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
809 /* Also used internally, don't stop capturing */
810 s->id = -1;
811 }
812 else {
813 ivtv_stop_v4l2_encode_stream(s, gop_end);
814 }
815 }
Hans Verkuil559e1962007-08-23 17:51:07 -0300816 if (!gop_end) {
817 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
818 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
819 ivtv_release_stream(s);
820 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300821}
822
Hans Verkuil31ec1352007-03-03 08:50:42 -0300823static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300824{
825 struct ivtv *itv = id->itv;
826 struct ivtv_stream *s = &itv->streams[id->type];
827
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300828 IVTV_DEBUG_FILE("close() of %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300829
Ian Armstrong666092c2010-05-23 22:27:49 -0300830 if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
831 test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
832 /* Restore registers we've changed & clean up any mess */
833 ivtv_yuv_close(itv);
834 }
835
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300836 /* Stop decoding */
837 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
838 IVTV_DEBUG_INFO("close stopping decode\n");
839
840 ivtv_stop_v4l2_decode_stream(s, flags, pts);
Hans Verkuilad8ff0f2007-08-20 16:01:58 -0300841 itv->output_mode = OUT_NONE;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300842 }
843 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
844 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
Ian Armstrong666092c2010-05-23 22:27:49 -0300845
Hans Verkuilad8ff0f2007-08-20 16:01:58 -0300846 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
Ian Armstrongcb50f542007-08-18 15:58:51 -0300847 itv->output_mode = OUT_NONE;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300848
849 itv->speed = 0;
Hans Verkuilac425142007-07-22 08:46:38 -0300850 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300851 ivtv_release_stream(s);
852}
853
Hans Verkuilbec43662008-12-30 06:58:20 -0300854int ivtv_v4l2_close(struct file *filp)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300855{
Hans Verkuil09250192010-03-27 14:10:13 -0300856 struct v4l2_fh *fh = filp->private_data;
857 struct ivtv_open_id *id = fh2id(fh);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300858 struct ivtv *itv = id->itv;
859 struct ivtv_stream *s = &itv->streams[id->type];
860
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300861 IVTV_DEBUG_FILE("close %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300862
Hans Verkuilffb48772010-05-01 08:03:24 -0300863 v4l2_prio_close(&itv->prio, id->prio);
Hans Verkuil09250192010-03-27 14:10:13 -0300864 v4l2_fh_del(fh);
865 v4l2_fh_exit(fh);
Hans Verkuild46c17d2007-03-10 17:59:15 -0300866
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300867 /* Easy case first: this stream was never claimed by us */
868 if (s->id != id->open_id) {
869 kfree(id);
870 return 0;
871 }
872
873 /* 'Unclaim' this stream */
874
875 /* Stop radio */
Hans Verkuilbaa40722007-08-19 07:10:55 -0300876 mutex_lock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300877 if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
878 /* Closing radio device, return to TV mode */
879 ivtv_mute(itv);
880 /* Mark that the radio is no longer in use */
881 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
882 /* Switch tuner to TV */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300883 ivtv_call_all(itv, core, s_std, itv->std);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300884 /* Select correct audio input (i.e. TV tuner or Line in) */
885 ivtv_audio_set_io(itv);
Hans Verkuil3ff4ad82009-04-01 03:15:52 -0300886 if (itv->hw_flags & IVTV_HW_SAA711X) {
887 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
888 SAA7115_FREQ_32_11_MHZ, 0);
Hans Verkuilf2100d82007-05-17 08:08:45 -0300889 }
Hans Verkuil2f7362e2007-10-14 17:27:56 -0300890 if (atomic_read(&itv->capturing) > 0) {
891 /* Undo video mute */
892 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
893 itv->params.video_mute | (itv->params.video_mute_yuv << 8));
894 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300895 /* Done! Unmute and continue. */
896 ivtv_unmute(itv);
897 ivtv_release_stream(s);
898 } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
Hans Verkuil5a338c32007-07-22 09:39:56 -0300899 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
900
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300901 ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
Hans Verkuil5a338c32007-07-22 09:39:56 -0300902
903 /* If all output streams are closed, and if the user doesn't have
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300904 IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
Hans Verkuil5a338c32007-07-22 09:39:56 -0300905 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300906 /* disable CC on TV-out */
907 ivtv_disable_cc(itv);
Hans Verkuil5a338c32007-07-22 09:39:56 -0300908 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300909 } else {
910 ivtv_stop_capture(id, 0);
911 }
912 kfree(id);
Hans Verkuilbaa40722007-08-19 07:10:55 -0300913 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300914 return 0;
915}
916
Hans Verkuilbaa40722007-08-19 07:10:55 -0300917static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300918{
Ian Armstrong914610e2010-06-12 13:33:21 -0300919#ifdef CONFIG_VIDEO_ADV_DEBUG
920 struct video_device *vdev = video_devdata(filp);
921#endif
Hans Verkuilbaa40722007-08-19 07:10:55 -0300922 struct ivtv *itv = s->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300923 struct ivtv_open_id *item;
Hans Verkuil09250192010-03-27 14:10:13 -0300924 int res = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300925
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300926 IVTV_DEBUG_FILE("open %s\n", s->name);
Hans Verkuilc976bc82007-07-22 12:52:40 -0300927
Ian Armstrong914610e2010-06-12 13:33:21 -0300928#ifdef CONFIG_VIDEO_ADV_DEBUG
929 if (ivtv_fw_debug) {
930 IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
931 video_device_node_name(vdev));
932 IVTV_WARN("Selected firmware errors will be ignored\n");
933 }
934
935 /* Unless ivtv_fw_debug is set, error out if firmware dead. */
936 if (ivtv_firmware_check(itv, "ivtv_serialized_open") && !ivtv_fw_debug)
937 return -EIO;
938#else
939 if (ivtv_firmware_check(itv, "ivtv_serialized_open"))
940 return -EIO;
941#endif
942
Hans Verkuilbaa40722007-08-19 07:10:55 -0300943 if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300944 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
945 return -EBUSY;
946
Hans Verkuilbaa40722007-08-19 07:10:55 -0300947 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300948 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
949 return -EBUSY;
950
Hans Verkuilbaa40722007-08-19 07:10:55 -0300951 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300952 if (read_reg(0x82c) == 0) {
953 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
954 /* return -ENODEV; */
955 }
956 ivtv_udma_alloc(itv);
957 }
958
959 /* Allocate memory */
Hans Verkuil09250192010-03-27 14:10:13 -0300960 item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300961 if (NULL == item) {
962 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
963 return -ENOMEM;
964 }
Hans Verkuil09250192010-03-27 14:10:13 -0300965 v4l2_fh_init(&item->fh, s->vdev);
966 if (s->type == IVTV_DEC_STREAM_TYPE_YUV ||
967 s->type == IVTV_DEC_STREAM_TYPE_MPG) {
968 res = v4l2_event_alloc(&item->fh, 60);
969 }
970 if (res < 0) {
971 v4l2_fh_exit(&item->fh);
972 kfree(item);
973 return res;
974 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300975 item->itv = itv;
Hans Verkuilbaa40722007-08-19 07:10:55 -0300976 item->type = s->type;
Hans Verkuild46c17d2007-03-10 17:59:15 -0300977 v4l2_prio_open(&itv->prio, &item->prio);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300978
979 item->open_id = itv->open_id++;
Hans Verkuil09250192010-03-27 14:10:13 -0300980 filp->private_data = &item->fh;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300981
982 if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
983 /* Try to claim this stream */
984 if (ivtv_claim_stream(item, item->type)) {
985 /* No, it's already in use */
986 kfree(item);
987 return -EBUSY;
988 }
989
Hans Verkuil3562c432007-08-18 11:46:05 -0300990 if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
991 if (atomic_read(&itv->capturing) > 0) {
992 /* switching to radio while capture is
993 in progress is not polite */
Hans Verkuilaf3420b2007-10-12 06:18:30 -0300994 ivtv_release_stream(s);
Hans Verkuil09250192010-03-27 14:10:13 -0300995 v4l2_fh_exit(&item->fh);
Hans Verkuil3562c432007-08-18 11:46:05 -0300996 kfree(item);
997 return -EBUSY;
998 }
999 }
1000 /* Mark that the radio is being used. */
1001 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001002 /* We have the radio */
1003 ivtv_mute(itv);
1004 /* Switch tuner to radio */
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001005 ivtv_call_all(itv, tuner, s_radio);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001006 /* Select the correct audio input (i.e. radio tuner) */
1007 ivtv_audio_set_io(itv);
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001008 if (itv->hw_flags & IVTV_HW_SAA711X) {
Hans Verkuil3ff4ad82009-04-01 03:15:52 -03001009 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
1010 SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
Hans Verkuilf2100d82007-05-17 08:08:45 -03001011 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001012 /* Done! Unmute and continue. */
1013 ivtv_unmute(itv);
1014 }
1015
1016 /* YUV or MPG Decoding Mode? */
Ian Armstrongc240ad02007-10-16 03:21:46 -03001017 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001018 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
Ian Armstrongc240ad02007-10-16 03:21:46 -03001019 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001020 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
Ian Armstrongc240ad02007-10-16 03:21:46 -03001021 /* For yuv, we need to know the dma size before we start */
1022 itv->dma_data_req_size =
Ian Armstrong77aded62007-11-05 14:27:09 -03001023 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
Ian Armstrongc240ad02007-10-16 03:21:46 -03001024 itv->yuv_info.stream_size = 0;
1025 }
Hans Verkuil09250192010-03-27 14:10:13 -03001026 v4l2_fh_add(&item->fh);
Hans Verkuilbaa40722007-08-19 07:10:55 -03001027 return 0;
1028}
1029
Hans Verkuilbec43662008-12-30 06:58:20 -03001030int ivtv_v4l2_open(struct file *filp)
Hans Verkuilbaa40722007-08-19 07:10:55 -03001031{
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001032 int res;
Hans Verkuilbaa40722007-08-19 07:10:55 -03001033 struct ivtv *itv = NULL;
1034 struct ivtv_stream *s = NULL;
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001035 struct video_device *vdev = video_devdata(filp);
Hans Verkuilbaa40722007-08-19 07:10:55 -03001036
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001037 s = video_get_drvdata(vdev);
1038 itv = s->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001039
Hans Verkuilbaa40722007-08-19 07:10:55 -03001040 mutex_lock(&itv->serialize_lock);
1041 if (ivtv_init_on_first_open(itv)) {
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001042 IVTV_ERR("Failed to initialize on device %s\n",
1043 video_device_node_name(vdev));
Hans Verkuilbaa40722007-08-19 07:10:55 -03001044 mutex_unlock(&itv->serialize_lock);
1045 return -ENXIO;
1046 }
1047 res = ivtv_serialized_open(s, filp);
1048 mutex_unlock(&itv->serialize_lock);
1049 return res;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001050}
1051
1052void ivtv_mute(struct ivtv *itv)
1053{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001054 if (atomic_read(&itv->capturing))
1055 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001056 IVTV_DEBUG_INFO("Mute\n");
1057}
1058
1059void ivtv_unmute(struct ivtv *itv)
1060{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001061 if (atomic_read(&itv->capturing)) {
Hans Verkuil3562c432007-08-18 11:46:05 -03001062 ivtv_msleep_timeout(100, 0);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001063 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
1064 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
1065 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001066 IVTV_DEBUG_INFO("Unmute\n");
1067}