0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "ivtv-driver.h"
0011 #include "ivtv-fileops.h"
0012 #include "ivtv-i2c.h"
0013 #include "ivtv-queue.h"
0014 #include "ivtv-udma.h"
0015 #include "ivtv-irq.h"
0016 #include "ivtv-vbi.h"
0017 #include "ivtv-mailbox.h"
0018 #include "ivtv-routing.h"
0019 #include "ivtv-streams.h"
0020 #include "ivtv-yuv.h"
0021 #include "ivtv-ioctl.h"
0022 #include "ivtv-cards.h"
0023 #include "ivtv-firmware.h"
0024 #include <media/v4l2-event.h>
0025 #include <media/i2c/saa7115.h>
0026
0027
0028
0029
0030
0031
0032 int ivtv_claim_stream(struct ivtv_open_id *id, int type)
0033 {
0034 struct ivtv *itv = id->itv;
0035 struct ivtv_stream *s = &itv->streams[type];
0036 struct ivtv_stream *s_vbi;
0037 int vbi_type;
0038
0039 if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
0040
0041 if (s->fh == &id->fh) {
0042
0043 return 0;
0044 }
0045 if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
0046 type == IVTV_ENC_STREAM_TYPE_VBI)) {
0047
0048
0049
0050 s->fh = &id->fh;
0051 IVTV_DEBUG_INFO("Start Read VBI\n");
0052 return 0;
0053 }
0054
0055 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
0056 return -EBUSY;
0057 }
0058 s->fh = &id->fh;
0059 if (type == IVTV_DEC_STREAM_TYPE_VBI) {
0060
0061 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
0062 }
0063
0064
0065
0066
0067
0068 if (type == IVTV_DEC_STREAM_TYPE_MPG) {
0069 vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
0070 } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
0071 itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
0072 vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
0073 } else {
0074 return 0;
0075 }
0076 s_vbi = &itv->streams[vbi_type];
0077
0078 if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
0079
0080 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
0081 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
0082 }
0083
0084 set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
0085 return 0;
0086 }
0087 EXPORT_SYMBOL(ivtv_claim_stream);
0088
0089
0090
0091 void ivtv_release_stream(struct ivtv_stream *s)
0092 {
0093 struct ivtv *itv = s->itv;
0094 struct ivtv_stream *s_vbi;
0095
0096 s->fh = NULL;
0097 if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
0098 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
0099
0100 return;
0101 }
0102 if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
0103 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
0104 return;
0105 }
0106
0107 ivtv_flush_queues(s);
0108
0109
0110 if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
0111 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
0112
0113
0114
0115
0116 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
0117 s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
0118 else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
0119 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
0120 else
0121 return;
0122
0123
0124 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
0125
0126 return;
0127 }
0128 if (s_vbi->fh) {
0129
0130 return;
0131 }
0132
0133 if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
0134 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
0135 clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
0136 ivtv_flush_queues(s_vbi);
0137 }
0138 EXPORT_SYMBOL(ivtv_release_stream);
0139
0140 static void ivtv_dualwatch(struct ivtv *itv)
0141 {
0142 struct v4l2_tuner vt;
0143 u32 new_stereo_mode;
0144 const u32 dual = 0x02;
0145
0146 new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
0147 memset(&vt, 0, sizeof(vt));
0148 ivtv_call_all(itv, tuner, g_tuner, &vt);
0149 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
0150 new_stereo_mode = dual;
0151
0152 if (new_stereo_mode == itv->dualwatch_stereo_mode)
0153 return;
0154
0155 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
0156 itv->dualwatch_stereo_mode, new_stereo_mode);
0157 if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
0158 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
0159 }
0160
0161 static void ivtv_update_pgm_info(struct ivtv *itv)
0162 {
0163 u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
0164 int cnt;
0165 int i = 0;
0166
0167 if (wr_idx >= itv->pgm_info_num) {
0168 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
0169 return;
0170 }
0171 cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
0172 while (i < cnt) {
0173 int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
0174 struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
0175 u32 addr = itv->pgm_info_offset + 4 + idx * 24;
0176 const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
0177 V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
0178
0179
0180 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
0181 if (e->offset > itv->mpg_data_received) {
0182 break;
0183 }
0184 e->offset += itv->vbi_data_inserted;
0185 e->length = read_enc(addr);
0186 e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
0187 e->flags = mapping[read_enc(addr + 12) & 7];
0188 i++;
0189 }
0190 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
0191 }
0192
0193 static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
0194 {
0195 struct ivtv *itv = s->itv;
0196 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
0197 struct ivtv_buffer *buf;
0198 DEFINE_WAIT(wait);
0199
0200 *err = 0;
0201 while (1) {
0202 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
0203
0204 ivtv_update_pgm_info(itv);
0205
0206 if (time_after(jiffies,
0207 itv->dualwatch_jiffies +
0208 msecs_to_jiffies(1000))) {
0209 itv->dualwatch_jiffies = jiffies;
0210 ivtv_dualwatch(itv);
0211 }
0212
0213 if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
0214 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
0215 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
0216
0217 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
0218 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
0219 }
0220 }
0221 buf = &itv->vbi.sliced_mpeg_buf;
0222 if (buf->readpos != buf->bytesused) {
0223 return buf;
0224 }
0225 }
0226
0227
0228 buf = ivtv_dequeue(s, &s->q_io);
0229 if (buf)
0230 return buf;
0231
0232
0233 buf = ivtv_dequeue(s, &s->q_full);
0234 if (buf) {
0235 if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
0236 return buf;
0237 buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
0238 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
0239
0240 ivtv_buf_swap(buf);
0241 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
0242
0243 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
0244 }
0245 return buf;
0246 }
0247
0248
0249 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
0250 IVTV_DEBUG_INFO("EOS %s\n", s->name);
0251 return NULL;
0252 }
0253
0254
0255 if (non_block) {
0256 *err = -EAGAIN;
0257 return NULL;
0258 }
0259
0260
0261 mutex_unlock(&itv->serialize_lock);
0262 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
0263
0264 if (!s->q_full.buffers)
0265 schedule();
0266 finish_wait(&s->waitq, &wait);
0267 mutex_lock(&itv->serialize_lock);
0268 if (signal_pending(current)) {
0269
0270 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
0271 *err = -EINTR;
0272 return NULL;
0273 }
0274 }
0275 }
0276
0277 static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
0278 {
0279 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
0280
0281 itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
0282 itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
0283 itv->vbi.sliced_mpeg_buf.readpos = 0;
0284 }
0285
0286 static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
0287 char __user *ubuf, size_t ucount)
0288 {
0289 struct ivtv *itv = s->itv;
0290 size_t len = buf->bytesused - buf->readpos;
0291
0292 if (len > ucount) len = ucount;
0293 if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
0294 !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
0295 const char *start = buf->buf + buf->readpos;
0296 const char *p = start + 1;
0297 const u8 *q;
0298 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
0299 int stuffing, i;
0300
0301 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
0302 p = q + 1;
0303 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
0304 q[1] != 0 || q[2] != 1 || q[3] != ch) {
0305 continue;
0306 }
0307 if (!itv->search_pack_header) {
0308 if ((q[6] & 0xc0) != 0x80)
0309 continue;
0310 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
0311 ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
0312 ch = 0xba;
0313 itv->search_pack_header = 1;
0314 p = q + 9;
0315 }
0316 continue;
0317 }
0318 stuffing = q[13] & 7;
0319
0320 for (i = 0; i < stuffing; i++)
0321 if (q[14 + i] != 0xff)
0322 break;
0323 if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
0324 q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
0325 q[16 + stuffing] == 1) {
0326 itv->search_pack_header = 0;
0327 len = (char *)q - start;
0328 ivtv_setup_sliced_vbi_buf(itv);
0329 break;
0330 }
0331 }
0332 }
0333 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
0334 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
0335 return -EFAULT;
0336 }
0337
0338
0339
0340 buf->readpos += len;
0341 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
0342 itv->mpg_data_received += len;
0343 return len;
0344 }
0345
0346 static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
0347 {
0348 struct ivtv *itv = s->itv;
0349 size_t tot_written = 0;
0350 int single_frame = 0;
0351
0352 if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) {
0353
0354 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
0355 return -EIO;
0356 }
0357
0358
0359
0360 if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
0361 (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
0362 single_frame = 1;
0363
0364 for (;;) {
0365 struct ivtv_buffer *buf;
0366 int rc;
0367
0368 buf = ivtv_get_buffer(s, non_block, &rc);
0369
0370 if (buf == NULL) {
0371
0372 if (tot_written)
0373 break;
0374
0375 if (rc == 0) {
0376 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0377 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0378 ivtv_release_stream(s);
0379 }
0380
0381 return rc;
0382 }
0383 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
0384 if (buf != &itv->vbi.sliced_mpeg_buf) {
0385 ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
0386 }
0387 else if (buf->readpos == buf->bytesused) {
0388 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
0389 itv->vbi.sliced_mpeg_size[idx] = 0;
0390 itv->vbi.inserted_frame++;
0391 itv->vbi_data_inserted += buf->bytesused;
0392 }
0393 if (rc < 0)
0394 return rc;
0395 tot_written += rc;
0396
0397 if (tot_written == tot_count || single_frame)
0398 break;
0399 }
0400 return tot_written;
0401 }
0402
0403 static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
0404 loff_t *pos, int non_block)
0405 {
0406 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
0407 struct ivtv *itv = s->itv;
0408
0409 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
0410 if (rc > 0)
0411 *pos += rc;
0412 return rc;
0413 }
0414
0415 int ivtv_start_capture(struct ivtv_open_id *id)
0416 {
0417 struct ivtv *itv = id->itv;
0418 struct ivtv_stream *s = &itv->streams[id->type];
0419 struct ivtv_stream *s_vbi;
0420
0421 if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
0422 s->type == IVTV_DEC_STREAM_TYPE_MPG ||
0423 s->type == IVTV_DEC_STREAM_TYPE_YUV ||
0424 s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
0425
0426 return -EINVAL;
0427 }
0428
0429
0430 if (ivtv_claim_stream(id, s->type))
0431 return -EBUSY;
0432
0433
0434 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
0435 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0436 return 0;
0437 }
0438
0439
0440
0441 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
0442 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0443 return 0;
0444 }
0445
0446
0447 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
0448 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
0449 test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
0450 !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
0451
0452
0453
0454 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
0455 IVTV_DEBUG_WARN("VBI capture start failed\n");
0456
0457
0458 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
0459 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0460
0461 ivtv_release_stream(s);
0462 return -EIO;
0463 }
0464 IVTV_DEBUG_INFO("VBI insertion started\n");
0465 }
0466
0467
0468 if (!ivtv_start_v4l2_encode_stream(s)) {
0469
0470 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0471
0472 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
0473 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
0474 return 0;
0475 }
0476
0477
0478 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
0479
0480
0481
0482
0483 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
0484 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
0485 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
0486 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
0487 }
0488 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0489 ivtv_release_stream(s);
0490 return -EIO;
0491 }
0492
0493 ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
0494 {
0495 struct ivtv_open_id *id = fh2id(filp->private_data);
0496 struct ivtv *itv = id->itv;
0497 struct ivtv_stream *s = &itv->streams[id->type];
0498 ssize_t rc;
0499
0500 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
0501
0502 if (mutex_lock_interruptible(&itv->serialize_lock))
0503 return -ERESTARTSYS;
0504 rc = ivtv_start_capture(id);
0505 if (!rc)
0506 rc = ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
0507 mutex_unlock(&itv->serialize_lock);
0508 return rc;
0509 }
0510
0511 int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
0512 {
0513 struct ivtv *itv = id->itv;
0514 struct ivtv_stream *s = &itv->streams[id->type];
0515 int rc;
0516
0517 if (atomic_read(&itv->decoding) == 0) {
0518 if (ivtv_claim_stream(id, s->type)) {
0519
0520 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
0521 return -EBUSY;
0522 }
0523 rc = ivtv_start_v4l2_decode_stream(s, 0);
0524 if (rc < 0) {
0525 if (rc == -EAGAIN)
0526 rc = ivtv_start_v4l2_decode_stream(s, 0);
0527 if (rc < 0)
0528 return rc;
0529 }
0530 }
0531 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
0532 return ivtv_set_speed(itv, speed);
0533 return 0;
0534 }
0535
0536 static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
0537 {
0538 struct ivtv_open_id *id = fh2id(filp->private_data);
0539 struct ivtv *itv = id->itv;
0540 struct ivtv_stream *s = &itv->streams[id->type];
0541 struct yuv_playback_info *yi = &itv->yuv_info;
0542 struct ivtv_buffer *buf;
0543 struct ivtv_queue q;
0544 int bytes_written = 0;
0545 int mode;
0546 int rc;
0547 DEFINE_WAIT(wait);
0548
0549 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
0550
0551 if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
0552 s->type != IVTV_DEC_STREAM_TYPE_YUV &&
0553 s->type != IVTV_DEC_STREAM_TYPE_VOUT)
0554
0555 return -EINVAL;
0556
0557
0558 if (ivtv_claim_stream(id, s->type))
0559 return -EBUSY;
0560
0561
0562 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
0563 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
0564
0565 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0566 return ivtv_write_vbi_from_user(itv,
0567 (const struct v4l2_sliced_vbi_data __user *)user_buf, elems);
0568 }
0569
0570 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
0571
0572 if (ivtv_set_output_mode(itv, mode) != mode) {
0573 ivtv_release_stream(s);
0574 return -EBUSY;
0575 }
0576 ivtv_queue_init(&q);
0577 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0578
0579
0580 rc = ivtv_start_decoding(id, itv->speed);
0581 if (rc) {
0582 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
0583
0584
0585 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
0586 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0587 return rc;
0588 }
0589
0590 retry:
0591
0592
0593 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
0594 while (count >= itv->dma_data_req_size) {
0595 rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
0596
0597 if (rc < 0)
0598 return rc;
0599
0600 bytes_written += itv->dma_data_req_size;
0601 user_buf += itv->dma_data_req_size;
0602 count -= itv->dma_data_req_size;
0603 }
0604 if (count == 0) {
0605 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
0606 return bytes_written;
0607 }
0608 }
0609
0610 for (;;) {
0611
0612 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
0613 ivtv_enqueue(s, buf, &q);
0614 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
0615 ivtv_enqueue(s, buf, &q);
0616 }
0617 if (q.buffers)
0618 break;
0619 if (filp->f_flags & O_NONBLOCK)
0620 return -EAGAIN;
0621 mutex_unlock(&itv->serialize_lock);
0622 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
0623
0624 if (!s->q_free.buffers)
0625 schedule();
0626 finish_wait(&s->waitq, &wait);
0627 mutex_lock(&itv->serialize_lock);
0628 if (signal_pending(current)) {
0629 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
0630 return -EINTR;
0631 }
0632 }
0633
0634
0635 while ((buf = ivtv_dequeue(s, &q))) {
0636
0637
0638 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
0639 yi->stream_size + count > itv->dma_data_req_size)
0640 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
0641 itv->dma_data_req_size - yi->stream_size);
0642 else
0643 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
0644
0645
0646 if (rc < 0) {
0647 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
0648 return rc;
0649 }
0650 user_buf += rc;
0651 count -= rc;
0652 bytes_written += rc;
0653
0654 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
0655 yi->stream_size += rc;
0656
0657 if (yi->stream_size == itv->dma_data_req_size) {
0658 ivtv_enqueue(s, buf, &s->q_full);
0659 yi->stream_size = 0;
0660 break;
0661 }
0662 }
0663
0664 if (buf->bytesused != s->buf_size) {
0665
0666 ivtv_enqueue(s, buf, &s->q_io);
0667 break;
0668 }
0669
0670 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
0671 ivtv_buf_swap(buf);
0672 ivtv_enqueue(s, buf, &s->q_full);
0673 }
0674
0675 if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
0676 if (s->q_full.length >= itv->dma_data_req_size) {
0677 int got_sig;
0678
0679 if (mode == OUT_YUV)
0680 ivtv_yuv_setup_stream_frame(itv);
0681
0682 mutex_unlock(&itv->serialize_lock);
0683 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
0684 while (!(got_sig = signal_pending(current)) &&
0685 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
0686 schedule();
0687 }
0688 finish_wait(&itv->dma_waitq, &wait);
0689 mutex_lock(&itv->serialize_lock);
0690 if (got_sig) {
0691 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
0692 return -EINTR;
0693 }
0694
0695 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
0696 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
0697 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
0698 }
0699 }
0700
0701
0702 if (count && !(filp->f_flags & O_NONBLOCK))
0703 goto retry;
0704 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
0705 return bytes_written;
0706 }
0707
0708 ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
0709 {
0710 struct ivtv_open_id *id = fh2id(filp->private_data);
0711 struct ivtv *itv = id->itv;
0712 ssize_t res;
0713
0714 if (mutex_lock_interruptible(&itv->serialize_lock))
0715 return -ERESTARTSYS;
0716 res = ivtv_write(filp, user_buf, count, pos);
0717 mutex_unlock(&itv->serialize_lock);
0718 return res;
0719 }
0720
0721 __poll_t ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
0722 {
0723 struct ivtv_open_id *id = fh2id(filp->private_data);
0724 struct ivtv *itv = id->itv;
0725 struct ivtv_stream *s = &itv->streams[id->type];
0726 __poll_t res = 0;
0727
0728
0729 IVTV_DEBUG_HI_FILE("Decoder poll\n");
0730
0731
0732
0733 if (!list_empty(&id->fh.subscribed)) {
0734 poll_wait(filp, &id->fh.wait, wait);
0735
0736 clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
0737 if (v4l2_event_pending(&id->fh))
0738 res = EPOLLPRI;
0739 } else {
0740
0741
0742 poll_wait(filp, &s->waitq, wait);
0743 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
0744 if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
0745 test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
0746 res = EPOLLPRI;
0747 }
0748
0749
0750 if (s->q_free.buffers)
0751 res |= EPOLLOUT | EPOLLWRNORM;
0752 return res;
0753 }
0754
0755 __poll_t ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait)
0756 {
0757 __poll_t req_events = poll_requested_events(wait);
0758 struct ivtv_open_id *id = fh2id(filp->private_data);
0759 struct ivtv *itv = id->itv;
0760 struct ivtv_stream *s = &itv->streams[id->type];
0761 int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0762 __poll_t res = 0;
0763
0764
0765 if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags) &&
0766 s->type != IVTV_ENC_STREAM_TYPE_RAD &&
0767 (req_events & (EPOLLIN | EPOLLRDNORM))) {
0768 int rc;
0769
0770 mutex_lock(&itv->serialize_lock);
0771 rc = ivtv_start_capture(id);
0772 mutex_unlock(&itv->serialize_lock);
0773 if (rc) {
0774 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
0775 s->name, rc);
0776 return EPOLLERR;
0777 }
0778 IVTV_DEBUG_FILE("Encoder poll started capture\n");
0779 }
0780
0781
0782 IVTV_DEBUG_HI_FILE("Encoder poll\n");
0783 poll_wait(filp, &s->waitq, wait);
0784 if (v4l2_event_pending(&id->fh))
0785 res |= EPOLLPRI;
0786 else
0787 poll_wait(filp, &id->fh.wait, wait);
0788
0789 if (s->q_full.length || s->q_io.length)
0790 return res | EPOLLIN | EPOLLRDNORM;
0791 if (eof)
0792 return res | EPOLLHUP;
0793 return res;
0794 }
0795
0796 void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
0797 {
0798 struct ivtv *itv = id->itv;
0799 struct ivtv_stream *s = &itv->streams[id->type];
0800
0801 IVTV_DEBUG_FILE("close() of %s\n", s->name);
0802
0803
0804
0805
0806 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
0807 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
0808
0809 IVTV_DEBUG_INFO("close stopping capture\n");
0810
0811
0812 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
0813 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
0814 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
0815 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
0816 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
0817 }
0818 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
0819 id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
0820 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
0821
0822 s->fh = NULL;
0823 }
0824 else {
0825 ivtv_stop_v4l2_encode_stream(s, gop_end);
0826 }
0827 }
0828 if (!gop_end) {
0829 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0830 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0831 ivtv_release_stream(s);
0832 }
0833 }
0834
0835 static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
0836 {
0837 struct ivtv *itv = id->itv;
0838 struct ivtv_stream *s = &itv->streams[id->type];
0839
0840 IVTV_DEBUG_FILE("close() of %s\n", s->name);
0841
0842 if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
0843 test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
0844
0845 ivtv_yuv_close(itv);
0846 }
0847
0848
0849 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
0850 IVTV_DEBUG_INFO("close stopping decode\n");
0851
0852 ivtv_stop_v4l2_decode_stream(s, flags, pts);
0853 itv->output_mode = OUT_NONE;
0854 }
0855 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
0856 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
0857
0858 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
0859 itv->output_mode = OUT_NONE;
0860
0861 itv->speed = 0;
0862 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
0863 ivtv_release_stream(s);
0864 }
0865
0866 int ivtv_v4l2_close(struct file *filp)
0867 {
0868 struct v4l2_fh *fh = filp->private_data;
0869 struct ivtv_open_id *id = fh2id(fh);
0870 struct ivtv *itv = id->itv;
0871 struct ivtv_stream *s = &itv->streams[id->type];
0872
0873 IVTV_DEBUG_FILE("close %s\n", s->name);
0874
0875 mutex_lock(&itv->serialize_lock);
0876
0877
0878 if (id->type == IVTV_ENC_STREAM_TYPE_RAD &&
0879 v4l2_fh_is_singular_file(filp)) {
0880
0881 ivtv_mute(itv);
0882
0883 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
0884
0885 ivtv_call_all(itv, video, s_std, itv->std);
0886
0887 ivtv_audio_set_io(itv);
0888 if (itv->hw_flags & IVTV_HW_SAA711X) {
0889 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
0890 SAA7115_FREQ_32_11_MHZ, 0);
0891 }
0892 if (atomic_read(&itv->capturing) > 0) {
0893
0894 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
0895 v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
0896 (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
0897 }
0898
0899 ivtv_unmute(itv);
0900 }
0901
0902 v4l2_fh_del(fh);
0903 v4l2_fh_exit(fh);
0904
0905
0906 if (s->fh != &id->fh)
0907 goto close_done;
0908
0909
0910
0911 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
0912 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
0913
0914 ivtv_stop_decoding(id, V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
0915
0916
0917
0918 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
0919
0920 ivtv_disable_cc(itv);
0921 }
0922 } else {
0923 ivtv_stop_capture(id, 0);
0924 }
0925 close_done:
0926 kfree(id);
0927 mutex_unlock(&itv->serialize_lock);
0928 return 0;
0929 }
0930
0931 static int ivtv_open(struct file *filp)
0932 {
0933 struct video_device *vdev = video_devdata(filp);
0934 struct ivtv_stream *s = video_get_drvdata(vdev);
0935 struct ivtv *itv = s->itv;
0936 struct ivtv_open_id *item;
0937 int res = 0;
0938
0939 IVTV_DEBUG_FILE("open %s\n", s->name);
0940
0941 if (ivtv_init_on_first_open(itv)) {
0942 IVTV_ERR("Failed to initialize on device %s\n",
0943 video_device_node_name(vdev));
0944 return -ENXIO;
0945 }
0946
0947 #ifdef CONFIG_VIDEO_ADV_DEBUG
0948
0949 if (ivtv_fw_debug) {
0950 IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
0951 video_device_node_name(vdev));
0952 IVTV_WARN("Selected firmware errors will be ignored\n");
0953 } else {
0954 #else
0955 if (1) {
0956 #endif
0957 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
0958 if (res == -EAGAIN)
0959 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
0960 if (res < 0)
0961 return -EIO;
0962 }
0963
0964 if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
0965 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
0966 return -EBUSY;
0967
0968 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
0969 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
0970 return -EBUSY;
0971
0972 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
0973 if (read_reg(0x82c) == 0) {
0974 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
0975
0976 }
0977 ivtv_udma_alloc(itv);
0978 }
0979
0980
0981 item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
0982 if (NULL == item) {
0983 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
0984 return -ENOMEM;
0985 }
0986 v4l2_fh_init(&item->fh, &s->vdev);
0987 item->itv = itv;
0988 item->type = s->type;
0989
0990 filp->private_data = &item->fh;
0991 v4l2_fh_add(&item->fh);
0992
0993 if (item->type == IVTV_ENC_STREAM_TYPE_RAD &&
0994 v4l2_fh_is_singular_file(filp)) {
0995 if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
0996 if (atomic_read(&itv->capturing) > 0) {
0997
0998
0999 v4l2_fh_del(&item->fh);
1000 v4l2_fh_exit(&item->fh);
1001 kfree(item);
1002 return -EBUSY;
1003 }
1004 }
1005
1006 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
1007
1008 ivtv_mute(itv);
1009
1010 ivtv_call_all(itv, tuner, s_radio);
1011
1012 ivtv_audio_set_io(itv);
1013 if (itv->hw_flags & IVTV_HW_SAA711X) {
1014 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
1015 SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
1016 }
1017
1018 ivtv_unmute(itv);
1019 }
1020
1021
1022 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
1023 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1024 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
1025 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1026
1027 itv->dma_data_req_size =
1028 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
1029 itv->yuv_info.stream_size = 0;
1030 }
1031 return 0;
1032 }
1033
1034 int ivtv_v4l2_open(struct file *filp)
1035 {
1036 struct video_device *vdev = video_devdata(filp);
1037 int res;
1038
1039 if (mutex_lock_interruptible(vdev->lock))
1040 return -ERESTARTSYS;
1041 res = ivtv_open(filp);
1042 mutex_unlock(vdev->lock);
1043 return res;
1044 }
1045
1046 void ivtv_mute(struct ivtv *itv)
1047 {
1048 if (atomic_read(&itv->capturing))
1049 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
1050 IVTV_DEBUG_INFO("Mute\n");
1051 }
1052
1053 void ivtv_unmute(struct ivtv *itv)
1054 {
1055 if (atomic_read(&itv->capturing)) {
1056 ivtv_msleep_timeout(100, 0);
1057 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
1058 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
1059 }
1060 IVTV_DEBUG_INFO("Unmute\n");
1061 }