1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 init/start/stop/exit stream functions 4 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com> 5 Copyright (C) 2004 Chris Kennedy <c@groovy.org> 6 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@kernel.org> 7 */ 8 9 /* License: GPL 10 * Author: Kevin Thayer <nufan_wfk at yahoo dot com> 11 * 12 * This file will hold API related functions, both internal (firmware api) 13 * and external (v4l2, etc) 14 * 15 * ----- 16 * MPG600/MPG160 support by T.Adachi <tadachi@tadachi-net.com> 17 * and Takeru KOMORIYA<komoriya@paken.org> 18 * 19 * AVerMedia M179 GPIO info by Chris Pinkham <cpinkham@bc2va.org> 20 * using information provided by Jiun-Kuei Jung @ AVerMedia. 21 */ 22 23 #include "ivtv-driver.h" 24 #include "ivtv-fileops.h" 25 #include "ivtv-queue.h" 26 #include "ivtv-mailbox.h" 27 #include "ivtv-ioctl.h" 28 #include "ivtv-irq.h" 29 #include "ivtv-yuv.h" 30 #include "ivtv-cards.h" 31 #include "ivtv-streams.h" 32 #include "ivtv-firmware.h" 33 #include <media/v4l2-event.h> 34 35 static const struct v4l2_file_operations ivtv_v4l2_enc_fops = { 36 .owner = THIS_MODULE, 37 .read = ivtv_v4l2_read, 38 .write = ivtv_v4l2_write, 39 .open = ivtv_v4l2_open, 40 .unlocked_ioctl = video_ioctl2, 41 #ifdef CONFIG_COMPAT 42 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */ 43 #endif 44 .release = ivtv_v4l2_close, 45 .poll = ivtv_v4l2_enc_poll, 46 }; 47 48 static const struct v4l2_file_operations ivtv_v4l2_dec_fops = { 49 .owner = THIS_MODULE, 50 .read = ivtv_v4l2_read, 51 .write = ivtv_v4l2_write, 52 .open = ivtv_v4l2_open, 53 .unlocked_ioctl = video_ioctl2, 54 #ifdef CONFIG_COMPAT 55 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */ 56 #endif 57 .release = ivtv_v4l2_close, 58 .poll = ivtv_v4l2_dec_poll, 59 }; 60 61 static const struct v4l2_file_operations ivtv_v4l2_radio_fops = { 62 .owner = THIS_MODULE, 63 .open = ivtv_v4l2_open, 64 .unlocked_ioctl = video_ioctl2, 65 #ifdef CONFIG_COMPAT 66 .compat_ioctl32 = video_ioctl2, /* for ivtv_default() */ 67 #endif 68 .release = ivtv_v4l2_close, 69 .poll = ivtv_v4l2_enc_poll, 70 }; 71 72 #define IVTV_V4L2_DEC_MPG_OFFSET 16 /* offset from 0 to register decoder mpg v4l2 minors on */ 73 #define IVTV_V4L2_ENC_PCM_OFFSET 24 /* offset from 0 to register pcm v4l2 minors on */ 74 #define IVTV_V4L2_ENC_YUV_OFFSET 32 /* offset from 0 to register yuv v4l2 minors on */ 75 #define IVTV_V4L2_DEC_YUV_OFFSET 48 /* offset from 0 to register decoder yuv v4l2 minors on */ 76 #define IVTV_V4L2_DEC_VBI_OFFSET 8 /* offset from 0 to register decoder vbi input v4l2 minors on */ 77 #define IVTV_V4L2_DEC_VOUT_OFFSET 16 /* offset from 0 to register vbi output v4l2 minors on */ 78 79 static struct { 80 const char *name; 81 int vfl_type; 82 int num_offset; 83 int dma, pio; 84 u32 v4l2_caps; 85 const struct v4l2_file_operations *fops; 86 } ivtv_stream_info[] = { 87 { /* IVTV_ENC_STREAM_TYPE_MPG */ 88 "encoder MPG", 89 VFL_TYPE_VIDEO, 0, 90 DMA_FROM_DEVICE, 0, 91 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER | 92 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 93 &ivtv_v4l2_enc_fops 94 }, 95 { /* IVTV_ENC_STREAM_TYPE_YUV */ 96 "encoder YUV", 97 VFL_TYPE_VIDEO, IVTV_V4L2_ENC_YUV_OFFSET, 98 DMA_FROM_DEVICE, 0, 99 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER | 100 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 101 &ivtv_v4l2_enc_fops 102 }, 103 { /* IVTV_ENC_STREAM_TYPE_VBI */ 104 "encoder VBI", 105 VFL_TYPE_VBI, 0, 106 DMA_FROM_DEVICE, 0, 107 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_TUNER | 108 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 109 &ivtv_v4l2_enc_fops 110 }, 111 { /* IVTV_ENC_STREAM_TYPE_PCM */ 112 "encoder PCM", 113 VFL_TYPE_VIDEO, IVTV_V4L2_ENC_PCM_OFFSET, 114 DMA_FROM_DEVICE, 0, 115 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 116 &ivtv_v4l2_enc_fops 117 }, 118 { /* IVTV_ENC_STREAM_TYPE_RAD */ 119 "encoder radio", 120 VFL_TYPE_RADIO, 0, 121 DMA_NONE, 1, 122 V4L2_CAP_RADIO | V4L2_CAP_TUNER, 123 &ivtv_v4l2_radio_fops 124 }, 125 { /* IVTV_DEC_STREAM_TYPE_MPG */ 126 "decoder MPG", 127 VFL_TYPE_VIDEO, IVTV_V4L2_DEC_MPG_OFFSET, 128 DMA_TO_DEVICE, 0, 129 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 130 &ivtv_v4l2_dec_fops 131 }, 132 { /* IVTV_DEC_STREAM_TYPE_VBI */ 133 "decoder VBI", 134 VFL_TYPE_VBI, IVTV_V4L2_DEC_VBI_OFFSET, 135 DMA_NONE, 1, 136 V4L2_CAP_SLICED_VBI_CAPTURE | V4L2_CAP_READWRITE, 137 &ivtv_v4l2_enc_fops 138 }, 139 { /* IVTV_DEC_STREAM_TYPE_VOUT */ 140 "decoder VOUT", 141 VFL_TYPE_VBI, IVTV_V4L2_DEC_VOUT_OFFSET, 142 DMA_NONE, 1, 143 V4L2_CAP_SLICED_VBI_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 144 &ivtv_v4l2_dec_fops 145 }, 146 { /* IVTV_DEC_STREAM_TYPE_YUV */ 147 "decoder YUV", 148 VFL_TYPE_VIDEO, IVTV_V4L2_DEC_YUV_OFFSET, 149 DMA_TO_DEVICE, 0, 150 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, 151 &ivtv_v4l2_dec_fops 152 } 153 }; 154 155 static void ivtv_stream_init(struct ivtv *itv, int type) 156 { 157 struct ivtv_stream *s = &itv->streams[type]; 158 159 /* we need to keep vdev, so restore it afterwards */ 160 memset(s, 0, sizeof(*s)); 161 162 /* initialize ivtv_stream fields */ 163 s->itv = itv; 164 s->type = type; 165 s->name = ivtv_stream_info[type].name; 166 s->vdev.device_caps = ivtv_stream_info[type].v4l2_caps; 167 168 if (ivtv_stream_info[type].pio) 169 s->dma = DMA_NONE; 170 else 171 s->dma = ivtv_stream_info[type].dma; 172 s->buf_size = itv->stream_buf_size[type]; 173 if (s->buf_size) 174 s->buffers = (itv->options.kilobytes[type] * 1024 + s->buf_size - 1) / s->buf_size; 175 spin_lock_init(&s->qlock); 176 init_waitqueue_head(&s->waitq); 177 s->sg_handle = IVTV_DMA_UNMAPPED; 178 ivtv_queue_init(&s->q_free); 179 ivtv_queue_init(&s->q_full); 180 ivtv_queue_init(&s->q_dma); 181 ivtv_queue_init(&s->q_predma); 182 ivtv_queue_init(&s->q_io); 183 } 184 185 static int ivtv_prep_dev(struct ivtv *itv, int type) 186 { 187 struct ivtv_stream *s = &itv->streams[type]; 188 int num_offset = ivtv_stream_info[type].num_offset; 189 int num = itv->instance + ivtv_first_minor + num_offset; 190 191 /* These four fields are always initialized. If vdev.v4l2_dev == NULL, then 192 this stream is not in use. In that case no other fields but these 193 four can be used. */ 194 s->vdev.v4l2_dev = NULL; 195 s->itv = itv; 196 s->type = type; 197 s->name = ivtv_stream_info[type].name; 198 199 /* Check whether the radio is supported */ 200 if (type == IVTV_ENC_STREAM_TYPE_RAD && !(itv->v4l2_cap & V4L2_CAP_RADIO)) 201 return 0; 202 if (type >= IVTV_DEC_STREAM_TYPE_MPG && !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) 203 return 0; 204 205 /* User explicitly selected 0 buffers for these streams, so don't 206 create them. */ 207 if (ivtv_stream_info[type].dma != DMA_NONE && 208 itv->options.kilobytes[type] == 0) { 209 IVTV_INFO("Disabled %s device\n", ivtv_stream_info[type].name); 210 return 0; 211 } 212 213 ivtv_stream_init(itv, type); 214 215 snprintf(s->vdev.name, sizeof(s->vdev.name), "%s %s", 216 itv->v4l2_dev.name, s->name); 217 218 s->vdev.num = num; 219 s->vdev.v4l2_dev = &itv->v4l2_dev; 220 if (ivtv_stream_info[type].v4l2_caps & 221 (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT)) 222 s->vdev.vfl_dir = VFL_DIR_TX; 223 s->vdev.fops = ivtv_stream_info[type].fops; 224 s->vdev.ctrl_handler = itv->v4l2_dev.ctrl_handler; 225 s->vdev.release = video_device_release_empty; 226 s->vdev.tvnorms = V4L2_STD_ALL; 227 s->vdev.lock = &itv->serialize_lock; 228 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) { 229 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_AUDIO); 230 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_AUDIO); 231 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMAUDIO); 232 v4l2_disable_ioctl(&s->vdev, VIDIOC_ENUMINPUT); 233 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_INPUT); 234 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_INPUT); 235 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_FREQUENCY); 236 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_FREQUENCY); 237 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_TUNER); 238 v4l2_disable_ioctl(&s->vdev, VIDIOC_G_TUNER); 239 v4l2_disable_ioctl(&s->vdev, VIDIOC_S_STD); 240 } 241 ivtv_set_funcs(&s->vdev); 242 return 0; 243 } 244 245 /* Initialize v4l2 variables and prepare v4l2 devices */ 246 int ivtv_streams_setup(struct ivtv *itv) 247 { 248 int type; 249 250 /* Setup V4L2 Devices */ 251 for (type = 0; type < IVTV_MAX_STREAMS; type++) { 252 /* Prepare device */ 253 if (ivtv_prep_dev(itv, type)) 254 break; 255 256 if (itv->streams[type].vdev.v4l2_dev == NULL) 257 continue; 258 259 /* Allocate Stream */ 260 if (ivtv_stream_alloc(&itv->streams[type])) 261 break; 262 } 263 if (type == IVTV_MAX_STREAMS) 264 return 0; 265 266 /* One or more streams could not be initialized. Clean 'em all up. */ 267 ivtv_streams_cleanup(itv); 268 return -ENOMEM; 269 } 270 271 static int ivtv_reg_dev(struct ivtv *itv, int type) 272 { 273 struct ivtv_stream *s = &itv->streams[type]; 274 int vfl_type = ivtv_stream_info[type].vfl_type; 275 const char *name; 276 int num; 277 278 if (s->vdev.v4l2_dev == NULL) 279 return 0; 280 281 num = s->vdev.num; 282 /* card number + user defined offset + device offset */ 283 if (type != IVTV_ENC_STREAM_TYPE_MPG) { 284 struct ivtv_stream *s_mpg = &itv->streams[IVTV_ENC_STREAM_TYPE_MPG]; 285 286 if (s_mpg->vdev.v4l2_dev) 287 num = s_mpg->vdev.num + ivtv_stream_info[type].num_offset; 288 } 289 if (itv->osd_video_pbase && (type == IVTV_DEC_STREAM_TYPE_YUV || 290 type == IVTV_DEC_STREAM_TYPE_MPG)) { 291 s->vdev.device_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY; 292 itv->v4l2_cap |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY; 293 } 294 video_set_drvdata(&s->vdev, s); 295 296 /* Register device. First try the desired minor, then any free one. */ 297 if (video_register_device_no_warn(&s->vdev, vfl_type, num)) { 298 IVTV_ERR("Couldn't register v4l2 device for %s (device node number %d)\n", 299 s->name, num); 300 return -ENOMEM; 301 } 302 name = video_device_node_name(&s->vdev); 303 304 switch (vfl_type) { 305 case VFL_TYPE_VIDEO: 306 IVTV_INFO("Registered device %s for %s (%d kB)\n", 307 name, s->name, itv->options.kilobytes[type]); 308 break; 309 case VFL_TYPE_RADIO: 310 IVTV_INFO("Registered device %s for %s\n", 311 name, s->name); 312 break; 313 case VFL_TYPE_VBI: 314 if (itv->options.kilobytes[type]) 315 IVTV_INFO("Registered device %s for %s (%d kB)\n", 316 name, s->name, itv->options.kilobytes[type]); 317 else 318 IVTV_INFO("Registered device %s for %s\n", 319 name, s->name); 320 break; 321 } 322 return 0; 323 } 324 325 /* Register v4l2 devices */ 326 int ivtv_streams_register(struct ivtv *itv) 327 { 328 int type; 329 int err = 0; 330 331 /* Register V4L2 devices */ 332 for (type = 0; type < IVTV_MAX_STREAMS; type++) 333 err |= ivtv_reg_dev(itv, type); 334 335 if (err == 0) 336 return 0; 337 338 /* One or more streams could not be initialized. Clean 'em all up. */ 339 ivtv_streams_cleanup(itv); 340 return -ENOMEM; 341 } 342 343 /* Unregister v4l2 devices */ 344 void ivtv_streams_cleanup(struct ivtv *itv) 345 { 346 int type; 347 348 /* Teardown all streams */ 349 for (type = 0; type < IVTV_MAX_STREAMS; type++) { 350 struct video_device *vdev = &itv->streams[type].vdev; 351 352 if (vdev->v4l2_dev == NULL) 353 continue; 354 355 video_unregister_device(vdev); 356 ivtv_stream_free(&itv->streams[type]); 357 itv->streams[type].vdev.v4l2_dev = NULL; 358 } 359 } 360 361 static void ivtv_vbi_setup(struct ivtv *itv) 362 { 363 int raw = ivtv_raw_vbi(itv); 364 u32 data[CX2341X_MBOX_MAX_DATA]; 365 int lines; 366 int i; 367 368 /* Reset VBI */ 369 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, 0xffff , 0, 0, 0, 0); 370 371 /* setup VBI registers */ 372 if (raw) 373 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &itv->vbi.in.fmt.vbi); 374 else 375 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, &itv->vbi.in.fmt.sliced); 376 377 /* determine number of lines and total number of VBI bytes. 378 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1 379 The '- 1' byte is probably an unused U or V byte. Or something... 380 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal 381 header, 42 data bytes + checksum (to be confirmed) */ 382 if (raw) { 383 lines = itv->vbi.count * 2; 384 } else { 385 lines = itv->is_60hz ? 24 : 38; 386 if (itv->is_60hz && (itv->hw_flags & IVTV_HW_CX25840)) 387 lines += 2; 388 } 389 390 itv->vbi.enc_size = lines * (raw ? itv->vbi.raw_size : itv->vbi.sliced_size); 391 392 /* Note: sliced vs raw flag doesn't seem to have any effect 393 TODO: check mode (0x02) value with older ivtv versions. */ 394 data[0] = raw | 0x02 | (0xbd << 8); 395 396 /* Every X number of frames a VBI interrupt arrives (frames as in 25 or 30 fps) */ 397 data[1] = 1; 398 /* The VBI frames are stored in a ringbuffer with this size (with a VBI frame as unit) */ 399 data[2] = raw ? 4 : 4 * (itv->vbi.raw_size / itv->vbi.enc_size); 400 /* The start/stop codes determine which VBI lines end up in the raw VBI data area. 401 The codes are from table 24 in the saa7115 datasheet. Each raw/sliced/video line 402 is framed with codes FF0000XX where XX is the SAV/EAV (Start/End of Active Video) 403 code. These values for raw VBI are obtained from a driver disassembly. The sliced 404 start/stop codes was deduced from this, but they do not appear in the driver. 405 Other code pairs that I found are: 0x250E6249/0x13545454 and 0x25256262/0x38137F54. 406 However, I have no idea what these values are for. */ 407 if (itv->hw_flags & IVTV_HW_CX25840) { 408 /* Setup VBI for the cx25840 digitizer */ 409 if (raw) { 410 data[3] = 0x20602060; 411 data[4] = 0x30703070; 412 } else { 413 data[3] = 0xB0F0B0F0; 414 data[4] = 0xA0E0A0E0; 415 } 416 /* Lines per frame */ 417 data[5] = lines; 418 /* bytes per line */ 419 data[6] = (raw ? itv->vbi.raw_size : itv->vbi.sliced_size); 420 } else { 421 /* Setup VBI for the saa7115 digitizer */ 422 if (raw) { 423 data[3] = 0x25256262; 424 data[4] = 0x387F7F7F; 425 } else { 426 data[3] = 0xABABECEC; 427 data[4] = 0xB6F1F1F1; 428 } 429 /* Lines per frame */ 430 data[5] = lines; 431 /* bytes per line */ 432 data[6] = itv->vbi.enc_size / lines; 433 } 434 435 IVTV_DEBUG_INFO( 436 "Setup VBI API header 0x%08x pkts %d buffs %d ln %d sz %d\n", 437 data[0], data[1], data[2], data[5], data[6]); 438 439 ivtv_api(itv, CX2341X_ENC_SET_VBI_CONFIG, 7, data); 440 441 /* returns the VBI encoder memory area. */ 442 itv->vbi.enc_start = data[2]; 443 itv->vbi.fpi = data[0]; 444 if (!itv->vbi.fpi) 445 itv->vbi.fpi = 1; 446 447 IVTV_DEBUG_INFO("Setup VBI start 0x%08x frames %d fpi %d\n", 448 itv->vbi.enc_start, data[1], itv->vbi.fpi); 449 450 /* select VBI lines. 451 Note that the sliced argument seems to have no effect. */ 452 for (i = 2; i <= 24; i++) { 453 int valid; 454 455 if (itv->is_60hz) { 456 valid = i >= 10 && i < 22; 457 } else { 458 valid = i >= 6 && i < 24; 459 } 460 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, i - 1, 461 valid, 0 , 0, 0); 462 ivtv_vapi(itv, CX2341X_ENC_SET_VBI_LINE, 5, (i - 1) | 0x80000000, 463 valid, 0, 0, 0); 464 } 465 466 /* Remaining VBI questions: 467 - Is it possible to select particular VBI lines only for inclusion in the MPEG 468 stream? Currently you can only get the first X lines. 469 - Is mixed raw and sliced VBI possible? 470 - What's the meaning of the raw/sliced flag? 471 - What's the meaning of params 2, 3 & 4 of the Select VBI command? */ 472 } 473 474 int ivtv_start_v4l2_encode_stream(struct ivtv_stream *s) 475 { 476 u32 data[CX2341X_MBOX_MAX_DATA]; 477 struct ivtv *itv = s->itv; 478 int captype = 0, subtype = 0; 479 int enable_passthrough = 0; 480 481 if (s->vdev.v4l2_dev == NULL) 482 return -EINVAL; 483 484 IVTV_DEBUG_INFO("Start encoder stream %s\n", s->name); 485 486 switch (s->type) { 487 case IVTV_ENC_STREAM_TYPE_MPG: 488 captype = 0; 489 subtype = 3; 490 491 /* Stop Passthrough */ 492 if (itv->output_mode == OUT_PASSTHROUGH) { 493 ivtv_passthrough_mode(itv, 0); 494 enable_passthrough = 1; 495 } 496 itv->mpg_data_received = itv->vbi_data_inserted = 0; 497 itv->dualwatch_jiffies = jiffies; 498 itv->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode); 499 itv->search_pack_header = 0; 500 break; 501 502 case IVTV_ENC_STREAM_TYPE_YUV: 503 if (itv->output_mode == OUT_PASSTHROUGH) { 504 captype = 2; 505 subtype = 11; /* video+audio+decoder */ 506 break; 507 } 508 captype = 1; 509 subtype = 1; 510 break; 511 case IVTV_ENC_STREAM_TYPE_PCM: 512 captype = 1; 513 subtype = 2; 514 break; 515 case IVTV_ENC_STREAM_TYPE_VBI: 516 captype = 1; 517 subtype = 4; 518 519 itv->vbi.frame = 0; 520 itv->vbi.inserted_frame = 0; 521 memset(itv->vbi.sliced_mpeg_size, 522 0, sizeof(itv->vbi.sliced_mpeg_size)); 523 break; 524 default: 525 return -EINVAL; 526 } 527 s->subtype = subtype; 528 s->buffers_stolen = 0; 529 530 /* Clear Streamoff flags in case left from last capture */ 531 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 532 533 if (atomic_read(&itv->capturing) == 0) { 534 int digitizer; 535 536 /* Always use frame based mode. Experiments have demonstrated that byte 537 stream based mode results in dropped frames and corruption. Not often, 538 but occasionally. Many thanks go to Leonard Orb who spent a lot of 539 effort and time trying to trace the cause of the drop outs. */ 540 /* 1 frame per DMA */ 541 /*ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 128, 0); */ 542 ivtv_vapi(itv, CX2341X_ENC_SET_DMA_BLOCK_SIZE, 2, 1, 1); 543 544 /* Stuff from Windows, we don't know what it is */ 545 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 0); 546 /* According to the docs, this should be correct. However, this is 547 untested. I don't dare enable this without having tested it. 548 Only very few old cards actually have this hardware combination. 549 ivtv_vapi(itv, CX2341X_ENC_SET_VERT_CROP_LINE, 1, 550 ((itv->hw_flags & IVTV_HW_SAA7114) && itv->is_60hz) ? 10001 : 0); 551 */ 552 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 3, !itv->has_cx23415); 553 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 8, 0); 554 ivtv_vapi(itv, CX2341X_ENC_MISC, 2, 4, 1); 555 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12); 556 557 /* assign placeholder */ 558 ivtv_vapi(itv, CX2341X_ENC_SET_PLACEHOLDER, 12, 559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 560 561 if (itv->card->hw_all & (IVTV_HW_SAA7115 | IVTV_HW_SAA717X)) 562 digitizer = 0xF1; 563 else if (itv->card->hw_all & IVTV_HW_SAA7114) 564 digitizer = 0xEF; 565 else /* cx25840 */ 566 digitizer = 0x140; 567 568 ivtv_vapi(itv, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, digitizer, digitizer); 569 570 /* Setup VBI */ 571 if (itv->v4l2_cap & V4L2_CAP_VBI_CAPTURE) { 572 ivtv_vbi_setup(itv); 573 } 574 575 /* assign program index info. Mask 7: select I/P/B, Num_req: 400 max */ 576 ivtv_vapi_result(itv, data, CX2341X_ENC_SET_PGM_INDEX_INFO, 2, 7, 400); 577 itv->pgm_info_offset = data[0]; 578 itv->pgm_info_num = data[1]; 579 itv->pgm_info_write_idx = 0; 580 itv->pgm_info_read_idx = 0; 581 582 IVTV_DEBUG_INFO("PGM Index at 0x%08x with %d elements\n", 583 itv->pgm_info_offset, itv->pgm_info_num); 584 585 /* Setup API for Stream */ 586 cx2341x_handler_setup(&itv->cxhdl); 587 588 /* mute if capturing radio */ 589 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) 590 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1, 591 1 | (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8)); 592 } 593 594 /* Vsync Setup */ 595 if (itv->has_cx23415 && !test_and_set_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) { 596 /* event notification (on) */ 597 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_ENC_VIM_RST, -1); 598 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST); 599 } 600 601 if (atomic_read(&itv->capturing) == 0) { 602 /* Clear all Pending Interrupts */ 603 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE); 604 605 clear_bit(IVTV_F_I_EOS, &itv->i_flags); 606 607 cx2341x_handler_set_busy(&itv->cxhdl, 1); 608 609 /* Initialize Digitizer for Capture */ 610 /* Avoid tinny audio problem - ensure audio clocks are going */ 611 v4l2_subdev_call(itv->sd_audio, audio, s_stream, 1); 612 /* Avoid unpredictable PCI bus hang - disable video clocks */ 613 if (itv->sd_video_is_streaming) 614 v4l2_subdev_call(itv->sd_video, video, s_stream, 0); 615 ivtv_msleep_timeout(300, 0); 616 ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0); 617 v4l2_subdev_call(itv->sd_video, video, s_stream, 1); 618 itv->sd_video_is_streaming = true; 619 } 620 621 /* begin_capture */ 622 if (ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, captype, subtype)) 623 { 624 IVTV_DEBUG_WARN( "Error starting capture!\n"); 625 return -EINVAL; 626 } 627 628 /* Start Passthrough */ 629 if (enable_passthrough) { 630 ivtv_passthrough_mode(itv, 1); 631 } 632 633 if (s->type == IVTV_ENC_STREAM_TYPE_VBI) 634 ivtv_clear_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP); 635 else 636 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE); 637 638 /* you're live! sit back and await interrupts :) */ 639 atomic_inc(&itv->capturing); 640 return 0; 641 } 642 EXPORT_SYMBOL(ivtv_start_v4l2_encode_stream); 643 644 static int ivtv_setup_v4l2_decode_stream(struct ivtv_stream *s) 645 { 646 u32 data[CX2341X_MBOX_MAX_DATA]; 647 struct ivtv *itv = s->itv; 648 int datatype; 649 u16 width; 650 u16 height; 651 652 if (s->vdev.v4l2_dev == NULL) 653 return -EINVAL; 654 655 IVTV_DEBUG_INFO("Setting some initial decoder settings\n"); 656 657 width = itv->cxhdl.width; 658 height = itv->cxhdl.height; 659 660 /* set audio mode to left/stereo for dual/stereo mode. */ 661 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode); 662 663 /* set number of internal decoder buffers */ 664 ivtv_vapi(itv, CX2341X_DEC_SET_DISPLAY_BUFFERS, 1, 0); 665 666 /* prebuffering */ 667 ivtv_vapi(itv, CX2341X_DEC_SET_PREBUFFERING, 1, 1); 668 669 /* extract from user packets */ 670 ivtv_vapi_result(itv, data, CX2341X_DEC_EXTRACT_VBI, 1, 1); 671 itv->vbi.dec_start = data[0]; 672 673 IVTV_DEBUG_INFO("Decoder VBI RE-Insert start 0x%08x size 0x%08x\n", 674 itv->vbi.dec_start, data[1]); 675 676 /* set decoder source settings */ 677 /* Data type: 0 = mpeg from host, 678 1 = yuv from encoder, 679 2 = yuv_from_host */ 680 switch (s->type) { 681 case IVTV_DEC_STREAM_TYPE_YUV: 682 if (itv->output_mode == OUT_PASSTHROUGH) { 683 datatype = 1; 684 } else { 685 /* Fake size to avoid switching video standard */ 686 datatype = 2; 687 width = 720; 688 height = itv->is_out_50hz ? 576 : 480; 689 } 690 IVTV_DEBUG_INFO("Setup DEC YUV Stream data[0] = %d\n", datatype); 691 break; 692 case IVTV_DEC_STREAM_TYPE_MPG: 693 default: 694 datatype = 0; 695 break; 696 } 697 if (ivtv_vapi(itv, CX2341X_DEC_SET_DECODER_SOURCE, 4, datatype, 698 width, height, itv->cxhdl.audio_properties)) { 699 IVTV_DEBUG_WARN("Couldn't initialize decoder source\n"); 700 } 701 702 /* Decoder sometimes dies here, so wait a moment */ 703 ivtv_msleep_timeout(10, 0); 704 705 /* Known failure point for firmware, so check */ 706 return ivtv_firmware_check(itv, "ivtv_setup_v4l2_decode_stream"); 707 } 708 709 int ivtv_start_v4l2_decode_stream(struct ivtv_stream *s, int gop_offset) 710 { 711 struct ivtv *itv = s->itv; 712 int rc; 713 714 if (s->vdev.v4l2_dev == NULL) 715 return -EINVAL; 716 717 if (test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) 718 return 0; /* already started */ 719 720 IVTV_DEBUG_INFO("Starting decode stream %s (gop_offset %d)\n", s->name, gop_offset); 721 722 rc = ivtv_setup_v4l2_decode_stream(s); 723 if (rc < 0) { 724 clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 725 return rc; 726 } 727 728 /* set dma size to 65536 bytes */ 729 ivtv_vapi(itv, CX2341X_DEC_SET_DMA_BLOCK_SIZE, 1, 65536); 730 731 /* Clear Streamoff */ 732 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 733 734 /* Zero out decoder counters */ 735 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[0]); 736 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[1]); 737 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[2]); 738 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA_END].data[3]); 739 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[0]); 740 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[1]); 741 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[2]); 742 writel(0, &itv->dec_mbox.mbox[IVTV_MBOX_DMA].data[3]); 743 744 /* turn on notification of dual/stereo mode change */ 745 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 1, IVTV_IRQ_DEC_AUD_MODE_CHG, -1); 746 747 /* start playback */ 748 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, gop_offset, 0); 749 750 /* Let things settle before we actually start */ 751 ivtv_msleep_timeout(10, 0); 752 753 /* Clear the following Interrupt mask bits for decoding */ 754 ivtv_clear_irq_mask(itv, IVTV_IRQ_MASK_DECODE); 755 IVTV_DEBUG_IRQ("IRQ Mask is now: 0x%08x\n", itv->irqmask); 756 757 /* you're live! sit back and await interrupts :) */ 758 atomic_inc(&itv->decoding); 759 return 0; 760 } 761 762 void ivtv_stop_all_captures(struct ivtv *itv) 763 { 764 int i; 765 766 for (i = IVTV_MAX_STREAMS - 1; i >= 0; i--) { 767 struct ivtv_stream *s = &itv->streams[i]; 768 769 if (s->vdev.v4l2_dev == NULL) 770 continue; 771 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) { 772 ivtv_stop_v4l2_encode_stream(s, 0); 773 } 774 } 775 } 776 777 int ivtv_stop_v4l2_encode_stream(struct ivtv_stream *s, int gop_end) 778 { 779 struct ivtv *itv = s->itv; 780 DECLARE_WAITQUEUE(wait, current); 781 int cap_type; 782 int stopmode; 783 784 if (s->vdev.v4l2_dev == NULL) 785 return -EINVAL; 786 787 /* This function assumes that you are allowed to stop the capture 788 and that we are actually capturing */ 789 790 IVTV_DEBUG_INFO("Stop Capture\n"); 791 792 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) 793 return 0; 794 if (atomic_read(&itv->capturing) == 0) 795 return 0; 796 797 switch (s->type) { 798 case IVTV_ENC_STREAM_TYPE_YUV: 799 cap_type = 1; 800 break; 801 case IVTV_ENC_STREAM_TYPE_PCM: 802 cap_type = 1; 803 break; 804 case IVTV_ENC_STREAM_TYPE_VBI: 805 cap_type = 1; 806 break; 807 case IVTV_ENC_STREAM_TYPE_MPG: 808 default: 809 cap_type = 0; 810 break; 811 } 812 813 /* Stop Capture Mode */ 814 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) { 815 stopmode = 0; 816 } else { 817 stopmode = 1; 818 } 819 820 /* end_capture */ 821 /* when: 0 = end of GOP 1 = NOW!, type: 0 = mpeg, subtype: 3 = video+audio */ 822 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, stopmode, cap_type, s->subtype); 823 824 if (!test_bit(IVTV_F_S_PASSTHROUGH, &s->s_flags)) { 825 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && gop_end) { 826 /* only run these if we're shutting down the last cap */ 827 unsigned long duration; 828 unsigned long then = jiffies; 829 830 add_wait_queue(&itv->eos_waitq, &wait); 831 832 set_current_state(TASK_INTERRUPTIBLE); 833 834 /* wait 2s for EOS interrupt */ 835 while (!test_bit(IVTV_F_I_EOS, &itv->i_flags) && 836 time_before(jiffies, 837 then + msecs_to_jiffies(2000))) { 838 schedule_timeout(msecs_to_jiffies(10)); 839 } 840 841 /* To convert jiffies to ms, we must multiply by 1000 842 * and divide by HZ. To avoid runtime division, we 843 * convert this to multiplication by 1000/HZ. 844 * Since integer division truncates, we get the best 845 * accuracy if we do a rounding calculation of the constant. 846 * Think of the case where HZ is 1024. 847 */ 848 duration = ((1000 + HZ / 2) / HZ) * (jiffies - then); 849 850 if (!test_bit(IVTV_F_I_EOS, &itv->i_flags)) { 851 IVTV_DEBUG_WARN("%s: EOS interrupt not received! stopping anyway.\n", s->name); 852 IVTV_DEBUG_WARN("%s: waited %lu ms.\n", s->name, duration); 853 } else { 854 IVTV_DEBUG_INFO("%s: EOS took %lu ms to occur.\n", s->name, duration); 855 } 856 set_current_state(TASK_RUNNING); 857 remove_wait_queue(&itv->eos_waitq, &wait); 858 set_bit(IVTV_F_S_STREAMOFF, &s->s_flags); 859 } 860 861 /* Handle any pending interrupts */ 862 ivtv_msleep_timeout(100, 0); 863 } 864 865 atomic_dec(&itv->capturing); 866 867 /* Clear capture and no-read bits */ 868 clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 869 870 if (s->type == IVTV_ENC_STREAM_TYPE_VBI) 871 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VBI_CAP); 872 873 if (atomic_read(&itv->capturing) > 0) { 874 return 0; 875 } 876 877 cx2341x_handler_set_busy(&itv->cxhdl, 0); 878 879 /* Set the following Interrupt mask bits for capture */ 880 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_CAPTURE); 881 timer_delete(&itv->dma_timer); 882 883 /* event notification (off) */ 884 if (test_and_clear_bit(IVTV_F_I_DIG_RST, &itv->i_flags)) { 885 /* type: 0 = refresh */ 886 /* on/off: 0 = off, intr: 0x10000000, mbox_id: -1: none */ 887 ivtv_vapi(itv, CX2341X_ENC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_ENC_VIM_RST, -1); 888 ivtv_set_irq_mask(itv, IVTV_IRQ_ENC_VIM_RST); 889 } 890 891 /* Raw-passthrough is implied on start. Make sure it's stopped so 892 the encoder will re-initialize when next started */ 893 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 7); 894 895 wake_up(&s->waitq); 896 897 return 0; 898 } 899 EXPORT_SYMBOL(ivtv_stop_v4l2_encode_stream); 900 901 int ivtv_stop_v4l2_decode_stream(struct ivtv_stream *s, int flags, u64 pts) 902 { 903 static const struct v4l2_event ev = { 904 .type = V4L2_EVENT_EOS, 905 }; 906 struct ivtv *itv = s->itv; 907 908 if (s->vdev.v4l2_dev == NULL) 909 return -EINVAL; 910 911 if (s->type != IVTV_DEC_STREAM_TYPE_YUV && s->type != IVTV_DEC_STREAM_TYPE_MPG) 912 return -EINVAL; 913 914 if (!test_bit(IVTV_F_S_STREAMING, &s->s_flags)) 915 return 0; 916 917 IVTV_DEBUG_INFO("Stop Decode at %llu, flags: %x\n", (unsigned long long)pts, flags); 918 919 /* Stop Decoder */ 920 if (!(flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) || pts) { 921 u32 tmp = 0; 922 923 /* Wait until the decoder is no longer running */ 924 if (pts) { 925 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 926 0, (u32)(pts & 0xffffffff), (u32)(pts >> 32)); 927 } 928 while (1) { 929 u32 data[CX2341X_MBOX_MAX_DATA]; 930 ivtv_vapi_result(itv, data, CX2341X_DEC_GET_XFER_INFO, 0); 931 if (s->q_full.buffers + s->q_dma.buffers == 0) { 932 if (tmp == data[3]) 933 break; 934 tmp = data[3]; 935 } 936 if (ivtv_msleep_timeout(100, 1)) 937 break; 938 } 939 } 940 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, flags & V4L2_DEC_CMD_STOP_TO_BLACK, 0, 0); 941 942 /* turn off notification of dual/stereo mode change */ 943 ivtv_vapi(itv, CX2341X_DEC_SET_EVENT_NOTIFICATION, 4, 0, 0, IVTV_IRQ_DEC_AUD_MODE_CHG, -1); 944 945 ivtv_set_irq_mask(itv, IVTV_IRQ_MASK_DECODE); 946 timer_delete(&itv->dma_timer); 947 948 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags); 949 clear_bit(IVTV_F_S_STREAMING, &s->s_flags); 950 ivtv_flush_queues(s); 951 952 /* decoder needs time to settle */ 953 ivtv_msleep_timeout(40, 0); 954 955 /* decrement decoding */ 956 atomic_dec(&itv->decoding); 957 958 set_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags); 959 wake_up(&itv->event_waitq); 960 v4l2_event_queue(&s->vdev, &ev); 961 962 /* wake up wait queues */ 963 wake_up(&s->waitq); 964 965 return 0; 966 } 967 968 int ivtv_passthrough_mode(struct ivtv *itv, int enable) 969 { 970 struct ivtv_stream *yuv_stream = &itv->streams[IVTV_ENC_STREAM_TYPE_YUV]; 971 struct ivtv_stream *dec_stream = &itv->streams[IVTV_DEC_STREAM_TYPE_YUV]; 972 973 if (yuv_stream->vdev.v4l2_dev == NULL || dec_stream->vdev.v4l2_dev == NULL) 974 return -EINVAL; 975 976 IVTV_DEBUG_INFO("ivtv ioctl: Select passthrough mode\n"); 977 978 /* Prevent others from starting/stopping streams while we 979 initiate/terminate passthrough mode */ 980 if (enable) { 981 if (itv->output_mode == OUT_PASSTHROUGH) { 982 return 0; 983 } 984 if (ivtv_set_output_mode(itv, OUT_PASSTHROUGH) != OUT_PASSTHROUGH) 985 return -EBUSY; 986 987 /* Fully initialize stream, and then unflag init */ 988 set_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags); 989 set_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags); 990 991 /* Setup YUV Decoder */ 992 ivtv_setup_v4l2_decode_stream(dec_stream); 993 994 /* Start Decoder */ 995 ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 1); 996 atomic_inc(&itv->decoding); 997 998 /* Setup capture if not already done */ 999 if (atomic_read(&itv->capturing) == 0) { 1000 cx2341x_handler_setup(&itv->cxhdl); 1001 cx2341x_handler_set_busy(&itv->cxhdl, 1); 1002 } 1003 1004 /* Start Passthrough Mode */ 1005 ivtv_vapi(itv, CX2341X_ENC_START_CAPTURE, 2, 2, 11); 1006 atomic_inc(&itv->capturing); 1007 return 0; 1008 } 1009 1010 if (itv->output_mode != OUT_PASSTHROUGH) 1011 return 0; 1012 1013 /* Stop Passthrough Mode */ 1014 ivtv_vapi(itv, CX2341X_ENC_STOP_CAPTURE, 3, 1, 2, 11); 1015 ivtv_vapi(itv, CX2341X_DEC_STOP_PLAYBACK, 3, 1, 0, 0); 1016 1017 atomic_dec(&itv->capturing); 1018 atomic_dec(&itv->decoding); 1019 clear_bit(IVTV_F_S_PASSTHROUGH, &dec_stream->s_flags); 1020 clear_bit(IVTV_F_S_STREAMING, &dec_stream->s_flags); 1021 itv->output_mode = OUT_NONE; 1022 if (atomic_read(&itv->capturing) == 0) 1023 cx2341x_handler_set_busy(&itv->cxhdl, 0); 1024 1025 return 0; 1026 } 1027