/* This function tries to claim the stream for a specific file descriptor. If no one else is using this stream then the stream is claimed and associated VBI streams are also automatically claimed. Possible error returns: -EBUSY if someone else has claimed
the stream or 0 on success. */ int ivtv_claim_stream(struct ivtv_open_id *id, int type)
{ struct ivtv *itv = id->itv; struct ivtv_stream *s = &itv->streams[type]; struct ivtv_stream *s_vbi; int vbi_type;
if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) { /* someone already claimed this stream */ if (s->id == id) { /* yes, this file descriptor did. So that's OK. */ return 0;
} if (s->id == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
type == IVTV_ENC_STREAM_TYPE_VBI)) { /* VBI is handled already internally, now also assign the file descriptor to this stream for external
reading of the stream. */
s->id = id;
IVTV_DEBUG_INFO("Start Read VBI\n"); return 0;
} /* someone else is using this stream already */
IVTV_DEBUG_INFO("Stream %d is busy\n", type); return -EBUSY;
}
s->id = id; if (type == IVTV_DEC_STREAM_TYPE_VBI) { /* Enable reinsertion interrupt */
ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
}
/* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI, IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI (provided VBI insertion is on and sliced VBI is selected), for all
other streams we're done */ if (type == IVTV_DEC_STREAM_TYPE_MPG) {
vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
} elseif (type == IVTV_ENC_STREAM_TYPE_MPG &&
itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
} else { return 0;
}
s_vbi = &itv->streams[vbi_type];
if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) { /* Enable reinsertion interrupt */ if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
} /* mark that it is used internally */
set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags); return 0;
}
EXPORT_SYMBOL(ivtv_claim_stream);
/* This function releases a previously claimed stream. It will take into
account associated VBI streams. */ void ivtv_release_stream(struct ivtv_stream *s)
{ struct ivtv *itv = s->itv; struct ivtv_stream *s_vbi;
s->id = NULL; if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { /* this stream is still in use internally */ return;
} if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name); return;
}
if (new_stereo_mode == itv->dualwatch_stereo_mode) return;
IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
itv->dualwatch_stereo_mode, new_stereo_mode); if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
}
staticvoid ivtv_update_pgm_info(struct ivtv *itv)
{
u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24; int cnt; int i = 0;
mutex_unlock(&itv->serialize_lock);
prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); /* New buffers might have become free before we were added to the waitqueue */ if (!s->q_free.buffers)
schedule();
finish_wait(&s->waitq, &wait);
mutex_lock(&itv->serialize_lock);
}
*err = 0; while (1) { if (s->type == IVTV_ENC_STREAM_TYPE_MPG) { /* Process pending program info updates and pending VBI data */
ivtv_update_pgm_info(itv);
if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
!test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) { /* byteswap and process VBI data */
ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
}
}
buf = &itv->vbi.sliced_mpeg_buf; if (buf->readpos != buf->bytesused) { return buf;
}
}
/* do we have leftover data? */
buf = ivtv_dequeue(s, &s->q_io); if (buf) return buf;
/* do we have new data? */
buf = ivtv_dequeue(s, &s->q_full); if (buf) { if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0) return buf;
buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP; if (s->type == IVTV_ENC_STREAM_TYPE_MPG) /* byteswap MPG data */
ivtv_buf_swap(buf); elseif (s->type != IVTV_DEC_STREAM_TYPE_VBI) { /* byteswap and process VBI data */
ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
} return buf;
}
/* return if end of stream */ if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
IVTV_DEBUG_INFO("EOS %s\n", s->name); return NULL;
}
/* return if file was opened with O_NONBLOCK */ if (non_block) {
*err = -EAGAIN; return NULL;
}
/* wait for more data to arrive */
ivtv_schedule(s); if (signal_pending(current)) { /* return if a signal was received */
IVTV_DEBUG_INFO("User stopped %s\n", s->name);
*err = -EINTR; return NULL;
}
}
}
if (atomic_read(&itv->capturing) == 0 && s->id == NULL) { /* shouldn't happen */
IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name); return -EIO;
}
/* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
arrive one-by-one, so make sure we never output more than one VBI frame at a time */ if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
(s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
single_frame = 1;
for (;;) { struct ivtv_buffer *buf; int rc;
buf = ivtv_get_buffer(s, non_block, &rc); /* if there is no data available... */ if (buf == NULL) { /* if we got data, then return that regardless */ if (tot_written) break; /* EOS condition */ if (rc == 0) {
clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
ivtv_release_stream(s);
} /* set errno */ return rc;
}
rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written); if (buf != &itv->vbi.sliced_mpeg_buf) {
ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
} elseif (buf->readpos == buf->bytesused) { int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
itv->vbi.sliced_mpeg_size[idx] = 0;
itv->vbi.inserted_frame++;
itv->vbi_data_inserted += buf->bytesused;
} if (rc < 0) return rc;
tot_written += rc;
if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
s->type == IVTV_DEC_STREAM_TYPE_MPG ||
s->type == IVTV_DEC_STREAM_TYPE_YUV ||
s->type == IVTV_DEC_STREAM_TYPE_VOUT) { /* you cannot read from these stream types. */ return -EINVAL;
}
/* Try to claim this stream. */ if (ivtv_claim_stream(id, s->type)) return -EBUSY;
/* This stream does not need to start capturing */ if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
set_bit(IVTV_F_S_APPL_IO, &s->s_flags); return 0;
}
/* If capture is already in progress, then we also have to
do nothing extra. */ if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
set_bit(IVTV_F_S_APPL_IO, &s->s_flags); return 0;
}
/* Start VBI capture if required */
s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
!test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed automatically when the MPG stream is claimed.
We only need to start the VBI capturing. */ if (ivtv_start_v4l2_encode_stream(s_vbi)) {
IVTV_DEBUG_WARN("VBI capture start failed\n");
/* Failure, clean up and return an error */
clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
clear_bit(IVTV_F_S_STREAMING, &s->s_flags); /* also releases the associated VBI stream */
ivtv_release_stream(s); return -EIO;
}
IVTV_DEBUG_INFO("VBI insertion started\n");
}
/* Tell the card to start capturing */ if (!ivtv_start_v4l2_encode_stream(s)) { /* We're done */
set_bit(IVTV_F_S_APPL_IO, &s->s_flags); /* Resume a possibly paused encoder */ if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1); return 0;
}
/* failure, clean up */
IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
/* Note: the IVTV_ENC_STREAM_TYPE_VBI is released automatically when the MPG stream is released.
We only need to stop the VBI capturing. */ if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
ivtv_stop_v4l2_encode_stream(s_vbi, 0);
clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
}
clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
ivtv_release_stream(s); return -EIO;
}
IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
s->type != IVTV_DEC_STREAM_TYPE_YUV &&
s->type != IVTV_DEC_STREAM_TYPE_VOUT) /* not decoder streams */ return -EINVAL;
/* Try to claim this stream */ if (ivtv_claim_stream(id, s->type)) return -EBUSY;
/* This stream does not need to start any decoding */ if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) { int elems = count / sizeof(struct v4l2_sliced_vbi_data);
retry: /* If possible, just DMA the entire frame - Check the data transfer size
since we may get here before the stream has been fully set-up */ if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) { while (count >= itv->dma_data_req_size) {
rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
for (;;) { /* Gather buffers */ while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
ivtv_enqueue(s, buf, &q); while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
ivtv_enqueue(s, buf, &q);
} if (q.buffers) break; if (filp->f_flags & O_NONBLOCK) return -EAGAIN;
ivtv_schedule(s); if (signal_pending(current)) {
IVTV_DEBUG_INFO("User stopped %s\n", s->name); return -EINTR;
}
}
/* copy user data into buffers */ while ((buf = ivtv_dequeue(s, &q))) { /* yuv is a pain. Don't copy more data than needed for a single
frame, otherwise we lose sync with the incoming stream */ if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
yi->stream_size + count > itv->dma_data_req_size)
rc = ivtv_buf_copy_from_user(s, buf, user_buf,
itv->dma_data_req_size - yi->stream_size); else
rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
/* Make sure we really got all the user data */ if (rc < 0) {
ivtv_queue_move(s, &q, NULL, &s->q_free, 0); return rc;
}
user_buf += rc;
count -= rc;
bytes_written += rc;
if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
yi->stream_size += rc; /* If we have a complete yuv frame, break loop now */ if (yi->stream_size == itv->dma_data_req_size) {
ivtv_enqueue(s, buf, &s->q_full);
yi->stream_size = 0; break;
}
}
if (buf->bytesused != s->buf_size) { /* incomplete, leave in q_io for next time */
ivtv_enqueue(s, buf, &s->q_io); break;
} /* Byteswap MPEG buffer */ if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
ivtv_buf_swap(buf);
ivtv_enqueue(s, buf, &s->q_full);
}
if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) { if (s->q_full.length >= itv->dma_data_req_size) { if (mode == OUT_YUV)
ivtv_yuv_setup_stream_frame(itv);
if (ivtv_schedule_dma(s)) {
IVTV_DEBUG_INFO("User interrupted %s\n", s->name); return -EINTR;
}
clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
}
} /* more user data is available, wait until buffers become free
to transfer the rest. */ if (count && !(filp->f_flags & O_NONBLOCK)) goto retry;
IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused); return bytes_written;
}
/* add stream's waitq to the poll list */
IVTV_DEBUG_HI_FILE("Decoder poll\n");
/* If there are subscribed events, then only use the new event
API instead of the old video.h based API. */ if (!list_empty(&id->fh.subscribed)) {
poll_wait(filp, &id->fh.wait, wait); /* Turn off the old-style vsync events */
clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); if (v4l2_event_pending(&id->fh))
res = EPOLLPRI;
} else { /* This is the old-style API which is here only for backwards
compatibility. */
poll_wait(filp, &s->waitq, wait);
set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
res = EPOLLPRI;
}
/* Allow write if buffers are available for writing */ if (s->q_free.buffers)
res |= EPOLLOUT | EPOLLWRNORM; return res;
}
/* Start a capture if there is none */ if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags) &&
s->type != IVTV_ENC_STREAM_TYPE_RAD &&
(req_events & (EPOLLIN | EPOLLRDNORM))) { int rc;
mutex_lock(&itv->serialize_lock);
rc = ivtv_start_capture(id);
mutex_unlock(&itv->serialize_lock); if (rc) {
IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
s->name, rc); return EPOLLERR;
}
IVTV_DEBUG_FILE("Encoder poll started capture\n");
}
/* add stream's waitq to the poll list */
IVTV_DEBUG_HI_FILE("Encoder poll\n");
poll_wait(filp, &s->waitq, wait); if (v4l2_event_pending(&id->fh))
res |= EPOLLPRI; else
poll_wait(filp, &id->fh.wait, wait);
if (s->q_full.length || s->q_io.length) return res | EPOLLIN | EPOLLRDNORM; if (eof) return res | EPOLLHUP; return res;
}
/* Stop radio */ if (id->type == IVTV_ENC_STREAM_TYPE_RAD &&
v4l2_fh_is_singular_file(filp)) { /* Closing radio device, return to TV mode */
ivtv_mute(itv); /* Mark that the radio is no longer in use */
clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); /* Switch tuner to TV */
ivtv_call_all(itv, video, s_std, itv->std); /* Select correct audio input (i.e. TV tuner or Line in) */
ivtv_audio_set_io(itv); if (itv->hw_flags & IVTV_HW_SAA711X) {
ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
SAA7115_FREQ_32_11_MHZ, 0);
} if (atomic_read(&itv->capturing) > 0) { /* Undo video mute */
ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
(v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
} /* Done! Unmute and continue. */
ivtv_unmute(itv);
}
v4l2_fh_del(fh);
v4l2_fh_exit(fh);
/* Easy case first: this stream was never claimed by us */ if (s->id != id) goto close_done;
/* 'Unclaim' this stream */
if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) { struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
/* If all output streams are closed, and if the user doesn't have
IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */ if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) { /* disable CC on TV-out */
ivtv_disable_cc(itv);
}
} else {
ivtv_stop_capture(id, 0);
}
close_done:
kfree(id);
mutex_unlock(&itv->serialize_lock); return 0;
}
if (ivtv_init_on_first_open(itv)) {
IVTV_ERR("Failed to initialize on device %s\n",
video_device_node_name(vdev)); return -ENXIO;
}
#ifdef CONFIG_VIDEO_ADV_DEBUG /* Unless ivtv_fw_debug is set, error out if firmware dead. */ if (ivtv_fw_debug) {
IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
video_device_node_name(vdev));
IVTV_WARN("Selected firmware errors will be ignored\n");
} else { #else if (1) { #endif
res = ivtv_firmware_check(itv, "ivtv_serialized_open"); if (res == -EAGAIN)
res = ivtv_firmware_check(itv, "ivtv_serialized_open"); if (res < 0) return -EIO;
}
if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags)) return -EBUSY;
if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags)) return -EBUSY;
if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { if (read_reg(0x82c) == 0) {
IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n"); /* return -ENODEV; */
}
ivtv_udma_alloc(itv);
}
if (item->type == IVTV_ENC_STREAM_TYPE_RAD &&
v4l2_fh_is_singular_file(filp)) { if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) { if (atomic_read(&itv->capturing) > 0) { /* switching to radio while capture is
in progress is not polite */
v4l2_fh_del(&item->fh);
v4l2_fh_exit(&item->fh);
kfree(item); return -EBUSY;
}
} /* Mark that the radio is being used. */
set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); /* We have the radio */
ivtv_mute(itv); /* Switch tuner to radio */
ivtv_call_all(itv, tuner, s_radio); /* Select the correct audio input (i.e. radio tuner) */
ivtv_audio_set_io(itv); if (itv->hw_flags & IVTV_HW_SAA711X) {
ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
} /* Done! Unmute and continue. */
ivtv_unmute(itv);
}
/* YUV or MPG Decoding Mode? */ if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
} elseif (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); /* For yuv, we need to know the dma size before we start */
itv->dma_data_req_size =
1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
itv->yuv_info.stream_size = 0;
} return 0;
}
int ivtv_v4l2_open(struct file *filp)
{ struct video_device *vdev = video_devdata(filp); int res;
if (mutex_lock_interruptible(vdev->lock)) return -ERESTARTSYS;
res = ivtv_open(filp);
mutex_unlock(vdev->lock); return res;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.