1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "ivtv-driver.h"
23#include "ivtv-fileops.h"
24#include "ivtv-i2c.h"
25#include "ivtv-queue.h"
26#include "ivtv-udma.h"
27#include "ivtv-irq.h"
28#include "ivtv-vbi.h"
29#include "ivtv-mailbox.h"
30#include "ivtv-routing.h"
31#include "ivtv-streams.h"
32#include "ivtv-yuv.h"
33#include "ivtv-ioctl.h"
34#include "ivtv-cards.h"
35#include "ivtv-firmware.h"
36#include <media/v4l2-event.h>
37#include <media/i2c/saa7115.h>
38
39
40
41
42
43
44int ivtv_claim_stream(struct ivtv_open_id *id, int type)
45{
46 struct ivtv *itv = id->itv;
47 struct ivtv_stream *s = &itv->streams[type];
48 struct ivtv_stream *s_vbi;
49 int vbi_type;
50
51 if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
52
53 if (s->fh == &id->fh) {
54
55 return 0;
56 }
57 if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
58 type == IVTV_ENC_STREAM_TYPE_VBI)) {
59
60
61
62 s->fh = &id->fh;
63 IVTV_DEBUG_INFO("Start Read VBI\n");
64 return 0;
65 }
66
67 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
68 return -EBUSY;
69 }
70 s->fh = &id->fh;
71 if (type == IVTV_DEC_STREAM_TYPE_VBI) {
72
73 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
74 }
75
76
77
78
79
80 if (type == IVTV_DEC_STREAM_TYPE_MPG) {
81 vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
82 } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
83 itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
84 vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
85 } else {
86 return 0;
87 }
88 s_vbi = &itv->streams[vbi_type];
89
90 if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
91
92 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
93 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
94 }
95
96 set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
97 return 0;
98}
99EXPORT_SYMBOL(ivtv_claim_stream);
100
101
102
103void ivtv_release_stream(struct ivtv_stream *s)
104{
105 struct ivtv *itv = s->itv;
106 struct ivtv_stream *s_vbi;
107
108 s->fh = NULL;
109 if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
110 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
111
112 return;
113 }
114 if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
115 IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
116 return;
117 }
118
119 ivtv_flush_queues(s);
120
121
122 if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
123 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
124
125
126
127
128 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
129 s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
130 else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
131 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
132 else
133 return;
134
135
136 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
137
138 return;
139 }
140 if (s_vbi->fh) {
141
142 return;
143 }
144
145 if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
146 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
147 clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
148 ivtv_flush_queues(s_vbi);
149}
150EXPORT_SYMBOL(ivtv_release_stream);
151
152static void ivtv_dualwatch(struct ivtv *itv)
153{
154 struct v4l2_tuner vt;
155 u32 new_stereo_mode;
156 const u32 dual = 0x02;
157
158 new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
159 memset(&vt, 0, sizeof(vt));
160 ivtv_call_all(itv, tuner, g_tuner, &vt);
161 if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
162 new_stereo_mode = dual;
163
164 if (new_stereo_mode == itv->dualwatch_stereo_mode)
165 return;
166
167 IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
168 itv->dualwatch_stereo_mode, new_stereo_mode);
169 if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
170 IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
171}
172
173static void ivtv_update_pgm_info(struct ivtv *itv)
174{
175 u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
176 int cnt;
177 int i = 0;
178
179 if (wr_idx >= itv->pgm_info_num) {
180 IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
181 return;
182 }
183 cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
184 while (i < cnt) {
185 int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
186 struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
187 u32 addr = itv->pgm_info_offset + 4 + idx * 24;
188 const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
189 V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
190
191
192 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
193 if (e->offset > itv->mpg_data_received) {
194 break;
195 }
196 e->offset += itv->vbi_data_inserted;
197 e->length = read_enc(addr);
198 e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
199 e->flags = mapping[read_enc(addr + 12) & 7];
200 i++;
201 }
202 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
203}
204
205static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
206{
207 struct ivtv *itv = s->itv;
208 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
209 struct ivtv_buffer *buf;
210 DEFINE_WAIT(wait);
211
212 *err = 0;
213 while (1) {
214 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
215
216 ivtv_update_pgm_info(itv);
217
218 if (time_after(jiffies,
219 itv->dualwatch_jiffies +
220 msecs_to_jiffies(1000))) {
221 itv->dualwatch_jiffies = jiffies;
222 ivtv_dualwatch(itv);
223 }
224
225 if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
226 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
227 while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
228
229 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
230 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
231 }
232 }
233 buf = &itv->vbi.sliced_mpeg_buf;
234 if (buf->readpos != buf->bytesused) {
235 return buf;
236 }
237 }
238
239
240 buf = ivtv_dequeue(s, &s->q_io);
241 if (buf)
242 return buf;
243
244
245 buf = ivtv_dequeue(s, &s->q_full);
246 if (buf) {
247 if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
248 return buf;
249 buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
250 if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
251
252 ivtv_buf_swap(buf);
253 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
254
255 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
256 }
257 return buf;
258 }
259
260
261 if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
262 IVTV_DEBUG_INFO("EOS %s\n", s->name);
263 return NULL;
264 }
265
266
267 if (non_block) {
268 *err = -EAGAIN;
269 return NULL;
270 }
271
272
273 mutex_unlock(&itv->serialize_lock);
274 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
275
276 if (!s->q_full.buffers)
277 schedule();
278 finish_wait(&s->waitq, &wait);
279 mutex_lock(&itv->serialize_lock);
280 if (signal_pending(current)) {
281
282 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
283 *err = -EINTR;
284 return NULL;
285 }
286 }
287}
288
289static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
290{
291 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
292
293 itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
294 itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
295 itv->vbi.sliced_mpeg_buf.readpos = 0;
296}
297
298static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
299 char __user *ubuf, size_t ucount)
300{
301 struct ivtv *itv = s->itv;
302 size_t len = buf->bytesused - buf->readpos;
303
304 if (len > ucount) len = ucount;
305 if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
306 !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
307 const char *start = buf->buf + buf->readpos;
308 const char *p = start + 1;
309 const u8 *q;
310 u8 ch = itv->search_pack_header ? 0xba : 0xe0;
311 int stuffing, i;
312
313 while (start + len > p && (q = memchr(p, 0, start + len - p))) {
314 p = q + 1;
315 if ((char *)q + 15 >= buf->buf + buf->bytesused ||
316 q[1] != 0 || q[2] != 1 || q[3] != ch) {
317 continue;
318 }
319 if (!itv->search_pack_header) {
320 if ((q[6] & 0xc0) != 0x80)
321 continue;
322 if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
323 ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
324 ch = 0xba;
325 itv->search_pack_header = 1;
326 p = q + 9;
327 }
328 continue;
329 }
330 stuffing = q[13] & 7;
331
332 for (i = 0; i < stuffing; i++)
333 if (q[14 + i] != 0xff)
334 break;
335 if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
336 q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
337 q[16 + stuffing] == 1) {
338 itv->search_pack_header = 0;
339 len = (char *)q - start;
340 ivtv_setup_sliced_vbi_buf(itv);
341 break;
342 }
343 }
344 }
345 if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
346 IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
347 return -EFAULT;
348 }
349
350
351
352 buf->readpos += len;
353 if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
354 itv->mpg_data_received += len;
355 return len;
356}
357
358static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
359{
360 struct ivtv *itv = s->itv;
361 size_t tot_written = 0;
362 int single_frame = 0;
363
364 if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) {
365
366 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
367 return -EIO;
368 }
369
370
371
372 if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
373 (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
374 single_frame = 1;
375
376 for (;;) {
377 struct ivtv_buffer *buf;
378 int rc;
379
380 buf = ivtv_get_buffer(s, non_block, &rc);
381
382 if (buf == NULL) {
383
384 if (tot_written)
385 break;
386
387 if (rc == 0) {
388 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
389 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
390 ivtv_release_stream(s);
391 }
392
393 return rc;
394 }
395 rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
396 if (buf != &itv->vbi.sliced_mpeg_buf) {
397 ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
398 }
399 else if (buf->readpos == buf->bytesused) {
400 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
401 itv->vbi.sliced_mpeg_size[idx] = 0;
402 itv->vbi.inserted_frame++;
403 itv->vbi_data_inserted += buf->bytesused;
404 }
405 if (rc < 0)
406 return rc;
407 tot_written += rc;
408
409 if (tot_written == tot_count || single_frame)
410 break;
411 }
412 return tot_written;
413}
414
415static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
416 loff_t *pos, int non_block)
417{
418 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
419 struct ivtv *itv = s->itv;
420
421 IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
422 if (rc > 0)
423 pos += rc;
424 return rc;
425}
426
427int ivtv_start_capture(struct ivtv_open_id *id)
428{
429 struct ivtv *itv = id->itv;
430 struct ivtv_stream *s = &itv->streams[id->type];
431 struct ivtv_stream *s_vbi;
432
433 if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
434 s->type == IVTV_DEC_STREAM_TYPE_MPG ||
435 s->type == IVTV_DEC_STREAM_TYPE_YUV ||
436 s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
437
438 return -EINVAL;
439 }
440
441
442 if (ivtv_claim_stream(id, s->type))
443 return -EBUSY;
444
445
446 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
447 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
448 return 0;
449 }
450
451
452
453 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
454 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
455 return 0;
456 }
457
458
459 s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
460 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
461 test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
462 !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
463
464
465
466 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
467 IVTV_DEBUG_WARN("VBI capture start failed\n");
468
469
470 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
471 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
472
473 ivtv_release_stream(s);
474 return -EIO;
475 }
476 IVTV_DEBUG_INFO("VBI insertion started\n");
477 }
478
479
480 if (!ivtv_start_v4l2_encode_stream(s)) {
481
482 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
483
484 if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
485 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
486 return 0;
487 }
488
489
490 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
491
492
493
494
495 if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
496 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
497 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
498 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
499 }
500 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
501 ivtv_release_stream(s);
502 return -EIO;
503}
504
505ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
506{
507 struct ivtv_open_id *id = fh2id(filp->private_data);
508 struct ivtv *itv = id->itv;
509 struct ivtv_stream *s = &itv->streams[id->type];
510 ssize_t rc;
511
512 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
513
514 if (mutex_lock_interruptible(&itv->serialize_lock))
515 return -ERESTARTSYS;
516 rc = ivtv_start_capture(id);
517 if (!rc)
518 rc = ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
519 mutex_unlock(&itv->serialize_lock);
520 return rc;
521}
522
523int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
524{
525 struct ivtv *itv = id->itv;
526 struct ivtv_stream *s = &itv->streams[id->type];
527 int rc;
528
529 if (atomic_read(&itv->decoding) == 0) {
530 if (ivtv_claim_stream(id, s->type)) {
531
532 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
533 return -EBUSY;
534 }
535 rc = ivtv_start_v4l2_decode_stream(s, 0);
536 if (rc < 0) {
537 if (rc == -EAGAIN)
538 rc = ivtv_start_v4l2_decode_stream(s, 0);
539 if (rc < 0)
540 return rc;
541 }
542 }
543 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
544 return ivtv_set_speed(itv, speed);
545 return 0;
546}
547
548static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
549{
550 struct ivtv_open_id *id = fh2id(filp->private_data);
551 struct ivtv *itv = id->itv;
552 struct ivtv_stream *s = &itv->streams[id->type];
553 struct yuv_playback_info *yi = &itv->yuv_info;
554 struct ivtv_buffer *buf;
555 struct ivtv_queue q;
556 int bytes_written = 0;
557 int mode;
558 int rc;
559 DEFINE_WAIT(wait);
560
561 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
562
563 if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
564 s->type != IVTV_DEC_STREAM_TYPE_YUV &&
565 s->type != IVTV_DEC_STREAM_TYPE_VOUT)
566
567 return -EINVAL;
568
569
570 if (ivtv_claim_stream(id, s->type))
571 return -EBUSY;
572
573
574 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
575 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
576
577 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
578 return ivtv_write_vbi_from_user(itv,
579 (const struct v4l2_sliced_vbi_data __user *)user_buf, elems);
580 }
581
582 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
583
584 if (ivtv_set_output_mode(itv, mode) != mode) {
585 ivtv_release_stream(s);
586 return -EBUSY;
587 }
588 ivtv_queue_init(&q);
589 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
590
591
592 rc = ivtv_start_decoding(id, itv->speed);
593 if (rc) {
594 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
595
596
597 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
598 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
599 return rc;
600 }
601
602retry:
603
604
605 if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
606 while (count >= itv->dma_data_req_size) {
607 rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
608
609 if (rc < 0)
610 return rc;
611
612 bytes_written += itv->dma_data_req_size;
613 user_buf += itv->dma_data_req_size;
614 count -= itv->dma_data_req_size;
615 }
616 if (count == 0) {
617 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
618 return bytes_written;
619 }
620 }
621
622 for (;;) {
623
624 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
625 ivtv_enqueue(s, buf, &q);
626 while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
627 ivtv_enqueue(s, buf, &q);
628 }
629 if (q.buffers)
630 break;
631 if (filp->f_flags & O_NONBLOCK)
632 return -EAGAIN;
633 mutex_unlock(&itv->serialize_lock);
634 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
635
636 if (!s->q_free.buffers)
637 schedule();
638 finish_wait(&s->waitq, &wait);
639 mutex_lock(&itv->serialize_lock);
640 if (signal_pending(current)) {
641 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
642 return -EINTR;
643 }
644 }
645
646
647 while ((buf = ivtv_dequeue(s, &q))) {
648
649
650 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
651 yi->stream_size + count > itv->dma_data_req_size)
652 rc = ivtv_buf_copy_from_user(s, buf, user_buf,
653 itv->dma_data_req_size - yi->stream_size);
654 else
655 rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
656
657
658 if (rc < 0) {
659 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
660 return rc;
661 }
662 user_buf += rc;
663 count -= rc;
664 bytes_written += rc;
665
666 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
667 yi->stream_size += rc;
668
669 if (yi->stream_size == itv->dma_data_req_size) {
670 ivtv_enqueue(s, buf, &s->q_full);
671 yi->stream_size = 0;
672 break;
673 }
674 }
675
676 if (buf->bytesused != s->buf_size) {
677
678 ivtv_enqueue(s, buf, &s->q_io);
679 break;
680 }
681
682 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
683 ivtv_buf_swap(buf);
684 ivtv_enqueue(s, buf, &s->q_full);
685 }
686
687 if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
688 if (s->q_full.length >= itv->dma_data_req_size) {
689 int got_sig;
690
691 if (mode == OUT_YUV)
692 ivtv_yuv_setup_stream_frame(itv);
693
694 mutex_unlock(&itv->serialize_lock);
695 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
696 while (!(got_sig = signal_pending(current)) &&
697 test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
698 schedule();
699 }
700 finish_wait(&itv->dma_waitq, &wait);
701 mutex_lock(&itv->serialize_lock);
702 if (got_sig) {
703 IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
704 return -EINTR;
705 }
706
707 clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
708 ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
709 ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
710 }
711 }
712
713
714 if (count && !(filp->f_flags & O_NONBLOCK))
715 goto retry;
716 IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
717 return bytes_written;
718}
719
720ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
721{
722 struct ivtv_open_id *id = fh2id(filp->private_data);
723 struct ivtv *itv = id->itv;
724 ssize_t res;
725
726 if (mutex_lock_interruptible(&itv->serialize_lock))
727 return -ERESTARTSYS;
728 res = ivtv_write(filp, user_buf, count, pos);
729 mutex_unlock(&itv->serialize_lock);
730 return res;
731}
732
733unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
734{
735 struct ivtv_open_id *id = fh2id(filp->private_data);
736 struct ivtv *itv = id->itv;
737 struct ivtv_stream *s = &itv->streams[id->type];
738 int res = 0;
739
740
741 IVTV_DEBUG_HI_FILE("Decoder poll\n");
742
743
744
745 if (!list_empty(&id->fh.subscribed)) {
746 poll_wait(filp, &id->fh.wait, wait);
747
748 clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
749 if (v4l2_event_pending(&id->fh))
750 res = POLLPRI;
751 } else {
752
753
754 poll_wait(filp, &s->waitq, wait);
755 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
756 if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
757 test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
758 res = POLLPRI;
759 }
760
761
762 if (s->q_free.buffers)
763 res |= POLLOUT | POLLWRNORM;
764 return res;
765}
766
767unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait)
768{
769 unsigned long req_events = poll_requested_events(wait);
770 struct ivtv_open_id *id = fh2id(filp->private_data);
771 struct ivtv *itv = id->itv;
772 struct ivtv_stream *s = &itv->streams[id->type];
773 int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
774 unsigned res = 0;
775
776
777 if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags) &&
778 s->type != IVTV_ENC_STREAM_TYPE_RAD &&
779 (req_events & (POLLIN | POLLRDNORM))) {
780 int rc;
781
782 mutex_lock(&itv->serialize_lock);
783 rc = ivtv_start_capture(id);
784 mutex_unlock(&itv->serialize_lock);
785 if (rc) {
786 IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
787 s->name, rc);
788 return POLLERR;
789 }
790 IVTV_DEBUG_FILE("Encoder poll started capture\n");
791 }
792
793
794 IVTV_DEBUG_HI_FILE("Encoder poll\n");
795 poll_wait(filp, &s->waitq, wait);
796 if (v4l2_event_pending(&id->fh))
797 res |= POLLPRI;
798 else
799 poll_wait(filp, &id->fh.wait, wait);
800
801 if (s->q_full.length || s->q_io.length)
802 return res | POLLIN | POLLRDNORM;
803 if (eof)
804 return res | POLLHUP;
805 return res;
806}
807
808void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
809{
810 struct ivtv *itv = id->itv;
811 struct ivtv_stream *s = &itv->streams[id->type];
812
813 IVTV_DEBUG_FILE("close() of %s\n", s->name);
814
815
816
817
818 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
819 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
820
821 IVTV_DEBUG_INFO("close stopping capture\n");
822
823
824 if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
825 test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
826 !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
827 IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
828 ivtv_stop_v4l2_encode_stream(s_vbi, 0);
829 }
830 if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
831 id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
832 test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
833
834 s->fh = NULL;
835 }
836 else {
837 ivtv_stop_v4l2_encode_stream(s, gop_end);
838 }
839 }
840 if (!gop_end) {
841 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
842 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
843 ivtv_release_stream(s);
844 }
845}
846
847static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
848{
849 struct ivtv *itv = id->itv;
850 struct ivtv_stream *s = &itv->streams[id->type];
851
852 IVTV_DEBUG_FILE("close() of %s\n", s->name);
853
854 if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
855 test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
856
857 ivtv_yuv_close(itv);
858 }
859
860
861 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
862 IVTV_DEBUG_INFO("close stopping decode\n");
863
864 ivtv_stop_v4l2_decode_stream(s, flags, pts);
865 itv->output_mode = OUT_NONE;
866 }
867 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
868 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
869
870 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
871 itv->output_mode = OUT_NONE;
872
873 itv->speed = 0;
874 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
875 ivtv_release_stream(s);
876}
877
878int ivtv_v4l2_close(struct file *filp)
879{
880 struct v4l2_fh *fh = filp->private_data;
881 struct ivtv_open_id *id = fh2id(fh);
882 struct ivtv *itv = id->itv;
883 struct ivtv_stream *s = &itv->streams[id->type];
884
885 IVTV_DEBUG_FILE("close %s\n", s->name);
886
887 mutex_lock(&itv->serialize_lock);
888
889
890 if (id->type == IVTV_ENC_STREAM_TYPE_RAD &&
891 v4l2_fh_is_singular_file(filp)) {
892
893 ivtv_mute(itv);
894
895 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
896
897 ivtv_call_all(itv, video, s_std, itv->std);
898
899 ivtv_audio_set_io(itv);
900 if (itv->hw_flags & IVTV_HW_SAA711X) {
901 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
902 SAA7115_FREQ_32_11_MHZ, 0);
903 }
904 if (atomic_read(&itv->capturing) > 0) {
905
906 ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
907 v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
908 (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
909 }
910
911 ivtv_unmute(itv);
912 }
913
914 v4l2_fh_del(fh);
915 v4l2_fh_exit(fh);
916
917
918 if (s->fh != &id->fh)
919 goto close_done;
920
921
922
923 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
924 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
925
926 ivtv_stop_decoding(id, V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
927
928
929
930 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
931
932 ivtv_disable_cc(itv);
933 }
934 } else {
935 ivtv_stop_capture(id, 0);
936 }
937close_done:
938 kfree(id);
939 mutex_unlock(&itv->serialize_lock);
940 return 0;
941}
942
943static int ivtv_open(struct file *filp)
944{
945 struct video_device *vdev = video_devdata(filp);
946 struct ivtv_stream *s = video_get_drvdata(vdev);
947 struct ivtv *itv = s->itv;
948 struct ivtv_open_id *item;
949 int res = 0;
950
951 IVTV_DEBUG_FILE("open %s\n", s->name);
952
953 if (ivtv_init_on_first_open(itv)) {
954 IVTV_ERR("Failed to initialize on device %s\n",
955 video_device_node_name(vdev));
956 return -ENXIO;
957 }
958
959#ifdef CONFIG_VIDEO_ADV_DEBUG
960
961 if (ivtv_fw_debug) {
962 IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
963 video_device_node_name(vdev));
964 IVTV_WARN("Selected firmware errors will be ignored\n");
965 } else {
966#else
967 if (1) {
968#endif
969 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
970 if (res == -EAGAIN)
971 res = ivtv_firmware_check(itv, "ivtv_serialized_open");
972 if (res < 0)
973 return -EIO;
974 }
975
976 if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
977 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
978 return -EBUSY;
979
980 if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
981 test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
982 return -EBUSY;
983
984 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
985 if (read_reg(0x82c) == 0) {
986 IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
987
988 }
989 ivtv_udma_alloc(itv);
990 }
991
992
993 item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
994 if (NULL == item) {
995 IVTV_DEBUG_WARN("nomem on v4l2 open\n");
996 return -ENOMEM;
997 }
998 v4l2_fh_init(&item->fh, &s->vdev);
999 item->itv = itv;
1000 item->type = s->type;
1001
1002 filp->private_data = &item->fh;
1003 v4l2_fh_add(&item->fh);
1004
1005 if (item->type == IVTV_ENC_STREAM_TYPE_RAD &&
1006 v4l2_fh_is_singular_file(filp)) {
1007 if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1008 if (atomic_read(&itv->capturing) > 0) {
1009
1010
1011 v4l2_fh_del(&item->fh);
1012 v4l2_fh_exit(&item->fh);
1013 kfree(item);
1014 return -EBUSY;
1015 }
1016 }
1017
1018 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
1019
1020 ivtv_mute(itv);
1021
1022 ivtv_call_all(itv, tuner, s_radio);
1023
1024 ivtv_audio_set_io(itv);
1025 if (itv->hw_flags & IVTV_HW_SAA711X) {
1026 ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
1027 SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
1028 }
1029
1030 ivtv_unmute(itv);
1031 }
1032
1033
1034 if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
1035 clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1036 } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
1037 set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
1038
1039 itv->dma_data_req_size =
1040 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
1041 itv->yuv_info.stream_size = 0;
1042 }
1043 return 0;
1044}
1045
1046int ivtv_v4l2_open(struct file *filp)
1047{
1048 struct video_device *vdev = video_devdata(filp);
1049 int res;
1050
1051 if (mutex_lock_interruptible(vdev->lock))
1052 return -ERESTARTSYS;
1053 res = ivtv_open(filp);
1054 mutex_unlock(vdev->lock);
1055 return res;
1056}
1057
1058void ivtv_mute(struct ivtv *itv)
1059{
1060 if (atomic_read(&itv->capturing))
1061 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
1062 IVTV_DEBUG_INFO("Mute\n");
1063}
1064
1065void ivtv_unmute(struct ivtv *itv)
1066{
1067 if (atomic_read(&itv->capturing)) {
1068 ivtv_msleep_timeout(100, 0);
1069 ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
1070 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
1071 }
1072 IVTV_DEBUG_INFO("Unmute\n");
1073}
1074