blob: bee9605bd273e0b294c06b562a4df0451f295af8 [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"
Hans Verkuil09250192010-03-27 14:10:13 -030035#include <media/v4l2-event.h>
Hans Verkuilf2100d82007-05-17 08:08:45 -030036#include <media/saa7115.h>
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030037
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. */
Adrian Bunk95f73c52008-07-22 14:20:12 -030043static int ivtv_claim_stream(struct ivtv_open_id *id, int type)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030044{
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 &&
Hans Verkuila8b86432008-10-04 08:05:30 -030082 itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030083 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. */
101void 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
149static void ivtv_dualwatch(struct ivtv *itv)
150{
151 struct v4l2_tuner vt;
Andy Walls0d82fe82009-01-01 19:02:31 -0300152 u32 new_bitmap;
153 u32 new_stereo_mode;
154 const u32 stereo_mask = 0x0300;
155 const u32 dual = 0x0200;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300156
157 new_stereo_mode = itv->params.audio_properties & stereo_mask;
158 memset(&vt, 0, sizeof(vt));
Hans Verkuil67ec09f2008-11-29 19:38:23 -0300159 ivtv_call_all(itv, tuner, g_tuner, &vt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300160 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
178static 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;
Hans Verkuil5614b022007-08-23 17:48:41 -0300193 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
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300196
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);
Hans Verkuil5614b022007-08-23 17:48:41 -0300204 e->flags = mapping[read_enc(addr + 12) & 7];
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300205 i++;
206 }
207 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
208}
209
210static 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
Julia Lawall168c6262008-04-16 16:13:15 -0300223 if (time_after(jiffies,
224 itv->dualwatch_jiffies +
225 msecs_to_jiffies(1000))) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300226 itv->dualwatch_jiffies = jiffies;
227 ivtv_dualwatch(itv);
228 }
229
230 if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
231 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
232 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
233 /* byteswap and process VBI data */
234 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
235 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
236 }
237 }
238 buf = &itv->vbi.sliced_mpeg_buf;
239 if (buf->readpos != buf->bytesused) {
240 return buf;
241 }
242 }
243
244 /* do we have leftover data? */
245 buf = ivtv_dequeue(s, &s->q_io);
246 if (buf)
247 return buf;
248
249 /* do we have new data? */
250 buf = ivtv_dequeue(s, &s->q_full);
251 if (buf) {
Hans Verkuilf4071b82007-07-28 12:07:12 -0300252 if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300253 return buf;
Hans Verkuilf4071b82007-07-28 12:07:12 -0300254 buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300255 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
256 /* byteswap MPG data */
257 ivtv_buf_swap(buf);
258 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
259 /* byteswap and process VBI data */
260 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
261 }
262 return buf;
263 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300264
265 /* return if end of stream */
266 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300267 IVTV_DEBUG_INFO("EOS %s\n", s->name);
268 return NULL;
269 }
270
Hans Verkuil559e1962007-08-23 17:51:07 -0300271 /* return if file was opened with O_NONBLOCK */
272 if (non_block) {
273 *err = -EAGAIN;
274 return NULL;
275 }
276
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300277 /* wait for more data to arrive */
278 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
279 /* New buffers might have become available before we were added to the waitqueue */
280 if (!s->q_full.buffers)
281 schedule();
282 finish_wait(&s->waitq, &wait);
283 if (signal_pending(current)) {
284 /* return if a signal was received */
285 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
286 *err = -EINTR;
287 return NULL;
288 }
289 }
290}
291
292static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
293{
294 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
295
296 itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
297 itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
298 itv->vbi.sliced_mpeg_buf.readpos = 0;
299}
300
301static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
302 char __user *ubuf, size_t ucount)
303{
304 struct ivtv *itv = s->itv;
305 size_t len = buf->bytesused - buf->readpos;
306
307 if (len > ucount) len = ucount;
308 if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
Hans Verkuila8b86432008-10-04 08:05:30 -0300309 !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300310 const char *start = buf->buf + buf->readpos;
311 const char *p = start + 1;
312 const u8 *q;
313 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
314 int stuffing, i;
315
316 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
317 p = q + 1;
318 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
319 q[1] != 0 || q[2] != 1 || q[3] != ch) {
320 continue;
321 }
322 if (!itv->search_pack_header) {
323 if ((q[6] & 0xc0) != 0x80)
324 continue;
325 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
326 ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
327 ch = 0xba;
328 itv->search_pack_header = 1;
329 p = q + 9;
330 }
331 continue;
332 }
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 && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
339 q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
340 q[16 + stuffing] == 1) {
341 itv->search_pack_header = 0;
342 len = (char *)q - start;
343 ivtv_setup_sliced_vbi_buf(itv);
344 break;
345 }
346 }
347 }
348 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
349 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
350 return -EFAULT;
351 }
352 /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
353 buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
354 buf == &itv->vbi.sliced_mpeg_buf); */
355 buf->readpos += len;
356 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
357 itv->mpg_data_received += len;
358 return len;
359}
360
361static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
362{
363 struct ivtv *itv = s->itv;
364 size_t tot_written = 0;
365 int single_frame = 0;
366
367 if (atomic_read(&itv->capturing) == 0 && s->id == -1) {
368 /* shouldn't happen */
369 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
370 return -EIO;
371 }
372
373 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
374 arrive one-by-one, so make sure we never output more than one VBI frame at a time */
375 if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
Hans Verkuila8b86432008-10-04 08:05:30 -0300376 (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300377 single_frame = 1;
378
379 for (;;) {
380 struct ivtv_buffer *buf;
381 int rc;
382
383 buf = ivtv_get_buffer(s, non_block, &rc);
Hans Verkuil559e1962007-08-23 17:51:07 -0300384 /* if there is no data available... */
385 if (buf == NULL) {
386 /* if we got data, then return that regardless */
387 if (tot_written)
388 break;
389 /* EOS condition */
390 if (rc == 0) {
391 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
392 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
393 ivtv_release_stream(s);
394 }
395 /* set errno */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300396 return rc;
Hans Verkuil559e1962007-08-23 17:51:07 -0300397 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300398 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
399 if (buf != &itv->vbi.sliced_mpeg_buf) {
400 ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
401 }
402 else if (buf->readpos == buf->bytesused) {
403 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
404 itv->vbi.sliced_mpeg_size[idx] = 0;
405 itv->vbi.inserted_frame++;
406 itv->vbi_data_inserted += buf->bytesused;
407 }
408 if (rc < 0)
409 return rc;
410 tot_written += rc;
411
412 if (tot_written == tot_count || single_frame)
413 break;
414 }
415 return tot_written;
416}
417
418static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
419 loff_t *pos, int non_block)
420{
421 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
422 struct ivtv *itv = s->itv;
423
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300424 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300425 if (rc > 0)
426 pos += rc;
427 return rc;
428}
429
430int ivtv_start_capture(struct ivtv_open_id *id)
431{
432 struct ivtv *itv = id->itv;
433 struct ivtv_stream *s = &itv->streams[id->type];
434 struct ivtv_stream *s_vbi;
435
436 if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
437 s->type == IVTV_DEC_STREAM_TYPE_MPG ||
438 s->type == IVTV_DEC_STREAM_TYPE_YUV ||
439 s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
440 /* you cannot read from these stream types. */
441 return -EPERM;
442 }
443
444 /* Try to claim this stream. */
445 if (ivtv_claim_stream(id, s->type))
446 return -EBUSY;
447
448 /* This stream does not need to start capturing */
449 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
450 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
451 return 0;
452 }
453
454 /* If capture is already in progress, then we also have to
455 do nothing extra. */
456 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
457 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
458 return 0;
459 }
460
461 /* Start VBI capture if required */
462 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
463 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
464 test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
465 !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
466 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
467 automatically when the MPG stream is claimed.
468 We only need to start the VBI capturing. */
469 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
470 IVTV_DEBUG_WARN("VBI capture start failed\n");
471
472 /* Failure, clean up and return an error */
473 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
474 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
475 /* also releases the associated VBI stream */
476 ivtv_release_stream(s);
477 return -EIO;
478 }
479 IVTV_DEBUG_INFO("VBI insertion started\n");
480 }
481
482 /* Tell the card to start capturing */
483 if (!ivtv_start_v4l2_encode_stream(s)) {
484 /* We're done */
485 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
486 /* Resume a possibly paused encoder */
487 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
488 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
489 return 0;
490 }
491
492 /* failure, clean up */
493 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
494
495 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
496 automatically when the MPG stream is released.
497 We only need to stop the VBI capturing. */
498 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
499 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
500 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
501 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
502 }
503 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
504 ivtv_release_stream(s);
505 return -EIO;
506}
507
508ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
509{
Hans Verkuil09250192010-03-27 14:10:13 -0300510 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300511 struct ivtv *itv = id->itv;
512 struct ivtv_stream *s = &itv->streams[id->type];
513 int rc;
514
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300515 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300516
Hans Verkuilbaa40722007-08-19 07:10:55 -0300517 mutex_lock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300518 rc = ivtv_start_capture(id);
Hans Verkuilbaa40722007-08-19 07:10:55 -0300519 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300520 if (rc)
521 return rc;
522 return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
523}
524
525int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
526{
527 struct ivtv *itv = id->itv;
528 struct ivtv_stream *s = &itv->streams[id->type];
529
530 if (atomic_read(&itv->decoding) == 0) {
531 if (ivtv_claim_stream(id, s->type)) {
532 /* someone else is using this stream already */
533 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
534 return -EBUSY;
535 }
536 ivtv_start_v4l2_decode_stream(s, 0);
537 }
538 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
539 return ivtv_set_speed(itv, speed);
540 return 0;
541}
542
543ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
544{
Hans Verkuil09250192010-03-27 14:10:13 -0300545 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300546 struct ivtv *itv = id->itv;
547 struct ivtv_stream *s = &itv->streams[id->type];
Ian Armstrongc240ad02007-10-16 03:21:46 -0300548 struct yuv_playback_info *yi = &itv->yuv_info;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300549 struct ivtv_buffer *buf;
550 struct ivtv_queue q;
551 int bytes_written = 0;
552 int mode;
553 int rc;
554 DEFINE_WAIT(wait);
555
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300556 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300557
558 if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
559 s->type != IVTV_DEC_STREAM_TYPE_YUV &&
560 s->type != IVTV_DEC_STREAM_TYPE_VOUT)
561 /* not decoder streams */
562 return -EPERM;
563
564 /* Try to claim this stream */
565 if (ivtv_claim_stream(id, s->type))
566 return -EBUSY;
567
568 /* This stream does not need to start any decoding */
569 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300570 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
571
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300572 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300573 ivtv_write_vbi(itv, (const struct v4l2_sliced_vbi_data *)user_buf, elems);
574 return elems * sizeof(struct v4l2_sliced_vbi_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300575 }
576
577 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
578
579 if (ivtv_set_output_mode(itv, mode) != mode) {
580 ivtv_release_stream(s);
581 return -EBUSY;
582 }
583 ivtv_queue_init(&q);
584 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
585
Ian Armstrong464e9f32008-06-21 09:25:23 -0300586 /* Start decoder (returns 0 if already started) */
587 mutex_lock(&itv->serialize_lock);
588 rc = ivtv_start_decoding(id, itv->speed);
589 mutex_unlock(&itv->serialize_lock);
590 if (rc) {
591 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
592
593 /* failure, clean up */
594 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
595 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
596 return rc;
597 }
598
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300599retry:
Ian Armstrong77aded62007-11-05 14:27:09 -0300600 /* If possible, just DMA the entire frame - Check the data transfer size
601 since we may get here before the stream has been fully set-up */
602 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
603 while (count >= itv->dma_data_req_size) {
Ian Armstrong2e668962008-10-11 06:39:05 -0300604 rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
605
606 if (rc < 0)
607 return rc;
608
609 bytes_written += itv->dma_data_req_size;
610 user_buf += itv->dma_data_req_size;
611 count -= itv->dma_data_req_size;
Ian Armstrong77aded62007-11-05 14:27:09 -0300612 }
613 if (count == 0) {
614 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
615 return bytes_written;
616 }
617 }
618
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300619 for (;;) {
620 /* Gather buffers */
621 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
622 ivtv_enqueue(s, buf, &q);
623 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
624 ivtv_enqueue(s, buf, &q);
625 }
626 if (q.buffers)
627 break;
628 if (filp->f_flags & O_NONBLOCK)
629 return -EAGAIN;
630 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
631 /* New buffers might have become free before we were added to the waitqueue */
632 if (!s->q_free.buffers)
633 schedule();
634 finish_wait(&s->waitq, &wait);
635 if (signal_pending(current)) {
636 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
637 return -EINTR;
638 }
639 }
640
641 /* copy user data into buffers */
642 while ((buf = ivtv_dequeue(s, &q))) {
Ian Armstrongc240ad02007-10-16 03:21:46 -0300643 /* yuv is a pain. Don't copy more data than needed for a single
644 frame, otherwise we lose sync with the incoming stream */
645 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
646 yi->stream_size + count > itv->dma_data_req_size)
647 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
648 itv->dma_data_req_size - yi->stream_size);
649 else
650 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300651
Ian Armstrongc240ad02007-10-16 03:21:46 -0300652 /* Make sure we really got all the user data */
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300653 if (rc < 0) {
654 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
655 return rc;
656 }
657 user_buf += rc;
658 count -= rc;
659 bytes_written += rc;
660
Ian Armstrongc240ad02007-10-16 03:21:46 -0300661 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
662 yi->stream_size += rc;
663 /* If we have a complete yuv frame, break loop now */
664 if (yi->stream_size == itv->dma_data_req_size) {
665 ivtv_enqueue(s, buf, &s->q_full);
666 yi->stream_size = 0;
667 break;
668 }
669 }
670
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300671 if (buf->bytesused != s->buf_size) {
672 /* incomplete, leave in q_io for next time */
673 ivtv_enqueue(s, buf, &s->q_io);
674 break;
675 }
676 /* Byteswap MPEG buffer */
677 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
678 ivtv_buf_swap(buf);
679 ivtv_enqueue(s, buf, &s->q_full);
680 }
681
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300682 if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
683 if (s->q_full.length >= itv->dma_data_req_size) {
684 int got_sig;
685
Ian Armstrong77aded62007-11-05 14:27:09 -0300686 if (mode == OUT_YUV)
687 ivtv_yuv_setup_stream_frame(itv);
688
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300689 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
690 while (!(got_sig = signal_pending(current)) &&
691 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
692 schedule();
693 }
694 finish_wait(&itv->dma_waitq, &wait);
695 if (got_sig) {
696 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
697 return -EINTR;
698 }
699
700 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
701 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
702 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
703 }
704 }
705 /* more user data is available, wait until buffers become free
706 to transfer the rest. */
707 if (count && !(filp->f_flags & O_NONBLOCK))
708 goto retry;
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300709 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 -0300710 return bytes_written;
711}
712
713unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
714{
Hans Verkuil09250192010-03-27 14:10:13 -0300715 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300716 struct ivtv *itv = id->itv;
717 struct ivtv_stream *s = &itv->streams[id->type];
718 int res = 0;
719
720 /* add stream's waitq to the poll list */
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300721 IVTV_DEBUG_HI_FILE("Decoder poll\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300722
Hans Verkuil09250192010-03-27 14:10:13 -0300723 /* If there are subscribed events, then only use the new event
724 API instead of the old video.h based API. */
725 if (!list_empty(&id->fh.events->subscribed)) {
726 poll_wait(filp, &id->fh.events->wait, wait);
727 /* Turn off the old-style vsync events */
728 clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
729 if (v4l2_event_pending(&id->fh))
730 res = POLLPRI;
731 } else {
732 /* This is the old-style API which is here only for backwards
733 compatibility. */
734 poll_wait(filp, &s->waitq, wait);
735 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
736 if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
737 test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
738 res = POLLPRI;
739 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300740
741 /* Allow write if buffers are available for writing */
742 if (s->q_free.buffers)
743 res |= POLLOUT | POLLWRNORM;
744 return res;
745}
746
747unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
748{
Hans Verkuil09250192010-03-27 14:10:13 -0300749 struct ivtv_open_id *id = fh2id(filp->private_data);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300750 struct ivtv *itv = id->itv;
751 struct ivtv_stream *s = &itv->streams[id->type];
752 int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
753
754 /* Start a capture if there is none */
755 if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
Hans Verkuilbaa40722007-08-19 07:10:55 -0300756 int rc;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300757
Hans Verkuilbaa40722007-08-19 07:10:55 -0300758 mutex_lock(&itv->serialize_lock);
759 rc = ivtv_start_capture(id);
760 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300761 if (rc) {
762 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
763 s->name, rc);
764 return POLLERR;
765 }
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300766 IVTV_DEBUG_FILE("Encoder poll started capture\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300767 }
768
769 /* add stream's waitq to the poll list */
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300770 IVTV_DEBUG_HI_FILE("Encoder poll\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300771 poll_wait(filp, &s->waitq, wait);
772
Hans Verkuil16928be2008-04-28 12:18:00 -0300773 if (s->q_full.length || s->q_io.length)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300774 return POLLIN | POLLRDNORM;
Hans Verkuil16928be2008-04-28 12:18:00 -0300775 if (eof)
776 return POLLHUP;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300777 return 0;
778}
779
780void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
781{
782 struct ivtv *itv = id->itv;
783 struct ivtv_stream *s = &itv->streams[id->type];
784
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300785 IVTV_DEBUG_FILE("close() of %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300786
787 /* 'Unclaim' this stream */
788
789 /* Stop capturing */
790 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
791 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
792
793 IVTV_DEBUG_INFO("close stopping capture\n");
794 /* Special case: a running VBI capture for VBI insertion
795 in the mpeg stream. Need to stop that too. */
796 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
797 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
798 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
799 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
800 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
801 }
802 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
803 id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
804 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
805 /* Also used internally, don't stop capturing */
806 s->id = -1;
807 }
808 else {
809 ivtv_stop_v4l2_encode_stream(s, gop_end);
810 }
811 }
Hans Verkuil559e1962007-08-23 17:51:07 -0300812 if (!gop_end) {
813 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
814 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
815 ivtv_release_stream(s);
816 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300817}
818
Hans Verkuil31ec1352007-03-03 08:50:42 -0300819static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300820{
821 struct ivtv *itv = id->itv;
822 struct ivtv_stream *s = &itv->streams[id->type];
823
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300824 IVTV_DEBUG_FILE("close() of %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300825
826 /* Stop decoding */
827 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
828 IVTV_DEBUG_INFO("close stopping decode\n");
829
830 ivtv_stop_v4l2_decode_stream(s, flags, pts);
Hans Verkuilad8ff0f2007-08-20 16:01:58 -0300831 itv->output_mode = OUT_NONE;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300832 }
833 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
834 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
835 if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
836 /* Restore registers we've changed & clean up any mess we've made */
837 ivtv_yuv_close(itv);
838 }
Hans Verkuilad8ff0f2007-08-20 16:01:58 -0300839 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
Ian Armstrongcb50f542007-08-18 15:58:51 -0300840 itv->output_mode = OUT_NONE;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300841
842 itv->speed = 0;
Hans Verkuilac425142007-07-22 08:46:38 -0300843 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300844 ivtv_release_stream(s);
845}
846
Hans Verkuilbec43662008-12-30 06:58:20 -0300847int ivtv_v4l2_close(struct file *filp)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300848{
Hans Verkuil09250192010-03-27 14:10:13 -0300849 struct v4l2_fh *fh = filp->private_data;
850 struct ivtv_open_id *id = fh2id(fh);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300851 struct ivtv *itv = id->itv;
852 struct ivtv_stream *s = &itv->streams[id->type];
853
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300854 IVTV_DEBUG_FILE("close %s\n", s->name);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300855
Hans Verkuild46c17d2007-03-10 17:59:15 -0300856 v4l2_prio_close(&itv->prio, &id->prio);
Hans Verkuil09250192010-03-27 14:10:13 -0300857 v4l2_fh_del(fh);
858 v4l2_fh_exit(fh);
Hans Verkuild46c17d2007-03-10 17:59:15 -0300859
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300860 /* Easy case first: this stream was never claimed by us */
861 if (s->id != id->open_id) {
862 kfree(id);
863 return 0;
864 }
865
866 /* 'Unclaim' this stream */
867
868 /* Stop radio */
Hans Verkuilbaa40722007-08-19 07:10:55 -0300869 mutex_lock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300870 if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
871 /* Closing radio device, return to TV mode */
872 ivtv_mute(itv);
873 /* Mark that the radio is no longer in use */
874 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
875 /* Switch tuner to TV */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300876 ivtv_call_all(itv, core, s_std, itv->std);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300877 /* Select correct audio input (i.e. TV tuner or Line in) */
878 ivtv_audio_set_io(itv);
Hans Verkuil3ff4ad82009-04-01 03:15:52 -0300879 if (itv->hw_flags & IVTV_HW_SAA711X) {
880 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
881 SAA7115_FREQ_32_11_MHZ, 0);
Hans Verkuilf2100d82007-05-17 08:08:45 -0300882 }
Hans Verkuil2f7362e2007-10-14 17:27:56 -0300883 if (atomic_read(&itv->capturing) > 0) {
884 /* Undo video mute */
885 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
886 itv->params.video_mute | (itv->params.video_mute_yuv << 8));
887 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300888 /* Done! Unmute and continue. */
889 ivtv_unmute(itv);
890 ivtv_release_stream(s);
891 } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
Hans Verkuil5a338c32007-07-22 09:39:56 -0300892 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
893
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300894 ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
Hans Verkuil5a338c32007-07-22 09:39:56 -0300895
896 /* If all output streams are closed, and if the user doesn't have
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300897 IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
Hans Verkuil5a338c32007-07-22 09:39:56 -0300898 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
Hans Verkuil2f3a9892007-08-25 14:11:23 -0300899 /* disable CC on TV-out */
900 ivtv_disable_cc(itv);
Hans Verkuil5a338c32007-07-22 09:39:56 -0300901 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300902 } else {
903 ivtv_stop_capture(id, 0);
904 }
905 kfree(id);
Hans Verkuilbaa40722007-08-19 07:10:55 -0300906 mutex_unlock(&itv->serialize_lock);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300907 return 0;
908}
909
Hans Verkuilbaa40722007-08-19 07:10:55 -0300910static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300911{
Hans Verkuilbaa40722007-08-19 07:10:55 -0300912 struct ivtv *itv = s->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300913 struct ivtv_open_id *item;
Hans Verkuil09250192010-03-27 14:10:13 -0300914 int res = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300915
Hans Verkuil1aa32c22007-08-19 06:08:58 -0300916 IVTV_DEBUG_FILE("open %s\n", s->name);
Hans Verkuilc976bc82007-07-22 12:52:40 -0300917
Hans Verkuilbaa40722007-08-19 07:10:55 -0300918 if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300919 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
920 return -EBUSY;
921
Hans Verkuilbaa40722007-08-19 07:10:55 -0300922 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300923 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
924 return -EBUSY;
925
Hans Verkuilbaa40722007-08-19 07:10:55 -0300926 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300927 if (read_reg(0x82c) == 0) {
928 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
929 /* return -ENODEV; */
930 }
931 ivtv_udma_alloc(itv);
932 }
933
934 /* Allocate memory */
Hans Verkuil09250192010-03-27 14:10:13 -0300935 item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300936 if (NULL == item) {
937 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
938 return -ENOMEM;
939 }
Hans Verkuil09250192010-03-27 14:10:13 -0300940 v4l2_fh_init(&item->fh, s->vdev);
941 if (s->type == IVTV_DEC_STREAM_TYPE_YUV ||
942 s->type == IVTV_DEC_STREAM_TYPE_MPG) {
943 res = v4l2_event_alloc(&item->fh, 60);
944 }
945 if (res < 0) {
946 v4l2_fh_exit(&item->fh);
947 kfree(item);
948 return res;
949 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300950 item->itv = itv;
Hans Verkuilbaa40722007-08-19 07:10:55 -0300951 item->type = s->type;
Hans Verkuild46c17d2007-03-10 17:59:15 -0300952 v4l2_prio_open(&itv->prio, &item->prio);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300953
954 item->open_id = itv->open_id++;
Hans Verkuil09250192010-03-27 14:10:13 -0300955 filp->private_data = &item->fh;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300956
957 if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
958 /* Try to claim this stream */
959 if (ivtv_claim_stream(item, item->type)) {
960 /* No, it's already in use */
961 kfree(item);
962 return -EBUSY;
963 }
964
Hans Verkuil3562c432007-08-18 11:46:05 -0300965 if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
966 if (atomic_read(&itv->capturing) > 0) {
967 /* switching to radio while capture is
968 in progress is not polite */
Hans Verkuilaf3420b2007-10-12 06:18:30 -0300969 ivtv_release_stream(s);
Hans Verkuil09250192010-03-27 14:10:13 -0300970 v4l2_fh_exit(&item->fh);
Hans Verkuil3562c432007-08-18 11:46:05 -0300971 kfree(item);
972 return -EBUSY;
973 }
974 }
975 /* Mark that the radio is being used. */
976 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300977 /* We have the radio */
978 ivtv_mute(itv);
979 /* Switch tuner to radio */
Hans Verkuil67ec09f2008-11-29 19:38:23 -0300980 ivtv_call_all(itv, tuner, s_radio);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300981 /* Select the correct audio input (i.e. radio tuner) */
982 ivtv_audio_set_io(itv);
Hans Verkuil67ec09f2008-11-29 19:38:23 -0300983 if (itv->hw_flags & IVTV_HW_SAA711X) {
Hans Verkuil3ff4ad82009-04-01 03:15:52 -0300984 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
985 SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
Hans Verkuilf2100d82007-05-17 08:08:45 -0300986 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300987 /* Done! Unmute and continue. */
988 ivtv_unmute(itv);
989 }
990
991 /* YUV or MPG Decoding Mode? */
Ian Armstrongc240ad02007-10-16 03:21:46 -0300992 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300993 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
Ian Armstrongc240ad02007-10-16 03:21:46 -0300994 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300995 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
Ian Armstrongc240ad02007-10-16 03:21:46 -0300996 /* For yuv, we need to know the dma size before we start */
997 itv->dma_data_req_size =
Ian Armstrong77aded62007-11-05 14:27:09 -0300998 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
Ian Armstrongc240ad02007-10-16 03:21:46 -0300999 itv->yuv_info.stream_size = 0;
1000 }
Hans Verkuil09250192010-03-27 14:10:13 -03001001 v4l2_fh_add(&item->fh);
Hans Verkuilbaa40722007-08-19 07:10:55 -03001002 return 0;
1003}
1004
Hans Verkuilbec43662008-12-30 06:58:20 -03001005int ivtv_v4l2_open(struct file *filp)
Hans Verkuilbaa40722007-08-19 07:10:55 -03001006{
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001007 int res;
Hans Verkuilbaa40722007-08-19 07:10:55 -03001008 struct ivtv *itv = NULL;
1009 struct ivtv_stream *s = NULL;
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001010 struct video_device *vdev = video_devdata(filp);
Hans Verkuilbaa40722007-08-19 07:10:55 -03001011
Hans Verkuil67ec09f2008-11-29 19:38:23 -03001012 s = video_get_drvdata(vdev);
1013 itv = s->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001014
Hans Verkuilbaa40722007-08-19 07:10:55 -03001015 mutex_lock(&itv->serialize_lock);
1016 if (ivtv_init_on_first_open(itv)) {
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001017 IVTV_ERR("Failed to initialize on device %s\n",
1018 video_device_node_name(vdev));
Hans Verkuilbaa40722007-08-19 07:10:55 -03001019 mutex_unlock(&itv->serialize_lock);
1020 return -ENXIO;
1021 }
1022 res = ivtv_serialized_open(s, filp);
1023 mutex_unlock(&itv->serialize_lock);
1024 return res;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001025}
1026
1027void ivtv_mute(struct ivtv *itv)
1028{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001029 if (atomic_read(&itv->capturing))
1030 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001031 IVTV_DEBUG_INFO("Mute\n");
1032}
1033
1034void ivtv_unmute(struct ivtv *itv)
1035{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001036 if (atomic_read(&itv->capturing)) {
Hans Verkuil3562c432007-08-18 11:46:05 -03001037 ivtv_msleep_timeout(100, 0);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001038 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
1039 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
1040 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001041 IVTV_DEBUG_INFO("Unmute\n");
1042}