1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * driver for the SAA7146 based AV110 cards
4  * - audio and video MPEG decoder stuff
5  *
6  * Copyright (C) 1999-2002 Ralph  Metzler
7  *                       & Marcus Metzler for convergence integrated media GmbH
8  *
9  * originally based on code by:
10  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
11  *
12  * the project's page is at https://linuxtv.org
13  */
14 
15 #include <linux/ethtool.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/delay.h>
20 #include <linux/fs.h>
21 
22 #include "av7110.h"
23 #include "av7110_hw.h"
24 #include "av7110_av.h"
25 #include "av7110_ipack.h"
26 
27 /* MPEG-2 (ISO 13818 / H.222.0) stream types */
28 #define PROG_STREAM_MAP  0xBC
29 #define PRIVATE_STREAM1  0xBD
30 #define PADDING_STREAM	 0xBE
31 #define PRIVATE_STREAM2  0xBF
32 #define AUDIO_STREAM_S	 0xC0
33 #define AUDIO_STREAM_E	 0xDF
34 #define VIDEO_STREAM_S	 0xE0
35 #define VIDEO_STREAM_E	 0xEF
36 #define ECM_STREAM	 0xF0
37 #define EMM_STREAM	 0xF1
38 #define DSM_CC_STREAM	 0xF2
39 #define ISO13522_STREAM  0xF3
40 #define PROG_STREAM_DIR  0xFF
41 
42 #define PTS_DTS_FLAGS	 0xC0
43 
44 //pts_dts flags
45 #define PTS_ONLY	 0x80
46 #define PTS_DTS		 0xC0
47 #define TS_SIZE		 188
48 #define TRANS_ERROR	 0x80
49 #define PAY_START	 0x40
50 #define TRANS_PRIO	 0x20
51 #define PID_MASK_HI	 0x1F
52 //flags
53 #define TRANS_SCRMBL1	 0x80
54 #define TRANS_SCRMBL2	 0x40
55 #define ADAPT_FIELD	 0x20
56 #define PAYLOAD		 0x10
57 #define COUNT_MASK	 0x0F
58 
59 // adaptation flags
60 #define DISCON_IND	 0x80
61 #define RAND_ACC_IND	 0x40
62 #define ES_PRI_IND	 0x20
63 #define PCR_FLAG	 0x10
64 #define OPCR_FLAG	 0x08
65 #define SPLICE_FLAG	 0x04
66 #define TRANS_PRIV	 0x02
67 #define ADAP_EXT_FLAG	 0x01
68 
69 // adaptation extension flags
70 #define LTW_FLAG	 0x80
71 #define PIECE_RATE	 0x40
72 #define SEAM_SPLICE	 0x20
73 
74 static void p_to_t(u8 const *buf, long length, u16 pid,
75 		   u8 *counter, struct dvb_demux_feed *feed);
76 static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len);
77 
av7110_record_cb(struct dvb_filter_pes2ts * p2t,u8 * buf,size_t len)78 int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
79 {
80 	struct dvb_demux_feed *dvbdmxfeed = p2t->priv;
81 
82 	if (!(dvbdmxfeed->ts_type & TS_PACKET))
83 		return 0;
84 	if (buf[3] == 0xe0) {	 // video PES do not have a length in TS
85 		buf[4] = 0;
86 		buf[5] = 0;
87 	}
88 	if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
89 		return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
90 					 &dvbdmxfeed->feed.ts, NULL);
91 	else
92 		return dvb_filter_pes2ts(p2t, buf, len, 1);
93 }
94 
dvb_filter_pes2ts_cb(void * priv,unsigned char * data)95 static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
96 {
97 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)priv;
98 
99 	dvbdmxfeed->cb.ts(data, 188, NULL, 0,
100 			  &dvbdmxfeed->feed.ts, NULL);
101 	return 0;
102 }
103 
av7110_av_start_record(struct av7110 * av7110,int av,struct dvb_demux_feed * dvbdmxfeed)104 int av7110_av_start_record(struct av7110 *av7110, int av,
105 			   struct dvb_demux_feed *dvbdmxfeed)
106 {
107 	int ret = 0;
108 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
109 
110 	dprintk(2, "av7110:%p, dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
111 
112 	if (av7110->playing || (av7110->rec_mode & av))
113 		return -EBUSY;
114 	av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
115 	dvbdmx->recording = 1;
116 	av7110->rec_mode |= av;
117 
118 	switch (av7110->rec_mode) {
119 	case RP_AUDIO:
120 		dvb_filter_pes2ts_init(&av7110->p2t[0],
121 				       dvbdmx->pesfilter[0]->pid,
122 				       dvb_filter_pes2ts_cb,
123 				       (void *)dvbdmx->pesfilter[0]);
124 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
125 		break;
126 
127 	case RP_VIDEO:
128 		dvb_filter_pes2ts_init(&av7110->p2t[1],
129 				       dvbdmx->pesfilter[1]->pid,
130 				       dvb_filter_pes2ts_cb,
131 				       (void *)dvbdmx->pesfilter[1]);
132 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
133 		break;
134 
135 	case RP_AV:
136 		dvb_filter_pes2ts_init(&av7110->p2t[0],
137 				       dvbdmx->pesfilter[0]->pid,
138 				       dvb_filter_pes2ts_cb,
139 				       (void *)dvbdmx->pesfilter[0]);
140 		dvb_filter_pes2ts_init(&av7110->p2t[1],
141 				       dvbdmx->pesfilter[1]->pid,
142 				       dvb_filter_pes2ts_cb,
143 				       (void *)dvbdmx->pesfilter[1]);
144 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
145 		break;
146 	}
147 	return ret;
148 }
149 
av7110_av_start_play(struct av7110 * av7110,int av)150 int av7110_av_start_play(struct av7110 *av7110, int av)
151 {
152 	int ret = 0;
153 
154 	dprintk(2, "av7110:%p\n", av7110);
155 
156 	if (av7110->rec_mode)
157 		return -EBUSY;
158 	if (av7110->playing & av)
159 		return -EBUSY;
160 
161 	av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
162 
163 	if (av7110->playing == RP_NONE) {
164 		av7110_ipack_reset(&av7110->ipack[0]);
165 		av7110_ipack_reset(&av7110->ipack[1]);
166 	}
167 
168 	av7110->playing |= av;
169 	switch (av7110->playing) {
170 	case RP_AUDIO:
171 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
172 		break;
173 	case RP_VIDEO:
174 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
175 		av7110->sinfo = 0;
176 		break;
177 	case RP_AV:
178 		av7110->sinfo = 0;
179 		ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
180 		break;
181 	}
182 	return ret;
183 }
184 
av7110_av_stop(struct av7110 * av7110,int av)185 int av7110_av_stop(struct av7110 *av7110, int av)
186 {
187 	int ret = 0;
188 
189 	dprintk(2, "av7110:%p\n", av7110);
190 
191 	if (!(av7110->playing & av) && !(av7110->rec_mode & av))
192 		return 0;
193 	av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
194 	if (av7110->playing) {
195 		av7110->playing &= ~av;
196 		switch (av7110->playing) {
197 		case RP_AUDIO:
198 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
199 			break;
200 		case RP_VIDEO:
201 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
202 			break;
203 		case RP_NONE:
204 			ret = av7110_set_vidmode(av7110, av7110->vidmode);
205 			break;
206 		}
207 	} else {
208 		av7110->rec_mode &= ~av;
209 		switch (av7110->rec_mode) {
210 		case RP_AUDIO:
211 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
212 			break;
213 		case RP_VIDEO:
214 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
215 			break;
216 		case RP_NONE:
217 			break;
218 		}
219 	}
220 	return ret;
221 }
222 
av7110_pes_play(void * dest,struct dvb_ringbuffer * buf,int dlen)223 int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
224 {
225 	int len;
226 	u32 sync;
227 	u16 blen;
228 
229 	if (!dlen) {
230 		wake_up(&buf->queue);
231 		return -1;
232 	}
233 	while (1) {
234 		len = dvb_ringbuffer_avail(buf);
235 		if (len < 6) {
236 			wake_up(&buf->queue);
237 			return -1;
238 		}
239 		sync =  DVB_RINGBUFFER_PEEK(buf, 0) << 24;
240 		sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
241 		sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
242 		sync |= DVB_RINGBUFFER_PEEK(buf, 3);
243 
244 		if (((sync & ~0x0f) == 0x000001e0) ||
245 		    ((sync & ~0x1f) == 0x000001c0) ||
246 		    (sync == 0x000001bd))
247 			break;
248 		pr_info("resync\n");
249 		DVB_RINGBUFFER_SKIP(buf, 1);
250 	}
251 	blen =  DVB_RINGBUFFER_PEEK(buf, 4) << 8;
252 	blen |= DVB_RINGBUFFER_PEEK(buf, 5);
253 	blen += 6;
254 	if (len < blen || blen > dlen) {
255 		//pr_info("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
256 		wake_up(&buf->queue);
257 		return -1;
258 	}
259 
260 	dvb_ringbuffer_read(buf, dest, (size_t)blen);
261 
262 	dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
263 		(unsigned long)buf->pread, (unsigned long)buf->pwrite);
264 	wake_up(&buf->queue);
265 	return blen;
266 }
267 
av7110_set_volume(struct av7110 * av7110,unsigned int volleft,unsigned int volright)268 int av7110_set_volume(struct av7110 *av7110, unsigned int volleft,
269 		      unsigned int volright)
270 {
271 	unsigned int vol, val, balance = 0;
272 	int err;
273 
274 	dprintk(2, "av7110:%p\n", av7110);
275 
276 	av7110->mixer.volume_left = volleft;
277 	av7110->mixer.volume_right = volright;
278 
279 	switch (av7110->adac_type) {
280 	case DVB_ADAC_TI:
281 		volleft = (volleft * 256) / 1036;
282 		volright = (volright * 256) / 1036;
283 		if (volleft > 0x3f)
284 			volleft = 0x3f;
285 		if (volright > 0x3f)
286 			volright = 0x3f;
287 		err = SendDAC(av7110, 3, 0x80 + volleft);
288 		if (err)
289 			return err;
290 		return SendDAC(av7110, 4, volright);
291 
292 	case DVB_ADAC_CRYSTAL:
293 		volleft = 127 - volleft / 2;
294 		volright = 127 - volright / 2;
295 		i2c_writereg(av7110, 0x20, 0x03, volleft);
296 		i2c_writereg(av7110, 0x20, 0x04, volright);
297 		return 0;
298 
299 	case DVB_ADAC_MSP34x0:
300 		vol  = (volleft > volright) ? volleft : volright;
301 		val	= (vol * 0x73 / 255) << 8;
302 		if (vol > 0)
303 			balance = ((volright - volleft) * 127) / vol;
304 		msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
305 		msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
306 		msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
307 		return 0;
308 
309 	case DVB_ADAC_MSP34x5:
310 		vol = (volleft > volright) ? volleft : volright;
311 		val = (vol * 0x73 / 255) << 8;
312 		if (vol > 0)
313 			balance = ((volright - volleft) * 127) / vol;
314 		msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
315 		msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
316 		return 0;
317 	}
318 
319 	return 0;
320 }
321 
av7110_set_vidmode(struct av7110 * av7110,enum av7110_video_mode mode)322 int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
323 {
324 	int ret;
325 
326 	dprintk(2, "av7110:%p\n", av7110);
327 
328 	ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
329 
330 	if (!ret && !av7110->playing) {
331 		ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
332 				 av7110->pids[DMX_PES_AUDIO],
333 			   av7110->pids[DMX_PES_TELETEXT],
334 			   0, av7110->pids[DMX_PES_PCR]);
335 		if (!ret)
336 			ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
337 	}
338 	return ret;
339 }
340 
341 static enum av7110_video_mode sw2mode[16] = {
342 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
343 	AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
344 	AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
345 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
346 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
347 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
348 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
349 	AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
350 };
351 
get_video_format(struct av7110 * av7110,u8 * buf,int count)352 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
353 {
354 	int i;
355 	int hsize, vsize;
356 	int sw;
357 	u8 *p;
358 	int ret = 0;
359 
360 	dprintk(2, "av7110:%p\n", av7110);
361 
362 	if (av7110->sinfo)
363 		return 0;
364 	for (i = 7; i < count - 10; i++) {
365 		p = buf + i;
366 		if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
367 			continue;
368 		p += 4;
369 		hsize = ((p[1] & 0xF0) >> 4) | (p[0] << 4);
370 		vsize = ((p[1] & 0x0F) << 8) | (p[2]);
371 		sw = (p[3] & 0x0F);
372 		ret = av7110_set_vidmode(av7110, sw2mode[sw]);
373 		if (!ret) {
374 			dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
375 			av7110->sinfo = 1;
376 		}
377 		break;
378 	}
379 	return ret;
380 }
381 
382 /****************************************************************************
383  * I/O buffer management and control
384  ****************************************************************************/
385 
aux_ring_buffer_write(struct dvb_ringbuffer * rbuf,const u8 * buf,unsigned long count)386 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
387 					 const u8 *buf, unsigned long count)
388 {
389 	unsigned long todo = count;
390 	int free;
391 
392 	while (todo > 0) {
393 		if (dvb_ringbuffer_free(rbuf) < 2048) {
394 			if (wait_event_interruptible(rbuf->queue,
395 						     (dvb_ringbuffer_free(rbuf) >= 2048)))
396 				return count - todo;
397 		}
398 		free = dvb_ringbuffer_free(rbuf);
399 		if (free > todo)
400 			free = todo;
401 		dvb_ringbuffer_write(rbuf, buf, free);
402 		todo -= free;
403 		buf += free;
404 	}
405 
406 	return count - todo;
407 }
408 
play_video_cb(u8 * buf,int count,void * priv)409 static void play_video_cb(u8 *buf, int count, void *priv)
410 {
411 	struct av7110 *av7110 = (struct av7110 *)priv;
412 
413 	dprintk(2, "av7110:%p\n", av7110);
414 
415 	if ((buf[3] & 0xe0) == 0xe0) {
416 		get_video_format(av7110, buf, count);
417 		aux_ring_buffer_write(&av7110->avout, buf, count);
418 	} else {
419 		aux_ring_buffer_write(&av7110->aout, buf, count);
420 	}
421 }
422 
play_audio_cb(u8 * buf,int count,void * priv)423 static void play_audio_cb(u8 *buf, int count, void *priv)
424 {
425 	struct av7110 *av7110 = (struct av7110 *)priv;
426 
427 	dprintk(2, "av7110:%p\n", av7110);
428 
429 	aux_ring_buffer_write(&av7110->aout, buf, count);
430 }
431 
432 #define FREE_COND_TS (dvb_ringbuffer_free(rb) >= 4096)
433 
ts_play(struct av7110 * av7110,const char __user * buf,unsigned long count,int nonblock,int type)434 static ssize_t ts_play(struct av7110 *av7110, const char __user *buf,
435 		       unsigned long count, int nonblock, int type)
436 {
437 	struct dvb_ringbuffer *rb;
438 	u8 *kb;
439 	unsigned long todo = count;
440 
441 	dprintk(2, "type %d cnt %lu\n", type, count);
442 
443 	rb = (type) ? &av7110->avout : &av7110->aout;
444 	kb = av7110->kbuf[type];
445 
446 	if (!kb)
447 		return -ENOBUFS;
448 
449 	if (nonblock && !FREE_COND_TS)
450 		return -EWOULDBLOCK;
451 
452 	while (todo >= TS_SIZE) {
453 		if (!FREE_COND_TS) {
454 			if (nonblock)
455 				return count - todo;
456 			if (wait_event_interruptible(rb->queue, FREE_COND_TS))
457 				return count - todo;
458 		}
459 		if (copy_from_user(kb, buf, TS_SIZE))
460 			return -EFAULT;
461 		write_ts_to_decoder(av7110, type, kb, TS_SIZE);
462 		todo -= TS_SIZE;
463 		buf += TS_SIZE;
464 	}
465 
466 	return count - todo;
467 }
468 
469 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
470 		   dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
471 
dvb_play(struct av7110 * av7110,const char __user * buf,unsigned long count,int nonblock,int type)472 static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
473 			unsigned long count, int nonblock, int type)
474 {
475 	unsigned long todo = count, n;
476 
477 	dprintk(2, "av7110:%p\n", av7110);
478 
479 	if (!av7110->kbuf[type])
480 		return -ENOBUFS;
481 
482 	if (nonblock && !FREE_COND)
483 		return -EWOULDBLOCK;
484 
485 	while (todo > 0) {
486 		if (!FREE_COND) {
487 			if (nonblock)
488 				return count - todo;
489 			if (wait_event_interruptible(av7110->avout.queue,
490 						     FREE_COND))
491 				return count - todo;
492 		}
493 		n = todo;
494 		if (n > IPACKS * 2)
495 			n = IPACKS * 2;
496 		if (copy_from_user(av7110->kbuf[type], buf, n))
497 			return -EFAULT;
498 		av7110_ipack_instant_repack(av7110->kbuf[type], n,
499 					    &av7110->ipack[type]);
500 		todo -= n;
501 		buf += n;
502 	}
503 	return count - todo;
504 }
505 
dvb_play_kernel(struct av7110 * av7110,const u8 * buf,unsigned long count,int nonblock,int type)506 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
507 			       unsigned long count, int nonblock, int type)
508 {
509 	unsigned long todo = count, n;
510 
511 	dprintk(2, "av7110:%p\n", av7110);
512 
513 	if (!av7110->kbuf[type])
514 		return -ENOBUFS;
515 
516 	if (nonblock && !FREE_COND)
517 		return -EWOULDBLOCK;
518 
519 	while (todo > 0) {
520 		if (!FREE_COND) {
521 			if (nonblock)
522 				return count - todo;
523 			if (wait_event_interruptible(av7110->avout.queue,
524 						     FREE_COND))
525 				return count - todo;
526 		}
527 		n = todo;
528 		if (n > IPACKS * 2)
529 			n = IPACKS * 2;
530 		av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
531 		todo -= n;
532 		buf += n;
533 	}
534 	return count - todo;
535 }
536 
dvb_aplay(struct av7110 * av7110,const char __user * buf,unsigned long count,int nonblock,int type)537 static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
538 			 unsigned long count, int nonblock, int type)
539 {
540 	unsigned long todo = count, n;
541 
542 	dprintk(2, "av7110:%p\n", av7110);
543 
544 	if (!av7110->kbuf[type])
545 		return -ENOBUFS;
546 	if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
547 		return -EWOULDBLOCK;
548 
549 	while (todo > 0) {
550 		if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
551 			if (nonblock)
552 				return count - todo;
553 			if (wait_event_interruptible(av7110->aout.queue,
554 						     (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
555 				return count - todo;
556 		}
557 		n = todo;
558 		if (n > IPACKS * 2)
559 			n = IPACKS * 2;
560 		if (copy_from_user(av7110->kbuf[type], buf, n))
561 			return -EFAULT;
562 		av7110_ipack_instant_repack(av7110->kbuf[type], n,
563 					    &av7110->ipack[type]);
564 		todo -= n;
565 		buf += n;
566 	}
567 	return count - todo;
568 }
569 
av7110_p2t_init(struct av7110_p2t * p,struct dvb_demux_feed * feed)570 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
571 {
572 	memset(p->pes, 0, TS_SIZE);
573 	p->counter = 0;
574 	p->pos = 0;
575 	p->frags = 0;
576 	if (feed)
577 		p->feed = feed;
578 }
579 
clear_p2t(struct av7110_p2t * p)580 static void clear_p2t(struct av7110_p2t *p)
581 {
582 	memset(p->pes, 0, TS_SIZE);
583 //	p->counter = 0;
584 	p->pos = 0;
585 	p->frags = 0;
586 }
587 
find_pes_header(u8 const * buf,long length,int * frags)588 static int find_pes_header(u8 const *buf, long length, int *frags)
589 {
590 	int c = 0;
591 	int found = 0;
592 
593 	*frags = 0;
594 
595 	while (c < length - 3 && !found) {
596 		if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
597 		    buf[c + 2] == 0x01) {
598 			switch (buf[c + 3]) {
599 			case PROG_STREAM_MAP:
600 			case PRIVATE_STREAM2:
601 			case PROG_STREAM_DIR:
602 			case ECM_STREAM:
603 			case EMM_STREAM:
604 			case PADDING_STREAM:
605 			case DSM_CC_STREAM:
606 			case ISO13522_STREAM:
607 			case PRIVATE_STREAM1:
608 			case AUDIO_STREAM_S ... AUDIO_STREAM_E:
609 			case VIDEO_STREAM_S ... VIDEO_STREAM_E:
610 				found = 1;
611 				break;
612 
613 			default:
614 				c++;
615 				break;
616 			}
617 		} else {
618 			c++;
619 		}
620 	}
621 	if (c == length - 3 && !found) {
622 		if (buf[length - 1] == 0x00)
623 			*frags = 1;
624 		if (buf[length - 2] == 0x00 &&
625 		    buf[length - 1] == 0x00)
626 			*frags = 2;
627 		if (buf[length - 3] == 0x00 &&
628 		    buf[length - 2] == 0x00 &&
629 		    buf[length - 1] == 0x01)
630 			*frags = 3;
631 		return -1;
632 	}
633 
634 	return c;
635 }
636 
av7110_p2t_write(u8 const * buf,long length,u16 pid,struct av7110_p2t * p)637 void av7110_p2t_write(u8 const *buf, long length, u16 pid, struct av7110_p2t *p)
638 {
639 	int c, c2, l, add;
640 	int check, rest;
641 
642 	c = 0;
643 	c2 = 0;
644 	if (p->frags) {
645 		check = 0;
646 		switch (p->frags) {
647 		case 1:
648 			if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
649 				check = 1;
650 				c += 2;
651 			}
652 			break;
653 		case 2:
654 			if (buf[c] == 0x01) {
655 				check = 1;
656 				c++;
657 			}
658 			break;
659 		case 3:
660 			check = 1;
661 		}
662 		if (check) {
663 			switch (buf[c]) {
664 			case PROG_STREAM_MAP:
665 			case PRIVATE_STREAM2:
666 			case PROG_STREAM_DIR:
667 			case ECM_STREAM:
668 			case EMM_STREAM:
669 			case PADDING_STREAM:
670 			case DSM_CC_STREAM:
671 			case ISO13522_STREAM:
672 			case PRIVATE_STREAM1:
673 			case AUDIO_STREAM_S ... AUDIO_STREAM_E:
674 			case VIDEO_STREAM_S ... VIDEO_STREAM_E:
675 				p->pes[0] = 0x00;
676 				p->pes[1] = 0x00;
677 				p->pes[2] = 0x01;
678 				p->pes[3] = buf[c];
679 				p->pos = 4;
680 				memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
681 				c += (TS_SIZE - 4) - p->pos;
682 				p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
683 				clear_p2t(p);
684 				break;
685 
686 			default:
687 				c = 0;
688 				break;
689 			}
690 		}
691 		p->frags = 0;
692 	}
693 
694 	if (p->pos) {
695 		c2 = find_pes_header(buf + c, length - c, &p->frags);
696 		if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
697 			l = c2 + c;
698 		else
699 			l = (TS_SIZE - 4) - p->pos;
700 		memcpy(p->pes + p->pos, buf, l);
701 		c += l;
702 		p->pos += l;
703 		p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
704 		clear_p2t(p);
705 	}
706 
707 	add = 0;
708 	while (c < length) {
709 		c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
710 		if (c2 >= 0) {
711 			c2 += c + add;
712 			if (c2 > c) {
713 				p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
714 				c = c2;
715 				clear_p2t(p);
716 				add = 0;
717 			} else {
718 				add = 1;
719 			}
720 		} else {
721 			l = length - c;
722 			rest = l % (TS_SIZE - 4);
723 			l -= rest;
724 			p_to_t(buf + c, l, pid, &p->counter, p->feed);
725 			memcpy(p->pes, buf + c + l, rest);
726 			p->pos = rest;
727 			c = length;
728 		}
729 	}
730 }
731 
write_ts_header2(u16 pid,u8 * counter,int pes_start,u8 * buf,u8 length)732 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
733 {
734 	int i;
735 	int c = 0;
736 	int fill;
737 	u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
738 
739 	fill = (TS_SIZE - 4) - length;
740 	if (pes_start)
741 		tshead[1] = 0x40;
742 	if (fill)
743 		tshead[3] = 0x30;
744 	tshead[1] |= (u8)((pid & 0x1F00) >> 8);
745 	tshead[2] |= (u8)(pid & 0x00FF);
746 	tshead[3] |= ((*counter)++ & 0x0F);
747 	memcpy(buf, tshead, 4);
748 	c += 4;
749 
750 	if (fill) {
751 		buf[4] = fill - 1;
752 		c++;
753 		if (fill > 1) {
754 			buf[5] = 0x00;
755 			c++;
756 		}
757 		for (i = 6; i < fill + 4; i++) {
758 			buf[i] = 0xFF;
759 			c++;
760 		}
761 	}
762 
763 	return c;
764 }
765 
p_to_t(u8 const * buf,long length,u16 pid,u8 * counter,struct dvb_demux_feed * feed)766 static void p_to_t(u8 const *buf, long length, u16 pid, u8 *counter,
767 		   struct dvb_demux_feed *feed)
768 {
769 	int l, pes_start;
770 	u8 obuf[TS_SIZE];
771 	long c = 0;
772 
773 	pes_start = 0;
774 	if (length > 3 &&
775 	    buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
776 		switch (buf[3]) {
777 		case PROG_STREAM_MAP:
778 		case PRIVATE_STREAM2:
779 		case PROG_STREAM_DIR:
780 		case ECM_STREAM:
781 		case EMM_STREAM:
782 		case PADDING_STREAM:
783 		case DSM_CC_STREAM:
784 		case ISO13522_STREAM:
785 		case PRIVATE_STREAM1:
786 		case AUDIO_STREAM_S ... AUDIO_STREAM_E:
787 		case VIDEO_STREAM_S ... VIDEO_STREAM_E:
788 			pes_start = 1;
789 			break;
790 
791 		default:
792 			break;
793 		}
794 
795 	while (c < length) {
796 		memset(obuf, 0, TS_SIZE);
797 		if (length - c >= (TS_SIZE - 4)) {
798 			l = write_ts_header2(pid, counter, pes_start,
799 					     obuf, (TS_SIZE - 4));
800 			memcpy(obuf + l, buf + c, TS_SIZE - l);
801 			c += TS_SIZE - l;
802 		} else {
803 			l = write_ts_header2(pid, counter, pes_start,
804 					     obuf, length - c);
805 			memcpy(obuf + l, buf + c, TS_SIZE - l);
806 			c = length;
807 		}
808 		feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, NULL);
809 		pes_start = 0;
810 	}
811 }
812 
write_ts_to_decoder(struct av7110 * av7110,int type,const u8 * buf,size_t len)813 static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len)
814 {
815 	struct ipack *ipack = &av7110->ipack[type];
816 
817 	if (buf[1] & TRANS_ERROR) {
818 		av7110_ipack_reset(ipack);
819 		return -1;
820 	}
821 
822 	if (!(buf[3] & PAYLOAD))
823 		return -1;
824 
825 	if (buf[1] & PAY_START)
826 		av7110_ipack_flush(ipack);
827 
828 	if (buf[3] & ADAPT_FIELD) {
829 		if (buf[4] > len - 1 - 4)
830 			return 0;
831 		len -= buf[4] + 1;
832 		buf += buf[4] + 1;
833 	}
834 
835 	av7110_ipack_instant_repack(buf + 4, len - 4, ipack);
836 	return 0;
837 }
838 
av7110_write_to_decoder(struct dvb_demux_feed * feed,const u8 * buf,size_t len)839 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
840 {
841 	struct dvb_demux *demux = feed->demux;
842 	struct av7110 *av7110 = demux->priv;
843 
844 	dprintk(2, "av7110:%p\n", av7110);
845 
846 	if (av7110->full_ts && demux->dmx.frontend->source != DMX_MEMORY_FE)
847 		return 0;
848 
849 	switch (feed->pes_type) {
850 	case 0:
851 		if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
852 			return -EINVAL;
853 		break;
854 	case 1:
855 		if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
856 			return -EINVAL;
857 		break;
858 	default:
859 		return -1;
860 	}
861 
862 	return write_ts_to_decoder(av7110, feed->pes_type, buf, len);
863 }
864 
865 /******************************************************************************
866  * Video MPEG decoder events
867  ******************************************************************************/
dvb_video_add_event(struct av7110 * av7110,struct video_event * event)868 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
869 {
870 	struct dvb_video_events *events = &av7110->video_events;
871 	int wp;
872 
873 	spin_lock_bh(&events->lock);
874 
875 	wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
876 	if (wp == events->eventr) {
877 		events->overflow = 1;
878 		events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
879 	}
880 
881 	//FIXME: timestamp?
882 	memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
883 	events->eventw = wp;
884 
885 	spin_unlock_bh(&events->lock);
886 
887 	wake_up_interruptible(&events->wait_queue);
888 }
889 
dvb_video_get_event(struct av7110 * av7110,struct video_event * event,int flags)890 static int dvb_video_get_event(struct av7110 *av7110, struct video_event *event, int flags)
891 {
892 	struct dvb_video_events *events = &av7110->video_events;
893 
894 	if (events->overflow) {
895 		events->overflow = 0;
896 		return -EOVERFLOW;
897 	}
898 	if (events->eventw == events->eventr) {
899 		int ret;
900 
901 		if (flags & O_NONBLOCK)
902 			return -EWOULDBLOCK;
903 
904 		ret = wait_event_interruptible(events->wait_queue,
905 					       events->eventw != events->eventr);
906 		if (ret < 0)
907 			return ret;
908 	}
909 
910 	spin_lock_bh(&events->lock);
911 
912 	memcpy(event, &events->events[events->eventr],
913 	       sizeof(struct video_event));
914 	events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
915 
916 	spin_unlock_bh(&events->lock);
917 
918 	return 0;
919 }
920 
921 /******************************************************************************
922  * DVB device file operations
923  ******************************************************************************/
924 
dvb_video_poll(struct file * file,poll_table * wait)925 static __poll_t dvb_video_poll(struct file *file, poll_table *wait)
926 {
927 	struct dvb_device *dvbdev = file->private_data;
928 	struct av7110 *av7110 = dvbdev->priv;
929 	__poll_t mask = 0;
930 
931 	dprintk(2, "av7110:%p\n", av7110);
932 
933 	if ((file->f_flags & O_ACCMODE) != O_RDONLY)
934 		poll_wait(file, &av7110->avout.queue, wait);
935 
936 	poll_wait(file, &av7110->video_events.wait_queue, wait);
937 
938 	if (av7110->video_events.eventw != av7110->video_events.eventr)
939 		mask = EPOLLPRI;
940 
941 	if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
942 		if (av7110->playing) {
943 			if (FREE_COND)
944 				mask |= (EPOLLOUT | EPOLLWRNORM);
945 		} else {
946 			/* if not playing: may play if asked for */
947 			mask |= (EPOLLOUT | EPOLLWRNORM);
948 		}
949 	}
950 
951 	return mask;
952 }
953 
dvb_video_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)954 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
955 			       size_t count, loff_t *ppos)
956 {
957 	struct dvb_device *dvbdev = file->private_data;
958 	struct av7110 *av7110 = dvbdev->priv;
959 	unsigned char c;
960 
961 	dprintk(2, "av7110:%p\n", av7110);
962 
963 	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
964 		return -EPERM;
965 
966 	if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
967 		return -EPERM;
968 
969 	if (get_user(c, buf))
970 		return -EFAULT;
971 	if (c == 0x47 && count % TS_SIZE == 0)
972 		return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
973 	else
974 		return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
975 }
976 
dvb_audio_poll(struct file * file,poll_table * wait)977 static __poll_t dvb_audio_poll(struct file *file, poll_table *wait)
978 {
979 	struct dvb_device *dvbdev = file->private_data;
980 	struct av7110 *av7110 = dvbdev->priv;
981 	__poll_t mask = 0;
982 
983 	dprintk(2, "av7110:%p\n", av7110);
984 
985 	poll_wait(file, &av7110->aout.queue, wait);
986 
987 	if (av7110->playing) {
988 		if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
989 			mask |= (EPOLLOUT | EPOLLWRNORM);
990 	} else {
991 		/* if not playing: may play if asked for */
992 		mask = (EPOLLOUT | EPOLLWRNORM);
993 	}
994 
995 	return mask;
996 }
997 
dvb_audio_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)998 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
999 			       size_t count, loff_t *ppos)
1000 {
1001 	struct dvb_device *dvbdev = file->private_data;
1002 	struct av7110 *av7110 = dvbdev->priv;
1003 	unsigned char c;
1004 
1005 	dprintk(2, "av7110:%p\n", av7110);
1006 
1007 	if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
1008 		pr_err("not audio source memory\n");
1009 		return -EPERM;
1010 	}
1011 
1012 	if (get_user(c, buf))
1013 		return -EFAULT;
1014 	if (c == 0x47 && count % TS_SIZE == 0)
1015 		return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1016 	else
1017 		return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1018 }
1019 
1020 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1021 
1022 #define MIN_IFRAME 400000
1023 
play_iframe(struct av7110 * av7110,char __user * buf,unsigned int len,int nonblock)1024 static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
1025 {
1026 	unsigned int i, n;
1027 	int progressive = 0;
1028 	int match = 0;
1029 
1030 	dprintk(2, "av7110:%p\n", av7110);
1031 
1032 	if (len == 0)
1033 		return 0;
1034 
1035 	if (!(av7110->playing & RP_VIDEO)) {
1036 		if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
1037 			return -EBUSY;
1038 	}
1039 
1040 	/* search in buf for instances of 00 00 01 b5 1? */
1041 	for (i = 0; i < len; i++) {
1042 		unsigned char c;
1043 
1044 		if (get_user(c, buf + i))
1045 			return -EFAULT;
1046 		if (match == 5) {
1047 			progressive = c & 0x08;
1048 			match = 0;
1049 		}
1050 		if (c == 0x00) {
1051 			match = (match == 1 || match == 2) ? 2 : 1;
1052 			continue;
1053 		}
1054 		switch (match++) {
1055 		case 2:
1056 			if (c == 0x01)
1057 				continue;
1058 			break;
1059 		case 3:
1060 			if (c == 0xb5)
1061 				continue;
1062 			break;
1063 		case 4:
1064 			if ((c & 0xf0) == 0x10)
1065 				continue;
1066 			break;
1067 		}
1068 		match = 0;
1069 	}
1070 
1071 	/* setting n always > 1, fixes problems when playing stillframes
1072 	 * consisting of I- and P-Frames
1073 	 */
1074 	n = MIN_IFRAME / len + 1;
1075 
1076 	/* FIXME: nonblock? */
1077 	dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
1078 
1079 	for (i = 0; i < n; i++)
1080 		dvb_play(av7110, buf, len, 0, 1);
1081 
1082 	av7110_ipack_flush(&av7110->ipack[1]);
1083 
1084 	if (progressive)
1085 		return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1086 	else
1087 		return 0;
1088 }
1089 
1090 #ifdef CONFIG_COMPAT
1091 struct compat_video_still_picture {
1092 	compat_uptr_t iFrame;
1093 	s32 size;
1094 };
1095 
1096 #define VIDEO_STILLPICTURE32 _IOW('o', 30, struct compat_video_still_picture)
1097 
1098 struct compat_video_event {
1099 	__s32 type;
1100 	/* unused, make sure to use atomic time for y2038 if it ever gets used */
1101 	compat_long_t timestamp;
1102 	union {
1103 		video_size_t size;
1104 		unsigned int frame_rate;        /* in frames per 1000sec */
1105 		unsigned char vsync_field;      /* unknown/odd/even/progressive */
1106 	} u;
1107 };
1108 
1109 #define VIDEO_GET_EVENT32 _IOR('o', 28, struct compat_video_event)
1110 
dvb_compat_video_get_event(struct av7110 * av7110,struct compat_video_event * event,int flags)1111 static int dvb_compat_video_get_event(struct av7110 *av7110,
1112 				      struct compat_video_event *event, int flags)
1113 {
1114 	struct video_event ev;
1115 	int ret;
1116 
1117 	ret = dvb_video_get_event(av7110, &ev, flags);
1118 
1119 	*event = (struct compat_video_event) {
1120 		.type = ev.type,
1121 		.timestamp = ev.timestamp,
1122 		.u.size = ev.u.size,
1123 	};
1124 
1125 	return ret;
1126 }
1127 #endif
1128 
dvb_video_ioctl(struct file * file,unsigned int cmd,void * parg)1129 static int dvb_video_ioctl(struct file *file,
1130 			   unsigned int cmd, void *parg)
1131 {
1132 	struct dvb_device *dvbdev = file->private_data;
1133 	struct av7110 *av7110 = dvbdev->priv;
1134 	unsigned long arg = (unsigned long)parg;
1135 	int ret = 0;
1136 
1137 	dprintk(1, "av7110:%p, cmd=%04x\n", av7110, cmd);
1138 
1139 	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1140 		if (cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1141 		    cmd != VIDEO_GET_SIZE) {
1142 			return -EPERM;
1143 		}
1144 	}
1145 
1146 	if (mutex_lock_interruptible(&av7110->ioctl_mutex))
1147 		return -ERESTARTSYS;
1148 
1149 	switch (cmd) {
1150 	case VIDEO_STOP:
1151 		av7110->videostate.play_state = VIDEO_STOPPED;
1152 		if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1153 			ret = av7110_av_stop(av7110, RP_VIDEO);
1154 		else
1155 			ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
1156 				     av7110->videostate.video_blank ? 0 : 1);
1157 		if (!ret)
1158 			av7110->trickmode = TRICK_NONE;
1159 		break;
1160 
1161 	case VIDEO_PLAY:
1162 		av7110->trickmode = TRICK_NONE;
1163 		if (av7110->videostate.play_state == VIDEO_FREEZED) {
1164 			av7110->videostate.play_state = VIDEO_PLAYING;
1165 			ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1166 			if (ret)
1167 				break;
1168 		}
1169 		if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1170 			if (av7110->playing == RP_AV) {
1171 				ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1172 				if (ret)
1173 					break;
1174 				av7110->playing &= ~RP_VIDEO;
1175 			}
1176 			ret = av7110_av_start_play(av7110, RP_VIDEO);
1177 		}
1178 		if (!ret)
1179 			ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1180 		if (!ret)
1181 			av7110->videostate.play_state = VIDEO_PLAYING;
1182 		break;
1183 
1184 	case VIDEO_FREEZE:
1185 		av7110->videostate.play_state = VIDEO_FREEZED;
1186 		if (av7110->playing & RP_VIDEO)
1187 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1188 		else
1189 			ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1190 		if (!ret)
1191 			av7110->trickmode = TRICK_FREEZE;
1192 		break;
1193 
1194 	case VIDEO_CONTINUE:
1195 		if (av7110->playing & RP_VIDEO)
1196 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1197 		if (!ret)
1198 			ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1199 		if (!ret) {
1200 			av7110->videostate.play_state = VIDEO_PLAYING;
1201 			av7110->trickmode = TRICK_NONE;
1202 		}
1203 		break;
1204 
1205 	case VIDEO_SELECT_SOURCE:
1206 		av7110->videostate.stream_source = (video_stream_source_t)arg;
1207 		break;
1208 
1209 	case VIDEO_SET_BLANK:
1210 		av7110->videostate.video_blank = (int)arg;
1211 		break;
1212 
1213 	case VIDEO_GET_STATUS:
1214 		memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1215 		break;
1216 
1217 #ifdef CONFIG_COMPAT
1218 	case VIDEO_GET_EVENT32:
1219 		ret = dvb_compat_video_get_event(av7110, parg, file->f_flags);
1220 		break;
1221 #endif
1222 
1223 	case VIDEO_GET_EVENT:
1224 		ret = dvb_video_get_event(av7110, parg, file->f_flags);
1225 		break;
1226 
1227 	case VIDEO_GET_SIZE:
1228 		memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1229 		break;
1230 
1231 	case VIDEO_SET_DISPLAY_FORMAT:
1232 	{
1233 		video_displayformat_t format = (video_displayformat_t)arg;
1234 
1235 		switch (format) {
1236 		case VIDEO_PAN_SCAN:
1237 			av7110->display_panscan = VID_PAN_SCAN_PREF;
1238 			break;
1239 		case VIDEO_LETTER_BOX:
1240 			av7110->display_panscan = VID_VC_AND_PS_PREF;
1241 			break;
1242 		case VIDEO_CENTER_CUT_OUT:
1243 			av7110->display_panscan = VID_CENTRE_CUT_PREF;
1244 			break;
1245 		default:
1246 			ret = -EINVAL;
1247 		}
1248 		if (ret < 0)
1249 			break;
1250 		av7110->videostate.display_format = format;
1251 		ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1252 				    1, av7110->display_panscan);
1253 		break;
1254 	}
1255 
1256 	case VIDEO_SET_FORMAT:
1257 		if (arg > 1) {
1258 			ret = -EINVAL;
1259 			break;
1260 		}
1261 		av7110->display_ar = arg;
1262 		ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1263 				    1, (u16)arg);
1264 		break;
1265 
1266 #ifdef CONFIG_COMPAT
1267 	case VIDEO_STILLPICTURE32:
1268 	{
1269 		struct compat_video_still_picture *pic =
1270 			(struct compat_video_still_picture *)parg;
1271 		av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1272 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1273 		ret = play_iframe(av7110, compat_ptr(pic->iFrame),
1274 				  pic->size, file->f_flags & O_NONBLOCK);
1275 		break;
1276 	}
1277 #endif
1278 
1279 	case VIDEO_STILLPICTURE:
1280 	{
1281 		struct video_still_picture *pic =
1282 			(struct video_still_picture *)parg;
1283 		av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1284 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1285 		ret = play_iframe(av7110, pic->iFrame, pic->size,
1286 				  file->f_flags & O_NONBLOCK);
1287 		break;
1288 	}
1289 
1290 	case VIDEO_FAST_FORWARD:
1291 		//note: arg is ignored by firmware
1292 		if (av7110->playing & RP_VIDEO)
1293 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1294 					    __Scan_I, 2, AV_PES, 0);
1295 		else
1296 			ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
1297 		if (!ret) {
1298 			av7110->trickmode = TRICK_FAST;
1299 			av7110->videostate.play_state = VIDEO_PLAYING;
1300 		}
1301 		break;
1302 
1303 	case VIDEO_SLOWMOTION:
1304 		if (av7110->playing & RP_VIDEO) {
1305 			if (av7110->trickmode != TRICK_SLOW)
1306 				ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1307 			if (!ret)
1308 				ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1309 		} else {
1310 			ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1311 			if (!ret)
1312 				ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
1313 			if (!ret)
1314 				ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1315 		}
1316 		if (!ret) {
1317 			av7110->trickmode = TRICK_SLOW;
1318 			av7110->videostate.play_state = VIDEO_PLAYING;
1319 		}
1320 		break;
1321 
1322 	case VIDEO_GET_CAPABILITIES:
1323 		*(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1324 			VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1325 		break;
1326 
1327 	case VIDEO_CLEAR_BUFFER:
1328 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1329 		av7110_ipack_reset(&av7110->ipack[1]);
1330 		if (av7110->playing == RP_AV) {
1331 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1332 					    __Play, 2, AV_PES, 0);
1333 			if (ret)
1334 				break;
1335 			if (av7110->trickmode == TRICK_FAST)
1336 				ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1337 						    __Scan_I, 2, AV_PES, 0);
1338 			if (av7110->trickmode == TRICK_SLOW) {
1339 				ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1340 						    __Slow, 2, 0, 0);
1341 				if (!ret)
1342 					ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1343 			}
1344 			if (av7110->trickmode == TRICK_FREEZE)
1345 				ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
1346 		}
1347 		break;
1348 
1349 	case VIDEO_SET_STREAMTYPE:
1350 		break;
1351 
1352 	default:
1353 		ret = -ENOIOCTLCMD;
1354 		break;
1355 	}
1356 
1357 	mutex_unlock(&av7110->ioctl_mutex);
1358 	return ret;
1359 }
1360 
dvb_audio_ioctl(struct file * file,unsigned int cmd,void * parg)1361 static int dvb_audio_ioctl(struct file *file,
1362 			   unsigned int cmd, void *parg)
1363 {
1364 	struct dvb_device *dvbdev = file->private_data;
1365 	struct av7110 *av7110 = dvbdev->priv;
1366 	unsigned long arg = (unsigned long)parg;
1367 	int ret = 0;
1368 
1369 	dprintk(1, "av7110:%p, cmd=%04x\n", av7110, cmd);
1370 
1371 	if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1372 	    (cmd != AUDIO_GET_STATUS))
1373 		return -EPERM;
1374 
1375 	if (mutex_lock_interruptible(&av7110->ioctl_mutex))
1376 		return -ERESTARTSYS;
1377 
1378 	switch (cmd) {
1379 	case AUDIO_STOP:
1380 		if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1381 			ret = av7110_av_stop(av7110, RP_AUDIO);
1382 		else
1383 			ret = audcom(av7110, AUDIO_CMD_MUTE);
1384 		if (!ret)
1385 			av7110->audiostate.play_state = AUDIO_STOPPED;
1386 		break;
1387 
1388 	case AUDIO_PLAY:
1389 		if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1390 			ret = av7110_av_start_play(av7110, RP_AUDIO);
1391 		if (!ret)
1392 			ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1393 		if (!ret)
1394 			av7110->audiostate.play_state = AUDIO_PLAYING;
1395 		break;
1396 
1397 	case AUDIO_PAUSE:
1398 		ret = audcom(av7110, AUDIO_CMD_MUTE);
1399 		if (!ret)
1400 			av7110->audiostate.play_state = AUDIO_PAUSED;
1401 		break;
1402 
1403 	case AUDIO_CONTINUE:
1404 		if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1405 			av7110->audiostate.play_state = AUDIO_PLAYING;
1406 			ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1407 		}
1408 		break;
1409 
1410 	case AUDIO_SELECT_SOURCE:
1411 		av7110->audiostate.stream_source = (audio_stream_source_t)arg;
1412 		break;
1413 
1414 	case AUDIO_SET_MUTE:
1415 	{
1416 		ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1417 		if (!ret)
1418 			av7110->audiostate.mute_state = (int)arg;
1419 		break;
1420 	}
1421 
1422 	case AUDIO_SET_AV_SYNC:
1423 		av7110->audiostate.AV_sync_state = (int)arg;
1424 		ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1425 		break;
1426 
1427 	case AUDIO_SET_BYPASS_MODE:
1428 		if (FW_VERSION(av7110->arm_app) < 0x2621)
1429 			ret = -EINVAL;
1430 		av7110->audiostate.bypass_mode = (int)arg;
1431 		break;
1432 
1433 	case AUDIO_CHANNEL_SELECT:
1434 		av7110->audiostate.channel_select = (audio_channel_select_t)arg;
1435 		switch (av7110->audiostate.channel_select) {
1436 		case AUDIO_STEREO:
1437 			ret = audcom(av7110, AUDIO_CMD_STEREO);
1438 			if (!ret) {
1439 				if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1440 					i2c_writereg(av7110, 0x20, 0x02, 0x49);
1441 				else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1442 					msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1443 			}
1444 			break;
1445 		case AUDIO_MONO_LEFT:
1446 			ret = audcom(av7110, AUDIO_CMD_MONO_L);
1447 			if (!ret) {
1448 				if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1449 					i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1450 				else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1451 					msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1452 			}
1453 			break;
1454 		case AUDIO_MONO_RIGHT:
1455 			ret = audcom(av7110, AUDIO_CMD_MONO_R);
1456 			if (!ret) {
1457 				if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1458 					i2c_writereg(av7110, 0x20, 0x02, 0x45);
1459 				else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1460 					msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1461 			}
1462 			break;
1463 		default:
1464 			ret = -EINVAL;
1465 			break;
1466 		}
1467 		break;
1468 
1469 	case AUDIO_GET_STATUS:
1470 		memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1471 		break;
1472 
1473 	case AUDIO_GET_CAPABILITIES:
1474 		if (FW_VERSION(av7110->arm_app) < 0x2621)
1475 			*(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1476 		else
1477 			*(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1478 						AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1479 		break;
1480 
1481 	case AUDIO_CLEAR_BUFFER:
1482 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1483 		av7110_ipack_reset(&av7110->ipack[0]);
1484 		if (av7110->playing == RP_AV)
1485 			ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1486 					    __Play, 2, AV_PES, 0);
1487 		break;
1488 
1489 	case AUDIO_SET_ID:
1490 		break;
1491 
1492 	case AUDIO_SET_MIXER:
1493 	{
1494 		struct audio_mixer *amix = (struct audio_mixer *)parg;
1495 
1496 		ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1497 		break;
1498 	}
1499 
1500 	case AUDIO_SET_STREAMTYPE:
1501 		break;
1502 
1503 	default:
1504 		ret = -ENOIOCTLCMD;
1505 	}
1506 
1507 	mutex_unlock(&av7110->ioctl_mutex);
1508 	return ret;
1509 }
1510 
dvb_video_open(struct inode * inode,struct file * file)1511 static int dvb_video_open(struct inode *inode, struct file *file)
1512 {
1513 	struct dvb_device *dvbdev = file->private_data;
1514 	struct av7110 *av7110 = dvbdev->priv;
1515 	int err;
1516 
1517 	dprintk(2, "av7110:%p\n", av7110);
1518 
1519 	err = dvb_generic_open(inode, file);
1520 	if (err < 0)
1521 		return err;
1522 
1523 	if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1524 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1525 		dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1526 		av7110->video_blank = 1;
1527 		av7110->audiostate.AV_sync_state = 1;
1528 		av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1529 
1530 		/*  empty event queue */
1531 		av7110->video_events.eventr = 0;
1532 		av7110->video_events.eventw = 0;
1533 	}
1534 
1535 	return 0;
1536 }
1537 
dvb_video_release(struct inode * inode,struct file * file)1538 static int dvb_video_release(struct inode *inode, struct file *file)
1539 {
1540 	struct dvb_device *dvbdev = file->private_data;
1541 	struct av7110 *av7110 = dvbdev->priv;
1542 
1543 	dprintk(2, "av7110:%p\n", av7110);
1544 
1545 	if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1546 		av7110_av_stop(av7110, RP_VIDEO);
1547 
1548 	return dvb_generic_release(inode, file);
1549 }
1550 
dvb_audio_open(struct inode * inode,struct file * file)1551 static int dvb_audio_open(struct inode *inode, struct file *file)
1552 {
1553 	struct dvb_device *dvbdev = file->private_data;
1554 	struct av7110 *av7110 = dvbdev->priv;
1555 	int err = dvb_generic_open(inode, file);
1556 
1557 	dprintk(2, "av7110:%p\n", av7110);
1558 
1559 	if (err < 0)
1560 		return err;
1561 	dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1562 	av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1563 	return 0;
1564 }
1565 
dvb_audio_release(struct inode * inode,struct file * file)1566 static int dvb_audio_release(struct inode *inode, struct file *file)
1567 {
1568 	struct dvb_device *dvbdev = file->private_data;
1569 	struct av7110 *av7110 = dvbdev->priv;
1570 
1571 	dprintk(2, "av7110:%p\n", av7110);
1572 
1573 	av7110_av_stop(av7110, RP_AUDIO);
1574 	return dvb_generic_release(inode, file);
1575 }
1576 
1577 /******************************************************************************
1578  * driver registration
1579  ******************************************************************************/
1580 
1581 static const struct file_operations dvb_video_fops = {
1582 	.owner		= THIS_MODULE,
1583 	.write		= dvb_video_write,
1584 	.unlocked_ioctl	= dvb_generic_ioctl,
1585 	.compat_ioctl	= dvb_generic_ioctl,
1586 	.open		= dvb_video_open,
1587 	.release	= dvb_video_release,
1588 	.poll		= dvb_video_poll,
1589 	.llseek		= noop_llseek,
1590 };
1591 
1592 static struct dvb_device dvbdev_video = {
1593 	.priv		= NULL,
1594 	.users		= 6,
1595 	.readers	= 5,	/* arbitrary */
1596 	.writers	= 1,
1597 	.fops		= &dvb_video_fops,
1598 	.kernel_ioctl	= dvb_video_ioctl,
1599 };
1600 
1601 static const struct file_operations dvb_audio_fops = {
1602 	.owner		= THIS_MODULE,
1603 	.write		= dvb_audio_write,
1604 	.unlocked_ioctl	= dvb_generic_ioctl,
1605 	.compat_ioctl	= dvb_generic_ioctl,
1606 	.open		= dvb_audio_open,
1607 	.release	= dvb_audio_release,
1608 	.poll		= dvb_audio_poll,
1609 	.llseek		= noop_llseek,
1610 };
1611 
1612 static struct dvb_device dvbdev_audio = {
1613 	.priv		= NULL,
1614 	.users		= 1,
1615 	.writers	= 1,
1616 	.fops		= &dvb_audio_fops,
1617 	.kernel_ioctl	= dvb_audio_ioctl,
1618 };
1619 
av7110_av_register(struct av7110 * av7110)1620 int av7110_av_register(struct av7110 *av7110)
1621 {
1622 	av7110->audiostate.AV_sync_state = 0;
1623 	av7110->audiostate.mute_state = 0;
1624 	av7110->audiostate.play_state = AUDIO_STOPPED;
1625 	av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1626 	av7110->audiostate.channel_select = AUDIO_STEREO;
1627 	av7110->audiostate.bypass_mode = 0;
1628 
1629 	av7110->videostate.video_blank = 0;
1630 	av7110->videostate.play_state = VIDEO_STOPPED;
1631 	av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1632 	av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1633 	av7110->videostate.display_format = VIDEO_LETTER_BOX;
1634 	av7110->display_ar = VIDEO_FORMAT_4_3;
1635 	av7110->display_panscan = VID_VC_AND_PS_PREF;
1636 
1637 	init_waitqueue_head(&av7110->video_events.wait_queue);
1638 	spin_lock_init(&av7110->video_events.lock);
1639 	av7110->video_events.eventw = 0;
1640 	av7110->video_events.eventr = 0;
1641 	av7110->video_events.overflow = 0;
1642 	memset(&av7110->video_size, 0, sizeof(video_size_t));
1643 
1644 	dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1645 			    &dvbdev_video, av7110, DVB_DEVICE_VIDEO, 0);
1646 
1647 	dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1648 			    &dvbdev_audio, av7110, DVB_DEVICE_AUDIO, 0);
1649 
1650 	return 0;
1651 }
1652 
av7110_av_unregister(struct av7110 * av7110)1653 void av7110_av_unregister(struct av7110 *av7110)
1654 {
1655 	dvb_unregister_device(av7110->audio_dev);
1656 	dvb_unregister_device(av7110->video_dev);
1657 }
1658 
av7110_av_init(struct av7110 * av7110)1659 int av7110_av_init(struct av7110 *av7110)
1660 {
1661 	void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1662 	int i, ret;
1663 
1664 	for (i = 0; i < 2; i++) {
1665 		struct ipack *ipack = av7110->ipack + i;
1666 
1667 		ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1668 		if (ret < 0) {
1669 			if (i)
1670 				av7110_ipack_free(--ipack);
1671 			goto out;
1672 		}
1673 		ipack->data = av7110;
1674 	}
1675 
1676 	dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1677 	dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1678 
1679 	av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1680 	av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1681 out:
1682 	return ret;
1683 }
1684 
av7110_av_exit(struct av7110 * av7110)1685 void av7110_av_exit(struct av7110 *av7110)
1686 {
1687 	av7110_ipack_free(&av7110->ipack[0]);
1688 	av7110_ipack_free(&av7110->ipack[1]);
1689 }
1690