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/sched.h>
35 #include <linux/delay.h>
36 #include <linux/smp_lock.h>
40 #include "av7110_hw.h"
41 #include "av7110_av.h"
42 #include "av7110_ipack.h"
44 /* MPEG-2 (ISO 13818 / H.222.0) stream types */
45 #define PROG_STREAM_MAP 0xBC
46 #define PRIVATE_STREAM1 0xBD
47 #define PADDING_STREAM 0xBE
48 #define PRIVATE_STREAM2 0xBF
49 #define AUDIO_STREAM_S 0xC0
50 #define AUDIO_STREAM_E 0xDF
51 #define VIDEO_STREAM_S 0xE0
52 #define VIDEO_STREAM_E 0xEF
53 #define ECM_STREAM 0xF0
54 #define EMM_STREAM 0xF1
55 #define DSM_CC_STREAM 0xF2
56 #define ISO13522_STREAM 0xF3
57 #define PROG_STREAM_DIR 0xFF
59 #define PTS_DTS_FLAGS 0xC0
65 #define TRANS_ERROR 0x80
66 #define PAY_START 0x40
67 #define TRANS_PRIO 0x20
68 #define PID_MASK_HI 0x1F
70 #define TRANS_SCRMBL1 0x80
71 #define TRANS_SCRMBL2 0x40
72 #define ADAPT_FIELD 0x20
74 #define COUNT_MASK 0x0F
77 #define DISCON_IND 0x80
78 #define RAND_ACC_IND 0x40
79 #define ES_PRI_IND 0x20
81 #define OPCR_FLAG 0x08
82 #define SPLICE_FLAG 0x04
83 #define TRANS_PRIV 0x02
84 #define ADAP_EXT_FLAG 0x01
86 // adaptation extension flags
88 #define PIECE_RATE 0x40
89 #define SEAM_SPLICE 0x20
92 static void p_to_t(u8 const *buf, long int length, u16 pid,
93 u8 *counter, struct dvb_demux_feed *feed);
96 int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
98 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
100 if (!(dvbdmxfeed->ts_type & TS_PACKET))
102 if (buf[3] == 0xe0) // video PES do not have a length in TS
104 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
105 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
106 &dvbdmxfeed->feed.ts, DMX_OK);
108 return dvb_filter_pes2ts(p2t, buf, len, 1);
111 static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
113 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
115 dvbdmxfeed->cb.ts(data, 188, NULL, 0,
116 &dvbdmxfeed->feed.ts, DMX_OK);
120 int av7110_av_start_record(struct av7110 *av7110, int av,
121 struct dvb_demux_feed *dvbdmxfeed)
124 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
126 dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
128 if (av7110->playing || (av7110->rec_mode & av))
130 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
131 dvbdmx->recording = 1;
132 av7110->rec_mode |= av;
134 switch (av7110->rec_mode) {
136 dvb_filter_pes2ts_init(&av7110->p2t[0],
137 dvbdmx->pesfilter[0]->pid,
138 dvb_filter_pes2ts_cb,
139 (void *) dvbdmx->pesfilter[0]);
140 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
144 dvb_filter_pes2ts_init(&av7110->p2t[1],
145 dvbdmx->pesfilter[1]->pid,
146 dvb_filter_pes2ts_cb,
147 (void *) dvbdmx->pesfilter[1]);
148 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
152 dvb_filter_pes2ts_init(&av7110->p2t[0],
153 dvbdmx->pesfilter[0]->pid,
154 dvb_filter_pes2ts_cb,
155 (void *) dvbdmx->pesfilter[0]);
156 dvb_filter_pes2ts_init(&av7110->p2t[1],
157 dvbdmx->pesfilter[1]->pid,
158 dvb_filter_pes2ts_cb,
159 (void *) dvbdmx->pesfilter[1]);
160 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
166 int av7110_av_start_play(struct av7110 *av7110, int av)
169 dprintk(2, "av7110:%p, \n", av7110);
171 if (av7110->rec_mode)
173 if (av7110->playing & av)
176 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
178 if (av7110->playing == RP_NONE) {
179 av7110_ipack_reset(&av7110->ipack[0]);
180 av7110_ipack_reset(&av7110->ipack[1]);
183 av7110->playing |= av;
184 switch (av7110->playing) {
186 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
189 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
194 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
198 ret = av7110->playing;
202 int av7110_av_stop(struct av7110 *av7110, int av)
205 dprintk(2, "av7110:%p, \n", av7110);
207 if (!(av7110->playing & av) && !(av7110->rec_mode & av))
209 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
210 if (av7110->playing) {
211 av7110->playing &= ~av;
212 switch (av7110->playing) {
214 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
217 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
220 ret = av7110_set_vidmode(av7110, av7110->vidmode);
224 av7110->rec_mode &= ~av;
225 switch (av7110->rec_mode) {
227 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
230 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
240 int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
247 wake_up(&buf->queue);
251 if ((len = dvb_ringbuffer_avail(buf)) < 6)
253 sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
254 sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
255 sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
256 sync |= DVB_RINGBUFFER_PEEK(buf, 3);
258 if (((sync &~ 0x0f) == 0x000001e0) ||
259 ((sync &~ 0x1f) == 0x000001c0) ||
260 (sync == 0x000001bd))
263 DVB_RINGBUFFER_SKIP(buf, 1);
265 blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
266 blen |= DVB_RINGBUFFER_PEEK(buf, 5);
268 if (len < blen || blen > dlen) {
269 //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
270 wake_up(&buf->queue);
274 dvb_ringbuffer_read(buf, dest, (size_t) blen, 0);
276 dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
277 (unsigned long) buf->pread, (unsigned long) buf->pwrite);
278 wake_up(&buf->queue);
283 int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
285 int err, vol, val, balance = 0;
287 dprintk(2, "av7110:%p, \n", av7110);
289 av7110->mixer.volume_left = volleft;
290 av7110->mixer.volume_right = volright;
292 switch (av7110->adac_type) {
294 volleft = (volleft * 256) / 1036;
295 volright = (volright * 256) / 1036;
300 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
302 return SendDAC(av7110, 4, volright);
304 case DVB_ADAC_CRYSTAL:
305 volleft = 127 - volleft / 2;
306 volright = 127 - volright / 2;
307 i2c_writereg(av7110, 0x20, 0x03, volleft);
308 i2c_writereg(av7110, 0x20, 0x04, volright);
311 case DVB_ADAC_MSP34x0:
312 vol = (volleft > volright) ? volleft : volright;
313 val = (vol * 0x73 / 255) << 8;
315 balance = ((volright - volleft) * 127) / vol;
316 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
317 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
318 msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
321 case DVB_ADAC_MSP34x5:
322 vol = (volleft > volright) ? volleft : volright;
323 val = (vol * 0x73 / 255) << 8;
325 balance = ((volright - volleft) * 127) / vol;
326 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
327 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
334 int av7110_set_vidmode(struct av7110 *av7110, int mode)
337 dprintk(2, "av7110:%p, \n", av7110);
339 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
341 if (!ret && !av7110->playing) {
342 ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
343 av7110->pids[DMX_PES_AUDIO],
344 av7110->pids[DMX_PES_TELETEXT],
345 0, av7110->pids[DMX_PES_PCR]);
347 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
353 static int sw2mode[16] = {
354 VIDEO_MODE_PAL, VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL,
355 VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL, VIDEO_MODE_NTSC,
356 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL,
357 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL,
360 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
368 dprintk(2, "av7110:%p, \n", av7110);
372 for (i = 7; i < count - 10; i++) {
374 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
377 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
378 vsize = ((p[1] &0x0F) << 8) | (p[2]);
380 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
382 dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
391 /****************************************************************************
392 * I/O buffer management and control
393 ****************************************************************************/
395 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
396 const char *buf, unsigned long count)
398 unsigned long todo = count;
402 if (dvb_ringbuffer_free(rbuf) < 2048) {
403 if (wait_event_interruptible(rbuf->queue,
404 (dvb_ringbuffer_free(rbuf) >= 2048)))
407 free = dvb_ringbuffer_free(rbuf);
410 dvb_ringbuffer_write(rbuf, buf, free);
418 static void play_video_cb(u8 *buf, int count, void *priv)
420 struct av7110 *av7110 = (struct av7110 *) priv;
421 dprintk(2, "av7110:%p, \n", av7110);
423 if ((buf[3] & 0xe0) == 0xe0) {
424 get_video_format(av7110, buf, count);
425 aux_ring_buffer_write(&av7110->avout, buf, count);
427 aux_ring_buffer_write(&av7110->aout, buf, count);
430 static void play_audio_cb(u8 *buf, int count, void *priv)
432 struct av7110 *av7110 = (struct av7110 *) priv;
433 dprintk(2, "av7110:%p, \n", av7110);
435 aux_ring_buffer_write(&av7110->aout, buf, count);
438 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
439 dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
441 static ssize_t dvb_play(struct av7110 *av7110, const u8 __user *buf,
442 unsigned long count, int nonblock, int type)
444 unsigned long todo = count, n;
445 dprintk(2, "av7110:%p, \n", av7110);
447 if (!av7110->kbuf[type])
450 if (nonblock && !FREE_COND)
457 if (wait_event_interruptible(av7110->avout.queue,
464 if (copy_from_user(av7110->kbuf[type], buf, n))
466 av7110_ipack_instant_repack(av7110->kbuf[type], n,
467 &av7110->ipack[type]);
474 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
475 unsigned long count, int nonblock, int type)
477 unsigned long todo = count, n;
478 dprintk(2, "av7110:%p, \n", av7110);
480 if (!av7110->kbuf[type])
483 if (nonblock && !FREE_COND)
490 if (wait_event_interruptible(av7110->avout.queue,
497 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
504 static ssize_t dvb_aplay(struct av7110 *av7110, const u8 __user *buf,
505 unsigned long count, int nonblock, int type)
507 unsigned long todo = count, n;
508 dprintk(2, "av7110:%p, \n", av7110);
510 if (!av7110->kbuf[type])
512 if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
516 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
519 if (wait_event_interruptible(av7110->aout.queue,
520 (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
526 if (copy_from_user(av7110->kbuf[type], buf, n))
528 av7110_ipack_instant_repack(av7110->kbuf[type], n,
529 &av7110->ipack[type]);
536 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
538 memset(p->pes, 0, TS_SIZE);
546 static void clear_p2t(struct av7110_p2t *p)
548 memset(p->pes, 0, TS_SIZE);
555 static int find_pes_header(u8 const *buf, long int length, int *frags)
562 while (c < length - 3 && !found) {
563 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
564 buf[c + 2] == 0x01) {
565 switch ( buf[c + 3] ) {
566 case PROG_STREAM_MAP:
567 case PRIVATE_STREAM2:
568 case PROG_STREAM_DIR:
571 case PADDING_STREAM :
573 case ISO13522_STREAM:
574 case PRIVATE_STREAM1:
575 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
576 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
587 if (c == length - 3 && !found) {
588 if (buf[length - 1] == 0x00)
590 if (buf[length - 2] == 0x00 &&
591 buf[length - 1] == 0x00)
593 if (buf[length - 3] == 0x00 &&
594 buf[length - 2] == 0x00 &&
595 buf[length - 1] == 0x01)
603 void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
614 if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
620 if (buf[c] == 0x01) {
630 case PROG_STREAM_MAP:
631 case PRIVATE_STREAM2:
632 case PROG_STREAM_DIR:
635 case PADDING_STREAM :
637 case ISO13522_STREAM:
638 case PRIVATE_STREAM1:
639 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
640 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
646 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
647 c += (TS_SIZE - 4) - p->pos;
648 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
661 c2 = find_pes_header(buf + c, length - c, &p->frags);
662 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
665 l = (TS_SIZE - 4) - p->pos;
666 memcpy(p->pes + p->pos, buf, l);
669 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
675 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
679 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
687 rest = l % (TS_SIZE - 4);
689 p_to_t(buf + c, l, pid, &p->counter, p->feed);
690 memcpy(p->pes, buf + c + l, rest);
698 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
703 u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
705 fill = (TS_SIZE - 4) - length;
710 tshead[1] |= (u8)((pid & 0x1F00) >> 8);
711 tshead[2] |= (u8)(pid & 0x00FF);
712 tshead[3] |= ((*counter)++ & 0x0F);
713 memcpy(buf, tshead, 4);
723 for (i = 6; i < fill + 4; i++) {
733 static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
734 struct dvb_demux_feed *feed)
742 buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
744 case PROG_STREAM_MAP:
745 case PRIVATE_STREAM2:
746 case PROG_STREAM_DIR:
749 case PADDING_STREAM :
751 case ISO13522_STREAM:
752 case PRIVATE_STREAM1:
753 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
754 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
763 memset(obuf, 0, TS_SIZE);
764 if (length - c >= (TS_SIZE - 4)){
765 l = write_ts_header2(pid, counter, pes_start,
766 obuf, (TS_SIZE - 4));
767 memcpy(obuf + l, buf + c, TS_SIZE - l);
770 l = write_ts_header2(pid, counter, pes_start,
772 memcpy(obuf + l, buf + c, TS_SIZE - l);
775 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
781 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
783 struct dvb_demux *demux = feed->demux;
784 struct av7110 *av7110 = (struct av7110 *) demux->priv;
785 struct ipack *ipack = &av7110->ipack[feed->pes_type];
787 dprintk(2, "av7110:%p, \n", av7110);
789 switch (feed->pes_type) {
791 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
795 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
802 if (!(buf[3] & 0x10)) /* no payload? */
805 av7110_ipack_flush(ipack);
807 if (buf[3] & 0x20) { /* adaptation field? */
814 av7110_ipack_instant_repack(buf + 4, len - 4, &av7110->ipack[feed->pes_type]);
820 /******************************************************************************
821 * Video MPEG decoder events
822 ******************************************************************************/
823 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
825 struct dvb_video_events *events = &av7110->video_events;
828 spin_lock_bh(&events->lock);
830 wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
831 if (wp == events->eventr) {
832 events->overflow = 1;
833 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
837 memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
840 spin_unlock_bh(&events->lock);
842 wake_up_interruptible(&events->wait_queue);
846 static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
848 struct dvb_video_events *events = &av7110->video_events;
850 if (events->overflow) {
851 events->overflow = 0;
854 if (events->eventw == events->eventr) {
857 if (flags & O_NONBLOCK)
860 ret = wait_event_interruptible(events->wait_queue,
861 events->eventw != events->eventr);
866 spin_lock_bh(&events->lock);
868 memcpy(event, &events->events[events->eventr],
869 sizeof(struct video_event));
870 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
872 spin_unlock_bh(&events->lock);
878 /******************************************************************************
879 * DVB device file operations
880 ******************************************************************************/
882 static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
884 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
885 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
886 unsigned int mask = 0;
888 dprintk(2, "av7110:%p, \n", av7110);
890 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
891 poll_wait(file, &av7110->avout.queue, wait);
893 poll_wait(file, &av7110->video_events.wait_queue, wait);
895 if (av7110->video_events.eventw != av7110->video_events.eventr)
898 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
899 if (av7110->playing) {
901 mask |= (POLLOUT | POLLWRNORM);
902 } else /* if not playing: may play if asked for */
903 mask |= (POLLOUT | POLLWRNORM);
909 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
910 size_t count, loff_t *ppos)
912 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
913 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
915 dprintk(2, "av7110:%p, \n", av7110);
917 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
920 if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
923 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
926 static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
928 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
929 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
930 unsigned int mask = 0;
932 dprintk(2, "av7110:%p, \n", av7110);
934 poll_wait(file, &av7110->aout.queue, wait);
936 if (av7110->playing) {
937 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
938 mask |= (POLLOUT | POLLWRNORM);
939 } else /* if not playing: may play if asked for */
940 mask = (POLLOUT | POLLWRNORM);
945 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
946 size_t count, loff_t *ppos)
948 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
949 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
951 dprintk(2, "av7110:%p, \n", av7110);
953 if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
954 printk(KERN_ERR "not audio source memory\n");
957 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
960 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
962 #define MIN_IFRAME 400000
964 static int play_iframe(struct av7110 *av7110, u8 __user *buf, unsigned int len, int nonblock)
968 dprintk(2, "av7110:%p, \n", av7110);
970 if (!(av7110->playing & RP_VIDEO)) {
971 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
975 /* setting n always > 1, fixes problems when playing stillframes
976 consisting of I- and P-Frames */
977 n = MIN_IFRAME / len + 1;
979 /* FIXME: nonblock? */
980 dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
982 for (i = 0; i < n; i++)
983 dvb_play(av7110, buf, len, 0, 1);
985 av7110_ipack_flush(&av7110->ipack[1]);
990 static int dvb_video_ioctl(struct inode *inode, struct file *file,
991 unsigned int cmd, void *parg)
993 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
994 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
995 unsigned long arg = (unsigned long) parg;
998 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1000 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1001 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1002 cmd != VIDEO_GET_SIZE ) {
1009 av7110->videostate.play_state = VIDEO_STOPPED;
1010 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1011 ret = av7110_av_stop(av7110, RP_VIDEO);
1013 ret = vidcom(av7110, VIDEO_CMD_STOP,
1014 av7110->videostate.video_blank ? 0 : 1);
1016 av7110->trickmode = TRICK_NONE;
1020 av7110->trickmode = TRICK_NONE;
1021 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1022 av7110->videostate.play_state = VIDEO_PLAYING;
1023 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1028 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1029 if (av7110->playing == RP_AV) {
1030 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1033 av7110->playing &= ~RP_VIDEO;
1035 ret = av7110_av_start_play(av7110, RP_VIDEO);
1038 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1040 av7110->videostate.play_state = VIDEO_PLAYING;
1044 av7110->videostate.play_state = VIDEO_FREEZED;
1045 if (av7110->playing & RP_VIDEO)
1046 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1048 ret = vidcom(av7110, VIDEO_CMD_FREEZE, 1);
1050 av7110->trickmode = TRICK_FREEZE;
1053 case VIDEO_CONTINUE:
1054 if (av7110->playing & RP_VIDEO)
1055 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1057 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1059 av7110->videostate.play_state = VIDEO_PLAYING;
1060 av7110->trickmode = TRICK_NONE;
1064 case VIDEO_SELECT_SOURCE:
1065 av7110->videostate.stream_source = (video_stream_source_t) arg;
1068 case VIDEO_SET_BLANK:
1069 av7110->videostate.video_blank = (int) arg;
1072 case VIDEO_GET_STATUS:
1073 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1076 case VIDEO_GET_EVENT:
1077 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1080 case VIDEO_GET_SIZE:
1081 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1084 case VIDEO_SET_DISPLAY_FORMAT:
1086 video_displayformat_t format = (video_displayformat_t) arg;
1090 case VIDEO_PAN_SCAN:
1091 val = VID_PAN_SCAN_PREF;
1094 case VIDEO_LETTER_BOX:
1095 val = VID_VC_AND_PS_PREF;
1098 case VIDEO_CENTER_CUT_OUT:
1099 val = VID_CENTRE_CUT_PREF;
1107 av7110->videostate.display_format = format;
1108 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1113 case VIDEO_SET_FORMAT:
1118 av7110->display_ar = arg;
1119 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1123 case VIDEO_STILLPICTURE:
1125 struct video_still_picture *pic =
1126 (struct video_still_picture *) parg;
1127 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1128 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1129 ret = play_iframe(av7110, pic->iFrame, pic->size,
1130 file->f_flags & O_NONBLOCK);
1134 case VIDEO_FAST_FORWARD:
1135 //note: arg is ignored by firmware
1136 if (av7110->playing & RP_VIDEO)
1137 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1138 __Scan_I, 2, AV_PES, 0);
1140 ret = vidcom(av7110, VIDEO_CMD_FFWD, arg);
1142 av7110->trickmode = TRICK_FAST;
1143 av7110->videostate.play_state = VIDEO_PLAYING;
1147 case VIDEO_SLOWMOTION:
1148 if (av7110->playing&RP_VIDEO) {
1149 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1151 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1153 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1155 ret = vidcom(av7110, VIDEO_CMD_STOP, 0);
1157 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1160 av7110->trickmode = TRICK_SLOW;
1161 av7110->videostate.play_state = VIDEO_PLAYING;
1165 case VIDEO_GET_CAPABILITIES:
1166 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1167 VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1170 case VIDEO_CLEAR_BUFFER:
1171 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1172 av7110_ipack_reset(&av7110->ipack[1]);
1174 if (av7110->playing == RP_AV) {
1175 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1176 __Play, 2, AV_PES, 0);
1179 if (av7110->trickmode == TRICK_FAST)
1180 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1181 __Scan_I, 2, AV_PES, 0);
1182 if (av7110->trickmode == TRICK_SLOW) {
1183 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1186 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1188 if (av7110->trickmode == TRICK_FREEZE)
1189 ret = vidcom(av7110, VIDEO_CMD_STOP, 1);
1193 case VIDEO_SET_STREAMTYPE:
1204 static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1205 unsigned int cmd, void *parg)
1207 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1208 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1209 unsigned long arg = (unsigned long) parg;
1212 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1214 if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1215 (cmd != AUDIO_GET_STATUS))
1220 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1221 ret = av7110_av_stop(av7110, RP_AUDIO);
1223 ret = audcom(av7110, AUDIO_CMD_MUTE);
1225 av7110->audiostate.play_state = AUDIO_STOPPED;
1229 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1230 ret = av7110_av_start_play(av7110, RP_AUDIO);
1232 ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1234 av7110->audiostate.play_state = AUDIO_PLAYING;
1238 ret = audcom(av7110, AUDIO_CMD_MUTE);
1240 av7110->audiostate.play_state = AUDIO_PAUSED;
1243 case AUDIO_CONTINUE:
1244 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1245 av7110->audiostate.play_state = AUDIO_PLAYING;
1246 ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1250 case AUDIO_SELECT_SOURCE:
1251 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1254 case AUDIO_SET_MUTE:
1256 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1258 av7110->audiostate.mute_state = (int) arg;
1262 case AUDIO_SET_AV_SYNC:
1263 av7110->audiostate.AV_sync_state = (int) arg;
1264 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1267 case AUDIO_SET_BYPASS_MODE:
1268 if (FW_VERSION(av7110->arm_app) < 0x2621)
1270 av7110->audiostate.bypass_mode = (int)arg;
1273 case AUDIO_CHANNEL_SELECT:
1274 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1276 switch(av7110->audiostate.channel_select) {
1278 ret = audcom(av7110, AUDIO_CMD_STEREO);
1280 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1281 i2c_writereg(av7110, 0x20, 0x02, 0x49);
1282 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1283 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1287 case AUDIO_MONO_LEFT:
1288 ret = audcom(av7110, AUDIO_CMD_MONO_L);
1290 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1291 i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1292 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1293 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1297 case AUDIO_MONO_RIGHT:
1298 ret = audcom(av7110, AUDIO_CMD_MONO_R);
1300 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1301 i2c_writereg(av7110, 0x20, 0x02, 0x45);
1302 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1303 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1313 case AUDIO_GET_STATUS:
1314 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1317 case AUDIO_GET_CAPABILITIES:
1318 if (FW_VERSION(av7110->arm_app) < 0x2621)
1319 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1321 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1322 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1325 case AUDIO_CLEAR_BUFFER:
1326 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1327 av7110_ipack_reset(&av7110->ipack[0]);
1328 if (av7110->playing == RP_AV)
1329 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1330 __Play, 2, AV_PES, 0);
1335 case AUDIO_SET_MIXER:
1337 struct audio_mixer *amix = (struct audio_mixer *)parg;
1339 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1342 case AUDIO_SET_STREAMTYPE:
1351 static int dvb_video_open(struct inode *inode, struct file *file)
1353 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1354 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1357 dprintk(2, "av7110:%p, \n", av7110);
1359 if ((err = dvb_generic_open(inode, file)) < 0)
1362 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1363 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1364 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1365 av7110->video_blank = 1;
1366 av7110->audiostate.AV_sync_state = 1;
1367 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1369 /* empty event queue */
1370 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1376 static int dvb_video_release(struct inode *inode, struct file *file)
1378 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1379 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1381 dprintk(2, "av7110:%p, \n", av7110);
1383 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1384 av7110_av_stop(av7110, RP_VIDEO);
1387 return dvb_generic_release(inode, file);
1390 static int dvb_audio_open(struct inode *inode, struct file *file)
1392 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1393 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1394 int err=dvb_generic_open(inode, file);
1396 dprintk(2, "av7110:%p, \n", av7110);
1400 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1401 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1405 static int dvb_audio_release(struct inode *inode, struct file *file)
1407 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1408 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1410 dprintk(2, "av7110:%p, \n", av7110);
1412 av7110_av_stop(av7110, RP_AUDIO);
1413 return dvb_generic_release(inode, file);
1418 /******************************************************************************
1419 * driver registration
1420 ******************************************************************************/
1422 static struct file_operations dvb_video_fops = {
1423 .owner = THIS_MODULE,
1424 .write = dvb_video_write,
1425 .ioctl = dvb_generic_ioctl,
1426 .open = dvb_video_open,
1427 .release = dvb_video_release,
1428 .poll = dvb_video_poll,
1431 static struct dvb_device dvbdev_video = {
1434 .readers = 5, /* arbitrary */
1436 .fops = &dvb_video_fops,
1437 .kernel_ioctl = dvb_video_ioctl,
1440 static struct file_operations dvb_audio_fops = {
1441 .owner = THIS_MODULE,
1442 .write = dvb_audio_write,
1443 .ioctl = dvb_generic_ioctl,
1444 .open = dvb_audio_open,
1445 .release = dvb_audio_release,
1446 .poll = dvb_audio_poll,
1449 static struct dvb_device dvbdev_audio = {
1453 .fops = &dvb_audio_fops,
1454 .kernel_ioctl = dvb_audio_ioctl,
1458 int av7110_av_register(struct av7110 *av7110)
1460 av7110->audiostate.AV_sync_state = 0;
1461 av7110->audiostate.mute_state = 0;
1462 av7110->audiostate.play_state = AUDIO_STOPPED;
1463 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1464 av7110->audiostate.channel_select = AUDIO_STEREO;
1465 av7110->audiostate.bypass_mode = 0;
1467 av7110->videostate.video_blank = 0;
1468 av7110->videostate.play_state = VIDEO_STOPPED;
1469 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1470 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1471 av7110->videostate.display_format = VIDEO_CENTER_CUT_OUT;
1472 av7110->display_ar = VIDEO_FORMAT_4_3;
1474 init_waitqueue_head(&av7110->video_events.wait_queue);
1475 spin_lock_init(&av7110->video_events.lock);
1476 av7110->video_events.eventw = av7110->video_events.eventr = 0;
1477 av7110->video_events.overflow = 0;
1478 memset(&av7110->video_size, 0, sizeof (video_size_t));
1480 dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1481 &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1483 dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1484 &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1489 void av7110_av_unregister(struct av7110 *av7110)
1491 dvb_unregister_device(av7110->audio_dev);
1492 dvb_unregister_device(av7110->video_dev);
1495 int av7110_av_init(struct av7110 *av7110)
1497 void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1500 for (i = 0; i < 2; i++) {
1501 struct ipack *ipack = av7110->ipack + i;
1503 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1506 av7110_ipack_free(--ipack);
1509 ipack->data = av7110;
1512 dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1513 dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1515 av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1516 av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1521 void av7110_av_exit(struct av7110 *av7110)
1523 av7110_ipack_free(&av7110->ipack[0]);
1524 av7110_ipack_free(&av7110->ipack[1]);