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>
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.
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.
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
22 #include "ivtv-driver.h"
23 #include "ivtv-fileops.h"
25 #include "ivtv-queue.h"
26 #include "ivtv-udma.h"
29 #include "ivtv-mailbox.h"
30 #include "ivtv-routing.h"
31 #include "ivtv-streams.h"
33 #include "ivtv-ioctl.h"
34 #include "ivtv-cards.h"
35 #include <media/saa7115.h>
37 /* This function tries to claim the stream for a specific file descriptor.
38 If no one else is using this stream then the stream is claimed and
39 associated VBI streams are also automatically claimed.
40 Possible error returns: -EBUSY if someone else has claimed
41 the stream or 0 on success. */
42 int ivtv_claim_stream(struct ivtv_open_id
*id
, int type
)
44 struct ivtv
*itv
= id
->itv
;
45 struct ivtv_stream
*s
= &itv
->streams
[type
];
46 struct ivtv_stream
*s_vbi
;
49 if (test_and_set_bit(IVTV_F_S_CLAIMED
, &s
->s_flags
)) {
50 /* someone already claimed this stream */
51 if (s
->id
== id
->open_id
) {
52 /* yes, this file descriptor did. So that's OK. */
55 if (s
->id
== -1 && (type
== IVTV_DEC_STREAM_TYPE_VBI
||
56 type
== IVTV_ENC_STREAM_TYPE_VBI
)) {
57 /* VBI is handled already internally, now also assign
58 the file descriptor to this stream for external
59 reading of the stream. */
61 IVTV_DEBUG_INFO("Start Read VBI\n");
64 /* someone else is using this stream already */
65 IVTV_DEBUG_INFO("Stream %d is busy\n", type
);
69 if (type
== IVTV_DEC_STREAM_TYPE_VBI
) {
70 /* Enable reinsertion interrupt */
71 ivtv_clear_irq_mask(itv
, IVTV_IRQ_DEC_VBI_RE_INSERT
);
74 /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
75 IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
76 (provided VBI insertion is on and sliced VBI is selected), for all
77 other streams we're done */
78 if (type
== IVTV_DEC_STREAM_TYPE_MPG
) {
79 vbi_type
= IVTV_DEC_STREAM_TYPE_VBI
;
80 } else if (type
== IVTV_ENC_STREAM_TYPE_MPG
&&
81 itv
->vbi
.insert_mpeg
&& itv
->vbi
.sliced_in
->service_set
) {
82 vbi_type
= IVTV_ENC_STREAM_TYPE_VBI
;
86 s_vbi
= &itv
->streams
[vbi_type
];
88 if (!test_and_set_bit(IVTV_F_S_CLAIMED
, &s_vbi
->s_flags
)) {
89 /* Enable reinsertion interrupt */
90 if (vbi_type
== IVTV_DEC_STREAM_TYPE_VBI
)
91 ivtv_clear_irq_mask(itv
, IVTV_IRQ_DEC_VBI_RE_INSERT
);
93 /* mark that it is used internally */
94 set_bit(IVTV_F_S_INTERNAL_USE
, &s_vbi
->s_flags
);
98 /* This function releases a previously claimed stream. It will take into
99 account associated VBI streams. */
100 void ivtv_release_stream(struct ivtv_stream
*s
)
102 struct ivtv
*itv
= s
->itv
;
103 struct ivtv_stream
*s_vbi
;
106 if ((s
->type
== IVTV_DEC_STREAM_TYPE_VBI
|| s
->type
== IVTV_ENC_STREAM_TYPE_VBI
) &&
107 test_bit(IVTV_F_S_INTERNAL_USE
, &s
->s_flags
)) {
108 /* this stream is still in use internally */
111 if (!test_and_clear_bit(IVTV_F_S_CLAIMED
, &s
->s_flags
)) {
112 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s
->name
);
116 ivtv_flush_queues(s
);
118 /* disable reinsertion interrupt */
119 if (s
->type
== IVTV_DEC_STREAM_TYPE_VBI
)
120 ivtv_set_irq_mask(itv
, IVTV_IRQ_DEC_VBI_RE_INSERT
);
122 /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
123 IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
124 for all other streams we're done */
125 if (s
->type
== IVTV_DEC_STREAM_TYPE_MPG
)
126 s_vbi
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_VBI
];
127 else if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
)
128 s_vbi
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
132 /* clear internal use flag */
133 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE
, &s_vbi
->s_flags
)) {
134 /* was already cleared */
137 if (s_vbi
->id
!= -1) {
138 /* VBI stream still claimed by a file descriptor */
141 /* disable reinsertion interrupt */
142 if (s_vbi
->type
== IVTV_DEC_STREAM_TYPE_VBI
)
143 ivtv_set_irq_mask(itv
, IVTV_IRQ_DEC_VBI_RE_INSERT
);
144 clear_bit(IVTV_F_S_CLAIMED
, &s_vbi
->s_flags
);
145 ivtv_flush_queues(s_vbi
);
148 static void ivtv_dualwatch(struct ivtv
*itv
)
150 struct v4l2_tuner vt
;
153 const u16 stereo_mask
= 0x0300;
154 const u16 dual
= 0x0200;
156 new_stereo_mode
= itv
->params
.audio_properties
& stereo_mask
;
157 memset(&vt
, 0, sizeof(vt
));
158 ivtv_call_i2c_clients(itv
, VIDIOC_G_TUNER
, &vt
);
159 if (vt
.audmode
== V4L2_TUNER_MODE_LANG1_LANG2
&& (vt
.rxsubchans
& V4L2_TUNER_SUB_LANG2
))
160 new_stereo_mode
= dual
;
162 if (new_stereo_mode
== itv
->dualwatch_stereo_mode
)
165 new_bitmap
= new_stereo_mode
| (itv
->params
.audio_properties
& ~stereo_mask
);
167 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
168 itv
->dualwatch_stereo_mode
, new_stereo_mode
, new_bitmap
);
170 if (ivtv_vapi(itv
, CX2341X_ENC_SET_AUDIO_PROPERTIES
, 1, new_bitmap
) == 0) {
171 itv
->dualwatch_stereo_mode
= new_stereo_mode
;
174 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
177 static void ivtv_update_pgm_info(struct ivtv
*itv
)
179 u32 wr_idx
= (read_enc(itv
->pgm_info_offset
) - itv
->pgm_info_offset
- 4) / 24;
183 if (wr_idx
>= itv
->pgm_info_num
) {
184 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx
, itv
->pgm_info_num
);
187 cnt
= (wr_idx
+ itv
->pgm_info_num
- itv
->pgm_info_write_idx
) % itv
->pgm_info_num
;
189 int idx
= (itv
->pgm_info_write_idx
+ i
) % itv
->pgm_info_num
;
190 struct v4l2_enc_idx_entry
*e
= itv
->pgm_info
+ idx
;
191 u32 addr
= itv
->pgm_info_offset
+ 4 + idx
* 24;
192 const int mapping
[8] = { -1, V4L2_ENC_IDX_FRAME_I
, V4L2_ENC_IDX_FRAME_P
, -1,
193 V4L2_ENC_IDX_FRAME_B
, -1, -1, -1 };
196 e
->offset
= read_enc(addr
+ 4) + ((u64
)read_enc(addr
+ 8) << 32);
197 if (e
->offset
> itv
->mpg_data_received
) {
200 e
->offset
+= itv
->vbi_data_inserted
;
201 e
->length
= read_enc(addr
);
202 e
->pts
= read_enc(addr
+ 16) + ((u64
)(read_enc(addr
+ 20) & 1) << 32);
203 e
->flags
= mapping
[read_enc(addr
+ 12) & 7];
206 itv
->pgm_info_write_idx
= (itv
->pgm_info_write_idx
+ i
) % itv
->pgm_info_num
;
209 static struct ivtv_buffer
*ivtv_get_buffer(struct ivtv_stream
*s
, int non_block
, int *err
)
211 struct ivtv
*itv
= s
->itv
;
212 struct ivtv_stream
*s_vbi
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
213 struct ivtv_buffer
*buf
;
218 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
) {
219 /* Process pending program info updates and pending VBI data */
220 ivtv_update_pgm_info(itv
);
222 if (jiffies
- itv
->dualwatch_jiffies
> msecs_to_jiffies(1000)) {
223 itv
->dualwatch_jiffies
= jiffies
;
227 if (test_bit(IVTV_F_S_INTERNAL_USE
, &s_vbi
->s_flags
) &&
228 !test_bit(IVTV_F_S_APPL_IO
, &s_vbi
->s_flags
)) {
229 while ((buf
= ivtv_dequeue(s_vbi
, &s_vbi
->q_full
))) {
230 /* byteswap and process VBI data */
231 ivtv_process_vbi_data(itv
, buf
, s_vbi
->dma_pts
, s_vbi
->type
);
232 ivtv_enqueue(s_vbi
, buf
, &s_vbi
->q_free
);
235 buf
= &itv
->vbi
.sliced_mpeg_buf
;
236 if (buf
->readpos
!= buf
->bytesused
) {
241 /* do we have leftover data? */
242 buf
= ivtv_dequeue(s
, &s
->q_io
);
246 /* do we have new data? */
247 buf
= ivtv_dequeue(s
, &s
->q_full
);
249 if ((buf
->b_flags
& IVTV_F_B_NEED_BUF_SWAP
) == 0)
251 buf
->b_flags
&= ~IVTV_F_B_NEED_BUF_SWAP
;
252 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
)
253 /* byteswap MPG data */
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
);
262 /* return if end of stream */
263 if (s
->type
!= IVTV_DEC_STREAM_TYPE_VBI
&& !test_bit(IVTV_F_S_STREAMING
, &s
->s_flags
)) {
264 IVTV_DEBUG_INFO("EOS %s\n", s
->name
);
268 /* return if file was opened with O_NONBLOCK */
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
)
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
);
289 static void ivtv_setup_sliced_vbi_buf(struct ivtv
*itv
)
291 int idx
= itv
->vbi
.inserted_frame
% IVTV_VBI_FRAMES
;
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;
298 static size_t ivtv_copy_buf_to_user(struct ivtv_stream
*s
, struct ivtv_buffer
*buf
,
299 char __user
*ubuf
, size_t ucount
)
301 struct ivtv
*itv
= s
->itv
;
302 size_t len
= buf
->bytesused
- buf
->readpos
;
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;
310 u8 ch
= itv
->search_pack_header
? 0xba : 0xe0;
313 while (start
+ len
> p
&& (q
= memchr(p
, 0, start
+ len
- p
))) {
315 if ((char *)q
+ 15 >= buf
->buf
+ buf
->bytesused
||
316 q
[1] != 0 || q
[2] != 1 || q
[3] != ch
) {
319 if (!itv
->search_pack_header
) {
320 if ((q
[6] & 0xc0) != 0x80)
322 if (((q
[7] & 0xc0) == 0x80 && (q
[9] & 0xf0) == 0x20) ||
323 ((q
[7] & 0xc0) == 0xc0 && (q
[9] & 0xf0) == 0x30)) {
325 itv
->search_pack_header
= 1;
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)
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
);
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
);
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); */
353 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
&& buf
!= &itv
->vbi
.sliced_mpeg_buf
)
354 itv
->mpg_data_received
+= len
;
358 static ssize_t
ivtv_read(struct ivtv_stream
*s
, char __user
*ubuf
, size_t tot_count
, int non_block
)
360 struct ivtv
*itv
= s
->itv
;
361 size_t tot_written
= 0;
362 int single_frame
= 0;
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
);
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
))
377 struct ivtv_buffer
*buf
;
380 buf
= ivtv_get_buffer(s
, non_block
, &rc
);
381 /* if there is no data available... */
383 /* if we got data, then return that regardless */
388 clear_bit(IVTV_F_S_STREAMOFF
, &s
->s_flags
);
389 clear_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
390 ivtv_release_stream(s
);
395 rc
= ivtv_copy_buf_to_user(s
, buf
, ubuf
+ tot_written
, tot_count
- tot_written
);
396 if (buf
!= &itv
->vbi
.sliced_mpeg_buf
) {
397 ivtv_enqueue(s
, buf
, (buf
->readpos
== buf
->bytesused
) ? &s
->q_free
: &s
->q_io
);
399 else if (buf
->readpos
== buf
->bytesused
) {
400 int idx
= itv
->vbi
.inserted_frame
% IVTV_VBI_FRAMES
;
401 itv
->vbi
.sliced_mpeg_size
[idx
] = 0;
402 itv
->vbi
.inserted_frame
++;
403 itv
->vbi_data_inserted
+= buf
->bytesused
;
409 if (tot_written
== tot_count
|| single_frame
)
415 static ssize_t
ivtv_read_pos(struct ivtv_stream
*s
, char __user
*ubuf
, size_t count
,
416 loff_t
*pos
, int non_block
)
418 ssize_t rc
= count
? ivtv_read(s
, ubuf
, count
, non_block
) : 0;
419 struct ivtv
*itv
= s
->itv
;
421 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count
, s
->name
, rc
);
427 int ivtv_start_capture(struct ivtv_open_id
*id
)
429 struct ivtv
*itv
= id
->itv
;
430 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
431 struct ivtv_stream
*s_vbi
;
433 if (s
->type
== IVTV_ENC_STREAM_TYPE_RAD
||
434 s
->type
== IVTV_DEC_STREAM_TYPE_MPG
||
435 s
->type
== IVTV_DEC_STREAM_TYPE_YUV
||
436 s
->type
== IVTV_DEC_STREAM_TYPE_VOUT
) {
437 /* you cannot read from these stream types. */
441 /* Try to claim this stream. */
442 if (ivtv_claim_stream(id
, s
->type
))
445 /* This stream does not need to start capturing */
446 if (s
->type
== IVTV_DEC_STREAM_TYPE_VBI
) {
447 set_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
451 /* If capture is already in progress, then we also have to
453 if (test_bit(IVTV_F_S_STREAMOFF
, &s
->s_flags
) || test_and_set_bit(IVTV_F_S_STREAMING
, &s
->s_flags
)) {
454 set_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
458 /* Start VBI capture if required */
459 s_vbi
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
460 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
&&
461 test_bit(IVTV_F_S_INTERNAL_USE
, &s_vbi
->s_flags
) &&
462 !test_and_set_bit(IVTV_F_S_STREAMING
, &s_vbi
->s_flags
)) {
463 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
464 automatically when the MPG stream is claimed.
465 We only need to start the VBI capturing. */
466 if (ivtv_start_v4l2_encode_stream(s_vbi
)) {
467 IVTV_DEBUG_WARN("VBI capture start failed\n");
469 /* Failure, clean up and return an error */
470 clear_bit(IVTV_F_S_STREAMING
, &s_vbi
->s_flags
);
471 clear_bit(IVTV_F_S_STREAMING
, &s
->s_flags
);
472 /* also releases the associated VBI stream */
473 ivtv_release_stream(s
);
476 IVTV_DEBUG_INFO("VBI insertion started\n");
479 /* Tell the card to start capturing */
480 if (!ivtv_start_v4l2_encode_stream(s
)) {
482 set_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
483 /* Resume a possibly paused encoder */
484 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED
, &itv
->i_flags
))
485 ivtv_vapi(itv
, CX2341X_ENC_PAUSE_ENCODER
, 1, 1);
489 /* failure, clean up */
490 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s
->name
);
492 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
493 automatically when the MPG stream is released.
494 We only need to stop the VBI capturing. */
495 if (s
->type
== IVTV_ENC_STREAM_TYPE_MPG
&&
496 test_bit(IVTV_F_S_STREAMING
, &s_vbi
->s_flags
)) {
497 ivtv_stop_v4l2_encode_stream(s_vbi
, 0);
498 clear_bit(IVTV_F_S_STREAMING
, &s_vbi
->s_flags
);
500 clear_bit(IVTV_F_S_STREAMING
, &s
->s_flags
);
501 ivtv_release_stream(s
);
505 ssize_t
ivtv_v4l2_read(struct file
* filp
, char __user
*buf
, size_t count
, loff_t
* pos
)
507 struct ivtv_open_id
*id
= filp
->private_data
;
508 struct ivtv
*itv
= id
->itv
;
509 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
512 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count
, s
->name
);
514 mutex_lock(&itv
->serialize_lock
);
515 rc
= ivtv_start_capture(id
);
516 mutex_unlock(&itv
->serialize_lock
);
519 return ivtv_read_pos(s
, buf
, count
, pos
, filp
->f_flags
& O_NONBLOCK
);
522 int ivtv_start_decoding(struct ivtv_open_id
*id
, int speed
)
524 struct ivtv
*itv
= id
->itv
;
525 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
527 if (atomic_read(&itv
->decoding
) == 0) {
528 if (ivtv_claim_stream(id
, s
->type
)) {
529 /* someone else is using this stream already */
530 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
533 ivtv_start_v4l2_decode_stream(s
, 0);
535 if (s
->type
== IVTV_DEC_STREAM_TYPE_MPG
)
536 return ivtv_set_speed(itv
, speed
);
540 ssize_t
ivtv_v4l2_write(struct file
*filp
, const char __user
*user_buf
, size_t count
, loff_t
*pos
)
542 struct ivtv_open_id
*id
= filp
->private_data
;
543 struct ivtv
*itv
= id
->itv
;
544 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
545 struct yuv_playback_info
*yi
= &itv
->yuv_info
;
546 struct ivtv_buffer
*buf
;
548 int bytes_written
= 0;
553 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count
, s
->name
);
555 if (s
->type
!= IVTV_DEC_STREAM_TYPE_MPG
&&
556 s
->type
!= IVTV_DEC_STREAM_TYPE_YUV
&&
557 s
->type
!= IVTV_DEC_STREAM_TYPE_VOUT
)
558 /* not decoder streams */
561 /* Try to claim this stream */
562 if (ivtv_claim_stream(id
, s
->type
))
565 /* This stream does not need to start any decoding */
566 if (s
->type
== IVTV_DEC_STREAM_TYPE_VOUT
) {
567 int elems
= count
/ sizeof(struct v4l2_sliced_vbi_data
);
569 set_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
570 ivtv_write_vbi(itv
, (const struct v4l2_sliced_vbi_data
*)user_buf
, elems
);
571 return elems
* sizeof(struct v4l2_sliced_vbi_data
);
574 mode
= s
->type
== IVTV_DEC_STREAM_TYPE_MPG
? OUT_MPG
: OUT_YUV
;
576 if (ivtv_set_output_mode(itv
, mode
) != mode
) {
577 ivtv_release_stream(s
);
581 set_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
586 while (q
.length
- q
.bytesused
< count
&& (buf
= ivtv_dequeue(s
, &s
->q_io
)))
587 ivtv_enqueue(s
, buf
, &q
);
588 while (q
.length
- q
.bytesused
< count
&& (buf
= ivtv_dequeue(s
, &s
->q_free
))) {
589 ivtv_enqueue(s
, buf
, &q
);
593 if (filp
->f_flags
& O_NONBLOCK
)
595 prepare_to_wait(&s
->waitq
, &wait
, TASK_INTERRUPTIBLE
);
596 /* New buffers might have become free before we were added to the waitqueue */
597 if (!s
->q_free
.buffers
)
599 finish_wait(&s
->waitq
, &wait
);
600 if (signal_pending(current
)) {
601 IVTV_DEBUG_INFO("User stopped %s\n", s
->name
);
606 /* copy user data into buffers */
607 while ((buf
= ivtv_dequeue(s
, &q
))) {
608 /* yuv is a pain. Don't copy more data than needed for a single
609 frame, otherwise we lose sync with the incoming stream */
610 if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
&&
611 yi
->stream_size
+ count
> itv
->dma_data_req_size
)
612 rc
= ivtv_buf_copy_from_user(s
, buf
, user_buf
,
613 itv
->dma_data_req_size
- yi
->stream_size
);
615 rc
= ivtv_buf_copy_from_user(s
, buf
, user_buf
, count
);
617 /* Make sure we really got all the user data */
619 ivtv_queue_move(s
, &q
, NULL
, &s
->q_free
, 0);
626 if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
) {
627 yi
->stream_size
+= rc
;
628 /* If we have a complete yuv frame, break loop now */
629 if (yi
->stream_size
== itv
->dma_data_req_size
) {
630 ivtv_enqueue(s
, buf
, &s
->q_full
);
636 if (buf
->bytesused
!= s
->buf_size
) {
637 /* incomplete, leave in q_io for next time */
638 ivtv_enqueue(s
, buf
, &s
->q_io
);
641 /* Byteswap MPEG buffer */
642 if (s
->type
== IVTV_DEC_STREAM_TYPE_MPG
)
644 ivtv_enqueue(s
, buf
, &s
->q_full
);
647 /* Start decoder (returns 0 if already started) */
648 mutex_lock(&itv
->serialize_lock
);
649 rc
= ivtv_start_decoding(id
, itv
->speed
);
650 mutex_unlock(&itv
->serialize_lock
);
652 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s
->name
);
654 /* failure, clean up */
655 clear_bit(IVTV_F_S_STREAMING
, &s
->s_flags
);
656 clear_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
659 if (test_bit(IVTV_F_S_NEEDS_DATA
, &s
->s_flags
)) {
660 if (s
->q_full
.length
>= itv
->dma_data_req_size
) {
663 prepare_to_wait(&itv
->dma_waitq
, &wait
, TASK_INTERRUPTIBLE
);
664 while (!(got_sig
= signal_pending(current
)) &&
665 test_bit(IVTV_F_S_DMA_PENDING
, &s
->s_flags
)) {
668 finish_wait(&itv
->dma_waitq
, &wait
);
670 IVTV_DEBUG_INFO("User interrupted %s\n", s
->name
);
674 clear_bit(IVTV_F_S_NEEDS_DATA
, &s
->s_flags
);
675 ivtv_queue_move(s
, &s
->q_full
, NULL
, &s
->q_predma
, itv
->dma_data_req_size
);
676 ivtv_dma_stream_dec_prepare(s
, itv
->dma_data_req_offset
+ IVTV_DECODER_OFFSET
, 1);
679 /* more user data is available, wait until buffers become free
680 to transfer the rest. */
681 if (count
&& !(filp
->f_flags
& O_NONBLOCK
))
683 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written
, s
->name
, s
->q_full
.bytesused
);
684 return bytes_written
;
687 unsigned int ivtv_v4l2_dec_poll(struct file
*filp
, poll_table
*wait
)
689 struct ivtv_open_id
*id
= filp
->private_data
;
690 struct ivtv
*itv
= id
->itv
;
691 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
694 /* add stream's waitq to the poll list */
695 IVTV_DEBUG_HI_FILE("Decoder poll\n");
696 poll_wait(filp
, &s
->waitq
, wait
);
698 set_bit(IVTV_F_I_EV_VSYNC_ENABLED
, &itv
->i_flags
);
699 if (test_bit(IVTV_F_I_EV_VSYNC
, &itv
->i_flags
) ||
700 test_bit(IVTV_F_I_EV_DEC_STOPPED
, &itv
->i_flags
))
703 /* Allow write if buffers are available for writing */
704 if (s
->q_free
.buffers
)
705 res
|= POLLOUT
| POLLWRNORM
;
709 unsigned int ivtv_v4l2_enc_poll(struct file
*filp
, poll_table
* wait
)
711 struct ivtv_open_id
*id
= filp
->private_data
;
712 struct ivtv
*itv
= id
->itv
;
713 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
714 int eof
= test_bit(IVTV_F_S_STREAMOFF
, &s
->s_flags
);
716 /* Start a capture if there is none */
717 if (!eof
&& !test_bit(IVTV_F_S_STREAMING
, &s
->s_flags
)) {
720 mutex_lock(&itv
->serialize_lock
);
721 rc
= ivtv_start_capture(id
);
722 mutex_unlock(&itv
->serialize_lock
);
724 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
728 IVTV_DEBUG_FILE("Encoder poll started capture\n");
731 /* add stream's waitq to the poll list */
732 IVTV_DEBUG_HI_FILE("Encoder poll\n");
733 poll_wait(filp
, &s
->waitq
, wait
);
735 if (eof
|| s
->q_full
.length
)
736 return POLLIN
| POLLRDNORM
;
740 void ivtv_stop_capture(struct ivtv_open_id
*id
, int gop_end
)
742 struct ivtv
*itv
= id
->itv
;
743 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
745 IVTV_DEBUG_FILE("close() of %s\n", s
->name
);
747 /* 'Unclaim' this stream */
750 if (test_bit(IVTV_F_S_STREAMING
, &s
->s_flags
)) {
751 struct ivtv_stream
*s_vbi
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_VBI
];
753 IVTV_DEBUG_INFO("close stopping capture\n");
754 /* Special case: a running VBI capture for VBI insertion
755 in the mpeg stream. Need to stop that too. */
756 if (id
->type
== IVTV_ENC_STREAM_TYPE_MPG
&&
757 test_bit(IVTV_F_S_STREAMING
, &s_vbi
->s_flags
) &&
758 !test_bit(IVTV_F_S_APPL_IO
, &s_vbi
->s_flags
)) {
759 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
760 ivtv_stop_v4l2_encode_stream(s_vbi
, 0);
762 if ((id
->type
== IVTV_DEC_STREAM_TYPE_VBI
||
763 id
->type
== IVTV_ENC_STREAM_TYPE_VBI
) &&
764 test_bit(IVTV_F_S_INTERNAL_USE
, &s
->s_flags
)) {
765 /* Also used internally, don't stop capturing */
769 ivtv_stop_v4l2_encode_stream(s
, gop_end
);
773 clear_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
774 clear_bit(IVTV_F_S_STREAMOFF
, &s
->s_flags
);
775 ivtv_release_stream(s
);
779 static void ivtv_stop_decoding(struct ivtv_open_id
*id
, int flags
, u64 pts
)
781 struct ivtv
*itv
= id
->itv
;
782 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
784 IVTV_DEBUG_FILE("close() of %s\n", s
->name
);
787 if (test_bit(IVTV_F_S_STREAMING
, &s
->s_flags
)) {
788 IVTV_DEBUG_INFO("close stopping decode\n");
790 ivtv_stop_v4l2_decode_stream(s
, flags
, pts
);
791 itv
->output_mode
= OUT_NONE
;
793 clear_bit(IVTV_F_S_APPL_IO
, &s
->s_flags
);
794 clear_bit(IVTV_F_S_STREAMOFF
, &s
->s_flags
);
795 if (id
->type
== IVTV_DEC_STREAM_TYPE_YUV
&& test_bit(IVTV_F_I_DECODING_YUV
, &itv
->i_flags
)) {
796 /* Restore registers we've changed & clean up any mess we've made */
799 if (itv
->output_mode
== OUT_UDMA_YUV
&& id
->yuv_frames
)
800 itv
->output_mode
= OUT_NONE
;
803 clear_bit(IVTV_F_I_DEC_PAUSED
, &itv
->i_flags
);
804 ivtv_release_stream(s
);
807 int ivtv_v4l2_close(struct inode
*inode
, struct file
*filp
)
809 struct ivtv_open_id
*id
= filp
->private_data
;
810 struct ivtv
*itv
= id
->itv
;
811 struct ivtv_stream
*s
= &itv
->streams
[id
->type
];
813 IVTV_DEBUG_FILE("close %s\n", s
->name
);
815 v4l2_prio_close(&itv
->prio
, &id
->prio
);
817 /* Easy case first: this stream was never claimed by us */
818 if (s
->id
!= id
->open_id
) {
823 /* 'Unclaim' this stream */
826 mutex_lock(&itv
->serialize_lock
);
827 if (id
->type
== IVTV_ENC_STREAM_TYPE_RAD
) {
828 /* Closing radio device, return to TV mode */
830 /* Mark that the radio is no longer in use */
831 clear_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
);
832 /* Switch tuner to TV */
833 ivtv_call_i2c_clients(itv
, VIDIOC_S_STD
, &itv
->std
);
834 /* Select correct audio input (i.e. TV tuner or Line in) */
835 ivtv_audio_set_io(itv
);
836 if (itv
->hw_flags
& IVTV_HW_SAA711X
)
838 struct v4l2_crystal_freq crystal_freq
;
839 crystal_freq
.freq
= SAA7115_FREQ_32_11_MHZ
;
840 crystal_freq
.flags
= 0;
841 ivtv_saa7115(itv
, VIDIOC_INT_S_CRYSTAL_FREQ
, &crystal_freq
);
843 if (atomic_read(&itv
->capturing
) > 0) {
844 /* Undo video mute */
845 ivtv_vapi(itv
, CX2341X_ENC_MUTE_VIDEO
, 1,
846 itv
->params
.video_mute
| (itv
->params
.video_mute_yuv
<< 8));
848 /* Done! Unmute and continue. */
850 ivtv_release_stream(s
);
851 } else if (s
->type
>= IVTV_DEC_STREAM_TYPE_MPG
) {
852 struct ivtv_stream
*s_vout
= &itv
->streams
[IVTV_DEC_STREAM_TYPE_VOUT
];
854 ivtv_stop_decoding(id
, VIDEO_CMD_STOP_TO_BLACK
| VIDEO_CMD_STOP_IMMEDIATELY
, 0);
856 /* If all output streams are closed, and if the user doesn't have
857 IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
858 if (itv
->output_mode
== OUT_NONE
&& !test_bit(IVTV_F_S_APPL_IO
, &s_vout
->s_flags
)) {
859 /* disable CC on TV-out */
860 ivtv_disable_cc(itv
);
863 ivtv_stop_capture(id
, 0);
866 mutex_unlock(&itv
->serialize_lock
);
870 static int ivtv_serialized_open(struct ivtv_stream
*s
, struct file
*filp
)
872 struct ivtv
*itv
= s
->itv
;
873 struct ivtv_open_id
*item
;
875 IVTV_DEBUG_FILE("open %s\n", s
->name
);
877 if (s
->type
== IVTV_DEC_STREAM_TYPE_MPG
&&
878 test_bit(IVTV_F_S_CLAIMED
, &itv
->streams
[IVTV_DEC_STREAM_TYPE_YUV
].s_flags
))
881 if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
&&
882 test_bit(IVTV_F_S_CLAIMED
, &itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
].s_flags
))
885 if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
) {
886 if (read_reg(0x82c) == 0) {
887 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
888 /* return -ENODEV; */
890 ivtv_udma_alloc(itv
);
893 /* Allocate memory */
894 item
= kmalloc(sizeof(struct ivtv_open_id
), GFP_KERNEL
);
896 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
900 item
->type
= s
->type
;
901 v4l2_prio_open(&itv
->prio
, &item
->prio
);
903 item
->open_id
= itv
->open_id
++;
904 filp
->private_data
= item
;
906 if (item
->type
== IVTV_ENC_STREAM_TYPE_RAD
) {
907 /* Try to claim this stream */
908 if (ivtv_claim_stream(item
, item
->type
)) {
909 /* No, it's already in use */
914 if (!test_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
)) {
915 if (atomic_read(&itv
->capturing
) > 0) {
916 /* switching to radio while capture is
917 in progress is not polite */
918 ivtv_release_stream(s
);
923 /* Mark that the radio is being used. */
924 set_bit(IVTV_F_I_RADIO_USER
, &itv
->i_flags
);
925 /* We have the radio */
927 /* Switch tuner to radio */
928 ivtv_call_i2c_clients(itv
, AUDC_SET_RADIO
, NULL
);
929 /* Select the correct audio input (i.e. radio tuner) */
930 ivtv_audio_set_io(itv
);
931 if (itv
->hw_flags
& IVTV_HW_SAA711X
)
933 struct v4l2_crystal_freq crystal_freq
;
934 crystal_freq
.freq
= SAA7115_FREQ_32_11_MHZ
;
935 crystal_freq
.flags
= SAA7115_FREQ_FL_APLL
;
936 ivtv_saa7115(itv
, VIDIOC_INT_S_CRYSTAL_FREQ
, &crystal_freq
);
938 /* Done! Unmute and continue. */
942 /* YUV or MPG Decoding Mode? */
943 if (s
->type
== IVTV_DEC_STREAM_TYPE_MPG
) {
944 clear_bit(IVTV_F_I_DEC_YUV
, &itv
->i_flags
);
945 } else if (s
->type
== IVTV_DEC_STREAM_TYPE_YUV
) {
946 set_bit(IVTV_F_I_DEC_YUV
, &itv
->i_flags
);
947 /* For yuv, we need to know the dma size before we start */
948 itv
->dma_data_req_size
=
949 itv
->params
.width
* itv
->params
.height
* 3 / 2;
950 itv
->yuv_info
.stream_size
= 0;
955 int ivtv_v4l2_open(struct inode
*inode
, struct file
*filp
)
958 struct ivtv
*itv
= NULL
;
959 struct ivtv_stream
*s
= NULL
;
960 int minor
= iminor(inode
);
962 /* Find which card this open was on */
963 spin_lock(&ivtv_cards_lock
);
964 for (x
= 0; itv
== NULL
&& x
< ivtv_cards_active
; x
++) {
965 /* find out which stream this open was on */
966 for (y
= 0; y
< IVTV_MAX_STREAMS
; y
++) {
967 s
= &ivtv_cards
[x
]->streams
[y
];
968 if (s
->v4l2dev
&& s
->v4l2dev
->minor
== minor
) {
974 spin_unlock(&ivtv_cards_lock
);
977 /* Couldn't find a device registered
978 on that minor, shouldn't happen! */
979 printk(KERN_WARNING
"No ivtv device found on minor %d\n", minor
);
983 mutex_lock(&itv
->serialize_lock
);
984 if (ivtv_init_on_first_open(itv
)) {
985 IVTV_ERR("Failed to initialize on minor %d\n", minor
);
986 mutex_unlock(&itv
->serialize_lock
);
989 res
= ivtv_serialized_open(s
, filp
);
990 mutex_unlock(&itv
->serialize_lock
);
994 void ivtv_mute(struct ivtv
*itv
)
996 if (atomic_read(&itv
->capturing
))
997 ivtv_vapi(itv
, CX2341X_ENC_MUTE_AUDIO
, 1, 1);
998 IVTV_DEBUG_INFO("Mute\n");
1001 void ivtv_unmute(struct ivtv
*itv
)
1003 if (atomic_read(&itv
->capturing
)) {
1004 ivtv_msleep_timeout(100, 0);
1005 ivtv_vapi(itv
, CX2341X_ENC_MISC
, 1, 12);
1006 ivtv_vapi(itv
, CX2341X_ENC_MUTE_AUDIO
, 1, 0);
1008 IVTV_DEBUG_INFO("Unmute\n");