2 * av7110_av.c: audio and video MPEG decoder stuff
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 * the project's page is at http://www.linuxtv.org/dvb/
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/delay.h>
38 #include "av7110_hw.h"
39 #include "av7110_av.h"
40 #include "av7110_ipack.h"
42 /* MPEG-2 (ISO 13818 / H.222.0) stream types */
43 #define PROG_STREAM_MAP 0xBC
44 #define PRIVATE_STREAM1 0xBD
45 #define PADDING_STREAM 0xBE
46 #define PRIVATE_STREAM2 0xBF
47 #define AUDIO_STREAM_S 0xC0
48 #define AUDIO_STREAM_E 0xDF
49 #define VIDEO_STREAM_S 0xE0
50 #define VIDEO_STREAM_E 0xEF
51 #define ECM_STREAM 0xF0
52 #define EMM_STREAM 0xF1
53 #define DSM_CC_STREAM 0xF2
54 #define ISO13522_STREAM 0xF3
55 #define PROG_STREAM_DIR 0xFF
57 #define PTS_DTS_FLAGS 0xC0
63 #define TRANS_ERROR 0x80
64 #define PAY_START 0x40
65 #define TRANS_PRIO 0x20
66 #define PID_MASK_HI 0x1F
68 #define TRANS_SCRMBL1 0x80
69 #define TRANS_SCRMBL2 0x40
70 #define ADAPT_FIELD 0x20
72 #define COUNT_MASK 0x0F
75 #define DISCON_IND 0x80
76 #define RAND_ACC_IND 0x40
77 #define ES_PRI_IND 0x20
79 #define OPCR_FLAG 0x08
80 #define SPLICE_FLAG 0x04
81 #define TRANS_PRIV 0x02
82 #define ADAP_EXT_FLAG 0x01
84 // adaptation extension flags
86 #define PIECE_RATE 0x40
87 #define SEAM_SPLICE 0x20
90 static void p_to_t(u8 const *buf, long int length, u16 pid,
91 u8 *counter, struct dvb_demux_feed *feed);
94 int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
96 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
98 if (!(dvbdmxfeed->ts_type & TS_PACKET))
100 if (buf[3] == 0xe0) // video PES do not have a length in TS
102 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
103 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
104 &dvbdmxfeed->feed.ts, DMX_OK);
106 return dvb_filter_pes2ts(p2t, buf, len, 1);
109 static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
111 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
113 dvbdmxfeed->cb.ts(data, 188, NULL, 0,
114 &dvbdmxfeed->feed.ts, DMX_OK);
118 int av7110_av_start_record(struct av7110 *av7110, int av,
119 struct dvb_demux_feed *dvbdmxfeed)
122 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
124 dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
126 if (av7110->playing || (av7110->rec_mode & av))
128 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
129 dvbdmx->recording = 1;
130 av7110->rec_mode |= av;
132 switch (av7110->rec_mode) {
134 dvb_filter_pes2ts_init(&av7110->p2t[0],
135 dvbdmx->pesfilter[0]->pid,
136 dvb_filter_pes2ts_cb,
137 (void *) dvbdmx->pesfilter[0]);
138 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
142 dvb_filter_pes2ts_init(&av7110->p2t[1],
143 dvbdmx->pesfilter[1]->pid,
144 dvb_filter_pes2ts_cb,
145 (void *) dvbdmx->pesfilter[1]);
146 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
150 dvb_filter_pes2ts_init(&av7110->p2t[0],
151 dvbdmx->pesfilter[0]->pid,
152 dvb_filter_pes2ts_cb,
153 (void *) dvbdmx->pesfilter[0]);
154 dvb_filter_pes2ts_init(&av7110->p2t[1],
155 dvbdmx->pesfilter[1]->pid,
156 dvb_filter_pes2ts_cb,
157 (void *) dvbdmx->pesfilter[1]);
158 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
164 int av7110_av_start_play(struct av7110 *av7110, int av)
167 dprintk(2, "av7110:%p, \n", av7110);
169 if (av7110->rec_mode)
171 if (av7110->playing & av)
174 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
176 if (av7110->playing == RP_NONE) {
177 av7110_ipack_reset(&av7110->ipack[0]);
178 av7110_ipack_reset(&av7110->ipack[1]);
181 av7110->playing |= av;
182 switch (av7110->playing) {
184 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
187 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
192 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
196 ret = av7110->playing;
200 int av7110_av_stop(struct av7110 *av7110, int av)
203 dprintk(2, "av7110:%p, \n", av7110);
205 if (!(av7110->playing & av) && !(av7110->rec_mode & av))
207 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
208 if (av7110->playing) {
209 av7110->playing &= ~av;
210 switch (av7110->playing) {
212 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
215 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
218 ret = av7110_set_vidmode(av7110, av7110->vidmode);
222 av7110->rec_mode &= ~av;
223 switch (av7110->rec_mode) {
225 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
228 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
238 int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
245 wake_up(&buf->queue);
249 if ((len = dvb_ringbuffer_avail(buf)) < 6)
251 sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
252 sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
253 sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
254 sync |= DVB_RINGBUFFER_PEEK(buf, 3);
256 if (((sync &~ 0x0f) == 0x000001e0) ||
257 ((sync &~ 0x1f) == 0x000001c0) ||
258 (sync == 0x000001bd))
261 DVB_RINGBUFFER_SKIP(buf, 1);
263 blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
264 blen |= DVB_RINGBUFFER_PEEK(buf, 5);
266 if (len < blen || blen > dlen) {
267 //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
268 wake_up(&buf->queue);
272 dvb_ringbuffer_read(buf, dest, (size_t) blen, 0);
274 dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
275 (unsigned long) buf->pread, (unsigned long) buf->pwrite);
276 wake_up(&buf->queue);
281 int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
283 int err, vol, val, balance = 0;
285 dprintk(2, "av7110:%p, \n", av7110);
287 av7110->mixer.volume_left = volleft;
288 av7110->mixer.volume_right = volright;
290 switch (av7110->adac_type) {
292 volleft = (volleft * 256) / 1036;
293 volright = (volright * 256) / 1036;
298 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
300 return SendDAC(av7110, 4, volright);
302 case DVB_ADAC_CRYSTAL:
303 volleft = 127 - volleft / 2;
304 volright = 127 - volright / 2;
305 i2c_writereg(av7110, 0x20, 0x03, volleft);
306 i2c_writereg(av7110, 0x20, 0x04, volright);
309 case DVB_ADAC_MSP34x0:
310 vol = (volleft > volright) ? volleft : volright;
311 val = (vol * 0x73 / 255) << 8;
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 msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
319 case DVB_ADAC_MSP34x5:
320 vol = (volleft > volright) ? volleft : volright;
321 val = (vol * 0x73 / 255) << 8;
323 balance = ((volright - volleft) * 127) / vol;
324 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
325 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
332 int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
335 dprintk(2, "av7110:%p, \n", av7110);
337 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
339 if (!ret && !av7110->playing) {
340 ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
341 av7110->pids[DMX_PES_AUDIO],
342 av7110->pids[DMX_PES_TELETEXT],
343 0, av7110->pids[DMX_PES_PCR]);
345 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
351 static enum av7110_video_mode sw2mode[16] = {
352 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
353 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
354 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
355 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
356 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
357 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
358 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
359 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
362 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
370 dprintk(2, "av7110:%p, \n", av7110);
374 for (i = 7; i < count - 10; i++) {
376 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
379 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
380 vsize = ((p[1] &0x0F) << 8) | (p[2]);
382 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
384 dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
393 /****************************************************************************
394 * I/O buffer management and control
395 ****************************************************************************/
397 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
398 const u8 *buf, unsigned long count)
400 unsigned long todo = count;
404 if (dvb_ringbuffer_free(rbuf) < 2048) {
405 if (wait_event_interruptible(rbuf->queue,
406 (dvb_ringbuffer_free(rbuf) >= 2048)))
409 free = dvb_ringbuffer_free(rbuf);
412 dvb_ringbuffer_write(rbuf, buf, free);
420 static void play_video_cb(u8 *buf, int count, void *priv)
422 struct av7110 *av7110 = (struct av7110 *) priv;
423 dprintk(2, "av7110:%p, \n", av7110);
425 if ((buf[3] & 0xe0) == 0xe0) {
426 get_video_format(av7110, buf, count);
427 aux_ring_buffer_write(&av7110->avout, buf, count);
429 aux_ring_buffer_write(&av7110->aout, buf, count);
432 static void play_audio_cb(u8 *buf, int count, void *priv)
434 struct av7110 *av7110 = (struct av7110 *) priv;
435 dprintk(2, "av7110:%p, \n", av7110);
437 aux_ring_buffer_write(&av7110->aout, buf, count);
440 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
441 dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
443 static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
444 unsigned long count, int nonblock, int type)
446 unsigned long todo = count, n;
447 dprintk(2, "av7110:%p, \n", av7110);
449 if (!av7110->kbuf[type])
452 if (nonblock && !FREE_COND)
459 if (wait_event_interruptible(av7110->avout.queue,
466 if (copy_from_user(av7110->kbuf[type], buf, n))
468 av7110_ipack_instant_repack(av7110->kbuf[type], n,
469 &av7110->ipack[type]);
476 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
477 unsigned long count, int nonblock, int type)
479 unsigned long todo = count, n;
480 dprintk(2, "av7110:%p, \n", av7110);
482 if (!av7110->kbuf[type])
485 if (nonblock && !FREE_COND)
492 if (wait_event_interruptible(av7110->avout.queue,
499 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
506 static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
507 unsigned long count, int nonblock, int type)
509 unsigned long todo = count, n;
510 dprintk(2, "av7110:%p, \n", av7110);
512 if (!av7110->kbuf[type])
514 if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
518 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
521 if (wait_event_interruptible(av7110->aout.queue,
522 (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
528 if (copy_from_user(av7110->kbuf[type], buf, n))
530 av7110_ipack_instant_repack(av7110->kbuf[type], n,
531 &av7110->ipack[type]);
538 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
540 memset(p->pes, 0, TS_SIZE);
548 static void clear_p2t(struct av7110_p2t *p)
550 memset(p->pes, 0, TS_SIZE);
557 static int find_pes_header(u8 const *buf, long int length, int *frags)
564 while (c < length - 3 && !found) {
565 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
566 buf[c + 2] == 0x01) {
567 switch ( buf[c + 3] ) {
568 case PROG_STREAM_MAP:
569 case PRIVATE_STREAM2:
570 case PROG_STREAM_DIR:
573 case PADDING_STREAM :
575 case ISO13522_STREAM:
576 case PRIVATE_STREAM1:
577 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
578 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
589 if (c == length - 3 && !found) {
590 if (buf[length - 1] == 0x00)
592 if (buf[length - 2] == 0x00 &&
593 buf[length - 1] == 0x00)
595 if (buf[length - 3] == 0x00 &&
596 buf[length - 2] == 0x00 &&
597 buf[length - 1] == 0x01)
605 void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
616 if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
622 if (buf[c] == 0x01) {
632 case PROG_STREAM_MAP:
633 case PRIVATE_STREAM2:
634 case PROG_STREAM_DIR:
637 case PADDING_STREAM :
639 case ISO13522_STREAM:
640 case PRIVATE_STREAM1:
641 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
642 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
648 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
649 c += (TS_SIZE - 4) - p->pos;
650 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
663 c2 = find_pes_header(buf + c, length - c, &p->frags);
664 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
667 l = (TS_SIZE - 4) - p->pos;
668 memcpy(p->pes + p->pos, buf, l);
671 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
677 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
681 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
689 rest = l % (TS_SIZE - 4);
691 p_to_t(buf + c, l, pid, &p->counter, p->feed);
692 memcpy(p->pes, buf + c + l, rest);
700 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
705 u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
707 fill = (TS_SIZE - 4) - length;
712 tshead[1] |= (u8)((pid & 0x1F00) >> 8);
713 tshead[2] |= (u8)(pid & 0x00FF);
714 tshead[3] |= ((*counter)++ & 0x0F);
715 memcpy(buf, tshead, 4);
725 for (i = 6; i < fill + 4; i++) {
735 static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
736 struct dvb_demux_feed *feed)
744 buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
746 case PROG_STREAM_MAP:
747 case PRIVATE_STREAM2:
748 case PROG_STREAM_DIR:
751 case PADDING_STREAM :
753 case ISO13522_STREAM:
754 case PRIVATE_STREAM1:
755 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
756 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
765 memset(obuf, 0, TS_SIZE);
766 if (length - c >= (TS_SIZE - 4)){
767 l = write_ts_header2(pid, counter, pes_start,
768 obuf, (TS_SIZE - 4));
769 memcpy(obuf + l, buf + c, TS_SIZE - l);
772 l = write_ts_header2(pid, counter, pes_start,
774 memcpy(obuf + l, buf + c, TS_SIZE - l);
777 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
783 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
785 struct dvb_demux *demux = feed->demux;
786 struct av7110 *av7110 = (struct av7110 *) demux->priv;
787 struct ipack *ipack = &av7110->ipack[feed->pes_type];
789 dprintk(2, "av7110:%p, \n", av7110);
791 switch (feed->pes_type) {
793 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
797 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
804 if (!(buf[3] & 0x10)) /* no payload? */
807 av7110_ipack_flush(ipack);
809 if (buf[3] & 0x20) { /* adaptation field? */
816 av7110_ipack_instant_repack(buf + 4, len - 4, &av7110->ipack[feed->pes_type]);
822 /******************************************************************************
823 * Video MPEG decoder events
824 ******************************************************************************/
825 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
827 struct dvb_video_events *events = &av7110->video_events;
830 spin_lock_bh(&events->lock);
832 wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
833 if (wp == events->eventr) {
834 events->overflow = 1;
835 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
839 memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
842 spin_unlock_bh(&events->lock);
844 wake_up_interruptible(&events->wait_queue);
848 static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
850 struct dvb_video_events *events = &av7110->video_events;
852 if (events->overflow) {
853 events->overflow = 0;
856 if (events->eventw == events->eventr) {
859 if (flags & O_NONBLOCK)
862 ret = wait_event_interruptible(events->wait_queue,
863 events->eventw != events->eventr);
868 spin_lock_bh(&events->lock);
870 memcpy(event, &events->events[events->eventr],
871 sizeof(struct video_event));
872 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
874 spin_unlock_bh(&events->lock);
880 /******************************************************************************
881 * DVB device file operations
882 ******************************************************************************/
884 static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
886 struct dvb_device *dvbdev = file->private_data;
887 struct av7110 *av7110 = dvbdev->priv;
888 unsigned int mask = 0;
890 dprintk(2, "av7110:%p, \n", av7110);
892 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
893 poll_wait(file, &av7110->avout.queue, wait);
895 poll_wait(file, &av7110->video_events.wait_queue, wait);
897 if (av7110->video_events.eventw != av7110->video_events.eventr)
900 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
901 if (av7110->playing) {
903 mask |= (POLLOUT | POLLWRNORM);
904 } else /* if not playing: may play if asked for */
905 mask |= (POLLOUT | POLLWRNORM);
911 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
912 size_t count, loff_t *ppos)
914 struct dvb_device *dvbdev = file->private_data;
915 struct av7110 *av7110 = dvbdev->priv;
917 dprintk(2, "av7110:%p, \n", av7110);
919 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
922 if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
925 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
928 static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
930 struct dvb_device *dvbdev = file->private_data;
931 struct av7110 *av7110 = dvbdev->priv;
932 unsigned int mask = 0;
934 dprintk(2, "av7110:%p, \n", av7110);
936 poll_wait(file, &av7110->aout.queue, wait);
938 if (av7110->playing) {
939 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
940 mask |= (POLLOUT | POLLWRNORM);
941 } else /* if not playing: may play if asked for */
942 mask = (POLLOUT | POLLWRNORM);
947 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
948 size_t count, loff_t *ppos)
950 struct dvb_device *dvbdev = file->private_data;
951 struct av7110 *av7110 = dvbdev->priv;
953 dprintk(2, "av7110:%p, \n", av7110);
955 if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
956 printk(KERN_ERR "not audio source memory\n");
959 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
962 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
964 #define MIN_IFRAME 400000
966 static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
971 dprintk(2, "av7110:%p, \n", av7110);
973 if (!(av7110->playing & RP_VIDEO)) {
974 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
978 for (i = 0; i < len - 5; i++) {
979 /* get progressive flag from picture extension */
980 if (buf[i] == 0x00 && buf[i+1] == 0x00 &&
981 buf[i+2] == 0x01 && (unsigned char)buf[i+3] == 0xb5 &&
982 (buf[i+4] & 0xf0) == 0x10)
983 progressive = buf[i+5] & 0x08;
986 /* setting n always > 1, fixes problems when playing stillframes
987 consisting of I- and P-Frames */
988 n = MIN_IFRAME / len + 1;
990 /* FIXME: nonblock? */
991 dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
993 for (i = 0; i < n; i++)
994 dvb_play(av7110, buf, len, 0, 1);
996 av7110_ipack_flush(&av7110->ipack[1]);
999 return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1005 static int dvb_video_ioctl(struct inode *inode, struct file *file,
1006 unsigned int cmd, void *parg)
1008 struct dvb_device *dvbdev = file->private_data;
1009 struct av7110 *av7110 = dvbdev->priv;
1010 unsigned long arg = (unsigned long) parg;
1013 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1015 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1016 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1017 cmd != VIDEO_GET_SIZE ) {
1024 av7110->videostate.play_state = VIDEO_STOPPED;
1025 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1026 ret = av7110_av_stop(av7110, RP_VIDEO);
1028 ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
1029 av7110->videostate.video_blank ? 0 : 1);
1031 av7110->trickmode = TRICK_NONE;
1035 av7110->trickmode = TRICK_NONE;
1036 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1037 av7110->videostate.play_state = VIDEO_PLAYING;
1038 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1043 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1044 if (av7110->playing == RP_AV) {
1045 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1048 av7110->playing &= ~RP_VIDEO;
1050 ret = av7110_av_start_play(av7110, RP_VIDEO);
1053 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1055 av7110->videostate.play_state = VIDEO_PLAYING;
1059 av7110->videostate.play_state = VIDEO_FREEZED;
1060 if (av7110->playing & RP_VIDEO)
1061 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1063 ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1065 av7110->trickmode = TRICK_FREEZE;
1068 case VIDEO_CONTINUE:
1069 if (av7110->playing & RP_VIDEO)
1070 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1072 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1074 av7110->videostate.play_state = VIDEO_PLAYING;
1075 av7110->trickmode = TRICK_NONE;
1079 case VIDEO_SELECT_SOURCE:
1080 av7110->videostate.stream_source = (video_stream_source_t) arg;
1083 case VIDEO_SET_BLANK:
1084 av7110->videostate.video_blank = (int) arg;
1087 case VIDEO_GET_STATUS:
1088 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1091 case VIDEO_GET_EVENT:
1092 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1095 case VIDEO_GET_SIZE:
1096 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1099 case VIDEO_SET_DISPLAY_FORMAT:
1101 video_displayformat_t format = (video_displayformat_t) arg;
1104 case VIDEO_PAN_SCAN:
1105 av7110->display_panscan = VID_PAN_SCAN_PREF;
1108 case VIDEO_LETTER_BOX:
1109 av7110->display_panscan = VID_VC_AND_PS_PREF;
1112 case VIDEO_CENTER_CUT_OUT:
1113 av7110->display_panscan = VID_CENTRE_CUT_PREF;
1121 av7110->videostate.display_format = format;
1122 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1123 1, av7110->display_panscan);
1127 case VIDEO_SET_FORMAT:
1132 av7110->display_ar = arg;
1133 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1137 case VIDEO_STILLPICTURE:
1139 struct video_still_picture *pic =
1140 (struct video_still_picture *) parg;
1141 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1142 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1143 ret = play_iframe(av7110, pic->iFrame, pic->size,
1144 file->f_flags & O_NONBLOCK);
1148 case VIDEO_FAST_FORWARD:
1149 //note: arg is ignored by firmware
1150 if (av7110->playing & RP_VIDEO)
1151 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1152 __Scan_I, 2, AV_PES, 0);
1154 ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
1156 av7110->trickmode = TRICK_FAST;
1157 av7110->videostate.play_state = VIDEO_PLAYING;
1161 case VIDEO_SLOWMOTION:
1162 if (av7110->playing&RP_VIDEO) {
1163 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1165 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1167 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1169 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
1171 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1174 av7110->trickmode = TRICK_SLOW;
1175 av7110->videostate.play_state = VIDEO_PLAYING;
1179 case VIDEO_GET_CAPABILITIES:
1180 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1181 VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1184 case VIDEO_CLEAR_BUFFER:
1185 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1186 av7110_ipack_reset(&av7110->ipack[1]);
1188 if (av7110->playing == RP_AV) {
1189 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1190 __Play, 2, AV_PES, 0);
1193 if (av7110->trickmode == TRICK_FAST)
1194 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1195 __Scan_I, 2, AV_PES, 0);
1196 if (av7110->trickmode == TRICK_SLOW) {
1197 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1200 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1202 if (av7110->trickmode == TRICK_FREEZE)
1203 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
1207 case VIDEO_SET_STREAMTYPE:
1218 static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1219 unsigned int cmd, void *parg)
1221 struct dvb_device *dvbdev = file->private_data;
1222 struct av7110 *av7110 = dvbdev->priv;
1223 unsigned long arg = (unsigned long) parg;
1226 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1228 if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1229 (cmd != AUDIO_GET_STATUS))
1234 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1235 ret = av7110_av_stop(av7110, RP_AUDIO);
1237 ret = audcom(av7110, AUDIO_CMD_MUTE);
1239 av7110->audiostate.play_state = AUDIO_STOPPED;
1243 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1244 ret = av7110_av_start_play(av7110, RP_AUDIO);
1246 ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1248 av7110->audiostate.play_state = AUDIO_PLAYING;
1252 ret = audcom(av7110, AUDIO_CMD_MUTE);
1254 av7110->audiostate.play_state = AUDIO_PAUSED;
1257 case AUDIO_CONTINUE:
1258 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1259 av7110->audiostate.play_state = AUDIO_PLAYING;
1260 ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1264 case AUDIO_SELECT_SOURCE:
1265 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1268 case AUDIO_SET_MUTE:
1270 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1272 av7110->audiostate.mute_state = (int) arg;
1276 case AUDIO_SET_AV_SYNC:
1277 av7110->audiostate.AV_sync_state = (int) arg;
1278 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1281 case AUDIO_SET_BYPASS_MODE:
1282 if (FW_VERSION(av7110->arm_app) < 0x2621)
1284 av7110->audiostate.bypass_mode = (int)arg;
1287 case AUDIO_CHANNEL_SELECT:
1288 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1290 switch(av7110->audiostate.channel_select) {
1292 ret = audcom(av7110, AUDIO_CMD_STEREO);
1294 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1295 i2c_writereg(av7110, 0x20, 0x02, 0x49);
1296 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1297 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1301 case AUDIO_MONO_LEFT:
1302 ret = audcom(av7110, AUDIO_CMD_MONO_L);
1304 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1305 i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1306 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1307 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1311 case AUDIO_MONO_RIGHT:
1312 ret = audcom(av7110, AUDIO_CMD_MONO_R);
1314 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1315 i2c_writereg(av7110, 0x20, 0x02, 0x45);
1316 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1317 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1327 case AUDIO_GET_STATUS:
1328 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1331 case AUDIO_GET_CAPABILITIES:
1332 if (FW_VERSION(av7110->arm_app) < 0x2621)
1333 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1335 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1336 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1339 case AUDIO_CLEAR_BUFFER:
1340 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1341 av7110_ipack_reset(&av7110->ipack[0]);
1342 if (av7110->playing == RP_AV)
1343 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1344 __Play, 2, AV_PES, 0);
1349 case AUDIO_SET_MIXER:
1351 struct audio_mixer *amix = (struct audio_mixer *)parg;
1353 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1356 case AUDIO_SET_STREAMTYPE:
1365 static int dvb_video_open(struct inode *inode, struct file *file)
1367 struct dvb_device *dvbdev = file->private_data;
1368 struct av7110 *av7110 = dvbdev->priv;
1371 dprintk(2, "av7110:%p, \n", av7110);
1373 if ((err = dvb_generic_open(inode, file)) < 0)
1376 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1377 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1378 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1379 av7110->video_blank = 1;
1380 av7110->audiostate.AV_sync_state = 1;
1381 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1383 /* empty event queue */
1384 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1390 static int dvb_video_release(struct inode *inode, struct file *file)
1392 struct dvb_device *dvbdev = file->private_data;
1393 struct av7110 *av7110 = dvbdev->priv;
1395 dprintk(2, "av7110:%p, \n", av7110);
1397 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1398 av7110_av_stop(av7110, RP_VIDEO);
1401 return dvb_generic_release(inode, file);
1404 static int dvb_audio_open(struct inode *inode, struct file *file)
1406 struct dvb_device *dvbdev = file->private_data;
1407 struct av7110 *av7110 = dvbdev->priv;
1408 int err = dvb_generic_open(inode, file);
1410 dprintk(2, "av7110:%p, \n", av7110);
1414 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1415 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1419 static int dvb_audio_release(struct inode *inode, struct file *file)
1421 struct dvb_device *dvbdev = file->private_data;
1422 struct av7110 *av7110 = dvbdev->priv;
1424 dprintk(2, "av7110:%p, \n", av7110);
1426 av7110_av_stop(av7110, RP_AUDIO);
1427 return dvb_generic_release(inode, file);
1432 /******************************************************************************
1433 * driver registration
1434 ******************************************************************************/
1436 static struct file_operations dvb_video_fops = {
1437 .owner = THIS_MODULE,
1438 .write = dvb_video_write,
1439 .ioctl = dvb_generic_ioctl,
1440 .open = dvb_video_open,
1441 .release = dvb_video_release,
1442 .poll = dvb_video_poll,
1445 static struct dvb_device dvbdev_video = {
1448 .readers = 5, /* arbitrary */
1450 .fops = &dvb_video_fops,
1451 .kernel_ioctl = dvb_video_ioctl,
1454 static struct file_operations dvb_audio_fops = {
1455 .owner = THIS_MODULE,
1456 .write = dvb_audio_write,
1457 .ioctl = dvb_generic_ioctl,
1458 .open = dvb_audio_open,
1459 .release = dvb_audio_release,
1460 .poll = dvb_audio_poll,
1463 static struct dvb_device dvbdev_audio = {
1467 .fops = &dvb_audio_fops,
1468 .kernel_ioctl = dvb_audio_ioctl,
1472 int av7110_av_register(struct av7110 *av7110)
1474 av7110->audiostate.AV_sync_state = 0;
1475 av7110->audiostate.mute_state = 0;
1476 av7110->audiostate.play_state = AUDIO_STOPPED;
1477 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1478 av7110->audiostate.channel_select = AUDIO_STEREO;
1479 av7110->audiostate.bypass_mode = 0;
1481 av7110->videostate.video_blank = 0;
1482 av7110->videostate.play_state = VIDEO_STOPPED;
1483 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1484 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1485 av7110->videostate.display_format = VIDEO_LETTER_BOX;
1486 av7110->display_ar = VIDEO_FORMAT_4_3;
1487 av7110->display_panscan = VID_VC_AND_PS_PREF;
1489 init_waitqueue_head(&av7110->video_events.wait_queue);
1490 spin_lock_init(&av7110->video_events.lock);
1491 av7110->video_events.eventw = av7110->video_events.eventr = 0;
1492 av7110->video_events.overflow = 0;
1493 memset(&av7110->video_size, 0, sizeof (video_size_t));
1495 dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1496 &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1498 dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1499 &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1504 void av7110_av_unregister(struct av7110 *av7110)
1506 dvb_unregister_device(av7110->audio_dev);
1507 dvb_unregister_device(av7110->video_dev);
1510 int av7110_av_init(struct av7110 *av7110)
1512 void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1515 for (i = 0; i < 2; i++) {
1516 struct ipack *ipack = av7110->ipack + i;
1518 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1521 av7110_ipack_free(--ipack);
1524 ipack->data = av7110;
1527 dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1528 dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1530 av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1531 av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1536 void av7110_av_exit(struct av7110 *av7110)
1538 av7110_ipack_free(&av7110->ipack[0]);
1539 av7110_ipack_free(&av7110->ipack[1]);