162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci file operation functions 462306a36Sopenharmony_ci Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com> 562306a36Sopenharmony_ci Copyright (C) 2004 Chris Kennedy <c@groovy.org> 662306a36Sopenharmony_ci Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl> 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include "ivtv-driver.h" 1162306a36Sopenharmony_ci#include "ivtv-fileops.h" 1262306a36Sopenharmony_ci#include "ivtv-i2c.h" 1362306a36Sopenharmony_ci#include "ivtv-queue.h" 1462306a36Sopenharmony_ci#include "ivtv-udma.h" 1562306a36Sopenharmony_ci#include "ivtv-irq.h" 1662306a36Sopenharmony_ci#include "ivtv-vbi.h" 1762306a36Sopenharmony_ci#include "ivtv-mailbox.h" 1862306a36Sopenharmony_ci#include "ivtv-routing.h" 1962306a36Sopenharmony_ci#include "ivtv-streams.h" 2062306a36Sopenharmony_ci#include "ivtv-yuv.h" 2162306a36Sopenharmony_ci#include "ivtv-ioctl.h" 2262306a36Sopenharmony_ci#include "ivtv-cards.h" 2362306a36Sopenharmony_ci#include "ivtv-firmware.h" 2462306a36Sopenharmony_ci#include <media/v4l2-event.h> 2562306a36Sopenharmony_ci#include <media/i2c/saa7115.h> 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci/* This function tries to claim the stream for a specific file descriptor. 2862306a36Sopenharmony_ci If no one else is using this stream then the stream is claimed and 2962306a36Sopenharmony_ci associated VBI streams are also automatically claimed. 3062306a36Sopenharmony_ci Possible error returns: -EBUSY if someone else has claimed 3162306a36Sopenharmony_ci the stream or 0 on success. */ 3262306a36Sopenharmony_ciint ivtv_claim_stream(struct ivtv_open_id *id, int type) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci struct ivtv *itv = id->itv; 3562306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[type]; 3662306a36Sopenharmony_ci struct ivtv_stream *s_vbi; 3762306a36Sopenharmony_ci int vbi_type; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) { 4062306a36Sopenharmony_ci /* someone already claimed this stream */ 4162306a36Sopenharmony_ci if (s->fh == &id->fh) { 4262306a36Sopenharmony_ci /* yes, this file descriptor did. So that's OK. */ 4362306a36Sopenharmony_ci return 0; 4462306a36Sopenharmony_ci } 4562306a36Sopenharmony_ci if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI || 4662306a36Sopenharmony_ci type == IVTV_ENC_STREAM_TYPE_VBI)) { 4762306a36Sopenharmony_ci /* VBI is handled already internally, now also assign 4862306a36Sopenharmony_ci the file descriptor to this stream for external 4962306a36Sopenharmony_ci reading of the stream. */ 5062306a36Sopenharmony_ci s->fh = &id->fh; 5162306a36Sopenharmony_ci IVTV_DEBUG_INFO("Start Read VBI\n"); 5262306a36Sopenharmony_ci return 0; 5362306a36Sopenharmony_ci } 5462306a36Sopenharmony_ci /* someone else is using this stream already */ 5562306a36Sopenharmony_ci IVTV_DEBUG_INFO("Stream %d is busy\n", type); 5662306a36Sopenharmony_ci return -EBUSY; 5762306a36Sopenharmony_ci } 5862306a36Sopenharmony_ci s->fh = &id->fh; 5962306a36Sopenharmony_ci if (type == IVTV_DEC_STREAM_TYPE_VBI) { 6062306a36Sopenharmony_ci /* Enable reinsertion interrupt */ 6162306a36Sopenharmony_ci ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI, 6562306a36Sopenharmony_ci IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI 6662306a36Sopenharmony_ci (provided VBI insertion is on and sliced VBI is selected), for all 6762306a36Sopenharmony_ci other streams we're done */ 6862306a36Sopenharmony_ci if (type == IVTV_DEC_STREAM_TYPE_MPG) { 6962306a36Sopenharmony_ci vbi_type = IVTV_DEC_STREAM_TYPE_VBI; 7062306a36Sopenharmony_ci } else if (type == IVTV_ENC_STREAM_TYPE_MPG && 7162306a36Sopenharmony_ci itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) { 7262306a36Sopenharmony_ci vbi_type = IVTV_ENC_STREAM_TYPE_VBI; 7362306a36Sopenharmony_ci } else { 7462306a36Sopenharmony_ci return 0; 7562306a36Sopenharmony_ci } 7662306a36Sopenharmony_ci s_vbi = &itv->streams[vbi_type]; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) { 7962306a36Sopenharmony_ci /* Enable reinsertion interrupt */ 8062306a36Sopenharmony_ci if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI) 8162306a36Sopenharmony_ci ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); 8262306a36Sopenharmony_ci } 8362306a36Sopenharmony_ci /* mark that it is used internally */ 8462306a36Sopenharmony_ci set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags); 8562306a36Sopenharmony_ci return 0; 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_claim_stream); 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* This function releases a previously claimed stream. It will take into 9062306a36Sopenharmony_ci account associated VBI streams. */ 9162306a36Sopenharmony_civoid ivtv_release_stream(struct ivtv_stream *s) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci struct ivtv *itv = s->itv; 9462306a36Sopenharmony_ci struct ivtv_stream *s_vbi; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci s->fh = NULL; 9762306a36Sopenharmony_ci if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) && 9862306a36Sopenharmony_ci test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { 9962306a36Sopenharmony_ci /* this stream is still in use internally */ 10062306a36Sopenharmony_ci return; 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) { 10362306a36Sopenharmony_ci IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name); 10462306a36Sopenharmony_ci return; 10562306a36Sopenharmony_ci } 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci ivtv_flush_queues(s); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci /* disable reinsertion interrupt */ 11062306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_VBI) 11162306a36Sopenharmony_ci ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI, 11462306a36Sopenharmony_ci IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI, 11562306a36Sopenharmony_ci for all other streams we're done */ 11662306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_MPG) 11762306a36Sopenharmony_ci s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI]; 11862306a36Sopenharmony_ci else if (s->type == IVTV_ENC_STREAM_TYPE_MPG) 11962306a36Sopenharmony_ci s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; 12062306a36Sopenharmony_ci else 12162306a36Sopenharmony_ci return; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci /* clear internal use flag */ 12462306a36Sopenharmony_ci if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) { 12562306a36Sopenharmony_ci /* was already cleared */ 12662306a36Sopenharmony_ci return; 12762306a36Sopenharmony_ci } 12862306a36Sopenharmony_ci if (s_vbi->fh) { 12962306a36Sopenharmony_ci /* VBI stream still claimed by a file descriptor */ 13062306a36Sopenharmony_ci return; 13162306a36Sopenharmony_ci } 13262306a36Sopenharmony_ci /* disable reinsertion interrupt */ 13362306a36Sopenharmony_ci if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI) 13462306a36Sopenharmony_ci ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT); 13562306a36Sopenharmony_ci clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags); 13662306a36Sopenharmony_ci ivtv_flush_queues(s_vbi); 13762306a36Sopenharmony_ci} 13862306a36Sopenharmony_ciEXPORT_SYMBOL(ivtv_release_stream); 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_cistatic void ivtv_dualwatch(struct ivtv *itv) 14162306a36Sopenharmony_ci{ 14262306a36Sopenharmony_ci struct v4l2_tuner vt; 14362306a36Sopenharmony_ci u32 new_stereo_mode; 14462306a36Sopenharmony_ci const u32 dual = 0x02; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode); 14762306a36Sopenharmony_ci memset(&vt, 0, sizeof(vt)); 14862306a36Sopenharmony_ci ivtv_call_all(itv, tuner, g_tuner, &vt); 14962306a36Sopenharmony_ci if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2)) 15062306a36Sopenharmony_ci new_stereo_mode = dual; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci if (new_stereo_mode == itv->dualwatch_stereo_mode) 15362306a36Sopenharmony_ci return; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n", 15662306a36Sopenharmony_ci itv->dualwatch_stereo_mode, new_stereo_mode); 15762306a36Sopenharmony_ci if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode)) 15862306a36Sopenharmony_ci IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n"); 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_cistatic void ivtv_update_pgm_info(struct ivtv *itv) 16262306a36Sopenharmony_ci{ 16362306a36Sopenharmony_ci u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24; 16462306a36Sopenharmony_ci int cnt; 16562306a36Sopenharmony_ci int i = 0; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci if (wr_idx >= itv->pgm_info_num) { 16862306a36Sopenharmony_ci IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num); 16962306a36Sopenharmony_ci return; 17062306a36Sopenharmony_ci } 17162306a36Sopenharmony_ci cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num; 17262306a36Sopenharmony_ci while (i < cnt) { 17362306a36Sopenharmony_ci int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num; 17462306a36Sopenharmony_ci struct v4l2_enc_idx_entry *e = itv->pgm_info + idx; 17562306a36Sopenharmony_ci u32 addr = itv->pgm_info_offset + 4 + idx * 24; 17662306a36Sopenharmony_ci const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1, 17762306a36Sopenharmony_ci V4L2_ENC_IDX_FRAME_B, -1, -1, -1 }; 17862306a36Sopenharmony_ci // 1=I, 2=P, 4=B 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32); 18162306a36Sopenharmony_ci if (e->offset > itv->mpg_data_received) { 18262306a36Sopenharmony_ci break; 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci e->offset += itv->vbi_data_inserted; 18562306a36Sopenharmony_ci e->length = read_enc(addr); 18662306a36Sopenharmony_ci e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32); 18762306a36Sopenharmony_ci e->flags = mapping[read_enc(addr + 12) & 7]; 18862306a36Sopenharmony_ci i++; 18962306a36Sopenharmony_ci } 19062306a36Sopenharmony_ci itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num; 19162306a36Sopenharmony_ci} 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_cistatic struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err) 19462306a36Sopenharmony_ci{ 19562306a36Sopenharmony_ci struct ivtv *itv = s->itv; 19662306a36Sopenharmony_ci struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; 19762306a36Sopenharmony_ci struct ivtv_buffer *buf; 19862306a36Sopenharmony_ci DEFINE_WAIT(wait); 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci *err = 0; 20162306a36Sopenharmony_ci while (1) { 20262306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_MPG) { 20362306a36Sopenharmony_ci /* Process pending program info updates and pending VBI data */ 20462306a36Sopenharmony_ci ivtv_update_pgm_info(itv); 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci if (time_after(jiffies, 20762306a36Sopenharmony_ci itv->dualwatch_jiffies + 20862306a36Sopenharmony_ci msecs_to_jiffies(1000))) { 20962306a36Sopenharmony_ci itv->dualwatch_jiffies = jiffies; 21062306a36Sopenharmony_ci ivtv_dualwatch(itv); 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && 21462306a36Sopenharmony_ci !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { 21562306a36Sopenharmony_ci while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) { 21662306a36Sopenharmony_ci /* byteswap and process VBI data */ 21762306a36Sopenharmony_ci ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type); 21862306a36Sopenharmony_ci ivtv_enqueue(s_vbi, buf, &s_vbi->q_free); 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci } 22162306a36Sopenharmony_ci buf = &itv->vbi.sliced_mpeg_buf; 22262306a36Sopenharmony_ci if (buf->readpos != buf->bytesused) { 22362306a36Sopenharmony_ci return buf; 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci } 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci /* do we have leftover data? */ 22862306a36Sopenharmony_ci buf = ivtv_dequeue(s, &s->q_io); 22962306a36Sopenharmony_ci if (buf) 23062306a36Sopenharmony_ci return buf; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* do we have new data? */ 23362306a36Sopenharmony_ci buf = ivtv_dequeue(s, &s->q_full); 23462306a36Sopenharmony_ci if (buf) { 23562306a36Sopenharmony_ci if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0) 23662306a36Sopenharmony_ci return buf; 23762306a36Sopenharmony_ci buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP; 23862306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_MPG) 23962306a36Sopenharmony_ci /* byteswap MPG data */ 24062306a36Sopenharmony_ci ivtv_buf_swap(buf); 24162306a36Sopenharmony_ci else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) { 24262306a36Sopenharmony_ci /* byteswap and process VBI data */ 24362306a36Sopenharmony_ci ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type); 24462306a36Sopenharmony_ci } 24562306a36Sopenharmony_ci return buf; 24662306a36Sopenharmony_ci } 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci /* return if end of stream */ 24962306a36Sopenharmony_ci if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { 25062306a36Sopenharmony_ci IVTV_DEBUG_INFO("EOS %s\n", s->name); 25162306a36Sopenharmony_ci return NULL; 25262306a36Sopenharmony_ci } 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci /* return if file was opened with O_NONBLOCK */ 25562306a36Sopenharmony_ci if (non_block) { 25662306a36Sopenharmony_ci *err = -EAGAIN; 25762306a36Sopenharmony_ci return NULL; 25862306a36Sopenharmony_ci } 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci /* wait for more data to arrive */ 26162306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 26262306a36Sopenharmony_ci prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); 26362306a36Sopenharmony_ci /* New buffers might have become available before we were added to the waitqueue */ 26462306a36Sopenharmony_ci if (!s->q_full.buffers) 26562306a36Sopenharmony_ci schedule(); 26662306a36Sopenharmony_ci finish_wait(&s->waitq, &wait); 26762306a36Sopenharmony_ci mutex_lock(&itv->serialize_lock); 26862306a36Sopenharmony_ci if (signal_pending(current)) { 26962306a36Sopenharmony_ci /* return if a signal was received */ 27062306a36Sopenharmony_ci IVTV_DEBUG_INFO("User stopped %s\n", s->name); 27162306a36Sopenharmony_ci *err = -EINTR; 27262306a36Sopenharmony_ci return NULL; 27362306a36Sopenharmony_ci } 27462306a36Sopenharmony_ci } 27562306a36Sopenharmony_ci} 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_cistatic void ivtv_setup_sliced_vbi_buf(struct ivtv *itv) 27862306a36Sopenharmony_ci{ 27962306a36Sopenharmony_ci int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx]; 28262306a36Sopenharmony_ci itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx]; 28362306a36Sopenharmony_ci itv->vbi.sliced_mpeg_buf.readpos = 0; 28462306a36Sopenharmony_ci} 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_cistatic size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf, 28762306a36Sopenharmony_ci char __user *ubuf, size_t ucount) 28862306a36Sopenharmony_ci{ 28962306a36Sopenharmony_ci struct ivtv *itv = s->itv; 29062306a36Sopenharmony_ci size_t len = buf->bytesused - buf->readpos; 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci if (len > ucount) len = ucount; 29362306a36Sopenharmony_ci if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG && 29462306a36Sopenharmony_ci !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) { 29562306a36Sopenharmony_ci const char *start = buf->buf + buf->readpos; 29662306a36Sopenharmony_ci const char *p = start + 1; 29762306a36Sopenharmony_ci const u8 *q; 29862306a36Sopenharmony_ci u8 ch = itv->search_pack_header ? 0xba : 0xe0; 29962306a36Sopenharmony_ci int stuffing, i; 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci while (start + len > p && (q = memchr(p, 0, start + len - p))) { 30262306a36Sopenharmony_ci p = q + 1; 30362306a36Sopenharmony_ci if ((char *)q + 15 >= buf->buf + buf->bytesused || 30462306a36Sopenharmony_ci q[1] != 0 || q[2] != 1 || q[3] != ch) { 30562306a36Sopenharmony_ci continue; 30662306a36Sopenharmony_ci } 30762306a36Sopenharmony_ci if (!itv->search_pack_header) { 30862306a36Sopenharmony_ci if ((q[6] & 0xc0) != 0x80) 30962306a36Sopenharmony_ci continue; 31062306a36Sopenharmony_ci if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) || 31162306a36Sopenharmony_ci ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) { 31262306a36Sopenharmony_ci ch = 0xba; 31362306a36Sopenharmony_ci itv->search_pack_header = 1; 31462306a36Sopenharmony_ci p = q + 9; 31562306a36Sopenharmony_ci } 31662306a36Sopenharmony_ci continue; 31762306a36Sopenharmony_ci } 31862306a36Sopenharmony_ci stuffing = q[13] & 7; 31962306a36Sopenharmony_ci /* all stuffing bytes must be 0xff */ 32062306a36Sopenharmony_ci for (i = 0; i < stuffing; i++) 32162306a36Sopenharmony_ci if (q[14 + i] != 0xff) 32262306a36Sopenharmony_ci break; 32362306a36Sopenharmony_ci if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 && 32462306a36Sopenharmony_ci q[14 + stuffing] == 0 && q[15 + stuffing] == 0 && 32562306a36Sopenharmony_ci q[16 + stuffing] == 1) { 32662306a36Sopenharmony_ci itv->search_pack_header = 0; 32762306a36Sopenharmony_ci len = (char *)q - start; 32862306a36Sopenharmony_ci ivtv_setup_sliced_vbi_buf(itv); 32962306a36Sopenharmony_ci break; 33062306a36Sopenharmony_ci } 33162306a36Sopenharmony_ci } 33262306a36Sopenharmony_ci } 33362306a36Sopenharmony_ci if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) { 33462306a36Sopenharmony_ci IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name); 33562306a36Sopenharmony_ci return -EFAULT; 33662306a36Sopenharmony_ci } 33762306a36Sopenharmony_ci /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount, 33862306a36Sopenharmony_ci buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len, 33962306a36Sopenharmony_ci buf == &itv->vbi.sliced_mpeg_buf); */ 34062306a36Sopenharmony_ci buf->readpos += len; 34162306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf) 34262306a36Sopenharmony_ci itv->mpg_data_received += len; 34362306a36Sopenharmony_ci return len; 34462306a36Sopenharmony_ci} 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_cistatic ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block) 34762306a36Sopenharmony_ci{ 34862306a36Sopenharmony_ci struct ivtv *itv = s->itv; 34962306a36Sopenharmony_ci size_t tot_written = 0; 35062306a36Sopenharmony_ci int single_frame = 0; 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) { 35362306a36Sopenharmony_ci /* shouldn't happen */ 35462306a36Sopenharmony_ci IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name); 35562306a36Sopenharmony_ci return -EIO; 35662306a36Sopenharmony_ci } 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should 35962306a36Sopenharmony_ci arrive one-by-one, so make sure we never output more than one VBI frame at a time */ 36062306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_VBI || 36162306a36Sopenharmony_ci (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv))) 36262306a36Sopenharmony_ci single_frame = 1; 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci for (;;) { 36562306a36Sopenharmony_ci struct ivtv_buffer *buf; 36662306a36Sopenharmony_ci int rc; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci buf = ivtv_get_buffer(s, non_block, &rc); 36962306a36Sopenharmony_ci /* if there is no data available... */ 37062306a36Sopenharmony_ci if (buf == NULL) { 37162306a36Sopenharmony_ci /* if we got data, then return that regardless */ 37262306a36Sopenharmony_ci if (tot_written) 37362306a36Sopenharmony_ci break; 37462306a36Sopenharmony_ci /* EOS condition */ 37562306a36Sopenharmony_ci if (rc == 0) { 37662306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 37762306a36Sopenharmony_ci clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); 37862306a36Sopenharmony_ci ivtv_release_stream(s); 37962306a36Sopenharmony_ci } 38062306a36Sopenharmony_ci /* set errno */ 38162306a36Sopenharmony_ci return rc; 38262306a36Sopenharmony_ci } 38362306a36Sopenharmony_ci rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written); 38462306a36Sopenharmony_ci if (buf != &itv->vbi.sliced_mpeg_buf) { 38562306a36Sopenharmony_ci ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io); 38662306a36Sopenharmony_ci } 38762306a36Sopenharmony_ci else if (buf->readpos == buf->bytesused) { 38862306a36Sopenharmony_ci int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES; 38962306a36Sopenharmony_ci itv->vbi.sliced_mpeg_size[idx] = 0; 39062306a36Sopenharmony_ci itv->vbi.inserted_frame++; 39162306a36Sopenharmony_ci itv->vbi_data_inserted += buf->bytesused; 39262306a36Sopenharmony_ci } 39362306a36Sopenharmony_ci if (rc < 0) 39462306a36Sopenharmony_ci return rc; 39562306a36Sopenharmony_ci tot_written += rc; 39662306a36Sopenharmony_ci 39762306a36Sopenharmony_ci if (tot_written == tot_count || single_frame) 39862306a36Sopenharmony_ci break; 39962306a36Sopenharmony_ci } 40062306a36Sopenharmony_ci return tot_written; 40162306a36Sopenharmony_ci} 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_cistatic ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count, 40462306a36Sopenharmony_ci loff_t *pos, int non_block) 40562306a36Sopenharmony_ci{ 40662306a36Sopenharmony_ci ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0; 40762306a36Sopenharmony_ci struct ivtv *itv = s->itv; 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc); 41062306a36Sopenharmony_ci if (rc > 0) 41162306a36Sopenharmony_ci *pos += rc; 41262306a36Sopenharmony_ci return rc; 41362306a36Sopenharmony_ci} 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ciint ivtv_start_capture(struct ivtv_open_id *id) 41662306a36Sopenharmony_ci{ 41762306a36Sopenharmony_ci struct ivtv *itv = id->itv; 41862306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 41962306a36Sopenharmony_ci struct ivtv_stream *s_vbi; 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_RAD || 42262306a36Sopenharmony_ci s->type == IVTV_DEC_STREAM_TYPE_MPG || 42362306a36Sopenharmony_ci s->type == IVTV_DEC_STREAM_TYPE_YUV || 42462306a36Sopenharmony_ci s->type == IVTV_DEC_STREAM_TYPE_VOUT) { 42562306a36Sopenharmony_ci /* you cannot read from these stream types. */ 42662306a36Sopenharmony_ci return -EINVAL; 42762306a36Sopenharmony_ci } 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci /* Try to claim this stream. */ 43062306a36Sopenharmony_ci if (ivtv_claim_stream(id, s->type)) 43162306a36Sopenharmony_ci return -EBUSY; 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci /* This stream does not need to start capturing */ 43462306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_VBI) { 43562306a36Sopenharmony_ci set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 43662306a36Sopenharmony_ci return 0; 43762306a36Sopenharmony_ci } 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci /* If capture is already in progress, then we also have to 44062306a36Sopenharmony_ci do nothing extra. */ 44162306a36Sopenharmony_ci if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) { 44262306a36Sopenharmony_ci set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 44362306a36Sopenharmony_ci return 0; 44462306a36Sopenharmony_ci } 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ci /* Start VBI capture if required */ 44762306a36Sopenharmony_ci s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; 44862306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_MPG && 44962306a36Sopenharmony_ci test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) && 45062306a36Sopenharmony_ci !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { 45162306a36Sopenharmony_ci /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed 45262306a36Sopenharmony_ci automatically when the MPG stream is claimed. 45362306a36Sopenharmony_ci We only need to start the VBI capturing. */ 45462306a36Sopenharmony_ci if (ivtv_start_v4l2_encode_stream(s_vbi)) { 45562306a36Sopenharmony_ci IVTV_DEBUG_WARN("VBI capture start failed\n"); 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci /* Failure, clean up and return an error */ 45862306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); 45962306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 46062306a36Sopenharmony_ci /* also releases the associated VBI stream */ 46162306a36Sopenharmony_ci ivtv_release_stream(s); 46262306a36Sopenharmony_ci return -EIO; 46362306a36Sopenharmony_ci } 46462306a36Sopenharmony_ci IVTV_DEBUG_INFO("VBI insertion started\n"); 46562306a36Sopenharmony_ci } 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ci /* Tell the card to start capturing */ 46862306a36Sopenharmony_ci if (!ivtv_start_v4l2_encode_stream(s)) { 46962306a36Sopenharmony_ci /* We're done */ 47062306a36Sopenharmony_ci set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 47162306a36Sopenharmony_ci /* Resume a possibly paused encoder */ 47262306a36Sopenharmony_ci if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags)) 47362306a36Sopenharmony_ci ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1); 47462306a36Sopenharmony_ci return 0; 47562306a36Sopenharmony_ci } 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci /* failure, clean up */ 47862306a36Sopenharmony_ci IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name); 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ci /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released 48162306a36Sopenharmony_ci automatically when the MPG stream is released. 48262306a36Sopenharmony_ci We only need to stop the VBI capturing. */ 48362306a36Sopenharmony_ci if (s->type == IVTV_ENC_STREAM_TYPE_MPG && 48462306a36Sopenharmony_ci test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) { 48562306a36Sopenharmony_ci ivtv_stop_v4l2_encode_stream(s_vbi, 0); 48662306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags); 48762306a36Sopenharmony_ci } 48862306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 48962306a36Sopenharmony_ci ivtv_release_stream(s); 49062306a36Sopenharmony_ci return -EIO; 49162306a36Sopenharmony_ci} 49262306a36Sopenharmony_ci 49362306a36Sopenharmony_cissize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos) 49462306a36Sopenharmony_ci{ 49562306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(filp->private_data); 49662306a36Sopenharmony_ci struct ivtv *itv = id->itv; 49762306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 49862306a36Sopenharmony_ci ssize_t rc; 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name); 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ci if (mutex_lock_interruptible(&itv->serialize_lock)) 50362306a36Sopenharmony_ci return -ERESTARTSYS; 50462306a36Sopenharmony_ci rc = ivtv_start_capture(id); 50562306a36Sopenharmony_ci if (!rc) 50662306a36Sopenharmony_ci rc = ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK); 50762306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 50862306a36Sopenharmony_ci return rc; 50962306a36Sopenharmony_ci} 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_ciint ivtv_start_decoding(struct ivtv_open_id *id, int speed) 51262306a36Sopenharmony_ci{ 51362306a36Sopenharmony_ci struct ivtv *itv = id->itv; 51462306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 51562306a36Sopenharmony_ci int rc; 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci if (atomic_read(&itv->decoding) == 0) { 51862306a36Sopenharmony_ci if (ivtv_claim_stream(id, s->type)) { 51962306a36Sopenharmony_ci /* someone else is using this stream already */ 52062306a36Sopenharmony_ci IVTV_DEBUG_WARN("start decode, stream already claimed\n"); 52162306a36Sopenharmony_ci return -EBUSY; 52262306a36Sopenharmony_ci } 52362306a36Sopenharmony_ci rc = ivtv_start_v4l2_decode_stream(s, 0); 52462306a36Sopenharmony_ci if (rc < 0) { 52562306a36Sopenharmony_ci if (rc == -EAGAIN) 52662306a36Sopenharmony_ci rc = ivtv_start_v4l2_decode_stream(s, 0); 52762306a36Sopenharmony_ci if (rc < 0) 52862306a36Sopenharmony_ci return rc; 52962306a36Sopenharmony_ci } 53062306a36Sopenharmony_ci } 53162306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_MPG) 53262306a36Sopenharmony_ci return ivtv_set_speed(itv, speed); 53362306a36Sopenharmony_ci return 0; 53462306a36Sopenharmony_ci} 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_cistatic ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos) 53762306a36Sopenharmony_ci{ 53862306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(filp->private_data); 53962306a36Sopenharmony_ci struct ivtv *itv = id->itv; 54062306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 54162306a36Sopenharmony_ci struct yuv_playback_info *yi = &itv->yuv_info; 54262306a36Sopenharmony_ci struct ivtv_buffer *buf; 54362306a36Sopenharmony_ci struct ivtv_queue q; 54462306a36Sopenharmony_ci int bytes_written = 0; 54562306a36Sopenharmony_ci int mode; 54662306a36Sopenharmony_ci int rc; 54762306a36Sopenharmony_ci DEFINE_WAIT(wait); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name); 55062306a36Sopenharmony_ci 55162306a36Sopenharmony_ci if (s->type != IVTV_DEC_STREAM_TYPE_MPG && 55262306a36Sopenharmony_ci s->type != IVTV_DEC_STREAM_TYPE_YUV && 55362306a36Sopenharmony_ci s->type != IVTV_DEC_STREAM_TYPE_VOUT) 55462306a36Sopenharmony_ci /* not decoder streams */ 55562306a36Sopenharmony_ci return -EINVAL; 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci /* Try to claim this stream */ 55862306a36Sopenharmony_ci if (ivtv_claim_stream(id, s->type)) 55962306a36Sopenharmony_ci return -EBUSY; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci /* This stream does not need to start any decoding */ 56262306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) { 56362306a36Sopenharmony_ci int elems = count / sizeof(struct v4l2_sliced_vbi_data); 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 56662306a36Sopenharmony_ci return ivtv_write_vbi_from_user(itv, 56762306a36Sopenharmony_ci (const struct v4l2_sliced_vbi_data __user *)user_buf, elems); 56862306a36Sopenharmony_ci } 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV; 57162306a36Sopenharmony_ci 57262306a36Sopenharmony_ci if (ivtv_set_output_mode(itv, mode) != mode) { 57362306a36Sopenharmony_ci ivtv_release_stream(s); 57462306a36Sopenharmony_ci return -EBUSY; 57562306a36Sopenharmony_ci } 57662306a36Sopenharmony_ci ivtv_queue_init(&q); 57762306a36Sopenharmony_ci set_bit(IVTV_F_S_APPL_IO, &s->s_flags); 57862306a36Sopenharmony_ci 57962306a36Sopenharmony_ci /* Start decoder (returns 0 if already started) */ 58062306a36Sopenharmony_ci rc = ivtv_start_decoding(id, itv->speed); 58162306a36Sopenharmony_ci if (rc) { 58262306a36Sopenharmony_ci IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name); 58362306a36Sopenharmony_ci 58462306a36Sopenharmony_ci /* failure, clean up */ 58562306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 58662306a36Sopenharmony_ci clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); 58762306a36Sopenharmony_ci return rc; 58862306a36Sopenharmony_ci } 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ciretry: 59162306a36Sopenharmony_ci /* If possible, just DMA the entire frame - Check the data transfer size 59262306a36Sopenharmony_ci since we may get here before the stream has been fully set-up */ 59362306a36Sopenharmony_ci if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) { 59462306a36Sopenharmony_ci while (count >= itv->dma_data_req_size) { 59562306a36Sopenharmony_ci rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf); 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci if (rc < 0) 59862306a36Sopenharmony_ci return rc; 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ci bytes_written += itv->dma_data_req_size; 60162306a36Sopenharmony_ci user_buf += itv->dma_data_req_size; 60262306a36Sopenharmony_ci count -= itv->dma_data_req_size; 60362306a36Sopenharmony_ci } 60462306a36Sopenharmony_ci if (count == 0) { 60562306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused); 60662306a36Sopenharmony_ci return bytes_written; 60762306a36Sopenharmony_ci } 60862306a36Sopenharmony_ci } 60962306a36Sopenharmony_ci 61062306a36Sopenharmony_ci for (;;) { 61162306a36Sopenharmony_ci /* Gather buffers */ 61262306a36Sopenharmony_ci while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io))) 61362306a36Sopenharmony_ci ivtv_enqueue(s, buf, &q); 61462306a36Sopenharmony_ci while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) { 61562306a36Sopenharmony_ci ivtv_enqueue(s, buf, &q); 61662306a36Sopenharmony_ci } 61762306a36Sopenharmony_ci if (q.buffers) 61862306a36Sopenharmony_ci break; 61962306a36Sopenharmony_ci if (filp->f_flags & O_NONBLOCK) 62062306a36Sopenharmony_ci return -EAGAIN; 62162306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 62262306a36Sopenharmony_ci prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE); 62362306a36Sopenharmony_ci /* New buffers might have become free before we were added to the waitqueue */ 62462306a36Sopenharmony_ci if (!s->q_free.buffers) 62562306a36Sopenharmony_ci schedule(); 62662306a36Sopenharmony_ci finish_wait(&s->waitq, &wait); 62762306a36Sopenharmony_ci mutex_lock(&itv->serialize_lock); 62862306a36Sopenharmony_ci if (signal_pending(current)) { 62962306a36Sopenharmony_ci IVTV_DEBUG_INFO("User stopped %s\n", s->name); 63062306a36Sopenharmony_ci return -EINTR; 63162306a36Sopenharmony_ci } 63262306a36Sopenharmony_ci } 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci /* copy user data into buffers */ 63562306a36Sopenharmony_ci while ((buf = ivtv_dequeue(s, &q))) { 63662306a36Sopenharmony_ci /* yuv is a pain. Don't copy more data than needed for a single 63762306a36Sopenharmony_ci frame, otherwise we lose sync with the incoming stream */ 63862306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_YUV && 63962306a36Sopenharmony_ci yi->stream_size + count > itv->dma_data_req_size) 64062306a36Sopenharmony_ci rc = ivtv_buf_copy_from_user(s, buf, user_buf, 64162306a36Sopenharmony_ci itv->dma_data_req_size - yi->stream_size); 64262306a36Sopenharmony_ci else 64362306a36Sopenharmony_ci rc = ivtv_buf_copy_from_user(s, buf, user_buf, count); 64462306a36Sopenharmony_ci 64562306a36Sopenharmony_ci /* Make sure we really got all the user data */ 64662306a36Sopenharmony_ci if (rc < 0) { 64762306a36Sopenharmony_ci ivtv_queue_move(s, &q, NULL, &s->q_free, 0); 64862306a36Sopenharmony_ci return rc; 64962306a36Sopenharmony_ci } 65062306a36Sopenharmony_ci user_buf += rc; 65162306a36Sopenharmony_ci count -= rc; 65262306a36Sopenharmony_ci bytes_written += rc; 65362306a36Sopenharmony_ci 65462306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { 65562306a36Sopenharmony_ci yi->stream_size += rc; 65662306a36Sopenharmony_ci /* If we have a complete yuv frame, break loop now */ 65762306a36Sopenharmony_ci if (yi->stream_size == itv->dma_data_req_size) { 65862306a36Sopenharmony_ci ivtv_enqueue(s, buf, &s->q_full); 65962306a36Sopenharmony_ci yi->stream_size = 0; 66062306a36Sopenharmony_ci break; 66162306a36Sopenharmony_ci } 66262306a36Sopenharmony_ci } 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ci if (buf->bytesused != s->buf_size) { 66562306a36Sopenharmony_ci /* incomplete, leave in q_io for next time */ 66662306a36Sopenharmony_ci ivtv_enqueue(s, buf, &s->q_io); 66762306a36Sopenharmony_ci break; 66862306a36Sopenharmony_ci } 66962306a36Sopenharmony_ci /* Byteswap MPEG buffer */ 67062306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_MPG) 67162306a36Sopenharmony_ci ivtv_buf_swap(buf); 67262306a36Sopenharmony_ci ivtv_enqueue(s, buf, &s->q_full); 67362306a36Sopenharmony_ci } 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_ci if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) { 67662306a36Sopenharmony_ci if (s->q_full.length >= itv->dma_data_req_size) { 67762306a36Sopenharmony_ci int got_sig; 67862306a36Sopenharmony_ci 67962306a36Sopenharmony_ci if (mode == OUT_YUV) 68062306a36Sopenharmony_ci ivtv_yuv_setup_stream_frame(itv); 68162306a36Sopenharmony_ci 68262306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 68362306a36Sopenharmony_ci prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE); 68462306a36Sopenharmony_ci while (!(got_sig = signal_pending(current)) && 68562306a36Sopenharmony_ci test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) { 68662306a36Sopenharmony_ci schedule(); 68762306a36Sopenharmony_ci } 68862306a36Sopenharmony_ci finish_wait(&itv->dma_waitq, &wait); 68962306a36Sopenharmony_ci mutex_lock(&itv->serialize_lock); 69062306a36Sopenharmony_ci if (got_sig) { 69162306a36Sopenharmony_ci IVTV_DEBUG_INFO("User interrupted %s\n", s->name); 69262306a36Sopenharmony_ci return -EINTR; 69362306a36Sopenharmony_ci } 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ci clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); 69662306a36Sopenharmony_ci ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size); 69762306a36Sopenharmony_ci ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1); 69862306a36Sopenharmony_ci } 69962306a36Sopenharmony_ci } 70062306a36Sopenharmony_ci /* more user data is available, wait until buffers become free 70162306a36Sopenharmony_ci to transfer the rest. */ 70262306a36Sopenharmony_ci if (count && !(filp->f_flags & O_NONBLOCK)) 70362306a36Sopenharmony_ci goto retry; 70462306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused); 70562306a36Sopenharmony_ci return bytes_written; 70662306a36Sopenharmony_ci} 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_cissize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos) 70962306a36Sopenharmony_ci{ 71062306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(filp->private_data); 71162306a36Sopenharmony_ci struct ivtv *itv = id->itv; 71262306a36Sopenharmony_ci ssize_t res; 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci if (mutex_lock_interruptible(&itv->serialize_lock)) 71562306a36Sopenharmony_ci return -ERESTARTSYS; 71662306a36Sopenharmony_ci res = ivtv_write(filp, user_buf, count, pos); 71762306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 71862306a36Sopenharmony_ci return res; 71962306a36Sopenharmony_ci} 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_ci__poll_t ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait) 72262306a36Sopenharmony_ci{ 72362306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(filp->private_data); 72462306a36Sopenharmony_ci struct ivtv *itv = id->itv; 72562306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 72662306a36Sopenharmony_ci __poll_t res = 0; 72762306a36Sopenharmony_ci 72862306a36Sopenharmony_ci /* add stream's waitq to the poll list */ 72962306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("Decoder poll\n"); 73062306a36Sopenharmony_ci 73162306a36Sopenharmony_ci /* If there are subscribed events, then only use the new event 73262306a36Sopenharmony_ci API instead of the old video.h based API. */ 73362306a36Sopenharmony_ci if (!list_empty(&id->fh.subscribed)) { 73462306a36Sopenharmony_ci poll_wait(filp, &id->fh.wait, wait); 73562306a36Sopenharmony_ci /* Turn off the old-style vsync events */ 73662306a36Sopenharmony_ci clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); 73762306a36Sopenharmony_ci if (v4l2_event_pending(&id->fh)) 73862306a36Sopenharmony_ci res = EPOLLPRI; 73962306a36Sopenharmony_ci } else { 74062306a36Sopenharmony_ci /* This is the old-style API which is here only for backwards 74162306a36Sopenharmony_ci compatibility. */ 74262306a36Sopenharmony_ci poll_wait(filp, &s->waitq, wait); 74362306a36Sopenharmony_ci set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags); 74462306a36Sopenharmony_ci if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) || 74562306a36Sopenharmony_ci test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags)) 74662306a36Sopenharmony_ci res = EPOLLPRI; 74762306a36Sopenharmony_ci } 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci /* Allow write if buffers are available for writing */ 75062306a36Sopenharmony_ci if (s->q_free.buffers) 75162306a36Sopenharmony_ci res |= EPOLLOUT | EPOLLWRNORM; 75262306a36Sopenharmony_ci return res; 75362306a36Sopenharmony_ci} 75462306a36Sopenharmony_ci 75562306a36Sopenharmony_ci__poll_t ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait) 75662306a36Sopenharmony_ci{ 75762306a36Sopenharmony_ci __poll_t req_events = poll_requested_events(wait); 75862306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(filp->private_data); 75962306a36Sopenharmony_ci struct ivtv *itv = id->itv; 76062306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 76162306a36Sopenharmony_ci int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 76262306a36Sopenharmony_ci __poll_t res = 0; 76362306a36Sopenharmony_ci 76462306a36Sopenharmony_ci /* Start a capture if there is none */ 76562306a36Sopenharmony_ci if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags) && 76662306a36Sopenharmony_ci s->type != IVTV_ENC_STREAM_TYPE_RAD && 76762306a36Sopenharmony_ci (req_events & (EPOLLIN | EPOLLRDNORM))) { 76862306a36Sopenharmony_ci int rc; 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci mutex_lock(&itv->serialize_lock); 77162306a36Sopenharmony_ci rc = ivtv_start_capture(id); 77262306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 77362306a36Sopenharmony_ci if (rc) { 77462306a36Sopenharmony_ci IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n", 77562306a36Sopenharmony_ci s->name, rc); 77662306a36Sopenharmony_ci return EPOLLERR; 77762306a36Sopenharmony_ci } 77862306a36Sopenharmony_ci IVTV_DEBUG_FILE("Encoder poll started capture\n"); 77962306a36Sopenharmony_ci } 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci /* add stream's waitq to the poll list */ 78262306a36Sopenharmony_ci IVTV_DEBUG_HI_FILE("Encoder poll\n"); 78362306a36Sopenharmony_ci poll_wait(filp, &s->waitq, wait); 78462306a36Sopenharmony_ci if (v4l2_event_pending(&id->fh)) 78562306a36Sopenharmony_ci res |= EPOLLPRI; 78662306a36Sopenharmony_ci else 78762306a36Sopenharmony_ci poll_wait(filp, &id->fh.wait, wait); 78862306a36Sopenharmony_ci 78962306a36Sopenharmony_ci if (s->q_full.length || s->q_io.length) 79062306a36Sopenharmony_ci return res | EPOLLIN | EPOLLRDNORM; 79162306a36Sopenharmony_ci if (eof) 79262306a36Sopenharmony_ci return res | EPOLLHUP; 79362306a36Sopenharmony_ci return res; 79462306a36Sopenharmony_ci} 79562306a36Sopenharmony_ci 79662306a36Sopenharmony_civoid ivtv_stop_capture(struct ivtv_open_id *id, int gop_end) 79762306a36Sopenharmony_ci{ 79862306a36Sopenharmony_ci struct ivtv *itv = id->itv; 79962306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci IVTV_DEBUG_FILE("close() of %s\n", s->name); 80262306a36Sopenharmony_ci 80362306a36Sopenharmony_ci /* 'Unclaim' this stream */ 80462306a36Sopenharmony_ci 80562306a36Sopenharmony_ci /* Stop capturing */ 80662306a36Sopenharmony_ci if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { 80762306a36Sopenharmony_ci struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI]; 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci IVTV_DEBUG_INFO("close stopping capture\n"); 81062306a36Sopenharmony_ci /* Special case: a running VBI capture for VBI insertion 81162306a36Sopenharmony_ci in the mpeg stream. Need to stop that too. */ 81262306a36Sopenharmony_ci if (id->type == IVTV_ENC_STREAM_TYPE_MPG && 81362306a36Sopenharmony_ci test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) && 81462306a36Sopenharmony_ci !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) { 81562306a36Sopenharmony_ci IVTV_DEBUG_INFO("close stopping embedded VBI capture\n"); 81662306a36Sopenharmony_ci ivtv_stop_v4l2_encode_stream(s_vbi, 0); 81762306a36Sopenharmony_ci } 81862306a36Sopenharmony_ci if ((id->type == IVTV_DEC_STREAM_TYPE_VBI || 81962306a36Sopenharmony_ci id->type == IVTV_ENC_STREAM_TYPE_VBI) && 82062306a36Sopenharmony_ci test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) { 82162306a36Sopenharmony_ci /* Also used internally, don't stop capturing */ 82262306a36Sopenharmony_ci s->fh = NULL; 82362306a36Sopenharmony_ci } 82462306a36Sopenharmony_ci else { 82562306a36Sopenharmony_ci ivtv_stop_v4l2_encode_stream(s, gop_end); 82662306a36Sopenharmony_ci } 82762306a36Sopenharmony_ci } 82862306a36Sopenharmony_ci if (!gop_end) { 82962306a36Sopenharmony_ci clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); 83062306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 83162306a36Sopenharmony_ci ivtv_release_stream(s); 83262306a36Sopenharmony_ci } 83362306a36Sopenharmony_ci} 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_cistatic void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts) 83662306a36Sopenharmony_ci{ 83762306a36Sopenharmony_ci struct ivtv *itv = id->itv; 83862306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 83962306a36Sopenharmony_ci 84062306a36Sopenharmony_ci IVTV_DEBUG_FILE("close() of %s\n", s->name); 84162306a36Sopenharmony_ci 84262306a36Sopenharmony_ci if (id->type == IVTV_DEC_STREAM_TYPE_YUV && 84362306a36Sopenharmony_ci test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) { 84462306a36Sopenharmony_ci /* Restore registers we've changed & clean up any mess */ 84562306a36Sopenharmony_ci ivtv_yuv_close(itv); 84662306a36Sopenharmony_ci } 84762306a36Sopenharmony_ci 84862306a36Sopenharmony_ci /* Stop decoding */ 84962306a36Sopenharmony_ci if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { 85062306a36Sopenharmony_ci IVTV_DEBUG_INFO("close stopping decode\n"); 85162306a36Sopenharmony_ci 85262306a36Sopenharmony_ci ivtv_stop_v4l2_decode_stream(s, flags, pts); 85362306a36Sopenharmony_ci itv->output_mode = OUT_NONE; 85462306a36Sopenharmony_ci } 85562306a36Sopenharmony_ci clear_bit(IVTV_F_S_APPL_IO, &s->s_flags); 85662306a36Sopenharmony_ci clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 85762306a36Sopenharmony_ci 85862306a36Sopenharmony_ci if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames) 85962306a36Sopenharmony_ci itv->output_mode = OUT_NONE; 86062306a36Sopenharmony_ci 86162306a36Sopenharmony_ci itv->speed = 0; 86262306a36Sopenharmony_ci clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags); 86362306a36Sopenharmony_ci ivtv_release_stream(s); 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_ciint ivtv_v4l2_close(struct file *filp) 86762306a36Sopenharmony_ci{ 86862306a36Sopenharmony_ci struct v4l2_fh *fh = filp->private_data; 86962306a36Sopenharmony_ci struct ivtv_open_id *id = fh2id(fh); 87062306a36Sopenharmony_ci struct ivtv *itv = id->itv; 87162306a36Sopenharmony_ci struct ivtv_stream *s = &itv->streams[id->type]; 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_ci IVTV_DEBUG_FILE("close %s\n", s->name); 87462306a36Sopenharmony_ci 87562306a36Sopenharmony_ci mutex_lock(&itv->serialize_lock); 87662306a36Sopenharmony_ci 87762306a36Sopenharmony_ci /* Stop radio */ 87862306a36Sopenharmony_ci if (id->type == IVTV_ENC_STREAM_TYPE_RAD && 87962306a36Sopenharmony_ci v4l2_fh_is_singular_file(filp)) { 88062306a36Sopenharmony_ci /* Closing radio device, return to TV mode */ 88162306a36Sopenharmony_ci ivtv_mute(itv); 88262306a36Sopenharmony_ci /* Mark that the radio is no longer in use */ 88362306a36Sopenharmony_ci clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); 88462306a36Sopenharmony_ci /* Switch tuner to TV */ 88562306a36Sopenharmony_ci ivtv_call_all(itv, video, s_std, itv->std); 88662306a36Sopenharmony_ci /* Select correct audio input (i.e. TV tuner or Line in) */ 88762306a36Sopenharmony_ci ivtv_audio_set_io(itv); 88862306a36Sopenharmony_ci if (itv->hw_flags & IVTV_HW_SAA711X) { 88962306a36Sopenharmony_ci ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq, 89062306a36Sopenharmony_ci SAA7115_FREQ_32_11_MHZ, 0); 89162306a36Sopenharmony_ci } 89262306a36Sopenharmony_ci if (atomic_read(&itv->capturing) > 0) { 89362306a36Sopenharmony_ci /* Undo video mute */ 89462306a36Sopenharmony_ci ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, 89562306a36Sopenharmony_ci v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) | 89662306a36Sopenharmony_ci (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8)); 89762306a36Sopenharmony_ci } 89862306a36Sopenharmony_ci /* Done! Unmute and continue. */ 89962306a36Sopenharmony_ci ivtv_unmute(itv); 90062306a36Sopenharmony_ci } 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_ci v4l2_fh_del(fh); 90362306a36Sopenharmony_ci v4l2_fh_exit(fh); 90462306a36Sopenharmony_ci 90562306a36Sopenharmony_ci /* Easy case first: this stream was never claimed by us */ 90662306a36Sopenharmony_ci if (s->fh != &id->fh) 90762306a36Sopenharmony_ci goto close_done; 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci /* 'Unclaim' this stream */ 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) { 91262306a36Sopenharmony_ci struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT]; 91362306a36Sopenharmony_ci 91462306a36Sopenharmony_ci ivtv_stop_decoding(id, V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0); 91562306a36Sopenharmony_ci 91662306a36Sopenharmony_ci /* If all output streams are closed, and if the user doesn't have 91762306a36Sopenharmony_ci IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */ 91862306a36Sopenharmony_ci if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) { 91962306a36Sopenharmony_ci /* disable CC on TV-out */ 92062306a36Sopenharmony_ci ivtv_disable_cc(itv); 92162306a36Sopenharmony_ci } 92262306a36Sopenharmony_ci } else { 92362306a36Sopenharmony_ci ivtv_stop_capture(id, 0); 92462306a36Sopenharmony_ci } 92562306a36Sopenharmony_ciclose_done: 92662306a36Sopenharmony_ci kfree(id); 92762306a36Sopenharmony_ci mutex_unlock(&itv->serialize_lock); 92862306a36Sopenharmony_ci return 0; 92962306a36Sopenharmony_ci} 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_cistatic int ivtv_open(struct file *filp) 93262306a36Sopenharmony_ci{ 93362306a36Sopenharmony_ci struct video_device *vdev = video_devdata(filp); 93462306a36Sopenharmony_ci struct ivtv_stream *s = video_get_drvdata(vdev); 93562306a36Sopenharmony_ci struct ivtv *itv = s->itv; 93662306a36Sopenharmony_ci struct ivtv_open_id *item; 93762306a36Sopenharmony_ci int res = 0; 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_ci IVTV_DEBUG_FILE("open %s\n", s->name); 94062306a36Sopenharmony_ci 94162306a36Sopenharmony_ci if (ivtv_init_on_first_open(itv)) { 94262306a36Sopenharmony_ci IVTV_ERR("Failed to initialize on device %s\n", 94362306a36Sopenharmony_ci video_device_node_name(vdev)); 94462306a36Sopenharmony_ci return -ENXIO; 94562306a36Sopenharmony_ci } 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG 94862306a36Sopenharmony_ci /* Unless ivtv_fw_debug is set, error out if firmware dead. */ 94962306a36Sopenharmony_ci if (ivtv_fw_debug) { 95062306a36Sopenharmony_ci IVTV_WARN("Opening %s with dead firmware lockout disabled\n", 95162306a36Sopenharmony_ci video_device_node_name(vdev)); 95262306a36Sopenharmony_ci IVTV_WARN("Selected firmware errors will be ignored\n"); 95362306a36Sopenharmony_ci } else { 95462306a36Sopenharmony_ci#else 95562306a36Sopenharmony_ci if (1) { 95662306a36Sopenharmony_ci#endif 95762306a36Sopenharmony_ci res = ivtv_firmware_check(itv, "ivtv_serialized_open"); 95862306a36Sopenharmony_ci if (res == -EAGAIN) 95962306a36Sopenharmony_ci res = ivtv_firmware_check(itv, "ivtv_serialized_open"); 96062306a36Sopenharmony_ci if (res < 0) 96162306a36Sopenharmony_ci return -EIO; 96262306a36Sopenharmony_ci } 96362306a36Sopenharmony_ci 96462306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_MPG && 96562306a36Sopenharmony_ci test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags)) 96662306a36Sopenharmony_ci return -EBUSY; 96762306a36Sopenharmony_ci 96862306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_YUV && 96962306a36Sopenharmony_ci test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags)) 97062306a36Sopenharmony_ci return -EBUSY; 97162306a36Sopenharmony_ci 97262306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { 97362306a36Sopenharmony_ci if (read_reg(0x82c) == 0) { 97462306a36Sopenharmony_ci IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n"); 97562306a36Sopenharmony_ci /* return -ENODEV; */ 97662306a36Sopenharmony_ci } 97762306a36Sopenharmony_ci ivtv_udma_alloc(itv); 97862306a36Sopenharmony_ci } 97962306a36Sopenharmony_ci 98062306a36Sopenharmony_ci /* Allocate memory */ 98162306a36Sopenharmony_ci item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL); 98262306a36Sopenharmony_ci if (NULL == item) { 98362306a36Sopenharmony_ci IVTV_DEBUG_WARN("nomem on v4l2 open\n"); 98462306a36Sopenharmony_ci return -ENOMEM; 98562306a36Sopenharmony_ci } 98662306a36Sopenharmony_ci v4l2_fh_init(&item->fh, &s->vdev); 98762306a36Sopenharmony_ci item->itv = itv; 98862306a36Sopenharmony_ci item->type = s->type; 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci filp->private_data = &item->fh; 99162306a36Sopenharmony_ci v4l2_fh_add(&item->fh); 99262306a36Sopenharmony_ci 99362306a36Sopenharmony_ci if (item->type == IVTV_ENC_STREAM_TYPE_RAD && 99462306a36Sopenharmony_ci v4l2_fh_is_singular_file(filp)) { 99562306a36Sopenharmony_ci if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) { 99662306a36Sopenharmony_ci if (atomic_read(&itv->capturing) > 0) { 99762306a36Sopenharmony_ci /* switching to radio while capture is 99862306a36Sopenharmony_ci in progress is not polite */ 99962306a36Sopenharmony_ci v4l2_fh_del(&item->fh); 100062306a36Sopenharmony_ci v4l2_fh_exit(&item->fh); 100162306a36Sopenharmony_ci kfree(item); 100262306a36Sopenharmony_ci return -EBUSY; 100362306a36Sopenharmony_ci } 100462306a36Sopenharmony_ci } 100562306a36Sopenharmony_ci /* Mark that the radio is being used. */ 100662306a36Sopenharmony_ci set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags); 100762306a36Sopenharmony_ci /* We have the radio */ 100862306a36Sopenharmony_ci ivtv_mute(itv); 100962306a36Sopenharmony_ci /* Switch tuner to radio */ 101062306a36Sopenharmony_ci ivtv_call_all(itv, tuner, s_radio); 101162306a36Sopenharmony_ci /* Select the correct audio input (i.e. radio tuner) */ 101262306a36Sopenharmony_ci ivtv_audio_set_io(itv); 101362306a36Sopenharmony_ci if (itv->hw_flags & IVTV_HW_SAA711X) { 101462306a36Sopenharmony_ci ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq, 101562306a36Sopenharmony_ci SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL); 101662306a36Sopenharmony_ci } 101762306a36Sopenharmony_ci /* Done! Unmute and continue. */ 101862306a36Sopenharmony_ci ivtv_unmute(itv); 101962306a36Sopenharmony_ci } 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci /* YUV or MPG Decoding Mode? */ 102262306a36Sopenharmony_ci if (s->type == IVTV_DEC_STREAM_TYPE_MPG) { 102362306a36Sopenharmony_ci clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); 102462306a36Sopenharmony_ci } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) { 102562306a36Sopenharmony_ci set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags); 102662306a36Sopenharmony_ci /* For yuv, we need to know the dma size before we start */ 102762306a36Sopenharmony_ci itv->dma_data_req_size = 102862306a36Sopenharmony_ci 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31); 102962306a36Sopenharmony_ci itv->yuv_info.stream_size = 0; 103062306a36Sopenharmony_ci } 103162306a36Sopenharmony_ci return 0; 103262306a36Sopenharmony_ci} 103362306a36Sopenharmony_ci 103462306a36Sopenharmony_ciint ivtv_v4l2_open(struct file *filp) 103562306a36Sopenharmony_ci{ 103662306a36Sopenharmony_ci struct video_device *vdev = video_devdata(filp); 103762306a36Sopenharmony_ci int res; 103862306a36Sopenharmony_ci 103962306a36Sopenharmony_ci if (mutex_lock_interruptible(vdev->lock)) 104062306a36Sopenharmony_ci return -ERESTARTSYS; 104162306a36Sopenharmony_ci res = ivtv_open(filp); 104262306a36Sopenharmony_ci mutex_unlock(vdev->lock); 104362306a36Sopenharmony_ci return res; 104462306a36Sopenharmony_ci} 104562306a36Sopenharmony_ci 104662306a36Sopenharmony_civoid ivtv_mute(struct ivtv *itv) 104762306a36Sopenharmony_ci{ 104862306a36Sopenharmony_ci if (atomic_read(&itv->capturing)) 104962306a36Sopenharmony_ci ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1); 105062306a36Sopenharmony_ci IVTV_DEBUG_INFO("Mute\n"); 105162306a36Sopenharmony_ci} 105262306a36Sopenharmony_ci 105362306a36Sopenharmony_civoid ivtv_unmute(struct ivtv *itv) 105462306a36Sopenharmony_ci{ 105562306a36Sopenharmony_ci if (atomic_read(&itv->capturing)) { 105662306a36Sopenharmony_ci ivtv_msleep_timeout(100, 0); 105762306a36Sopenharmony_ci ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12); 105862306a36Sopenharmony_ci ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0); 105962306a36Sopenharmony_ci } 106062306a36Sopenharmony_ci IVTV_DEBUG_INFO("Unmute\n"); 106162306a36Sopenharmony_ci} 1062