dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / media / pci / ivtv / ivtv-fileops.c
blob605d280d8a5f00f2f142498532eb2c53f40be8fa
1 /*
2 file operation functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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/saa7115.h>
39 /* This function tries to claim the stream for a specific file descriptor.
40 If no one else is using this stream then the stream is claimed and
41 associated VBI streams are also automatically claimed.
42 Possible error returns: -EBUSY if someone else has claimed
43 the stream or 0 on success. */
44 int ivtv_claim_stream(struct ivtv_open_id *id, int type)
46 struct ivtv *itv = id->itv;
47 struct ivtv_stream *s = &itv->streams[type];
48 struct ivtv_stream *s_vbi;
49 int vbi_type;
51 if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
52 /* someone already claimed this stream */
53 if (s->fh == &id->fh) {
54 /* yes, this file descriptor did. So that's OK. */
55 return 0;
57 if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
58 type == IVTV_ENC_STREAM_TYPE_VBI)) {
59 /* VBI is handled already internally, now also assign
60 the file descriptor to this stream for external
61 reading of the stream. */
62 s->fh = &id->fh;
63 IVTV_DEBUG_INFO("Start Read VBI\n");
64 return 0;
66 /* someone else is using this stream already */
67 IVTV_DEBUG_INFO("Stream %d is busy\n", type);
68 return -EBUSY;
70 s->fh = &id->fh;
71 if (type == IVTV_DEC_STREAM_TYPE_VBI) {
72 /* Enable reinsertion interrupt */
73 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
76 /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
77 IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
78 (provided VBI insertion is on and sliced VBI is selected), for all
79 other streams we're done */
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;
88 s_vbi = &itv->streams[vbi_type];
90 if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
91 /* Enable reinsertion interrupt */
92 if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
93 ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
95 /* mark that it is used internally */
96 set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
97 return 0;
99 EXPORT_SYMBOL(ivtv_claim_stream);
101 /* This function releases a previously claimed stream. It will take into
102 account associated VBI streams. */
103 void ivtv_release_stream(struct ivtv_stream *s)
105 struct ivtv *itv = s->itv;
106 struct ivtv_stream *s_vbi;
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 /* this stream is still in use internally */
112 return;
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;
119 ivtv_flush_queues(s);
121 /* disable reinsertion interrupt */
122 if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
123 ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
125 /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
126 IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
127 for all other streams we're done */
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;
135 /* clear internal use flag */
136 if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
137 /* was already cleared */
138 return;
140 if (s_vbi->fh) {
141 /* VBI stream still claimed by a file descriptor */
142 return;
144 /* disable reinsertion interrupt */
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);
150 EXPORT_SYMBOL(ivtv_release_stream);
152 static void ivtv_dualwatch(struct ivtv *itv)
154 struct v4l2_tuner vt;
155 u32 new_stereo_mode;
156 const u32 dual = 0x02;
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;
164 if (new_stereo_mode == itv->dualwatch_stereo_mode)
165 return;
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");
173 static void ivtv_update_pgm_info(struct ivtv *itv)
175 u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
176 int cnt;
177 int i = 0;
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;
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 // 1=I, 2=P, 4=B
192 e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
193 if (e->offset > itv->mpg_data_received) {
194 break;
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++;
202 itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
205 static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
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);
212 *err = 0;
213 while (1) {
214 if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
215 /* Process pending program info updates and pending VBI data */
216 ivtv_update_pgm_info(itv);
218 if (time_after(jiffies,
219 itv->dualwatch_jiffies +
220 msecs_to_jiffies(1000))) {
221 itv->dualwatch_jiffies = jiffies;
222 ivtv_dualwatch(itv);
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 /* byteswap and process VBI data */
229 ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
230 ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
233 buf = &itv->vbi.sliced_mpeg_buf;
234 if (buf->readpos != buf->bytesused) {
235 return buf;
239 /* do we have leftover data? */
240 buf = ivtv_dequeue(s, &s->q_io);
241 if (buf)
242 return buf;
244 /* do we have new data? */
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 /* byteswap MPG data */
252 ivtv_buf_swap(buf);
253 else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
254 /* byteswap and process VBI data */
255 ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
257 return buf;
260 /* return if end of stream */
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;
266 /* return if file was opened with O_NONBLOCK */
267 if (non_block) {
268 *err = -EAGAIN;
269 return NULL;
272 /* wait for more data to arrive */
273 mutex_unlock(&itv->serialize_lock);
274 prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
275 /* New buffers might have become available before we were added to the waitqueue */
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 /* return if a signal was received */
282 IVTV_DEBUG_INFO("User stopped %s\n", s->name);
283 *err = -EINTR;
284 return NULL;
289 static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
291 int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
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;
298 static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
299 char __user *ubuf, size_t ucount)
301 struct ivtv *itv = s->itv;
302 size_t len = buf->bytesused - buf->readpos;
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;
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;
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;
328 continue;
330 stuffing = q[13] & 7;
331 /* all stuffing bytes must be 0xff */
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;
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;
349 /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
350 buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
351 buf == &itv->vbi.sliced_mpeg_buf); */
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;
358 static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
360 struct ivtv *itv = s->itv;
361 size_t tot_written = 0;
362 int single_frame = 0;
364 if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) {
365 /* shouldn't happen */
366 IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
367 return -EIO;
370 /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
371 arrive one-by-one, so make sure we never output more than one VBI frame at a time */
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;
376 for (;;) {
377 struct ivtv_buffer *buf;
378 int rc;
380 buf = ivtv_get_buffer(s, non_block, &rc);
381 /* if there is no data available... */
382 if (buf == NULL) {
383 /* if we got data, then return that regardless */
384 if (tot_written)
385 break;
386 /* EOS condition */
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);
392 /* set errno */
393 return rc;
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);
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;
405 if (rc < 0)
406 return rc;
407 tot_written += rc;
409 if (tot_written == tot_count || single_frame)
410 break;
412 return tot_written;
415 static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
416 loff_t *pos, int non_block)
418 ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
419 struct ivtv *itv = s->itv;
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;
427 int ivtv_start_capture(struct ivtv_open_id *id)
429 struct ivtv *itv = id->itv;
430 struct ivtv_stream *s = &itv->streams[id->type];
431 struct ivtv_stream *s_vbi;
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 /* you cannot read from these stream types. */
438 return -EINVAL;
441 /* Try to claim this stream. */
442 if (ivtv_claim_stream(id, s->type))
443 return -EBUSY;
445 /* This stream does not need to start capturing */
446 if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
447 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
448 return 0;
451 /* If capture is already in progress, then we also have to
452 do nothing extra. */
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;
458 /* Start VBI capture if required */
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 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
464 automatically when the MPG stream is claimed.
465 We only need to start the VBI capturing. */
466 if (ivtv_start_v4l2_encode_stream(s_vbi)) {
467 IVTV_DEBUG_WARN("VBI capture start failed\n");
469 /* Failure, clean up and return an error */
470 clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
471 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
472 /* also releases the associated VBI stream */
473 ivtv_release_stream(s);
474 return -EIO;
476 IVTV_DEBUG_INFO("VBI insertion started\n");
479 /* Tell the card to start capturing */
480 if (!ivtv_start_v4l2_encode_stream(s)) {
481 /* We're done */
482 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
483 /* Resume a possibly paused encoder */
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;
489 /* failure, clean up */
490 IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
492 /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
493 automatically when the MPG stream is released.
494 We only need to stop the VBI capturing. */
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);
500 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
501 ivtv_release_stream(s);
502 return -EIO;
505 ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
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;
512 IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
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;
523 int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
525 struct ivtv *itv = id->itv;
526 struct ivtv_stream *s = &itv->streams[id->type];
527 int rc;
529 if (atomic_read(&itv->decoding) == 0) {
530 if (ivtv_claim_stream(id, s->type)) {
531 /* someone else is using this stream already */
532 IVTV_DEBUG_WARN("start decode, stream already claimed\n");
533 return -EBUSY;
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;
543 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
544 return ivtv_set_speed(itv, speed);
545 return 0;
548 static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
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);
561 IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
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 /* not decoder streams */
567 return -EINVAL;
569 /* Try to claim this stream */
570 if (ivtv_claim_stream(id, s->type))
571 return -EBUSY;
573 /* This stream does not need to start any decoding */
574 if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
575 int elems = count / sizeof(struct v4l2_sliced_vbi_data);
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);
582 mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
584 if (ivtv_set_output_mode(itv, mode) != mode) {
585 ivtv_release_stream(s);
586 return -EBUSY;
588 ivtv_queue_init(&q);
589 set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
591 /* Start decoder (returns 0 if already started) */
592 rc = ivtv_start_decoding(id, itv->speed);
593 if (rc) {
594 IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
596 /* failure, clean up */
597 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
598 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
599 return rc;
602 retry:
603 /* If possible, just DMA the entire frame - Check the data transfer size
604 since we may get here before the stream has been fully set-up */
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);
609 if (rc < 0)
610 return rc;
612 bytes_written += itv->dma_data_req_size;
613 user_buf += itv->dma_data_req_size;
614 count -= itv->dma_data_req_size;
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;
622 for (;;) {
623 /* Gather buffers */
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);
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 /* New buffers might have become free before we were added to the waitqueue */
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;
646 /* copy user data into buffers */
647 while ((buf = ivtv_dequeue(s, &q))) {
648 /* yuv is a pain. Don't copy more data than needed for a single
649 frame, otherwise we lose sync with the incoming stream */
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);
657 /* Make sure we really got all the user data */
658 if (rc < 0) {
659 ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
660 return rc;
662 user_buf += rc;
663 count -= rc;
664 bytes_written += rc;
666 if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
667 yi->stream_size += rc;
668 /* If we have a complete yuv frame, break loop now */
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;
676 if (buf->bytesused != s->buf_size) {
677 /* incomplete, leave in q_io for next time */
678 ivtv_enqueue(s, buf, &s->q_io);
679 break;
681 /* Byteswap MPEG buffer */
682 if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
683 ivtv_buf_swap(buf);
684 ivtv_enqueue(s, buf, &s->q_full);
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;
691 if (mode == OUT_YUV)
692 ivtv_yuv_setup_stream_frame(itv);
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();
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;
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);
712 /* more user data is available, wait until buffers become free
713 to transfer the rest. */
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;
720 ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
722 struct ivtv_open_id *id = fh2id(filp->private_data);
723 struct ivtv *itv = id->itv;
724 ssize_t res;
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;
733 unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
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;
740 /* add stream's waitq to the poll list */
741 IVTV_DEBUG_HI_FILE("Decoder poll\n");
743 /* If there are subscribed events, then only use the new event
744 API instead of the old video.h based API. */
745 if (!list_empty(&id->fh.subscribed)) {
746 poll_wait(filp, &id->fh.wait, wait);
747 /* Turn off the old-style vsync events */
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 /* This is the old-style API which is here only for backwards
753 compatibility. */
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;
761 /* Allow write if buffers are available for writing */
762 if (s->q_free.buffers)
763 res |= POLLOUT | POLLWRNORM;
764 return res;
767 unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait)
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;
776 /* Start a capture if there is none */
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;
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;
790 IVTV_DEBUG_FILE("Encoder poll started capture\n");
793 /* add stream's waitq to the poll list */
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);
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;
808 void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
810 struct ivtv *itv = id->itv;
811 struct ivtv_stream *s = &itv->streams[id->type];
813 IVTV_DEBUG_FILE("close() of %s\n", s->name);
815 /* 'Unclaim' this stream */
817 /* Stop capturing */
818 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
819 struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
821 IVTV_DEBUG_INFO("close stopping capture\n");
822 /* Special case: a running VBI capture for VBI insertion
823 in the mpeg stream. Need to stop that too. */
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);
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 /* Also used internally, don't stop capturing */
834 s->fh = NULL;
836 else {
837 ivtv_stop_v4l2_encode_stream(s, gop_end);
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);
847 static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
849 struct ivtv *itv = id->itv;
850 struct ivtv_stream *s = &itv->streams[id->type];
852 IVTV_DEBUG_FILE("close() of %s\n", s->name);
854 if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
855 test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
856 /* Restore registers we've changed & clean up any mess */
857 ivtv_yuv_close(itv);
860 /* Stop decoding */
861 if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
862 IVTV_DEBUG_INFO("close stopping decode\n");
864 ivtv_stop_v4l2_decode_stream(s, flags, pts);
865 itv->output_mode = OUT_NONE;
867 clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
868 clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
870 if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
871 itv->output_mode = OUT_NONE;
873 itv->speed = 0;
874 clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
875 ivtv_release_stream(s);
878 int ivtv_v4l2_close(struct file *filp)
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];
885 IVTV_DEBUG_FILE("close %s\n", s->name);
887 mutex_lock(&itv->serialize_lock);
889 /* Stop radio */
890 if (id->type == IVTV_ENC_STREAM_TYPE_RAD &&
891 v4l2_fh_is_singular_file(filp)) {
892 /* Closing radio device, return to TV mode */
893 ivtv_mute(itv);
894 /* Mark that the radio is no longer in use */
895 clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
896 /* Switch tuner to TV */
897 ivtv_call_all(itv, video, s_std, itv->std);
898 /* Select correct audio input (i.e. TV tuner or Line in) */
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);
904 if (atomic_read(&itv->capturing) > 0) {
905 /* Undo video mute */
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));
910 /* Done! Unmute and continue. */
911 ivtv_unmute(itv);
914 v4l2_fh_del(fh);
915 v4l2_fh_exit(fh);
917 /* Easy case first: this stream was never claimed by us */
918 if (s->fh != &id->fh)
919 goto close_done;
921 /* 'Unclaim' this stream */
923 if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
924 struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
926 ivtv_stop_decoding(id, V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY, 0);
928 /* If all output streams are closed, and if the user doesn't have
929 IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
930 if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
931 /* disable CC on TV-out */
932 ivtv_disable_cc(itv);
934 } else {
935 ivtv_stop_capture(id, 0);
937 close_done:
938 kfree(id);
939 mutex_unlock(&itv->serialize_lock);
940 return 0;
943 static int ivtv_open(struct file *filp)
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;
951 IVTV_DEBUG_FILE("open %s\n", s->name);
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;
959 #ifdef CONFIG_VIDEO_ADV_DEBUG
960 /* Unless ivtv_fw_debug is set, error out if firmware dead. */
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;
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;
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;
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 /* return -ENODEV; */
989 ivtv_udma_alloc(itv);
992 /* Allocate memory */
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;
998 v4l2_fh_init(&item->fh, &s->vdev);
999 item->itv = itv;
1000 item->type = s->type;
1002 filp->private_data = &item->fh;
1003 v4l2_fh_add(&item->fh);
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 /* switching to radio while capture is
1010 in progress is not polite */
1011 v4l2_fh_del(&item->fh);
1012 v4l2_fh_exit(&item->fh);
1013 kfree(item);
1014 return -EBUSY;
1017 /* Mark that the radio is being used. */
1018 set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
1019 /* We have the radio */
1020 ivtv_mute(itv);
1021 /* Switch tuner to radio */
1022 ivtv_call_all(itv, tuner, s_radio);
1023 /* Select the correct audio input (i.e. radio tuner) */
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);
1029 /* Done! Unmute and continue. */
1030 ivtv_unmute(itv);
1033 /* YUV or MPG Decoding Mode? */
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 /* For yuv, we need to know the dma size before we start */
1039 itv->dma_data_req_size =
1040 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
1041 itv->yuv_info.stream_size = 0;
1043 return 0;
1046 int ivtv_v4l2_open(struct file *filp)
1048 struct video_device *vdev = video_devdata(filp);
1049 int res;
1051 if (mutex_lock_interruptible(vdev->lock))
1052 return -ERESTARTSYS;
1053 res = ivtv_open(filp);
1054 mutex_unlock(vdev->lock);
1055 return res;
1058 void ivtv_mute(struct ivtv *itv)
1060 if (atomic_read(&itv->capturing))
1061 ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
1062 IVTV_DEBUG_INFO("Mute\n");
1065 void ivtv_unmute(struct ivtv *itv)
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);
1072 IVTV_DEBUG_INFO("Unmute\n");