Path: blob/master/drivers/media/video/ivtv/ivtv-streams.c
17782 views
/*1init/start/stop/exit stream functions2Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>3Copyright (C) 2004 Chris Kennedy <[email protected]>4Copyright (C) 2005-2007 Hans Verkuil <[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021/* License: GPL22* Author: Kevin Thayer <nufan_wfk at yahoo dot com>23*24* This file will hold API related functions, both internal (firmware api)25* and external (v4l2, etc)26*27* -----28* MPG600/MPG160 support by T.Adachi <[email protected]>29* and Takeru KOMORIYA<[email protected]>30*31* AVerMedia M179 GPIO info by Chris Pinkham <[email protected]>32* using information provided by Jiun-Kuei Jung @ AVerMedia.33*/3435#include "ivtv-driver.h"36#include "ivtv-fileops.h"37#include "ivtv-queue.h"38#include "ivtv-mailbox.h"39#include "ivtv-ioctl.h"40#include "ivtv-irq.h"41#include "ivtv-yuv.h"42#include "ivtv-cards.h"43#include "ivtv-streams.h"44#include "ivtv-firmware.h"45#include <media/v4l2-event.h>4647static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {48.owner = THIS_MODULE,49.read = ivtv_v4l2_read,50.write = ivtv_v4l2_write,51.open = ivtv_v4l2_open,52.unlocked_ioctl = ivtv_v4l2_ioctl,53.release = ivtv_v4l2_close,54.poll = ivtv_v4l2_enc_poll,55};5657static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {58.owner = THIS_MODULE,59.read = ivtv_v4l2_read,60.write = ivtv_v4l2_write,61.open = ivtv_v4l2_open,62.unlocked_ioctl = ivtv_v4l2_ioctl,63.release = ivtv_v4l2_close,64.poll = ivtv_v4l2_dec_poll,65};6667#define IVTV_V4L2_DEC_MPG_OFFSET 16 /* offset from 0 to register decoder mpg v4l2 minors on */68#define IVTV_V4L2_ENC_PCM_OFFSET 24 /* offset from 0 to register pcm v4l2 minors on */69#define IVTV_V4L2_ENC_YUV_OFFSET 32 /* offset from 0 to register yuv v4l2 minors on */70#define IVTV_V4L2_DEC_YUV_OFFSET 48 /* offset from 0 to register decoder yuv v4l2 minors on */71#define IVTV_V4L2_DEC_VBI_OFFSET 8 /* offset from 0 to register decoder vbi input v4l2 minors on */72#define IVTV_V4L2_DEC_VOUT_OFFSET 16 /* offset from 0 to register vbi output v4l2 minors on */7374static struct {75const char *name;76int vfl_type;77int num_offset;78int dma, pio;79enum v4l2_buf_type buf_type;80const struct v4l2_file_operations *fops;81} ivtv_stream_info[] = {82{ /* IVTV_ENC_STREAM_TYPE_MPG */83"encoder MPG",84VFL_TYPE_GRABBER, 0,85PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,86&ivtv_v4l2_enc_fops87},88{ /* IVTV_ENC_STREAM_TYPE_YUV */89"encoder YUV",90VFL_TYPE_GRABBER, IVTV_V4L2_ENC_YUV_OFFSET,91PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VIDEO_CAPTURE,92&ivtv_v4l2_enc_fops93},94{ /* IVTV_ENC_STREAM_TYPE_VBI */95"encoder VBI",96VFL_TYPE_VBI, 0,97PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_VBI_CAPTURE,98&ivtv_v4l2_enc_fops99},100{ /* IVTV_ENC_STREAM_TYPE_PCM */101"encoder PCM",102VFL_TYPE_GRABBER, IVTV_V4L2_ENC_PCM_OFFSET,103PCI_DMA_FROMDEVICE, 0, V4L2_BUF_TYPE_PRIVATE,104&ivtv_v4l2_enc_fops105},106{ /* IVTV_ENC_STREAM_TYPE_RAD */107"encoder radio",108VFL_TYPE_RADIO, 0,109PCI_DMA_NONE, 1, V4L2_BUF_TYPE_PRIVATE,110&ivtv_v4l2_enc_fops111},112{ /* IVTV_DEC_STREAM_TYPE_MPG */113"decoder MPG",114VFL_TYPE_GRABBER, IVTV_V4L2_DEC_MPG_OFFSET,115PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,116&ivtv_v4l2_dec_fops117},118{ /* IVTV_DEC_STREAM_TYPE_VBI */119"decoder VBI",120VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,121PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_CAPTURE,122&ivtv_v4l2_enc_fops123},124{ /* IVTV_DEC_STREAM_TYPE_VOUT */125"decoder VOUT",126VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,127PCI_DMA_NONE, 1, V4L2_BUF_TYPE_VBI_OUTPUT,128&ivtv_v4l2_dec_fops129},130{ /* IVTV_DEC_STREAM_TYPE_YUV */131"decoder YUV",132VFL_TYPE_GRABBER, IVTV_V4L2_DEC_YUV_OFFSET,133PCI_DMA_TODEVICE, 0, V4L2_BUF_TYPE_VIDEO_OUTPUT,134&ivtv_v4l2_dec_fops135}136};137138static void ivtv_stream_init(struct ivtv *itv, int type)139{140struct ivtv_stream *s = &itv->streams[type];141struct video_device *vdev = s->vdev;142143/* we need to keep vdev, so restore it afterwards */144memset(s, 0, sizeof(*s));145s->vdev = vdev;146147/* initialize ivtv_stream fields */148s->itv = itv;149s->type = type;150s->name = ivtv_stream_info[type].name;151152if (ivtv_stream_info[type].pio)153s->dma = PCI_DMA_NONE;154else155s->dma = ivtv_stream_info[type].dma;156s->buf_size = itv->stream_buf_size[type];157if (s->buf_size)158s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;159spin_lock_init(&s->qlock);160init_waitqueue_head(&s->waitq);161s->id = -1;162s->sg_handle = IVTV_DMA_UNMAPPED;163ivtv_queue_init(&s->q_free);164ivtv_queue_init(&s->q_full);165ivtv_queue_init(&s->q_dma);166ivtv_queue_init(&s->q_predma);167ivtv_queue_init(&s->q_io);168}169170static int ivtv_prep_dev(struct ivtv *itv, int type)171{172struct ivtv_stream *s = &itv->streams[type];173int num_offset = ivtv_stream_info[type].num_offset;174int num = itv->instance + ivtv_first_minor + num_offset;175176/* These four fields are always initialized. If vdev == NULL, then177this stream is not in use. In that case no other fields but these178four can be used. */179s->vdev = NULL;180s->itv = itv;181s->type = type;182s->name = ivtv_stream_info[type].name;183184/* Check whether the radio is supported */185if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))186return 0;187if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))188return 0;189190/* User explicitly selected 0 buffers for these streams, so don't191create them. */192if (ivtv_stream_info[type].dma != PCI_DMA_NONE &&193itv->options.kilobytes[type] == 0) {194IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);195return 0;196}197198ivtv_stream_init(itv, type);199200/* allocate and initialize the v4l2 video device structure */201s->vdev = video_device_alloc();202if (s->vdev == NULL) {203IVTV_ERR("Couldn't allocate v4l2 video_device for %s\n", s->name);204return -ENOMEM;205}206207snprintf(s->vdev->name, sizeof(s->vdev->name), "%s %s",208itv->v4l2_dev.name, s->name);209210s->vdev->num = num;211s->vdev->v4l2_dev = &itv->v4l2_dev;212s->vdev->ctrl_handler = itv->v4l2_dev.ctrl_handler;213s->vdev->fops = ivtv_stream_info[type].fops;214s->vdev->release = video_device_release;215s->vdev->tvnorms = V4L2_STD_ALL;216set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev->flags);217ivtv_set_funcs(s->vdev);218return 0;219}220221/* Initialize v4l2 variables and prepare v4l2 devices */222int ivtv_streams_setup(struct ivtv *itv)223{224int type;225226/* Setup V4L2 Devices */227for (type = 0; type < IVTV_MAX_STREAMS; type++) {228/* Prepare device */229if (ivtv_prep_dev(itv, type))230break;231232if (itv->streams[type].vdev == NULL)233continue;234235/* Allocate Stream */236if (ivtv_stream_alloc(&itv->streams[type]))237break;238}239if (type == IVTV_MAX_STREAMS)240return 0;241242/* One or more streams could not be initialized. Clean 'em all up. */243ivtv_streams_cleanup(itv, 0);244return -ENOMEM;245}246247static int ivtv_reg_dev(struct ivtv *itv, int type)248{249struct ivtv_stream *s = &itv->streams[type];250int vfl_type = ivtv_stream_info[type].vfl_type;251const char *name;252int num;253254if (s->vdev == NULL)255return 0;256257num = s->vdev->num;258/* card number + user defined offset + device offset */259if (type != IVTV_ENC_STREAM_TYPE_MPG) {260struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];261262if (s_mpg->vdev)263num = s_mpg->vdev->num + ivtv_stream_info[type].num_offset;264}265video_set_drvdata(s->vdev, s);266267/* Register device. First try the desired minor, then any free one. */268if (video_register_device_no_warn(s->vdev, vfl_type, num)) {269IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",270s->name, num);271video_device_release(s->vdev);272s->vdev = NULL;273return -ENOMEM;274}275name = video_device_node_name(s->vdev);276277switch (vfl_type) {278case VFL_TYPE_GRABBER:279IVTV_INFO("Registered device %s for %s (%d kB)\n",280name, s->name, itv->options.kilobytes[type]);281break;282case VFL_TYPE_RADIO:283IVTV_INFO("Registered device %s for %s\n",284name, s->name);285break;286case VFL_TYPE_VBI:287if (itv->options.kilobytes[type])288IVTV_INFO("Registered device %s for %s (%d kB)\n",289name, s->name, itv->options.kilobytes[type]);290else291IVTV_INFO("Registered device %s for %s\n",292name, s->name);293break;294}295return 0;296}297298/* Register v4l2 devices */299int ivtv_streams_register(struct ivtv *itv)300{301int type;302int err = 0;303304/* Register V4L2 devices */305for (type = 0; type < IVTV_MAX_STREAMS; type++)306err |= ivtv_reg_dev(itv, type);307308if (err == 0)309return 0;310311/* One or more streams could not be initialized. Clean 'em all up. */312ivtv_streams_cleanup(itv, 1);313return -ENOMEM;314}315316/* Unregister v4l2 devices */317void ivtv_streams_cleanup(struct ivtv *itv, int unregister)318{319int type;320321/* Teardown all streams */322for (type = 0; type < IVTV_MAX_STREAMS; type++) {323struct video_device *vdev = itv->streams[type].vdev;324325itv->streams[type].vdev = NULL;326if (vdev == NULL)327continue;328329ivtv_stream_free(&itv->streams[type]);330/* Unregister or release device */331if (unregister)332video_unregister_device(vdev);333else334video_device_release(vdev);335}336}337338static void ivtv_vbi_setup(struct ivtv *itv)339{340int raw = ivtv_raw_vbi(itv);341u32 data[CX2341X_MBOX_MAX_DATA];342int lines;343int i;344345/* Reset VBI */346ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);347348/* setup VBI registers */349if (raw)350v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);351else352v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);353354/* determine number of lines and total number of VBI bytes.355A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1356The '- 1' byte is probably an unused U or V byte. Or something...357A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal358header, 42 data bytes + checksum (to be confirmed) */359if (raw) {360lines = itv->vbi.count * 2;361} else {362lines = itv->is_60hz ? 24 : 38;363if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))364lines += 2;365}366367itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);368369/* Note: sliced vs raw flag doesn't seem to have any effect370TODO: check mode (0x02) value with older ivtv versions. */371data[0] = raw | 0x02 | (0xbd << 8);372373/* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */374data[1] = 1;375/* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */376data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);377/* The start/stop codes determine which VBI lines end up in the raw VBI data area.378The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line379is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video)380code. These values for raw VBI are obtained from a driver disassembly. The sliced381start/stop codes was deduced from this, but they do not appear in the driver.382Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54.383However, I have no idea what these values are for. */384if (itv->hw_flags & IVTV_HW_CX25840) {385/* Setup VBI for the cx25840 digitizer */386if (raw) {387data[3] = 0x20602060;388data[4] = 0x30703070;389} else {390data[3] = 0xB0F0B0F0;391data[4] = 0xA0E0A0E0;392}393/* Lines per frame */394data[5] = lines;395/* bytes per line */396data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);397} else {398/* Setup VBI for the saa7115 digitizer */399if (raw) {400data[3] = 0x25256262;401data[4] = 0x387F7F7F;402} else {403data[3] = 0xABABECEC;404data[4] = 0xB6F1F1F1;405}406/* Lines per frame */407data[5] = lines;408/* bytes per line */409data[6] = itv->vbi.enc_size / lines;410}411412IVTV_DEBUG_INFO(413"Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",414data[0], data[1], data[2], data[5], data[6]);415416ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);417418/* returns the VBI encoder memory area. */419itv->vbi.enc_start = data[2];420itv->vbi.fpi = data[0];421if (!itv->vbi.fpi)422itv->vbi.fpi = 1;423424IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",425itv->vbi.enc_start, data[1], itv->vbi.fpi);426427/* select VBI lines.428Note that the sliced argument seems to have no effect. */429for (i = 2; i <= 24; i++) {430int valid;431432if (itv->is_60hz) {433valid = i >= 10 && i < 22;434} else {435valid = i >= 6 && i < 24;436}437ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,438valid, 0 , 0, 0);439ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,440valid, 0, 0, 0);441}442443/* Remaining VBI questions:444- Is it possible to select particular VBI lines only for inclusion in the MPEG445stream? Currently you can only get the first X lines.446- Is mixed raw and sliced VBI possible?447- What's the meaning of the raw/sliced flag?448- What's the meaning of params 2, 3 & 4 of the Select VBI command? */449}450451int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)452{453u32 data[CX2341X_MBOX_MAX_DATA];454struct ivtv *itv = s->itv;455int captype = 0, subtype = 0;456int enable_passthrough = 0;457458if (s->vdev == NULL)459return -EINVAL;460461IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);462463switch (s->type) {464case IVTV_ENC_STREAM_TYPE_MPG:465captype = 0;466subtype = 3;467468/* Stop Passthrough */469if (itv->output_mode == OUT_PASSTHROUGH) {470ivtv_passthrough_mode(itv, 0);471enable_passthrough = 1;472}473itv->mpg_data_received = itv->vbi_data_inserted = 0;474itv->dualwatch_jiffies = jiffies;475itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);476itv->search_pack_header = 0;477break;478479case IVTV_ENC_STREAM_TYPE_YUV:480if (itv->output_mode == OUT_PASSTHROUGH) {481captype = 2;482subtype = 11; /* video+audio+decoder */483break;484}485captype = 1;486subtype = 1;487break;488case IVTV_ENC_STREAM_TYPE_PCM:489captype = 1;490subtype = 2;491break;492case IVTV_ENC_STREAM_TYPE_VBI:493captype = 1;494subtype = 4;495496itv->vbi.frame = 0;497itv->vbi.inserted_frame = 0;498memset(itv->vbi.sliced_mpeg_size,4990, sizeof(itv->vbi.sliced_mpeg_size));500break;501default:502return -EINVAL;503}504s->subtype = subtype;505s->buffers_stolen = 0;506507/* Clear Streamoff flags in case left from last capture */508clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);509510if (atomic_read(&itv->capturing) == 0) {511int digitizer;512513/* Always use frame based mode. Experiments have demonstrated that byte514stream based mode results in dropped frames and corruption. Not often,515but occasionally. Many thanks go to Leonard Orb who spent a lot of516effort and time trying to trace the cause of the drop outs. */517/* 1 frame per DMA */518/*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */519ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);520521/* Stuff from Windows, we don't know what it is */522ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);523/* According to the docs, this should be correct. However, this is524untested. I don't dare enable this without having tested it.525Only very few old cards actually have this hardware combination.526ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1,527((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0);528*/529ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);530ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);531ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);532ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);533534/* assign placeholder */535ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,5360, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);537538if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))539digitizer = 0xF1;540else if (itv->card->hw_all & IVTV_HW_SAA7114)541digitizer = 0xEF;542else /* cx25840 */543digitizer = 0x140;544545ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);546547/* Setup VBI */548if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {549ivtv_vbi_setup(itv);550}551552/* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */553ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);554itv->pgm_info_offset = data[0];555itv->pgm_info_num = data[1];556itv->pgm_info_write_idx = 0;557itv->pgm_info_read_idx = 0;558559IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",560itv->pgm_info_offset, itv->pgm_info_num);561562/* Setup API for Stream */563cx2341x_handler_setup(&itv->cxhdl);564565/* mute if capturing radio */566if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))567ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,5681 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));569}570571/* Vsync Setup */572if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {573/* event notification (on) */574ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);575ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);576}577578if (atomic_read(&itv->capturing) == 0) {579/* Clear all Pending Interrupts */580ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);581582clear_bit(IVTV_F_I_EOS, &itv->i_flags);583584cx2341x_handler_set_busy(&itv->cxhdl, 1);585586/* Initialize Digitizer for Capture */587/* Avoid tinny audio problem - ensure audio clocks are going */588v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);589/* Avoid unpredictable PCI bus hang - disable video clocks */590v4l2_subdev_call(itv->sd_video, video, s_stream, 0);591ivtv_msleep_timeout(300, 0);592ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);593v4l2_subdev_call(itv->sd_video, video, s_stream, 1);594}595596/* begin_capture */597if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))598{599IVTV_DEBUG_WARN( "Error starting capture!\n");600return -EINVAL;601}602603/* Start Passthrough */604if (enable_passthrough) {605ivtv_passthrough_mode(itv, 1);606}607608if (s->type == IVTV_ENC_STREAM_TYPE_VBI)609ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);610else611ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);612613/* you're live! sit back and await interrupts :) */614atomic_inc(&itv->capturing);615return 0;616}617618static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)619{620u32 data[CX2341X_MBOX_MAX_DATA];621struct ivtv *itv = s->itv;622int datatype;623u16 width;624u16 height;625626if (s->vdev == NULL)627return -EINVAL;628629IVTV_DEBUG_INFO("Setting some initial decoder settings\n");630631width = itv->cxhdl.width;632height = itv->cxhdl.height;633634/* set audio mode to left/stereo for dual/stereo mode. */635ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);636637/* set number of internal decoder buffers */638ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);639640/* prebuffering */641ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);642643/* extract from user packets */644ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);645itv->vbi.dec_start = data[0];646647IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",648itv->vbi.dec_start, data[1]);649650/* set decoder source settings */651/* Data type: 0 = mpeg from host,6521 = yuv from encoder,6532 = yuv_from_host */654switch (s->type) {655case IVTV_DEC_STREAM_TYPE_YUV:656if (itv->output_mode == OUT_PASSTHROUGH) {657datatype = 1;658} else {659/* Fake size to avoid switching video standard */660datatype = 2;661width = 720;662height = itv->is_out_50hz ? 576 : 480;663}664IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);665break;666case IVTV_DEC_STREAM_TYPE_MPG:667default:668datatype = 0;669break;670}671if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,672width, height, itv->cxhdl.audio_properties)) {673IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");674}675676/* Decoder sometimes dies here, so wait a moment */677ivtv_msleep_timeout(10, 0);678679/* Known failure point for firmware, so check */680return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");681}682683int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)684{685struct ivtv *itv = s->itv;686int rc;687688if (s->vdev == NULL)689return -EINVAL;690691if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))692return 0; /* already started */693694IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);695696rc = ivtv_setup_v4l2_decode_stream(s);697if (rc < 0) {698clear_bit(IVTV_F_S_STREAMING, &s->s_flags);699return rc;700}701702/* set dma size to 65536 bytes */703ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);704705/* Clear Streamoff */706clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);707708/* Zero out decoder counters */709writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);710writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);711writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);712writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);713writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);714writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);715writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);716writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);717718/* turn on notification of dual/stereo mode change */719ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);720721/* start playback */722ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);723724/* Let things settle before we actually start */725ivtv_msleep_timeout(10, 0);726727/* Clear the following Interrupt mask bits for decoding */728ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);729IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);730731/* you're live! sit back and await interrupts :) */732atomic_inc(&itv->decoding);733return 0;734}735736void ivtv_stop_all_captures(struct ivtv *itv)737{738int i;739740for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {741struct ivtv_stream *s = &itv->streams[i];742743if (s->vdev == NULL)744continue;745if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {746ivtv_stop_v4l2_encode_stream(s, 0);747}748}749}750751int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)752{753struct ivtv *itv = s->itv;754DECLARE_WAITQUEUE(wait, current);755int cap_type;756int stopmode;757758if (s->vdev == NULL)759return -EINVAL;760761/* This function assumes that you are allowed to stop the capture762and that we are actually capturing */763764IVTV_DEBUG_INFO("Stop Capture\n");765766if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)767return 0;768if (atomic_read(&itv->capturing) == 0)769return 0;770771switch (s->type) {772case IVTV_ENC_STREAM_TYPE_YUV:773cap_type = 1;774break;775case IVTV_ENC_STREAM_TYPE_PCM:776cap_type = 1;777break;778case IVTV_ENC_STREAM_TYPE_VBI:779cap_type = 1;780break;781case IVTV_ENC_STREAM_TYPE_MPG:782default:783cap_type = 0;784break;785}786787/* Stop Capture Mode */788if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {789stopmode = 0;790} else {791stopmode = 1;792}793794/* end_capture */795/* when: 0 = end of GOP 1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */796ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);797798if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {799if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {800/* only run these if we're shutting down the last cap */801unsigned long duration;802unsigned long then = jiffies;803804add_wait_queue(&itv->eos_waitq, &wait);805806set_current_state(TASK_INTERRUPTIBLE);807808/* wait 2s for EOS interrupt */809while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&810time_before(jiffies,811then + msecs_to_jiffies(2000))) {812schedule_timeout(msecs_to_jiffies(10));813}814815/* To convert jiffies to ms, we must multiply by 1000816* and divide by HZ. To avoid runtime division, we817* convert this to multiplication by 1000/HZ.818* Since integer division truncates, we get the best819* accuracy if we do a rounding calculation of the constant.820* Think of the case where HZ is 1024.821*/822duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);823824if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {825IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);826IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);827} else {828IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);829}830set_current_state(TASK_RUNNING);831remove_wait_queue(&itv->eos_waitq, &wait);832set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);833}834835/* Handle any pending interrupts */836ivtv_msleep_timeout(100, 0);837}838839atomic_dec(&itv->capturing);840841/* Clear capture and no-read bits */842clear_bit(IVTV_F_S_STREAMING, &s->s_flags);843844if (s->type == IVTV_ENC_STREAM_TYPE_VBI)845ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);846847if (atomic_read(&itv->capturing) > 0) {848return 0;849}850851cx2341x_handler_set_busy(&itv->cxhdl, 0);852853/* Set the following Interrupt mask bits for capture */854ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);855del_timer(&itv->dma_timer);856857/* event notification (off) */858if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {859/* type: 0 = refresh */860/* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */861ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);862ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);863}864865/* Raw-passthrough is implied on start. Make sure it's stopped so866the encoder will re-initialize when next started */867ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);868869wake_up(&s->waitq);870871return 0;872}873874int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)875{876static const struct v4l2_event ev = {877.type = V4L2_EVENT_EOS,878};879struct ivtv *itv = s->itv;880881if (s->vdev == NULL)882return -EINVAL;883884if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)885return -EINVAL;886887if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))888return 0;889890IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);891892/* Stop Decoder */893if (!(flags & VIDEO_CMD_STOP_IMMEDIATELY) || pts) {894u32 tmp = 0;895896/* Wait until the decoder is no longer running */897if (pts) {898ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,8990, (u32)(pts & 0xffffffff), (u32)(pts >> 32));900}901while (1) {902u32 data[CX2341X_MBOX_MAX_DATA];903ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);904if (s->q_full.buffers + s->q_dma.buffers == 0) {905if (tmp == data[3])906break;907tmp = data[3];908}909if (ivtv_msleep_timeout(100, 1))910break;911}912}913ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & VIDEO_CMD_STOP_TO_BLACK, 0, 0);914915/* turn off notification of dual/stereo mode change */916ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);917918ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);919del_timer(&itv->dma_timer);920921clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);922clear_bit(IVTV_F_S_STREAMING, &s->s_flags);923ivtv_flush_queues(s);924925/* decoder needs time to settle */926ivtv_msleep_timeout(40, 0);927928/* decrement decoding */929atomic_dec(&itv->decoding);930931set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);932wake_up(&itv->event_waitq);933v4l2_event_queue(s->vdev, &ev);934935/* wake up wait queues */936wake_up(&s->waitq);937938return 0;939}940941int ivtv_passthrough_mode(struct ivtv *itv, int enable)942{943struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];944struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];945946if (yuv_stream->vdev == NULL || dec_stream->vdev == NULL)947return -EINVAL;948949IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");950951/* Prevent others from starting/stopping streams while we952initiate/terminate passthrough mode */953if (enable) {954if (itv->output_mode == OUT_PASSTHROUGH) {955return 0;956}957if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)958return -EBUSY;959960/* Fully initialize stream, and then unflag init */961set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);962set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);963964/* Setup YUV Decoder */965ivtv_setup_v4l2_decode_stream(dec_stream);966967/* Start Decoder */968ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);969atomic_inc(&itv->decoding);970971/* Setup capture if not already done */972if (atomic_read(&itv->capturing) == 0) {973cx2341x_handler_setup(&itv->cxhdl);974cx2341x_handler_set_busy(&itv->cxhdl, 1);975}976977/* Start Passthrough Mode */978ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);979atomic_inc(&itv->capturing);980return 0;981}982983if (itv->output_mode != OUT_PASSTHROUGH)984return 0;985986/* Stop Passthrough Mode */987ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);988ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);989990atomic_dec(&itv->capturing);991atomic_dec(&itv->decoding);992clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);993clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);994itv->output_mode = OUT_NONE;995if (atomic_read(&itv->capturing) == 0)996cx2341x_handler_set_busy(&itv->cxhdl, 0);997998return 0;999}100010011002