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