1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     file operation 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@xs4all.nl>
7 
8  */
9 
10 #include "ivtv-driver.h"
11 #include "ivtv-fileops.h"
12 #include "ivtv-i2c.h"
13 #include "ivtv-queue.h"
14 #include "ivtv-udma.h"
15 #include "ivtv-irq.h"
16 #include "ivtv-vbi.h"
17 #include "ivtv-mailbox.h"
18 #include "ivtv-routing.h"
19 #include "ivtv-streams.h"
20 #include "ivtv-yuv.h"
21 #include "ivtv-ioctl.h"
22 #include "ivtv-cards.h"
23 #include "ivtv-firmware.h"
24 #include <linux/lockdep.h>
25 #include <media/v4l2-event.h>
26 #include <media/i2c/saa7115.h>
27 
28 /* This function tries to claim the stream for a specific file descriptor.
29    If no one else is using this stream then the stream is claimed and
30    associated VBI streams are also automatically claimed.
31    Possible error returns: -EBUSY if someone else has claimed
32    the stream or 0 on success. */
ivtv_claim_stream(struct ivtv_open_id * id,int type)33 int ivtv_claim_stream(struct ivtv_open_id *id, int type)
34 {
35 	struct ivtv *itv = id->itv;
36 	struct ivtv_stream *s = &itv->streams[type];
37 	struct ivtv_stream *s_vbi;
38 	int vbi_type;
39 
40 	if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
41 		/* someone already claimed this stream */
42 		if (s->fh == &id->fh) {
43 			/* yes, this file descriptor did. So that's OK. */
44 			return 0;
45 		}
46 		if (s->fh == NULL && (type == IVTV_DEC_STREAM_TYPE_VBI ||
47 					 type == IVTV_ENC_STREAM_TYPE_VBI)) {
48 			/* VBI is handled already internally, now also assign
49 			   the file descriptor to this stream for external
50 			   reading of the stream. */
51 			s->fh = &id->fh;
52 			IVTV_DEBUG_INFO("Start Read VBI\n");
53 			return 0;
54 		}
55 		/* someone else is using this stream already */
56 		IVTV_DEBUG_INFO("Stream %d is busy\n", type);
57 		return -EBUSY;
58 	}
59 	s->fh = &id->fh;
60 	if (type == IVTV_DEC_STREAM_TYPE_VBI) {
61 		/* Enable reinsertion interrupt */
62 		ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
63 	}
64 
65 	/* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
66 	   IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
67 	   (provided VBI insertion is on and sliced VBI is selected), for all
68 	   other streams we're done */
69 	if (type == IVTV_DEC_STREAM_TYPE_MPG) {
70 		vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
71 	} else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
72 		   itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
73 		vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
74 	} else {
75 		return 0;
76 	}
77 	s_vbi = &itv->streams[vbi_type];
78 
79 	if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
80 		/* Enable reinsertion interrupt */
81 		if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
82 			ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
83 	}
84 	/* mark that it is used internally */
85 	set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
86 	return 0;
87 }
88 EXPORT_SYMBOL(ivtv_claim_stream);
89 
90 /* This function releases a previously claimed stream. It will take into
91    account associated VBI streams. */
ivtv_release_stream(struct ivtv_stream * s)92 void ivtv_release_stream(struct ivtv_stream *s)
93 {
94 	struct ivtv *itv = s->itv;
95 	struct ivtv_stream *s_vbi;
96 
97 	s->fh = NULL;
98 	if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
99 		test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
100 		/* this stream is still in use internally */
101 		return;
102 	}
103 	if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
104 		IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
105 		return;
106 	}
107 
108 	ivtv_flush_queues(s);
109 
110 	/* disable reinsertion interrupt */
111 	if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
112 		ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
113 
114 	/* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
115 	   IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
116 	   for all other streams we're done */
117 	if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
118 		s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
119 	else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
120 		s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
121 	else
122 		return;
123 
124 	/* clear internal use flag */
125 	if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
126 		/* was already cleared */
127 		return;
128 	}
129 	if (s_vbi->fh) {
130 		/* VBI stream still claimed by a file descriptor */
131 		return;
132 	}
133 	/* disable reinsertion interrupt */
134 	if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
135 		ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
136 	clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
137 	ivtv_flush_queues(s_vbi);
138 }
139 EXPORT_SYMBOL(ivtv_release_stream);
140 
ivtv_dualwatch(struct ivtv * itv)141 static void ivtv_dualwatch(struct ivtv *itv)
142 {
143 	struct v4l2_tuner vt;
144 	u32 new_stereo_mode;
145 	const u32 dual = 0x02;
146 
147 	new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
148 	memset(&vt, 0, sizeof(vt));
149 	ivtv_call_all(itv, tuner, g_tuner, &vt);
150 	if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
151 		new_stereo_mode = dual;
152 
153 	if (new_stereo_mode == itv->dualwatch_stereo_mode)
154 		return;
155 
156 	IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
157 			   itv->dualwatch_stereo_mode, new_stereo_mode);
158 	if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
159 		IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
160 }
161 
ivtv_update_pgm_info(struct ivtv * itv)162 static void ivtv_update_pgm_info(struct ivtv *itv)
163 {
164 	u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
165 	int cnt;
166 	int i = 0;
167 
168 	if (wr_idx >= itv->pgm_info_num) {
169 		IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
170 		return;
171 	}
172 	cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
173 	while (i < cnt) {
174 		int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
175 		struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
176 		u32 addr = itv->pgm_info_offset + 4 + idx * 24;
177 		const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
178 			V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
179 					// 1=I, 2=P, 4=B
180 
181 		e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
182 		if (e->offset > itv->mpg_data_received) {
183 			break;
184 		}
185 		e->offset += itv->vbi_data_inserted;
186 		e->length = read_enc(addr);
187 		e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
188 		e->flags = mapping[read_enc(addr + 12) & 7];
189 		i++;
190 	}
191 	itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
192 }
193 
ivtv_schedule(struct ivtv_stream * s)194 static void ivtv_schedule(struct ivtv_stream *s)
195 {
196 	struct ivtv *itv = s->itv;
197 	DEFINE_WAIT(wait);
198 
199 	lockdep_assert_held(&itv->serialize_lock);
200 
201 	mutex_unlock(&itv->serialize_lock);
202 	prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
203 	/* New buffers might have become free before we were added to the waitqueue */
204 	if (!s->q_free.buffers)
205 		schedule();
206 	finish_wait(&s->waitq, &wait);
207 	mutex_lock(&itv->serialize_lock);
208 }
209 
ivtv_get_buffer(struct ivtv_stream * s,int non_block,int * err)210 static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
211 {
212 	struct ivtv *itv = s->itv;
213 	struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
214 	struct ivtv_buffer *buf;
215 
216 	*err = 0;
217 	while (1) {
218 		if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
219 			/* Process pending program info updates and pending VBI data */
220 			ivtv_update_pgm_info(itv);
221 
222 			if (time_after(jiffies,
223 				       itv->dualwatch_jiffies +
224 				       msecs_to_jiffies(1000))) {
225 				itv->dualwatch_jiffies = jiffies;
226 				ivtv_dualwatch(itv);
227 			}
228 
229 			if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
230 			    !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
231 				while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
232 					/* byteswap and process VBI data */
233 					ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
234 					ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
235 				}
236 			}
237 			buf = &itv->vbi.sliced_mpeg_buf;
238 			if (buf->readpos != buf->bytesused) {
239 				return buf;
240 			}
241 		}
242 
243 		/* do we have leftover data? */
244 		buf = ivtv_dequeue(s, &s->q_io);
245 		if (buf)
246 			return buf;
247 
248 		/* do we have new data? */
249 		buf = ivtv_dequeue(s, &s->q_full);
250 		if (buf) {
251 			if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
252 				return buf;
253 			buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
254 			if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
255 				/* byteswap MPG data */
256 				ivtv_buf_swap(buf);
257 			else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
258 				/* byteswap and process VBI data */
259 				ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
260 			}
261 			return buf;
262 		}
263 
264 		/* return if end of stream */
265 		if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
266 			IVTV_DEBUG_INFO("EOS %s\n", s->name);
267 			return NULL;
268 		}
269 
270 		/* return if file was opened with O_NONBLOCK */
271 		if (non_block) {
272 			*err = -EAGAIN;
273 			return NULL;
274 		}
275 
276 		/* wait for more data to arrive */
277 		ivtv_schedule(s);
278 		if (signal_pending(current)) {
279 			/* return if a signal was received */
280 			IVTV_DEBUG_INFO("User stopped %s\n", s->name);
281 			*err = -EINTR;
282 			return NULL;
283 		}
284 	}
285 }
286 
ivtv_setup_sliced_vbi_buf(struct ivtv * itv)287 static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
288 {
289 	int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
290 
291 	itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
292 	itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
293 	itv->vbi.sliced_mpeg_buf.readpos = 0;
294 }
295 
ivtv_copy_buf_to_user(struct ivtv_stream * s,struct ivtv_buffer * buf,char __user * ubuf,size_t ucount)296 static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
297 		char __user *ubuf, size_t ucount)
298 {
299 	struct ivtv *itv = s->itv;
300 	size_t len = buf->bytesused - buf->readpos;
301 
302 	if (len > ucount) len = ucount;
303 	if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
304 	    !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
305 		const char *start = buf->buf + buf->readpos;
306 		const char *p = start + 1;
307 		const u8 *q;
308 		u8 ch = itv->search_pack_header ? 0xba : 0xe0;
309 		int stuffing, i;
310 
311 		while (start + len > p && (q = memchr(p, 0, start + len - p))) {
312 			p = q + 1;
313 			if ((char *)q + 15 >= buf->buf + buf->bytesused ||
314 			    q[1] != 0 || q[2] != 1 || q[3] != ch) {
315 				continue;
316 			}
317 			if (!itv->search_pack_header) {
318 				if ((q[6] & 0xc0) != 0x80)
319 					continue;
320 				if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
321 				    ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
322 					ch = 0xba;
323 					itv->search_pack_header = 1;
324 					p = q + 9;
325 				}
326 				continue;
327 			}
328 			stuffing = q[13] & 7;
329 			/* all stuffing bytes must be 0xff */
330 			for (i = 0; i < stuffing; i++)
331 				if (q[14 + i] != 0xff)
332 					break;
333 			if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
334 					q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
335 					q[16 + stuffing] == 1) {
336 				itv->search_pack_header = 0;
337 				len = (char *)q - start;
338 				ivtv_setup_sliced_vbi_buf(itv);
339 				break;
340 			}
341 		}
342 	}
343 	if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
344 		IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
345 		return -EFAULT;
346 	}
347 	/*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
348 			buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
349 			buf == &itv->vbi.sliced_mpeg_buf); */
350 	buf->readpos += len;
351 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
352 		itv->mpg_data_received += len;
353 	return len;
354 }
355 
ivtv_read(struct ivtv_stream * s,char __user * ubuf,size_t tot_count,int non_block)356 static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
357 {
358 	struct ivtv *itv = s->itv;
359 	size_t tot_written = 0;
360 	int single_frame = 0;
361 
362 	if (atomic_read(&itv->capturing) == 0 && s->fh == NULL) {
363 		/* shouldn't happen */
364 		IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
365 		return -EIO;
366 	}
367 
368 	/* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
369 	   arrive one-by-one, so make sure we never output more than one VBI frame at a time */
370 	if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
371 	    (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
372 		single_frame = 1;
373 
374 	for (;;) {
375 		struct ivtv_buffer *buf;
376 		int rc;
377 
378 		buf = ivtv_get_buffer(s, non_block, &rc);
379 		/* if there is no data available... */
380 		if (buf == NULL) {
381 			/* if we got data, then return that regardless */
382 			if (tot_written)
383 				break;
384 			/* EOS condition */
385 			if (rc == 0) {
386 				clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
387 				clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
388 				ivtv_release_stream(s);
389 			}
390 			/* set errno */
391 			return rc;
392 		}
393 		rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
394 		if (buf != &itv->vbi.sliced_mpeg_buf) {
395 			ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
396 		}
397 		else if (buf->readpos == buf->bytesused) {
398 			int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
399 			itv->vbi.sliced_mpeg_size[idx] = 0;
400 			itv->vbi.inserted_frame++;
401 			itv->vbi_data_inserted += buf->bytesused;
402 		}
403 		if (rc < 0)
404 			return rc;
405 		tot_written += rc;
406 
407 		if (tot_written == tot_count || single_frame)
408 			break;
409 	}
410 	return tot_written;
411 }
412 
ivtv_read_pos(struct ivtv_stream * s,char __user * ubuf,size_t count,loff_t * pos,int non_block)413 static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
414 			loff_t *pos, int non_block)
415 {
416 	ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
417 	struct ivtv *itv = s->itv;
418 
419 	IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
420 	if (rc > 0)
421 		*pos += rc;
422 	return rc;
423 }
424 
ivtv_start_capture(struct ivtv_open_id * id)425 int ivtv_start_capture(struct ivtv_open_id *id)
426 {
427 	struct ivtv *itv = id->itv;
428 	struct ivtv_stream *s = &itv->streams[id->type];
429 	struct ivtv_stream *s_vbi;
430 
431 	if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
432 	    s->type == IVTV_DEC_STREAM_TYPE_MPG ||
433 	    s->type == IVTV_DEC_STREAM_TYPE_YUV ||
434 	    s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
435 		/* you cannot read from these stream types. */
436 		return -EINVAL;
437 	}
438 
439 	/* Try to claim this stream. */
440 	if (ivtv_claim_stream(id, s->type))
441 		return -EBUSY;
442 
443 	/* This stream does not need to start capturing */
444 	if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
445 		set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
446 		return 0;
447 	}
448 
449 	/* If capture is already in progress, then we also have to
450 	   do nothing extra. */
451 	if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
452 		set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
453 		return 0;
454 	}
455 
456 	/* Start VBI capture if required */
457 	s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
458 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
459 	    test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
460 	    !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
461 		/* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
462 		   automatically when the MPG stream is claimed.
463 		   We only need to start the VBI capturing. */
464 		if (ivtv_start_v4l2_encode_stream(s_vbi)) {
465 			IVTV_DEBUG_WARN("VBI capture start failed\n");
466 
467 			/* Failure, clean up and return an error */
468 			clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
469 			clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
470 			/* also releases the associated VBI stream */
471 			ivtv_release_stream(s);
472 			return -EIO;
473 		}
474 		IVTV_DEBUG_INFO("VBI insertion started\n");
475 	}
476 
477 	/* Tell the card to start capturing */
478 	if (!ivtv_start_v4l2_encode_stream(s)) {
479 		/* We're done */
480 		set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
481 		/* Resume a possibly paused encoder */
482 		if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
483 			ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
484 		return 0;
485 	}
486 
487 	/* failure, clean up */
488 	IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
489 
490 	/* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
491 	   automatically when the MPG stream is released.
492 	   We only need to stop the VBI capturing. */
493 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
494 	    test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
495 		ivtv_stop_v4l2_encode_stream(s_vbi, 0);
496 		clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
497 	}
498 	clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
499 	ivtv_release_stream(s);
500 	return -EIO;
501 }
502 
ivtv_v4l2_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)503 ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
504 {
505 	struct ivtv_open_id *id = fh2id(filp->private_data);
506 	struct ivtv *itv = id->itv;
507 	struct ivtv_stream *s = &itv->streams[id->type];
508 	ssize_t rc;
509 
510 	IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
511 
512 	if (mutex_lock_interruptible(&itv->serialize_lock))
513 		return -ERESTARTSYS;
514 	rc = ivtv_start_capture(id);
515 	if (!rc)
516 		rc = ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
517 	mutex_unlock(&itv->serialize_lock);
518 	return rc;
519 }
520 
ivtv_start_decoding(struct ivtv_open_id * id,int speed)521 int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
522 {
523 	struct ivtv *itv = id->itv;
524 	struct ivtv_stream *s = &itv->streams[id->type];
525 	int rc;
526 
527 	if (atomic_read(&itv->decoding) == 0) {
528 		if (ivtv_claim_stream(id, s->type)) {
529 			/* someone else is using this stream already */
530 			IVTV_DEBUG_WARN("start decode, stream already claimed\n");
531 			return -EBUSY;
532 		}
533 		rc = ivtv_start_v4l2_decode_stream(s, 0);
534 		if (rc < 0) {
535 			if (rc == -EAGAIN)
536 				rc = ivtv_start_v4l2_decode_stream(s, 0);
537 			if (rc < 0)
538 				return rc;
539 		}
540 	}
541 	if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
542 		return ivtv_set_speed(itv, speed);
543 	return 0;
544 }
545 
ivtv_schedule_dma(struct ivtv_stream * s)546 static int ivtv_schedule_dma(struct ivtv_stream *s)
547 {
548 	struct ivtv *itv = s->itv;
549 	int got_sig;
550 	DEFINE_WAIT(wait);
551 
552 	lockdep_assert_held(&itv->serialize_lock);
553 
554 	mutex_unlock(&itv->serialize_lock);
555 	prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
556 	while (!(got_sig = signal_pending(current)) &&
557 	       test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags))
558 		schedule();
559 	finish_wait(&itv->dma_waitq, &wait);
560 	mutex_lock(&itv->serialize_lock);
561 
562 	return got_sig;
563 }
564 
ivtv_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * pos)565 static ssize_t ivtv_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
566 {
567 	struct ivtv_open_id *id = fh2id(filp->private_data);
568 	struct ivtv *itv = id->itv;
569 	struct ivtv_stream *s = &itv->streams[id->type];
570 	struct yuv_playback_info *yi = &itv->yuv_info;
571 	struct ivtv_buffer *buf;
572 	struct ivtv_queue q;
573 	int bytes_written = 0;
574 	int mode;
575 	int rc;
576 
577 	IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
578 
579 	if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
580 	    s->type != IVTV_DEC_STREAM_TYPE_YUV &&
581 	    s->type != IVTV_DEC_STREAM_TYPE_VOUT)
582 		/* not decoder streams */
583 		return -EINVAL;
584 
585 	/* Try to claim this stream */
586 	if (ivtv_claim_stream(id, s->type))
587 		return -EBUSY;
588 
589 	/* This stream does not need to start any decoding */
590 	if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
591 		int elems = count / sizeof(struct v4l2_sliced_vbi_data);
592 
593 		set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
594 		return ivtv_write_vbi_from_user(itv,
595 		   (const struct v4l2_sliced_vbi_data __user *)user_buf, elems);
596 	}
597 
598 	mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
599 
600 	if (ivtv_set_output_mode(itv, mode) != mode) {
601 	    ivtv_release_stream(s);
602 	    return -EBUSY;
603 	}
604 	ivtv_queue_init(&q);
605 	set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
606 
607 	/* Start decoder (returns 0 if already started) */
608 	rc = ivtv_start_decoding(id, itv->speed);
609 	if (rc) {
610 		IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
611 
612 		/* failure, clean up */
613 		clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
614 		clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
615 		return rc;
616 	}
617 
618 retry:
619 	/* If possible, just DMA the entire frame - Check the data transfer size
620 	since we may get here before the stream has been fully set-up */
621 	if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
622 		while (count >= itv->dma_data_req_size) {
623 			rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
624 
625 			if (rc < 0)
626 				return rc;
627 
628 			bytes_written += itv->dma_data_req_size;
629 			user_buf += itv->dma_data_req_size;
630 			count -= itv->dma_data_req_size;
631 		}
632 		if (count == 0) {
633 			IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
634 			return bytes_written;
635 		}
636 	}
637 
638 	for (;;) {
639 		/* Gather buffers */
640 		while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
641 			ivtv_enqueue(s, buf, &q);
642 		while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
643 			ivtv_enqueue(s, buf, &q);
644 		}
645 		if (q.buffers)
646 			break;
647 		if (filp->f_flags & O_NONBLOCK)
648 			return -EAGAIN;
649 		ivtv_schedule(s);
650 		if (signal_pending(current)) {
651 			IVTV_DEBUG_INFO("User stopped %s\n", s->name);
652 			return -EINTR;
653 		}
654 	}
655 
656 	/* copy user data into buffers */
657 	while ((buf = ivtv_dequeue(s, &q))) {
658 		/* yuv is a pain. Don't copy more data than needed for a single
659 		   frame, otherwise we lose sync with the incoming stream */
660 		if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
661 		    yi->stream_size + count > itv->dma_data_req_size)
662 			rc  = ivtv_buf_copy_from_user(s, buf, user_buf,
663 				itv->dma_data_req_size - yi->stream_size);
664 		else
665 			rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
666 
667 		/* Make sure we really got all the user data */
668 		if (rc < 0) {
669 			ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
670 			return rc;
671 		}
672 		user_buf += rc;
673 		count -= rc;
674 		bytes_written += rc;
675 
676 		if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
677 			yi->stream_size += rc;
678 			/* If we have a complete yuv frame, break loop now */
679 			if (yi->stream_size == itv->dma_data_req_size) {
680 				ivtv_enqueue(s, buf, &s->q_full);
681 				yi->stream_size = 0;
682 				break;
683 			}
684 		}
685 
686 		if (buf->bytesused != s->buf_size) {
687 			/* incomplete, leave in q_io for next time */
688 			ivtv_enqueue(s, buf, &s->q_io);
689 			break;
690 		}
691 		/* Byteswap MPEG buffer */
692 		if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
693 			ivtv_buf_swap(buf);
694 		ivtv_enqueue(s, buf, &s->q_full);
695 	}
696 
697 	if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
698 		if (s->q_full.length >= itv->dma_data_req_size) {
699 			if (mode == OUT_YUV)
700 				ivtv_yuv_setup_stream_frame(itv);
701 
702 			if (ivtv_schedule_dma(s)) {
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 	/* 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;
718 }
719 
ivtv_v4l2_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * pos)720 ssize_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 
ivtv_v4l2_dec_poll(struct file * filp,poll_table * wait)733 __poll_t 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 	__poll_t res = 0;
739 
740 	/* add stream's waitq to the poll list */
741 	IVTV_DEBUG_HI_FILE("Decoder poll\n");
742 
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 = EPOLLPRI;
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 = EPOLLPRI;
759 	}
760 
761 	/* Allow write if buffers are available for writing */
762 	if (s->q_free.buffers)
763 		res |= EPOLLOUT | EPOLLWRNORM;
764 	return res;
765 }
766 
ivtv_v4l2_enc_poll(struct file * filp,poll_table * wait)767 __poll_t ivtv_v4l2_enc_poll(struct file *filp, poll_table *wait)
768 {
769 	__poll_t 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 	__poll_t res = 0;
775 
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 & (EPOLLIN | EPOLLRDNORM))) {
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 EPOLLERR;
789 		}
790 		IVTV_DEBUG_FILE("Encoder poll started capture\n");
791 	}
792 
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 |= EPOLLPRI;
798 	else
799 		poll_wait(filp, &id->fh.wait, wait);
800 
801 	if (s->q_full.length || s->q_io.length)
802 		return res | EPOLLIN | EPOLLRDNORM;
803 	if (eof)
804 		return res | EPOLLHUP;
805 	return res;
806 }
807 
ivtv_stop_capture(struct ivtv_open_id * id,int gop_end)808 void 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 	/* 'Unclaim' this stream */
816 
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];
820 
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);
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 			/* Also used internally, don't stop capturing */
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 
ivtv_stop_decoding(struct ivtv_open_id * id,int flags,u64 pts)847 static 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 		/* Restore registers we've changed & clean up any mess */
857 		ivtv_yuv_close(itv);
858 	}
859 
860 	/* Stop decoding */
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 
ivtv_v4l2_close(struct file * filp)878 int 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 	/* 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);
903 		}
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));
909 		}
910 		/* Done! Unmute and continue. */
911 		ivtv_unmute(itv);
912 	}
913 
914 	v4l2_fh_del(fh);
915 	v4l2_fh_exit(fh);
916 
917 	/* Easy case first: this stream was never claimed by us */
918 	if (s->fh != &id->fh)
919 		goto close_done;
920 
921 	/* 'Unclaim' this stream */
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 		/* 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);
933 		}
934 	} else {
935 		ivtv_stop_capture(id, 0);
936 	}
937 close_done:
938 	kfree(id);
939 	mutex_unlock(&itv->serialize_lock);
940 	return 0;
941 }
942 
ivtv_open(struct file * filp)943 static 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 	/* 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;
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 			/* return -ENODEV; */
988 		}
989 		ivtv_udma_alloc(itv);
990 	}
991 
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;
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 				/* 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;
1015 			}
1016 		}
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);
1028 		}
1029 		/* Done! Unmute and continue. */
1030 		ivtv_unmute(itv);
1031 	}
1032 
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;
1042 	}
1043 	return 0;
1044 }
1045 
1046 int 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 
1058 void 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 
1065 void 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