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/byteorder/swabb.h>
37 #include <linux/smp_lock.h>
41 #include "av7110_hw.h"
42 #include "av7110_av.h"
43 #include "av7110_ipack.h"
45 /* MPEG-2 (ISO 13818 / H.222.0) stream types */
46 #define PROG_STREAM_MAP 0xBC
47 #define PRIVATE_STREAM1 0xBD
48 #define PADDING_STREAM 0xBE
49 #define PRIVATE_STREAM2 0xBF
50 #define AUDIO_STREAM_S 0xC0
51 #define AUDIO_STREAM_E 0xDF
52 #define VIDEO_STREAM_S 0xE0
53 #define VIDEO_STREAM_E 0xEF
54 #define ECM_STREAM 0xF0
55 #define EMM_STREAM 0xF1
56 #define DSM_CC_STREAM 0xF2
57 #define ISO13522_STREAM 0xF3
58 #define PROG_STREAM_DIR 0xFF
60 #define PTS_DTS_FLAGS 0xC0
66 #define TRANS_ERROR 0x80
67 #define PAY_START 0x40
68 #define TRANS_PRIO 0x20
69 #define PID_MASK_HI 0x1F
71 #define TRANS_SCRMBL1 0x80
72 #define TRANS_SCRMBL2 0x40
73 #define ADAPT_FIELD 0x20
75 #define COUNT_MASK 0x0F
78 #define DISCON_IND 0x80
79 #define RAND_ACC_IND 0x40
80 #define ES_PRI_IND 0x20
82 #define OPCR_FLAG 0x08
83 #define SPLICE_FLAG 0x04
84 #define TRANS_PRIV 0x02
85 #define ADAP_EXT_FLAG 0x01
87 // adaptation extension flags
89 #define PIECE_RATE 0x40
90 #define SEAM_SPLICE 0x20
93 static void p_to_t(u8 const *buf, long int length, u16 pid,
94 u8 *counter, struct dvb_demux_feed *feed);
97 int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
99 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
101 if (!(dvbdmxfeed->ts_type & TS_PACKET))
103 if (buf[3] == 0xe0) // video PES do not have a length in TS
105 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
106 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
107 &dvbdmxfeed->feed.ts, DMX_OK);
109 return dvb_filter_pes2ts(p2t, buf, len, 1);
112 static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
114 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
116 dvbdmxfeed->cb.ts(data, 188, NULL, 0,
117 &dvbdmxfeed->feed.ts, DMX_OK);
121 int av7110_av_start_record(struct av7110 *av7110, int av,
122 struct dvb_demux_feed *dvbdmxfeed)
125 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
127 dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
129 if (av7110->playing || (av7110->rec_mode & av))
131 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
132 dvbdmx->recording = 1;
133 av7110->rec_mode |= av;
135 switch (av7110->rec_mode) {
137 dvb_filter_pes2ts_init(&av7110->p2t[0],
138 dvbdmx->pesfilter[0]->pid,
139 dvb_filter_pes2ts_cb,
140 (void *) dvbdmx->pesfilter[0]);
141 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
145 dvb_filter_pes2ts_init(&av7110->p2t[1],
146 dvbdmx->pesfilter[1]->pid,
147 dvb_filter_pes2ts_cb,
148 (void *) dvbdmx->pesfilter[1]);
149 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
153 dvb_filter_pes2ts_init(&av7110->p2t[0],
154 dvbdmx->pesfilter[0]->pid,
155 dvb_filter_pes2ts_cb,
156 (void *) dvbdmx->pesfilter[0]);
157 dvb_filter_pes2ts_init(&av7110->p2t[1],
158 dvbdmx->pesfilter[1]->pid,
159 dvb_filter_pes2ts_cb,
160 (void *) dvbdmx->pesfilter[1]);
161 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
167 int av7110_av_start_play(struct av7110 *av7110, int av)
170 dprintk(2, "av7110:%p, \n", av7110);
172 if (av7110->rec_mode)
174 if (av7110->playing & av)
177 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
179 if (av7110->playing == RP_NONE) {
180 av7110_ipack_reset(&av7110->ipack[0]);
181 av7110_ipack_reset(&av7110->ipack[1]);
184 av7110->playing |= av;
185 switch (av7110->playing) {
187 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
190 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
195 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
199 ret = av7110->playing;
203 int av7110_av_stop(struct av7110 *av7110, int av)
206 dprintk(2, "av7110:%p, \n", av7110);
208 if (!(av7110->playing & av) && !(av7110->rec_mode & av))
210 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
211 if (av7110->playing) {
212 av7110->playing &= ~av;
213 switch (av7110->playing) {
215 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
218 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
221 ret = av7110_set_vidmode(av7110, av7110->vidmode);
225 av7110->rec_mode &= ~av;
226 switch (av7110->rec_mode) {
228 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
231 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
241 int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
248 wake_up(&buf->queue);
252 if ((len = dvb_ringbuffer_avail(buf)) < 6)
254 sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
255 sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
256 sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
257 sync |= DVB_RINGBUFFER_PEEK(buf, 3);
259 if (((sync &~ 0x0f) == 0x000001e0) ||
260 ((sync &~ 0x1f) == 0x000001c0) ||
261 (sync == 0x000001bd))
264 DVB_RINGBUFFER_SKIP(buf, 1);
266 blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
267 blen |= DVB_RINGBUFFER_PEEK(buf, 5);
269 if (len < blen || blen > dlen) {
270 //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
271 wake_up(&buf->queue);
275 dvb_ringbuffer_read(buf, dest, (size_t) blen, 0);
277 dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
278 (unsigned long) buf->pread, (unsigned long) buf->pwrite);
279 wake_up(&buf->queue);
284 int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
286 int err, vol, val, balance = 0;
288 dprintk(2, "av7110:%p, \n", av7110);
290 av7110->mixer.volume_left = volleft;
291 av7110->mixer.volume_right = volright;
293 switch (av7110->adac_type) {
295 volleft = (volleft * 256) / 1036;
296 volright = (volright * 256) / 1036;
301 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
303 return SendDAC(av7110, 4, volright);
305 case DVB_ADAC_CRYSTAL:
306 volleft = 127 - volleft / 2;
307 volright = 127 - volright / 2;
308 i2c_writereg(av7110, 0x20, 0x03, volleft);
309 i2c_writereg(av7110, 0x20, 0x04, volright);
313 vol = (volleft > volright) ? volleft : volright;
314 val = (vol * 0x73 / 255) << 8;
316 balance = ((volright - volleft) * 127) / vol;
317 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
318 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
319 msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
325 int av7110_set_vidmode(struct av7110 *av7110, int mode)
328 dprintk(2, "av7110:%p, \n", av7110);
330 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
332 if (!ret && !av7110->playing) {
333 ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
334 av7110->pids[DMX_PES_AUDIO],
335 av7110->pids[DMX_PES_TELETEXT],
336 0, av7110->pids[DMX_PES_PCR]);
338 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
344 static int sw2mode[16] = {
345 VIDEO_MODE_PAL, VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL,
346 VIDEO_MODE_NTSC, VIDEO_MODE_NTSC, VIDEO_MODE_PAL, VIDEO_MODE_NTSC,
347 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL,
348 VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL, VIDEO_MODE_PAL,
351 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
359 dprintk(2, "av7110:%p, \n", av7110);
363 for (i = 7; i < count - 10; i++) {
365 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
368 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
369 vsize = ((p[1] &0x0F) << 8) | (p[2]);
371 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
373 dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
382 /****************************************************************************
383 * I/O buffer management and control
384 ****************************************************************************/
386 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
387 const char *buf, unsigned long count)
389 unsigned long todo = count;
393 if (dvb_ringbuffer_free(rbuf) < 2048) {
394 if (wait_event_interruptible(rbuf->queue,
395 (dvb_ringbuffer_free(rbuf) >= 2048)))
398 free = dvb_ringbuffer_free(rbuf);
401 dvb_ringbuffer_write(rbuf, buf, free);
409 static void play_video_cb(u8 *buf, int count, void *priv)
411 struct av7110 *av7110 = (struct av7110 *) priv;
412 dprintk(2, "av7110:%p, \n", av7110);
414 if ((buf[3] & 0xe0) == 0xe0) {
415 get_video_format(av7110, buf, count);
416 aux_ring_buffer_write(&av7110->avout, buf, count);
418 aux_ring_buffer_write(&av7110->aout, buf, count);
421 static void play_audio_cb(u8 *buf, int count, void *priv)
423 struct av7110 *av7110 = (struct av7110 *) priv;
424 dprintk(2, "av7110:%p, \n", av7110);
426 aux_ring_buffer_write(&av7110->aout, buf, count);
429 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
430 dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
432 static ssize_t dvb_play(struct av7110 *av7110, const u8 __user *buf,
433 unsigned long count, int nonblock, int type)
435 unsigned long todo = count, n;
436 dprintk(2, "av7110:%p, \n", av7110);
438 if (!av7110->kbuf[type])
441 if (nonblock && !FREE_COND)
448 if (wait_event_interruptible(av7110->avout.queue,
455 if (copy_from_user(av7110->kbuf[type], buf, n))
457 av7110_ipack_instant_repack(av7110->kbuf[type], n,
458 &av7110->ipack[type]);
465 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
466 unsigned long count, int nonblock, int type)
468 unsigned long todo = count, n;
469 dprintk(2, "av7110:%p, \n", av7110);
471 if (!av7110->kbuf[type])
474 if (nonblock && !FREE_COND)
481 if (wait_event_interruptible(av7110->avout.queue,
488 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
495 static ssize_t dvb_aplay(struct av7110 *av7110, const u8 __user *buf,
496 unsigned long count, int nonblock, int type)
498 unsigned long todo = count, n;
499 dprintk(2, "av7110:%p, \n", av7110);
501 if (!av7110->kbuf[type])
503 if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
507 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
510 if (wait_event_interruptible(av7110->aout.queue,
511 (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
517 if (copy_from_user(av7110->kbuf[type], buf, n))
519 av7110_ipack_instant_repack(av7110->kbuf[type], n,
520 &av7110->ipack[type]);
527 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
529 memset(p->pes, 0, TS_SIZE);
537 static void clear_p2t(struct av7110_p2t *p)
539 memset(p->pes, 0, TS_SIZE);
546 static int find_pes_header(u8 const *buf, long int length, int *frags)
553 while (c < length - 3 && !found) {
554 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
555 buf[c + 2] == 0x01) {
556 switch ( buf[c + 3] ) {
557 case PROG_STREAM_MAP:
558 case PRIVATE_STREAM2:
559 case PROG_STREAM_DIR:
562 case PADDING_STREAM :
564 case ISO13522_STREAM:
565 case PRIVATE_STREAM1:
566 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
567 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
578 if (c == length - 3 && !found) {
579 if (buf[length - 1] == 0x00)
581 if (buf[length - 2] == 0x00 &&
582 buf[length - 1] == 0x00)
584 if (buf[length - 3] == 0x00 &&
585 buf[length - 2] == 0x00 &&
586 buf[length - 1] == 0x01)
594 void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
605 if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
611 if (buf[c] == 0x01) {
621 case PROG_STREAM_MAP:
622 case PRIVATE_STREAM2:
623 case PROG_STREAM_DIR:
626 case PADDING_STREAM :
628 case ISO13522_STREAM:
629 case PRIVATE_STREAM1:
630 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
631 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
637 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
638 c += (TS_SIZE - 4) - p->pos;
639 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
652 c2 = find_pes_header(buf + c, length - c, &p->frags);
653 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
656 l = (TS_SIZE - 4) - p->pos;
657 memcpy(p->pes + p->pos, buf, l);
660 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
666 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
670 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
678 rest = l % (TS_SIZE - 4);
680 p_to_t(buf + c, l, pid, &p->counter, p->feed);
681 memcpy(p->pes, buf + c + l, rest);
689 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
694 u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
696 fill = (TS_SIZE - 4) - length;
701 tshead[1] |= (u8)((pid & 0x1F00) >> 8);
702 tshead[2] |= (u8)(pid & 0x00FF);
703 tshead[3] |= ((*counter)++ & 0x0F);
704 memcpy(buf, tshead, 4);
714 for (i = 6; i < fill + 4; i++) {
724 static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
725 struct dvb_demux_feed *feed)
733 buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
735 case PROG_STREAM_MAP:
736 case PRIVATE_STREAM2:
737 case PROG_STREAM_DIR:
740 case PADDING_STREAM :
742 case ISO13522_STREAM:
743 case PRIVATE_STREAM1:
744 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
745 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
754 memset(obuf, 0, TS_SIZE);
755 if (length - c >= (TS_SIZE - 4)){
756 l = write_ts_header2(pid, counter, pes_start,
757 obuf, (TS_SIZE - 4));
758 memcpy(obuf + l, buf + c, TS_SIZE - l);
761 l = write_ts_header2(pid, counter, pes_start,
763 memcpy(obuf + l, buf + c, TS_SIZE - l);
766 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
772 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
774 struct dvb_demux *demux = feed->demux;
775 struct av7110 *av7110 = (struct av7110 *) demux->priv;
776 struct ipack *ipack = &av7110->ipack[feed->pes_type];
778 dprintk(2, "av7110:%p, \n", av7110);
780 switch (feed->pes_type) {
782 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
786 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
793 if (!(buf[3] & 0x10)) /* no payload? */
796 av7110_ipack_flush(ipack);
798 if (buf[3] & 0x20) { /* adaptation field? */
805 av7110_ipack_instant_repack(buf + 4, len - 4, &av7110->ipack[feed->pes_type]);
811 /******************************************************************************
812 * Video MPEG decoder events
813 ******************************************************************************/
814 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
816 struct dvb_video_events *events = &av7110->video_events;
819 spin_lock_bh(&events->lock);
821 wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
822 if (wp == events->eventr) {
823 events->overflow = 1;
824 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
828 memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
831 spin_unlock_bh(&events->lock);
833 wake_up_interruptible(&events->wait_queue);
837 static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
839 struct dvb_video_events *events = &av7110->video_events;
841 if (events->overflow) {
842 events->overflow = 0;
845 if (events->eventw == events->eventr) {
848 if (flags & O_NONBLOCK)
851 ret = wait_event_interruptible(events->wait_queue,
852 events->eventw != events->eventr);
857 spin_lock_bh(&events->lock);
859 memcpy(event, &events->events[events->eventr],
860 sizeof(struct video_event));
861 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
863 spin_unlock_bh(&events->lock);
869 /******************************************************************************
870 * DVB device file operations
871 ******************************************************************************/
873 static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
875 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
876 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
877 unsigned int mask = 0;
879 dprintk(2, "av7110:%p, \n", av7110);
881 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
882 poll_wait(file, &av7110->avout.queue, wait);
884 poll_wait(file, &av7110->video_events.wait_queue, wait);
886 if (av7110->video_events.eventw != av7110->video_events.eventr)
889 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
890 if (av7110->playing) {
892 mask |= (POLLOUT | POLLWRNORM);
893 } else /* if not playing: may play if asked for */
894 mask |= (POLLOUT | POLLWRNORM);
900 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
903 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
904 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
906 dprintk(2, "av7110:%p, \n", av7110);
908 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
911 if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
914 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
917 static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
919 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
920 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
921 unsigned int mask = 0;
923 dprintk(2, "av7110:%p, \n", av7110);
925 poll_wait(file, &av7110->aout.queue, wait);
927 if (av7110->playing) {
928 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
929 mask |= (POLLOUT | POLLWRNORM);
930 } else /* if not playing: may play if asked for */
931 mask = (POLLOUT | POLLWRNORM);
936 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
937 size_t count, loff_t *ppos)
939 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
940 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
942 dprintk(2, "av7110:%p, \n", av7110);
944 if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
945 printk(KERN_ERR "not audio source memory\n");
948 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
951 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
953 #define MIN_IFRAME 400000
955 static int play_iframe(struct av7110 *av7110, u8 __user *buf, unsigned int len, int nonblock)
959 dprintk(2, "av7110:%p, \n", av7110);
961 if (!(av7110->playing & RP_VIDEO)) {
962 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
966 /* setting n always > 1, fixes problems when playing stillframes
967 consisting of I- and P-Frames */
968 n = MIN_IFRAME / len + 1;
970 /* FIXME: nonblock? */
971 dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
973 for (i = 0; i < n; i++)
974 dvb_play(av7110, buf, len, 0, 1);
976 av7110_ipack_flush(&av7110->ipack[1]);
981 static int dvb_video_ioctl(struct inode *inode, struct file *file,
982 unsigned int cmd, void *parg)
984 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
985 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
986 unsigned long arg = (unsigned long) parg;
989 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
991 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
992 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
993 cmd != VIDEO_GET_SIZE ) {
1000 av7110->videostate.play_state = VIDEO_STOPPED;
1001 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1002 ret = av7110_av_stop(av7110, RP_VIDEO);
1004 ret = vidcom(av7110, VIDEO_CMD_STOP,
1005 av7110->videostate.video_blank ? 0 : 1);
1007 av7110->trickmode = TRICK_NONE;
1011 av7110->trickmode = TRICK_NONE;
1012 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1013 av7110->videostate.play_state = VIDEO_PLAYING;
1014 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1019 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1020 if (av7110->playing == RP_AV) {
1021 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1024 av7110->playing &= ~RP_VIDEO;
1026 ret = av7110_av_start_play(av7110, RP_VIDEO);
1029 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1031 av7110->videostate.play_state = VIDEO_PLAYING;
1035 av7110->videostate.play_state = VIDEO_FREEZED;
1036 if (av7110->playing & RP_VIDEO)
1037 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1039 ret = vidcom(av7110, VIDEO_CMD_FREEZE, 1);
1041 av7110->trickmode = TRICK_FREEZE;
1044 case VIDEO_CONTINUE:
1045 if (av7110->playing & RP_VIDEO)
1046 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1048 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1050 av7110->videostate.play_state = VIDEO_PLAYING;
1051 av7110->trickmode = TRICK_NONE;
1055 case VIDEO_SELECT_SOURCE:
1056 av7110->videostate.stream_source = (video_stream_source_t) arg;
1059 case VIDEO_SET_BLANK:
1060 av7110->videostate.video_blank = (int) arg;
1063 case VIDEO_GET_STATUS:
1064 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1067 case VIDEO_GET_EVENT:
1068 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1071 case VIDEO_GET_SIZE:
1072 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1075 case VIDEO_SET_DISPLAY_FORMAT:
1077 video_displayformat_t format = (video_displayformat_t) arg;
1081 case VIDEO_PAN_SCAN:
1082 val = VID_PAN_SCAN_PREF;
1085 case VIDEO_LETTER_BOX:
1086 val = VID_VC_AND_PS_PREF;
1089 case VIDEO_CENTER_CUT_OUT:
1090 val = VID_CENTRE_CUT_PREF;
1098 av7110->videostate.display_format = format;
1099 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1104 case VIDEO_SET_FORMAT:
1109 av7110->display_ar = arg;
1110 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1114 case VIDEO_STILLPICTURE:
1116 struct video_still_picture *pic =
1117 (struct video_still_picture *) parg;
1118 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1119 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1120 ret = play_iframe(av7110, pic->iFrame, pic->size,
1121 file->f_flags & O_NONBLOCK);
1125 case VIDEO_FAST_FORWARD:
1126 //note: arg is ignored by firmware
1127 if (av7110->playing & RP_VIDEO)
1128 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1129 __Scan_I, 2, AV_PES, 0);
1131 ret = vidcom(av7110, VIDEO_CMD_FFWD, arg);
1133 av7110->trickmode = TRICK_FAST;
1134 av7110->videostate.play_state = VIDEO_PLAYING;
1138 case VIDEO_SLOWMOTION:
1139 if (av7110->playing&RP_VIDEO) {
1140 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1142 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1144 ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1146 ret = vidcom(av7110, VIDEO_CMD_STOP, 0);
1148 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1151 av7110->trickmode = TRICK_SLOW;
1152 av7110->videostate.play_state = VIDEO_PLAYING;
1156 case VIDEO_GET_CAPABILITIES:
1157 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1158 VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1161 case VIDEO_CLEAR_BUFFER:
1162 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1163 av7110_ipack_reset(&av7110->ipack[1]);
1165 if (av7110->playing == RP_AV) {
1166 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1167 __Play, 2, AV_PES, 0);
1170 if (av7110->trickmode == TRICK_FAST)
1171 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1172 __Scan_I, 2, AV_PES, 0);
1173 if (av7110->trickmode == TRICK_SLOW) {
1174 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1177 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1179 if (av7110->trickmode == TRICK_FREEZE)
1180 ret = vidcom(av7110, VIDEO_CMD_STOP, 1);
1184 case VIDEO_SET_STREAMTYPE:
1195 static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1196 unsigned int cmd, void *parg)
1198 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1199 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1200 unsigned long arg = (unsigned long) parg;
1203 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1205 if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1206 (cmd != AUDIO_GET_STATUS))
1211 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1212 ret = av7110_av_stop(av7110, RP_AUDIO);
1214 ret = audcom(av7110, AUDIO_CMD_MUTE);
1216 av7110->audiostate.play_state = AUDIO_STOPPED;
1220 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1221 ret = av7110_av_start_play(av7110, RP_AUDIO);
1223 ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1225 av7110->audiostate.play_state = AUDIO_PLAYING;
1229 ret = audcom(av7110, AUDIO_CMD_MUTE);
1231 av7110->audiostate.play_state = AUDIO_PAUSED;
1234 case AUDIO_CONTINUE:
1235 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1236 av7110->audiostate.play_state = AUDIO_PLAYING;
1237 ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1241 case AUDIO_SELECT_SOURCE:
1242 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1245 case AUDIO_SET_MUTE:
1247 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1249 av7110->audiostate.mute_state = (int) arg;
1253 case AUDIO_SET_AV_SYNC:
1254 av7110->audiostate.AV_sync_state = (int) arg;
1255 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1258 case AUDIO_SET_BYPASS_MODE:
1262 case AUDIO_CHANNEL_SELECT:
1263 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1265 switch(av7110->audiostate.channel_select) {
1267 ret = audcom(av7110, AUDIO_CMD_STEREO);
1269 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1270 i2c_writereg(av7110, 0x20, 0x02, 0x49);
1273 case AUDIO_MONO_LEFT:
1274 ret = audcom(av7110, AUDIO_CMD_MONO_L);
1276 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1277 i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1280 case AUDIO_MONO_RIGHT:
1281 ret = audcom(av7110, AUDIO_CMD_MONO_R);
1283 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1284 i2c_writereg(av7110, 0x20, 0x02, 0x45);
1293 case AUDIO_GET_STATUS:
1294 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1297 case AUDIO_GET_CAPABILITIES:
1298 *(int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1301 case AUDIO_CLEAR_BUFFER:
1302 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1303 av7110_ipack_reset(&av7110->ipack[0]);
1304 if (av7110->playing == RP_AV)
1305 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1306 __Play, 2, AV_PES, 0);
1311 case AUDIO_SET_MIXER:
1313 struct audio_mixer *amix = (struct audio_mixer *)parg;
1315 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1318 case AUDIO_SET_STREAMTYPE:
1327 static int dvb_video_open(struct inode *inode, struct file *file)
1329 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1330 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1333 dprintk(2, "av7110:%p, \n", av7110);
1335 if ((err = dvb_generic_open(inode, file)) < 0)
1338 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1339 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1340 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1341 av7110->video_blank = 1;
1342 av7110->audiostate.AV_sync_state = 1;
1343 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1345 /* empty event queue */
1346 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1352 static int dvb_video_release(struct inode *inode, struct file *file)
1354 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1355 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1357 dprintk(2, "av7110:%p, \n", av7110);
1359 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1360 av7110_av_stop(av7110, RP_VIDEO);
1363 return dvb_generic_release(inode, file);
1366 static int dvb_audio_open(struct inode *inode, struct file *file)
1368 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1369 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1370 int err=dvb_generic_open(inode, file);
1372 dprintk(2, "av7110:%p, \n", av7110);
1376 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1377 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1381 static int dvb_audio_release(struct inode *inode, struct file *file)
1383 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1384 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1386 dprintk(2, "av7110:%p, \n", av7110);
1388 av7110_av_stop(av7110, RP_AUDIO);
1389 return dvb_generic_release(inode, file);
1394 /******************************************************************************
1395 * driver registration
1396 ******************************************************************************/
1398 static struct file_operations dvb_video_fops = {
1399 .owner = THIS_MODULE,
1400 .write = dvb_video_write,
1401 .ioctl = dvb_generic_ioctl,
1402 .open = dvb_video_open,
1403 .release = dvb_video_release,
1404 .poll = dvb_video_poll,
1407 static struct dvb_device dvbdev_video = {
1410 .readers = 5, /* arbitrary */
1412 .fops = &dvb_video_fops,
1413 .kernel_ioctl = dvb_video_ioctl,
1416 static struct file_operations dvb_audio_fops = {
1417 .owner = THIS_MODULE,
1418 .write = dvb_audio_write,
1419 .ioctl = dvb_generic_ioctl,
1420 .open = dvb_audio_open,
1421 .release = dvb_audio_release,
1422 .poll = dvb_audio_poll,
1425 static struct dvb_device dvbdev_audio = {
1429 .fops = &dvb_audio_fops,
1430 .kernel_ioctl = dvb_audio_ioctl,
1434 int av7110_av_register(struct av7110 *av7110)
1436 av7110->audiostate.AV_sync_state = 0;
1437 av7110->audiostate.mute_state = 0;
1438 av7110->audiostate.play_state = AUDIO_STOPPED;
1439 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1440 av7110->audiostate.channel_select = AUDIO_STEREO;
1441 av7110->audiostate.bypass_mode = 0;
1443 av7110->videostate.video_blank = 0;
1444 av7110->videostate.play_state = VIDEO_STOPPED;
1445 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1446 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1447 av7110->videostate.display_format = VIDEO_CENTER_CUT_OUT;
1448 av7110->display_ar = VIDEO_FORMAT_4_3;
1450 init_waitqueue_head(&av7110->video_events.wait_queue);
1451 spin_lock_init(&av7110->video_events.lock);
1452 av7110->video_events.eventw = av7110->video_events.eventr = 0;
1453 av7110->video_events.overflow = 0;
1454 memset(&av7110->video_size, 0, sizeof (video_size_t));
1456 dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1457 &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1459 dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1460 &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1465 void av7110_av_unregister(struct av7110 *av7110)
1467 dvb_unregister_device(av7110->audio_dev);
1468 dvb_unregister_device(av7110->video_dev);
1471 int av7110_av_init(struct av7110 *av7110)
1473 void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1476 av7110->vidmode = VIDEO_MODE_PAL;
1478 for (i = 0; i < 2; i++) {
1479 struct ipack *ipack = av7110->ipack + i;
1481 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1484 av7110_ipack_free(--ipack);
1487 ipack->data = av7110;
1490 dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1491 dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1493 av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1494 av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1499 void av7110_av_exit(struct av7110 *av7110)
1501 av7110_ipack_free(&av7110->ipack[0]);
1502 av7110_ipack_free(&av7110->ipack[1]);