0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include "ivtv-driver.h"
0037 #include "ivtv-fileops.h"
0038 #include "ivtv-queue.h"
0039 #include "ivtv-mailbox.h"
0040 #include "ivtv-ioctl.h"
0041 #include "ivtv-irq.h"
0042 #include "ivtv-yuv.h"
0043 #include "ivtv-cards.h"
0044 #include "ivtv-streams.h"
0045 #include "ivtv-firmware.h"
0046 #include <media/v4l2-event.h>
0047
0048 static const struct v4l2_file_operations ivtv_v4l2_enc_fops = {
0049 .owner = THIS_MODULE,
0050 .read = ivtv_v4l2_read,
0051 .write = ivtv_v4l2_write,
0052 .open = ivtv_v4l2_open,
0053 .unlocked_ioctl = video_ioctl2,
0054 #ifdef CONFIG_COMPAT
0055 .compat_ioctl32 = video_ioctl2,
0056 #endif
0057 .release = ivtv_v4l2_close,
0058 .poll = ivtv_v4l2_enc_poll,
0059 };
0060
0061 static const struct v4l2_file_operations ivtv_v4l2_dec_fops = {
0062 .owner = THIS_MODULE,
0063 .read = ivtv_v4l2_read,
0064 .write = ivtv_v4l2_write,
0065 .open = ivtv_v4l2_open,
0066 .unlocked_ioctl = video_ioctl2,
0067 #ifdef CONFIG_COMPAT
0068 .compat_ioctl32 = video_ioctl2,
0069 #endif
0070 .release = ivtv_v4l2_close,
0071 .poll = ivtv_v4l2_dec_poll,
0072 };
0073
0074 static const struct v4l2_file_operations ivtv_v4l2_radio_fops = {
0075 .owner = THIS_MODULE,
0076 .open = ivtv_v4l2_open,
0077 .unlocked_ioctl = video_ioctl2,
0078 #ifdef CONFIG_COMPAT
0079 .compat_ioctl32 = video_ioctl2,
0080 #endif
0081 .release = ivtv_v4l2_close,
0082 .poll = ivtv_v4l2_enc_poll,
0083 };
0084
0085 #define IVTV_V4L2_DEC_MPG_OFFSET 16
0086 #define IVTV_V4L2_ENC_PCM_OFFSET 24
0087 #define IVTV_V4L2_ENC_YUV_OFFSET 32
0088 #define IVTV_V4L2_DEC_YUV_OFFSET 48
0089 #define IVTV_V4L2_DEC_VBI_OFFSET 8
0090 #define IVTV_V4L2_DEC_VOUT_OFFSET 16
0091
0092 static struct {
0093 const char *name;
0094 int vfl_type;
0095 int num_offset;
0096 int dma, pio;
0097 u32 v4l2_caps;
0098 const struct v4l2_file_operations *fops;
0099 } ivtv_stream_info[] = {
0100 {
0101 "encoder MPG",
0102 VFL_TYPE_VIDEO, 0,
0103 DMA_FROM_DEVICE, 0,
0104 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
0105 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0106 &ivtv_v4l2_enc_fops
0107 },
0108 {
0109 "encoder YUV",
0110 VFL_TYPE_VIDEO, IVTV_V4L2_ENC_YUV_OFFSET,
0111 DMA_FROM_DEVICE, 0,
0112 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
0113 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0114 &ivtv_v4l2_enc_fops
0115 },
0116 {
0117 "encoder VBI",
0118 VFL_TYPE_VBI, 0,
0119 DMA_FROM_DEVICE, 0,
0120 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER |
0121 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0122 &ivtv_v4l2_enc_fops
0123 },
0124 {
0125 "encoder PCM",
0126 VFL_TYPE_VIDEO, IVTV_V4L2_ENC_PCM_OFFSET,
0127 DMA_FROM_DEVICE, 0,
0128 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0129 &ivtv_v4l2_enc_fops
0130 },
0131 {
0132 "encoder radio",
0133 VFL_TYPE_RADIO, 0,
0134 DMA_NONE, 1,
0135 V4L2_CAP_RADIO | V4L2_CAP_TUNER,
0136 &ivtv_v4l2_radio_fops
0137 },
0138 {
0139 "decoder MPG",
0140 VFL_TYPE_VIDEO, IVTV_V4L2_DEC_MPG_OFFSET,
0141 DMA_TO_DEVICE, 0,
0142 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0143 &ivtv_v4l2_dec_fops
0144 },
0145 {
0146 "decoder VBI",
0147 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET,
0148 DMA_NONE, 1,
0149 V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE,
0150 &ivtv_v4l2_enc_fops
0151 },
0152 {
0153 "decoder VOUT",
0154 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET,
0155 DMA_NONE, 1,
0156 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0157 &ivtv_v4l2_dec_fops
0158 },
0159 {
0160 "decoder YUV",
0161 VFL_TYPE_VIDEO, IVTV_V4L2_DEC_YUV_OFFSET,
0162 DMA_TO_DEVICE, 0,
0163 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
0164 &ivtv_v4l2_dec_fops
0165 }
0166 };
0167
0168 static void ivtv_stream_init(struct ivtv *itv, int type)
0169 {
0170 struct ivtv_stream *s = &itv->streams[type];
0171
0172
0173 memset(s, 0, sizeof(*s));
0174
0175
0176 s->itv = itv;
0177 s->type = type;
0178 s->name = ivtv_stream_info[type].name;
0179 s->vdev.device_caps = ivtv_stream_info[type].v4l2_caps;
0180
0181 if (ivtv_stream_info[type].pio)
0182 s->dma = DMA_NONE;
0183 else
0184 s->dma = ivtv_stream_info[type].dma;
0185 s->buf_size = itv->stream_buf_size[type];
0186 if (s->buf_size)
0187 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size;
0188 spin_lock_init(&s->qlock);
0189 init_waitqueue_head(&s->waitq);
0190 s->sg_handle = IVTV_DMA_UNMAPPED;
0191 ivtv_queue_init(&s->q_free);
0192 ivtv_queue_init(&s->q_full);
0193 ivtv_queue_init(&s->q_dma);
0194 ivtv_queue_init(&s->q_predma);
0195 ivtv_queue_init(&s->q_io);
0196 }
0197
0198 static int ivtv_prep_dev(struct ivtv *itv, int type)
0199 {
0200 struct ivtv_stream *s = &itv->streams[type];
0201 int num_offset = ivtv_stream_info[type].num_offset;
0202 int num = itv->instance + ivtv_first_minor + num_offset;
0203
0204
0205
0206
0207 s->vdev.v4l2_dev = NULL;
0208 s->itv = itv;
0209 s->type = type;
0210 s->name = ivtv_stream_info[type].name;
0211
0212
0213 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO))
0214 return 0;
0215 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
0216 return 0;
0217
0218
0219
0220 if (ivtv_stream_info[type].dma != DMA_NONE &&
0221 itv->options.kilobytes[type] == 0) {
0222 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name);
0223 return 0;
0224 }
0225
0226 ivtv_stream_init(itv, type);
0227
0228 snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s",
0229 itv->v4l2_dev.name, s->name);
0230
0231 s->vdev.num = num;
0232 s->vdev.v4l2_dev = &itv->v4l2_dev;
0233 if (ivtv_stream_info[type].v4l2_caps &
0234 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT))
0235 s->vdev.vfl_dir = VFL_DIR_TX;
0236 s->vdev.fops = ivtv_stream_info[type].fops;
0237 s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler;
0238 s->vdev.release = video_device_release_empty;
0239 s->vdev.tvnorms = V4L2_STD_ALL;
0240 s->vdev.lock = &itv->serialize_lock;
0241 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
0242 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO);
0243 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO);
0244 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO);
0245 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT);
0246 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT);
0247 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT);
0248 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY);
0249 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY);
0250 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER);
0251 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER);
0252 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD);
0253 }
0254 ivtv_set_funcs(&s->vdev);
0255 return 0;
0256 }
0257
0258
0259 int ivtv_streams_setup(struct ivtv *itv)
0260 {
0261 int type;
0262
0263
0264 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
0265
0266 if (ivtv_prep_dev(itv, type))
0267 break;
0268
0269 if (itv->streams[type].vdev.v4l2_dev == NULL)
0270 continue;
0271
0272
0273 if (ivtv_stream_alloc(&itv->streams[type]))
0274 break;
0275 }
0276 if (type == IVTV_MAX_STREAMS)
0277 return 0;
0278
0279
0280 ivtv_streams_cleanup(itv);
0281 return -ENOMEM;
0282 }
0283
0284 static int ivtv_reg_dev(struct ivtv *itv, int type)
0285 {
0286 struct ivtv_stream *s = &itv->streams[type];
0287 int vfl_type = ivtv_stream_info[type].vfl_type;
0288 const char *name;
0289 int num;
0290
0291 if (s->vdev.v4l2_dev == NULL)
0292 return 0;
0293
0294 num = s->vdev.num;
0295
0296 if (type != IVTV_ENC_STREAM_TYPE_MPG) {
0297 struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG];
0298
0299 if (s_mpg->vdev.v4l2_dev)
0300 num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset;
0301 }
0302 if (itv->osd_video_pbase && (type == IVTV_DEC_STREAM_TYPE_YUV ||
0303 type == IVTV_DEC_STREAM_TYPE_MPG)) {
0304 s->vdev.device_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
0305 itv->v4l2_cap |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
0306 }
0307 video_set_drvdata(&s->vdev, s);
0308
0309
0310 if (video_register_device_no_warn(&s->vdev, vfl_type, num)) {
0311 IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
0312 s->name, num);
0313 return -ENOMEM;
0314 }
0315 name = video_device_node_name(&s->vdev);
0316
0317 switch (vfl_type) {
0318 case VFL_TYPE_VIDEO:
0319 IVTV_INFO("Registered device %s for %s (%d kB)\n",
0320 name, s->name, itv->options.kilobytes[type]);
0321 break;
0322 case VFL_TYPE_RADIO:
0323 IVTV_INFO("Registered device %s for %s\n",
0324 name, s->name);
0325 break;
0326 case VFL_TYPE_VBI:
0327 if (itv->options.kilobytes[type])
0328 IVTV_INFO("Registered device %s for %s (%d kB)\n",
0329 name, s->name, itv->options.kilobytes[type]);
0330 else
0331 IVTV_INFO("Registered device %s for %s\n",
0332 name, s->name);
0333 break;
0334 }
0335 return 0;
0336 }
0337
0338
0339 int ivtv_streams_register(struct ivtv *itv)
0340 {
0341 int type;
0342 int err = 0;
0343
0344
0345 for (type = 0; type < IVTV_MAX_STREAMS; type++)
0346 err |= ivtv_reg_dev(itv, type);
0347
0348 if (err == 0)
0349 return 0;
0350
0351
0352 ivtv_streams_cleanup(itv);
0353 return -ENOMEM;
0354 }
0355
0356
0357 void ivtv_streams_cleanup(struct ivtv *itv)
0358 {
0359 int type;
0360
0361
0362 for (type = 0; type < IVTV_MAX_STREAMS; type++) {
0363 struct video_device *vdev = &itv->streams[type].vdev;
0364
0365 if (vdev->v4l2_dev == NULL)
0366 continue;
0367
0368 video_unregister_device(vdev);
0369 ivtv_stream_free(&itv->streams[type]);
0370 itv->streams[type].vdev.v4l2_dev = NULL;
0371 }
0372 }
0373
0374 static void ivtv_vbi_setup(struct ivtv *itv)
0375 {
0376 int raw = ivtv_raw_vbi(itv);
0377 u32 data[CX2341X_MBOX_MAX_DATA];
0378 int lines;
0379 int i;
0380
0381
0382 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0);
0383
0384
0385 if (raw)
0386 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi);
0387 else
0388 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced);
0389
0390
0391
0392
0393
0394
0395 if (raw) {
0396 lines = itv->vbi.count * 2;
0397 } else {
0398 lines = itv->is_60hz ? 24 : 38;
0399 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840))
0400 lines += 2;
0401 }
0402
0403 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
0404
0405
0406
0407 data[0] = raw | 0x02 | (0xbd << 8);
0408
0409
0410 data[1] = 1;
0411
0412 data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size);
0413
0414
0415
0416
0417
0418
0419
0420 if (itv->hw_flags & IVTV_HW_CX25840) {
0421
0422 if (raw) {
0423 data[3] = 0x20602060;
0424 data[4] = 0x30703070;
0425 } else {
0426 data[3] = 0xB0F0B0F0;
0427 data[4] = 0xA0E0A0E0;
0428 }
0429
0430 data[5] = lines;
0431
0432 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size);
0433 } else {
0434
0435 if (raw) {
0436 data[3] = 0x25256262;
0437 data[4] = 0x387F7F7F;
0438 } else {
0439 data[3] = 0xABABECEC;
0440 data[4] = 0xB6F1F1F1;
0441 }
0442
0443 data[5] = lines;
0444
0445 data[6] = itv->vbi.enc_size / lines;
0446 }
0447
0448 IVTV_DEBUG_INFO(
0449 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n",
0450 data[0], data[1], data[2], data[5], data[6]);
0451
0452 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data);
0453
0454
0455 itv->vbi.enc_start = data[2];
0456 itv->vbi.fpi = data[0];
0457 if (!itv->vbi.fpi)
0458 itv->vbi.fpi = 1;
0459
0460 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n",
0461 itv->vbi.enc_start, data[1], itv->vbi.fpi);
0462
0463
0464
0465 for (i = 2; i <= 24; i++) {
0466 int valid;
0467
0468 if (itv->is_60hz) {
0469 valid = i >= 10 && i < 22;
0470 } else {
0471 valid = i >= 6 && i < 24;
0472 }
0473 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1,
0474 valid, 0 , 0, 0);
0475 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000,
0476 valid, 0, 0, 0);
0477 }
0478
0479
0480
0481
0482
0483
0484
0485 }
0486
0487 int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s)
0488 {
0489 u32 data[CX2341X_MBOX_MAX_DATA];
0490 struct ivtv *itv = s->itv;
0491 int captype = 0, subtype = 0;
0492 int enable_passthrough = 0;
0493
0494 if (s->vdev.v4l2_dev == NULL)
0495 return -EINVAL;
0496
0497 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name);
0498
0499 switch (s->type) {
0500 case IVTV_ENC_STREAM_TYPE_MPG:
0501 captype = 0;
0502 subtype = 3;
0503
0504
0505 if (itv->output_mode == OUT_PASSTHROUGH) {
0506 ivtv_passthrough_mode(itv, 0);
0507 enable_passthrough = 1;
0508 }
0509 itv->mpg_data_received = itv->vbi_data_inserted = 0;
0510 itv->dualwatch_jiffies = jiffies;
0511 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
0512 itv->search_pack_header = 0;
0513 break;
0514
0515 case IVTV_ENC_STREAM_TYPE_YUV:
0516 if (itv->output_mode == OUT_PASSTHROUGH) {
0517 captype = 2;
0518 subtype = 11;
0519 break;
0520 }
0521 captype = 1;
0522 subtype = 1;
0523 break;
0524 case IVTV_ENC_STREAM_TYPE_PCM:
0525 captype = 1;
0526 subtype = 2;
0527 break;
0528 case IVTV_ENC_STREAM_TYPE_VBI:
0529 captype = 1;
0530 subtype = 4;
0531
0532 itv->vbi.frame = 0;
0533 itv->vbi.inserted_frame = 0;
0534 memset(itv->vbi.sliced_mpeg_size,
0535 0, sizeof(itv->vbi.sliced_mpeg_size));
0536 break;
0537 default:
0538 return -EINVAL;
0539 }
0540 s->subtype = subtype;
0541 s->buffers_stolen = 0;
0542
0543
0544 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0545
0546 if (atomic_read(&itv->capturing) == 0) {
0547 int digitizer;
0548
0549
0550
0551
0552
0553
0554
0555 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1);
0556
0557
0558 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0);
0559
0560
0561
0562
0563
0564
0565 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415);
0566 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0);
0567 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1);
0568 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
0569
0570
0571 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12,
0572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
0573
0574 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X))
0575 digitizer = 0xF1;
0576 else if (itv->card->hw_all & IVTV_HW_SAA7114)
0577 digitizer = 0xEF;
0578 else
0579 digitizer = 0x140;
0580
0581 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer);
0582
0583
0584 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) {
0585 ivtv_vbi_setup(itv);
0586 }
0587
0588
0589 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400);
0590 itv->pgm_info_offset = data[0];
0591 itv->pgm_info_num = data[1];
0592 itv->pgm_info_write_idx = 0;
0593 itv->pgm_info_read_idx = 0;
0594
0595 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n",
0596 itv->pgm_info_offset, itv->pgm_info_num);
0597
0598
0599 cx2341x_handler_setup(&itv->cxhdl);
0600
0601
0602 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags))
0603 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
0604 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
0605 }
0606
0607
0608 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
0609
0610 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1);
0611 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
0612 }
0613
0614 if (atomic_read(&itv->capturing) == 0) {
0615
0616 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
0617
0618 clear_bit(IVTV_F_I_EOS, &itv->i_flags);
0619
0620 cx2341x_handler_set_busy(&itv->cxhdl, 1);
0621
0622
0623
0624 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1);
0625
0626 v4l2_subdev_call(itv->sd_video, video, s_stream, 0);
0627 ivtv_msleep_timeout(300, 0);
0628 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
0629 v4l2_subdev_call(itv->sd_video, video, s_stream, 1);
0630 }
0631
0632
0633 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype))
0634 {
0635 IVTV_DEBUG_WARN( "Error starting capture!\n");
0636 return -EINVAL;
0637 }
0638
0639
0640 if (enable_passthrough) {
0641 ivtv_passthrough_mode(itv, 1);
0642 }
0643
0644 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
0645 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
0646 else
0647 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
0648
0649
0650 atomic_inc(&itv->capturing);
0651 return 0;
0652 }
0653 EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream);
0654
0655 static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s)
0656 {
0657 u32 data[CX2341X_MBOX_MAX_DATA];
0658 struct ivtv *itv = s->itv;
0659 int datatype;
0660 u16 width;
0661 u16 height;
0662
0663 if (s->vdev.v4l2_dev == NULL)
0664 return -EINVAL;
0665
0666 IVTV_DEBUG_INFO("Setting some initial decoder settings\n");
0667
0668 width = itv->cxhdl.width;
0669 height = itv->cxhdl.height;
0670
0671
0672 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
0673
0674
0675 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0);
0676
0677
0678 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1);
0679
0680
0681 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1);
0682 itv->vbi.dec_start = data[0];
0683
0684 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n",
0685 itv->vbi.dec_start, data[1]);
0686
0687
0688
0689
0690
0691 switch (s->type) {
0692 case IVTV_DEC_STREAM_TYPE_YUV:
0693 if (itv->output_mode == OUT_PASSTHROUGH) {
0694 datatype = 1;
0695 } else {
0696
0697 datatype = 2;
0698 width = 720;
0699 height = itv->is_out_50hz ? 576 : 480;
0700 }
0701 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype);
0702 break;
0703 case IVTV_DEC_STREAM_TYPE_MPG:
0704 default:
0705 datatype = 0;
0706 break;
0707 }
0708 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype,
0709 width, height, itv->cxhdl.audio_properties)) {
0710 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n");
0711 }
0712
0713
0714 ivtv_msleep_timeout(10, 0);
0715
0716
0717 return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream");
0718 }
0719
0720 int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset)
0721 {
0722 struct ivtv *itv = s->itv;
0723 int rc;
0724
0725 if (s->vdev.v4l2_dev == NULL)
0726 return -EINVAL;
0727
0728 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags))
0729 return 0;
0730
0731 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset);
0732
0733 rc = ivtv_setup_v4l2_decode_stream(s);
0734 if (rc < 0) {
0735 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0736 return rc;
0737 }
0738
0739
0740 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536);
0741
0742
0743 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0744
0745
0746 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]);
0747 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]);
0748 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]);
0749 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]);
0750 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]);
0751 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]);
0752 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]);
0753 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]);
0754
0755
0756 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
0757
0758
0759 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0);
0760
0761
0762 ivtv_msleep_timeout(10, 0);
0763
0764
0765 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
0766 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask);
0767
0768
0769 atomic_inc(&itv->decoding);
0770 return 0;
0771 }
0772
0773 void ivtv_stop_all_captures(struct ivtv *itv)
0774 {
0775 int i;
0776
0777 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) {
0778 struct ivtv_stream *s = &itv->streams[i];
0779
0780 if (s->vdev.v4l2_dev == NULL)
0781 continue;
0782 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
0783 ivtv_stop_v4l2_encode_stream(s, 0);
0784 }
0785 }
0786 }
0787
0788 int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end)
0789 {
0790 struct ivtv *itv = s->itv;
0791 DECLARE_WAITQUEUE(wait, current);
0792 int cap_type;
0793 int stopmode;
0794
0795 if (s->vdev.v4l2_dev == NULL)
0796 return -EINVAL;
0797
0798
0799
0800
0801 IVTV_DEBUG_INFO("Stop Capture\n");
0802
0803 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT)
0804 return 0;
0805 if (atomic_read(&itv->capturing) == 0)
0806 return 0;
0807
0808 switch (s->type) {
0809 case IVTV_ENC_STREAM_TYPE_YUV:
0810 cap_type = 1;
0811 break;
0812 case IVTV_ENC_STREAM_TYPE_PCM:
0813 cap_type = 1;
0814 break;
0815 case IVTV_ENC_STREAM_TYPE_VBI:
0816 cap_type = 1;
0817 break;
0818 case IVTV_ENC_STREAM_TYPE_MPG:
0819 default:
0820 cap_type = 0;
0821 break;
0822 }
0823
0824
0825 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
0826 stopmode = 0;
0827 } else {
0828 stopmode = 1;
0829 }
0830
0831
0832
0833 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype);
0834
0835 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) {
0836 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) {
0837
0838 unsigned long duration;
0839 unsigned long then = jiffies;
0840
0841 add_wait_queue(&itv->eos_waitq, &wait);
0842
0843 set_current_state(TASK_INTERRUPTIBLE);
0844
0845
0846 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) &&
0847 time_before(jiffies,
0848 then + msecs_to_jiffies(2000))) {
0849 schedule_timeout(msecs_to_jiffies(10));
0850 }
0851
0852
0853
0854
0855
0856
0857
0858
0859 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then);
0860
0861 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) {
0862 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name);
0863 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration);
0864 } else {
0865 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration);
0866 }
0867 set_current_state(TASK_RUNNING);
0868 remove_wait_queue(&itv->eos_waitq, &wait);
0869 set_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0870 }
0871
0872
0873 ivtv_msleep_timeout(100, 0);
0874 }
0875
0876 atomic_dec(&itv->capturing);
0877
0878
0879 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0880
0881 if (s->type == IVTV_ENC_STREAM_TYPE_VBI)
0882 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP);
0883
0884 if (atomic_read(&itv->capturing) > 0) {
0885 return 0;
0886 }
0887
0888 cx2341x_handler_set_busy(&itv->cxhdl, 0);
0889
0890
0891 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE);
0892 del_timer(&itv->dma_timer);
0893
0894
0895 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) {
0896
0897
0898 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1);
0899 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST);
0900 }
0901
0902
0903
0904 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7);
0905
0906 wake_up(&s->waitq);
0907
0908 return 0;
0909 }
0910 EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream);
0911
0912 int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts)
0913 {
0914 static const struct v4l2_event ev = {
0915 .type = V4L2_EVENT_EOS,
0916 };
0917 struct ivtv *itv = s->itv;
0918
0919 if (s->vdev.v4l2_dev == NULL)
0920 return -EINVAL;
0921
0922 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG)
0923 return -EINVAL;
0924
0925 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags))
0926 return 0;
0927
0928 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags);
0929
0930
0931 if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) {
0932 u32 tmp = 0;
0933
0934
0935 if (pts) {
0936 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3,
0937 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32));
0938 }
0939 while (1) {
0940 u32 data[CX2341X_MBOX_MAX_DATA];
0941 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0);
0942 if (s->q_full.buffers + s->q_dma.buffers == 0) {
0943 if (tmp == data[3])
0944 break;
0945 tmp = data[3];
0946 }
0947 if (ivtv_msleep_timeout(100, 1))
0948 break;
0949 }
0950 }
0951 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0);
0952
0953
0954 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1);
0955
0956 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE);
0957 del_timer(&itv->dma_timer);
0958
0959 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
0960 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0961 ivtv_flush_queues(s);
0962
0963
0964 ivtv_msleep_timeout(40, 0);
0965
0966
0967 atomic_dec(&itv->decoding);
0968
0969 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags);
0970 wake_up(&itv->event_waitq);
0971 v4l2_event_queue(&s->vdev, &ev);
0972
0973
0974 wake_up(&s->waitq);
0975
0976 return 0;
0977 }
0978
0979 int ivtv_passthrough_mode(struct ivtv *itv, int enable)
0980 {
0981 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV];
0982 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV];
0983
0984 if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL)
0985 return -EINVAL;
0986
0987 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n");
0988
0989
0990
0991 if (enable) {
0992 if (itv->output_mode == OUT_PASSTHROUGH) {
0993 return 0;
0994 }
0995 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH)
0996 return -EBUSY;
0997
0998
0999 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1000 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1001
1002
1003 ivtv_setup_v4l2_decode_stream(dec_stream);
1004
1005
1006 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1);
1007 atomic_inc(&itv->decoding);
1008
1009
1010 if (atomic_read(&itv->capturing) == 0) {
1011 cx2341x_handler_setup(&itv->cxhdl);
1012 cx2341x_handler_set_busy(&itv->cxhdl, 1);
1013 }
1014
1015
1016 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11);
1017 atomic_inc(&itv->capturing);
1018 return 0;
1019 }
1020
1021 if (itv->output_mode != OUT_PASSTHROUGH)
1022 return 0;
1023
1024
1025 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11);
1026 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0);
1027
1028 atomic_dec(&itv->capturing);
1029 atomic_dec(&itv->decoding);
1030 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags);
1031 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags);
1032 itv->output_mode = OUT_NONE;
1033 if (atomic_read(&itv->capturing) == 0)
1034 cx2341x_handler_set_busy(&itv->cxhdl, 0);
1035
1036 return 0;
1037 }