V4L/DVB (4424): Add missing dvb-pll Kconfig infrastructure
[linux-2.6] / drivers / media / dvb / ttpci / av7110_av.c
1 /*
2  * av7110_av.c: audio and video MPEG decoder stuff
3  *
4  * Copyright (C) 1999-2002 Ralph  Metzler
5  *                       & Marcus Metzler for convergence integrated media GmbH
6  *
7  * originally based on code by:
8  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9  *
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.
14  *
15  *
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.
20  *
21  *
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
26  *
27  *
28  * the project's page is at http://www.linuxtv.org/dvb/
29  */
30
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>
37 #include <linux/fs.h>
38
39 #include "av7110.h"
40 #include "av7110_hw.h"
41 #include "av7110_av.h"
42 #include "av7110_ipack.h"
43
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
58
59 #define PTS_DTS_FLAGS    0xC0
60
61 //pts_dts flags
62 #define PTS_ONLY         0x80
63 #define PTS_DTS          0xC0
64 #define TS_SIZE          188
65 #define TRANS_ERROR      0x80
66 #define PAY_START        0x40
67 #define TRANS_PRIO       0x20
68 #define PID_MASK_HI      0x1F
69 //flags
70 #define TRANS_SCRMBL1    0x80
71 #define TRANS_SCRMBL2    0x40
72 #define ADAPT_FIELD      0x20
73 #define PAYLOAD          0x10
74 #define COUNT_MASK       0x0F
75
76 // adaptation flags
77 #define DISCON_IND       0x80
78 #define RAND_ACC_IND     0x40
79 #define ES_PRI_IND       0x20
80 #define PCR_FLAG         0x10
81 #define OPCR_FLAG        0x08
82 #define SPLICE_FLAG      0x04
83 #define TRANS_PRIV       0x02
84 #define ADAP_EXT_FLAG    0x01
85
86 // adaptation extension flags
87 #define LTW_FLAG         0x80
88 #define PIECE_RATE       0x40
89 #define SEAM_SPLICE      0x20
90
91
92 static void p_to_t(u8 const *buf, long int length, u16 pid,
93                    u8 *counter, struct dvb_demux_feed *feed);
94
95
96 int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
97 {
98         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
99
100         if (!(dvbdmxfeed->ts_type & TS_PACKET))
101                 return 0;
102         if (buf[3] == 0xe0)      // video PES do not have a length in TS
103                 buf[4] = buf[5] = 0;
104         if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
105                 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
106                                          &dvbdmxfeed->feed.ts, DMX_OK);
107         else
108                 return dvb_filter_pes2ts(p2t, buf, len, 1);
109 }
110
111 static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
112 {
113         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
114
115         dvbdmxfeed->cb.ts(data, 188, NULL, 0,
116                           &dvbdmxfeed->feed.ts, DMX_OK);
117         return 0;
118 }
119
120 int av7110_av_start_record(struct av7110 *av7110, int av,
121                            struct dvb_demux_feed *dvbdmxfeed)
122 {
123         int ret = 0;
124         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
125
126         dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
127
128         if (av7110->playing || (av7110->rec_mode & av))
129                 return -EBUSY;
130         av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
131         dvbdmx->recording = 1;
132         av7110->rec_mode |= av;
133
134         switch (av7110->rec_mode) {
135         case RP_AUDIO:
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);
141                 break;
142
143         case RP_VIDEO:
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);
149                 break;
150
151         case RP_AV:
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);
161                 break;
162         }
163         return ret;
164 }
165
166 int av7110_av_start_play(struct av7110 *av7110, int av)
167 {
168         int ret = 0;
169         dprintk(2, "av7110:%p, \n", av7110);
170
171         if (av7110->rec_mode)
172                 return -EBUSY;
173         if (av7110->playing & av)
174                 return -EBUSY;
175
176         av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
177
178         if (av7110->playing == RP_NONE) {
179                 av7110_ipack_reset(&av7110->ipack[0]);
180                 av7110_ipack_reset(&av7110->ipack[1]);
181         }
182
183         av7110->playing |= av;
184         switch (av7110->playing) {
185         case RP_AUDIO:
186                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
187                 break;
188         case RP_VIDEO:
189                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
190                 av7110->sinfo = 0;
191                 break;
192         case RP_AV:
193                 av7110->sinfo = 0;
194                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
195                 break;
196         }
197         if (!ret)
198                 ret = av7110->playing;
199         return ret;
200 }
201
202 int av7110_av_stop(struct av7110 *av7110, int av)
203 {
204         int ret = 0;
205         dprintk(2, "av7110:%p, \n", av7110);
206
207         if (!(av7110->playing & av) && !(av7110->rec_mode & av))
208                 return 0;
209         av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
210         if (av7110->playing) {
211                 av7110->playing &= ~av;
212                 switch (av7110->playing) {
213                 case RP_AUDIO:
214                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
215                         break;
216                 case RP_VIDEO:
217                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
218                         break;
219                 case RP_NONE:
220                         ret = av7110_set_vidmode(av7110, av7110->vidmode);
221                         break;
222                 }
223         } else {
224                 av7110->rec_mode &= ~av;
225                 switch (av7110->rec_mode) {
226                 case RP_AUDIO:
227                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
228                         break;
229                 case RP_VIDEO:
230                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
231                         break;
232                 case RP_NONE:
233                         break;
234                 }
235         }
236         return ret;
237 }
238
239
240 int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
241 {
242         int len;
243         u32 sync;
244         u16 blen;
245
246         if (!dlen) {
247                 wake_up(&buf->queue);
248                 return -1;
249         }
250         while (1) {
251                 if ((len = dvb_ringbuffer_avail(buf)) < 6)
252                         return -1;
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);
257
258                 if (((sync &~ 0x0f) == 0x000001e0) ||
259                     ((sync &~ 0x1f) == 0x000001c0) ||
260                     (sync == 0x000001bd))
261                         break;
262                 printk("resync\n");
263                 DVB_RINGBUFFER_SKIP(buf, 1);
264         }
265         blen =  DVB_RINGBUFFER_PEEK(buf, 4) << 8;
266         blen |= DVB_RINGBUFFER_PEEK(buf, 5);
267         blen += 6;
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);
271                 return -1;
272         }
273
274         dvb_ringbuffer_read(buf, dest, (size_t) blen, 0);
275
276         dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
277                (unsigned long) buf->pread, (unsigned long) buf->pwrite);
278         wake_up(&buf->queue);
279         return blen;
280 }
281
282
283 int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
284 {
285         int err, vol, val, balance = 0;
286
287         dprintk(2, "av7110:%p, \n", av7110);
288
289         av7110->mixer.volume_left = volleft;
290         av7110->mixer.volume_right = volright;
291
292         switch (av7110->adac_type) {
293         case DVB_ADAC_TI:
294                 volleft = (volleft * 256) / 1036;
295                 volright = (volright * 256) / 1036;
296                 if (volleft > 0x3f)
297                         volleft = 0x3f;
298                 if (volright > 0x3f)
299                         volright = 0x3f;
300                 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
301                         return err;
302                 return SendDAC(av7110, 4, volright);
303
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);
309                 return 0;
310
311         case DVB_ADAC_MSP34x0:
312                 vol  = (volleft > volright) ? volleft : volright;
313                 val     = (vol * 0x73 / 255) << 8;
314                 if (vol > 0)
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 */
319                 return 0;
320
321         case DVB_ADAC_MSP34x5:
322                 vol = (volleft > volright) ? volleft : volright;
323                 val = (vol * 0x73 / 255) << 8;
324                 if (vol > 0)
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 */
328                 return 0;
329         }
330
331         return 0;
332 }
333
334 int av7110_set_vidmode(struct av7110 *av7110, int mode)
335 {
336         int ret;
337         dprintk(2, "av7110:%p, \n", av7110);
338
339         ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
340
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]);
346                 if (!ret)
347                         ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
348         }
349         return ret;
350 }
351
352
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,
358 };
359
360 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
361 {
362         int i;
363         int hsize, vsize;
364         int sw;
365         u8 *p;
366         int ret = 0;
367
368         dprintk(2, "av7110:%p, \n", av7110);
369
370         if (av7110->sinfo)
371                 return 0;
372         for (i = 7; i < count - 10; i++) {
373                 p = buf + i;
374                 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
375                         continue;
376                 p += 4;
377                 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
378                 vsize = ((p[1] &0x0F) << 8) | (p[2]);
379                 sw = (p[3] & 0x0F);
380                 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
381                 if (!ret) {
382                         dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
383                         av7110->sinfo = 1;
384                 }
385                 break;
386         }
387         return ret;
388 }
389
390
391 /****************************************************************************
392  * I/O buffer management and control
393  ****************************************************************************/
394
395 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
396                                          const char *buf, unsigned long count)
397 {
398         unsigned long todo = count;
399         int free;
400
401         while (todo > 0) {
402                 if (dvb_ringbuffer_free(rbuf) < 2048) {
403                         if (wait_event_interruptible(rbuf->queue,
404                                                      (dvb_ringbuffer_free(rbuf) >= 2048)))
405                                 return count - todo;
406                 }
407                 free = dvb_ringbuffer_free(rbuf);
408                 if (free > todo)
409                         free = todo;
410                 dvb_ringbuffer_write(rbuf, buf, free);
411                 todo -= free;
412                 buf += free;
413         }
414
415         return count - todo;
416 }
417
418 static void play_video_cb(u8 *buf, int count, void *priv)
419 {
420         struct av7110 *av7110 = (struct av7110 *) priv;
421         dprintk(2, "av7110:%p, \n", av7110);
422
423         if ((buf[3] & 0xe0) == 0xe0) {
424                 get_video_format(av7110, buf, count);
425                 aux_ring_buffer_write(&av7110->avout, buf, count);
426         } else
427                 aux_ring_buffer_write(&av7110->aout, buf, count);
428 }
429
430 static void play_audio_cb(u8 *buf, int count, void *priv)
431 {
432         struct av7110 *av7110 = (struct av7110 *) priv;
433         dprintk(2, "av7110:%p, \n", av7110);
434
435         aux_ring_buffer_write(&av7110->aout, buf, count);
436 }
437
438 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
439                    dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
440
441 static ssize_t dvb_play(struct av7110 *av7110, const u8 __user *buf,
442                         unsigned long count, int nonblock, int type)
443 {
444         unsigned long todo = count, n;
445         dprintk(2, "av7110:%p, \n", av7110);
446
447         if (!av7110->kbuf[type])
448                 return -ENOBUFS;
449
450         if (nonblock && !FREE_COND)
451                 return -EWOULDBLOCK;
452
453         while (todo > 0) {
454                 if (!FREE_COND) {
455                         if (nonblock)
456                                 return count - todo;
457                         if (wait_event_interruptible(av7110->avout.queue,
458                                                      FREE_COND))
459                                 return count - todo;
460                 }
461                 n = todo;
462                 if (n > IPACKS * 2)
463                         n = IPACKS * 2;
464                 if (copy_from_user(av7110->kbuf[type], buf, n))
465                         return -EFAULT;
466                 av7110_ipack_instant_repack(av7110->kbuf[type], n,
467                                             &av7110->ipack[type]);
468                 todo -= n;
469                 buf += n;
470         }
471         return count - todo;
472 }
473
474 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
475                         unsigned long count, int nonblock, int type)
476 {
477         unsigned long todo = count, n;
478         dprintk(2, "av7110:%p, \n", av7110);
479
480         if (!av7110->kbuf[type])
481                 return -ENOBUFS;
482
483         if (nonblock && !FREE_COND)
484                 return -EWOULDBLOCK;
485
486         while (todo > 0) {
487                 if (!FREE_COND) {
488                         if (nonblock)
489                                 return count - todo;
490                         if (wait_event_interruptible(av7110->avout.queue,
491                                                      FREE_COND))
492                                 return count - todo;
493                 }
494                 n = todo;
495                 if (n > IPACKS * 2)
496                         n = IPACKS * 2;
497                 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
498                 todo -= n;
499                 buf += n;
500         }
501         return count - todo;
502 }
503
504 static ssize_t dvb_aplay(struct av7110 *av7110, const u8 __user *buf,
505                          unsigned long count, int nonblock, int type)
506 {
507         unsigned long todo = count, n;
508         dprintk(2, "av7110:%p, \n", av7110);
509
510         if (!av7110->kbuf[type])
511                 return -ENOBUFS;
512         if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
513                 return -EWOULDBLOCK;
514
515         while (todo > 0) {
516                 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
517                         if (nonblock)
518                                 return count - todo;
519                         if (wait_event_interruptible(av7110->aout.queue,
520                                         (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
521                                 return count-todo;
522                 }
523                 n = todo;
524                 if (n > IPACKS * 2)
525                         n = IPACKS * 2;
526                 if (copy_from_user(av7110->kbuf[type], buf, n))
527                         return -EFAULT;
528                 av7110_ipack_instant_repack(av7110->kbuf[type], n,
529                                             &av7110->ipack[type]);
530                 todo -= n;
531                 buf += n;
532         }
533         return count - todo;
534 }
535
536 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
537 {
538         memset(p->pes, 0, TS_SIZE);
539         p->counter = 0;
540         p->pos = 0;
541         p->frags = 0;
542         if (feed)
543                 p->feed = feed;
544 }
545
546 static void clear_p2t(struct av7110_p2t *p)
547 {
548         memset(p->pes, 0, TS_SIZE);
549 //      p->counter = 0;
550         p->pos = 0;
551         p->frags = 0;
552 }
553
554
555 static int find_pes_header(u8 const *buf, long int length, int *frags)
556 {
557         int c = 0;
558         int found = 0;
559
560         *frags = 0;
561
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:
569                         case ECM_STREAM     :
570                         case EMM_STREAM     :
571                         case PADDING_STREAM :
572                         case DSM_CC_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:
577                                 found = 1;
578                                 break;
579
580                         default:
581                                 c++;
582                                 break;
583                         }
584                 } else
585                         c++;
586         }
587         if (c == length - 3 && !found) {
588                 if (buf[length - 1] == 0x00)
589                         *frags = 1;
590                 if (buf[length - 2] == 0x00 &&
591                     buf[length - 1] == 0x00)
592                         *frags = 2;
593                 if (buf[length - 3] == 0x00 &&
594                     buf[length - 2] == 0x00 &&
595                     buf[length - 1] == 0x01)
596                         *frags = 3;
597                 return -1;
598         }
599
600         return c;
601 }
602
603 void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
604 {
605         int c, c2, l, add;
606         int check, rest;
607
608         c = 0;
609         c2 = 0;
610         if (p->frags){
611                 check = 0;
612                 switch(p->frags) {
613                 case 1:
614                         if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
615                                 check = 1;
616                                 c += 2;
617                         }
618                         break;
619                 case 2:
620                         if (buf[c] == 0x01) {
621                                 check = 1;
622                                 c++;
623                         }
624                         break;
625                 case 3:
626                         check = 1;
627                 }
628                 if (check) {
629                         switch (buf[c]) {
630                         case PROG_STREAM_MAP:
631                         case PRIVATE_STREAM2:
632                         case PROG_STREAM_DIR:
633                         case ECM_STREAM     :
634                         case EMM_STREAM     :
635                         case PADDING_STREAM :
636                         case DSM_CC_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:
641                                 p->pes[0] = 0x00;
642                                 p->pes[1] = 0x00;
643                                 p->pes[2] = 0x01;
644                                 p->pes[3] = buf[c];
645                                 p->pos = 4;
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);
649                                 clear_p2t(p);
650                                 break;
651
652                         default:
653                                 c = 0;
654                                 break;
655                         }
656                 }
657                 p->frags = 0;
658         }
659
660         if (p->pos) {
661                 c2 = find_pes_header(buf + c, length - c, &p->frags);
662                 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
663                         l = c2+c;
664                 else
665                         l = (TS_SIZE - 4) - p->pos;
666                 memcpy(p->pes + p->pos, buf, l);
667                 c += l;
668                 p->pos += l;
669                 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
670                 clear_p2t(p);
671         }
672
673         add = 0;
674         while (c < length) {
675                 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
676                 if (c2 >= 0) {
677                         c2 += c + add;
678                         if (c2 > c){
679                                 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
680                                 c = c2;
681                                 clear_p2t(p);
682                                 add = 0;
683                         } else
684                                 add = 1;
685                 } else {
686                         l = length - c;
687                         rest = l % (TS_SIZE - 4);
688                         l -= rest;
689                         p_to_t(buf + c, l, pid, &p->counter, p->feed);
690                         memcpy(p->pes, buf + c + l, rest);
691                         p->pos = rest;
692                         c = length;
693                 }
694         }
695 }
696
697
698 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
699 {
700         int i;
701         int c = 0;
702         int fill;
703         u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
704
705         fill = (TS_SIZE - 4) - length;
706         if (pes_start)
707                 tshead[1] = 0x40;
708         if (fill)
709                 tshead[3] = 0x30;
710         tshead[1] |= (u8)((pid & 0x1F00) >> 8);
711         tshead[2] |= (u8)(pid & 0x00FF);
712         tshead[3] |= ((*counter)++ & 0x0F);
713         memcpy(buf, tshead, 4);
714         c += 4;
715
716         if (fill) {
717                 buf[4] = fill - 1;
718                 c++;
719                 if (fill > 1) {
720                         buf[5] = 0x00;
721                         c++;
722                 }
723                 for (i = 6; i < fill + 4; i++) {
724                         buf[i] = 0xFF;
725                         c++;
726                 }
727         }
728
729         return c;
730 }
731
732
733 static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
734                    struct dvb_demux_feed *feed)
735 {
736         int l, pes_start;
737         u8 obuf[TS_SIZE];
738         long c = 0;
739
740         pes_start = 0;
741         if (length > 3 &&
742              buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
743                 switch (buf[3]) {
744                         case PROG_STREAM_MAP:
745                         case PRIVATE_STREAM2:
746                         case PROG_STREAM_DIR:
747                         case ECM_STREAM     :
748                         case EMM_STREAM     :
749                         case PADDING_STREAM :
750                         case DSM_CC_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:
755                                 pes_start = 1;
756                                 break;
757
758                         default:
759                                 break;
760                 }
761
762         while (c < length) {
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);
768                         c += TS_SIZE - l;
769                 } else {
770                         l = write_ts_header2(pid, counter, pes_start,
771                                              obuf, length - c);
772                         memcpy(obuf + l, buf + c, TS_SIZE - l);
773                         c = length;
774                 }
775                 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
776                 pes_start = 0;
777         }
778 }
779
780
781 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
782 {
783         struct dvb_demux *demux = feed->demux;
784         struct av7110 *av7110 = (struct av7110 *) demux->priv;
785         struct ipack *ipack = &av7110->ipack[feed->pes_type];
786
787         dprintk(2, "av7110:%p, \n", av7110);
788
789         switch (feed->pes_type) {
790         case 0:
791                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
792                         return -EINVAL;
793                 break;
794         case 1:
795                 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
796                         return -EINVAL;
797                 break;
798         default:
799                 return -1;
800         }
801
802         if (!(buf[3] & 0x10)) /* no payload? */
803                 return -1;
804         if (buf[1] & 0x40)
805                 av7110_ipack_flush(ipack);
806
807         if (buf[3] & 0x20) {  /* adaptation field? */
808                 len -= buf[4] + 1;
809                 buf += buf[4] + 1;
810                 if (!len)
811                         return 0;
812         }
813
814         av7110_ipack_instant_repack(buf + 4, len - 4, &av7110->ipack[feed->pes_type]);
815         return 0;
816 }
817
818
819
820 /******************************************************************************
821  * Video MPEG decoder events
822  ******************************************************************************/
823 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
824 {
825         struct dvb_video_events *events = &av7110->video_events;
826         int wp;
827
828         spin_lock_bh(&events->lock);
829
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;
834         }
835
836         //FIXME: timestamp?
837         memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
838         events->eventw = wp;
839
840         spin_unlock_bh(&events->lock);
841
842         wake_up_interruptible(&events->wait_queue);
843 }
844
845
846 static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
847 {
848         struct dvb_video_events *events = &av7110->video_events;
849
850         if (events->overflow) {
851                 events->overflow = 0;
852                 return -EOVERFLOW;
853         }
854         if (events->eventw == events->eventr) {
855                 int ret;
856
857                 if (flags & O_NONBLOCK)
858                         return -EWOULDBLOCK;
859
860                 ret = wait_event_interruptible(events->wait_queue,
861                                                events->eventw != events->eventr);
862                 if (ret < 0)
863                         return ret;
864         }
865
866         spin_lock_bh(&events->lock);
867
868         memcpy(event, &events->events[events->eventr],
869                sizeof(struct video_event));
870         events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
871
872         spin_unlock_bh(&events->lock);
873
874         return 0;
875 }
876
877
878 /******************************************************************************
879  * DVB device file operations
880  ******************************************************************************/
881
882 static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
883 {
884         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
885         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
886         unsigned int mask = 0;
887
888         dprintk(2, "av7110:%p, \n", av7110);
889
890         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
891                 poll_wait(file, &av7110->avout.queue, wait);
892
893         poll_wait(file, &av7110->video_events.wait_queue, wait);
894
895         if (av7110->video_events.eventw != av7110->video_events.eventr)
896                 mask = POLLPRI;
897
898         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
899                 if (av7110->playing) {
900                         if (FREE_COND)
901                                 mask |= (POLLOUT | POLLWRNORM);
902                         } else /* if not playing: may play if asked for */
903                                 mask |= (POLLOUT | POLLWRNORM);
904         }
905
906         return mask;
907 }
908
909 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
910                                size_t count, loff_t *ppos)
911 {
912         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
913         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
914
915         dprintk(2, "av7110:%p, \n", av7110);
916
917         if ((file->f_flags & O_ACCMODE) == O_RDONLY)
918                 return -EPERM;
919
920         if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
921                 return -EPERM;
922
923         return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
924 }
925
926 static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
927 {
928         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
929         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
930         unsigned int mask = 0;
931
932         dprintk(2, "av7110:%p, \n", av7110);
933
934         poll_wait(file, &av7110->aout.queue, wait);
935
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);
941
942         return mask;
943 }
944
945 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
946                                size_t count, loff_t *ppos)
947 {
948         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
949         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
950
951         dprintk(2, "av7110:%p, \n", av7110);
952
953         if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
954                 printk(KERN_ERR "not audio source memory\n");
955                 return -EPERM;
956         }
957         return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
958 }
959
960 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
961
962 #define MIN_IFRAME 400000
963
964 static int play_iframe(struct av7110 *av7110, u8 __user *buf, unsigned int len, int nonblock)
965 {
966         int i, n;
967
968         dprintk(2, "av7110:%p, \n", av7110);
969
970         if (!(av7110->playing & RP_VIDEO)) {
971                 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
972                         return -EBUSY;
973         }
974
975         /* setting n always > 1, fixes problems when playing stillframes
976            consisting of I- and P-Frames */
977         n = MIN_IFRAME / len + 1;
978
979         /* FIXME: nonblock? */
980         dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
981
982         for (i = 0; i < n; i++)
983                 dvb_play(av7110, buf, len, 0, 1);
984
985         av7110_ipack_flush(&av7110->ipack[1]);
986         return 0;
987 }
988
989
990 static int dvb_video_ioctl(struct inode *inode, struct file *file,
991                            unsigned int cmd, void *parg)
992 {
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;
996         int ret = 0;
997
998         dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
999
1000         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1001                 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1002                      cmd != VIDEO_GET_SIZE ) {
1003                         return -EPERM;
1004                 }
1005         }
1006
1007         switch (cmd) {
1008         case VIDEO_STOP:
1009                 av7110->videostate.play_state = VIDEO_STOPPED;
1010                 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1011                         ret = av7110_av_stop(av7110, RP_VIDEO);
1012                 else
1013                         ret = vidcom(av7110, VIDEO_CMD_STOP,
1014                                av7110->videostate.video_blank ? 0 : 1);
1015                 if (!ret)
1016                         av7110->trickmode = TRICK_NONE;
1017                 break;
1018
1019         case VIDEO_PLAY:
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);
1024                         if (ret)
1025                                 break;
1026                 }
1027
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);
1031                                 if (ret)
1032                                         break;
1033                                 av7110->playing &= ~RP_VIDEO;
1034                         }
1035                         ret = av7110_av_start_play(av7110, RP_VIDEO);
1036                 }
1037                 if (!ret)
1038                         ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1039                 if (!ret)
1040                         av7110->videostate.play_state = VIDEO_PLAYING;
1041                 break;
1042
1043         case VIDEO_FREEZE:
1044                 av7110->videostate.play_state = VIDEO_FREEZED;
1045                 if (av7110->playing & RP_VIDEO)
1046                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1047                 else
1048                         ret = vidcom(av7110, VIDEO_CMD_FREEZE, 1);
1049                 if (!ret)
1050                         av7110->trickmode = TRICK_FREEZE;
1051                 break;
1052
1053         case VIDEO_CONTINUE:
1054                 if (av7110->playing & RP_VIDEO)
1055                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1056                 if (!ret)
1057                         ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1058                 if (!ret) {
1059                         av7110->videostate.play_state = VIDEO_PLAYING;
1060                         av7110->trickmode = TRICK_NONE;
1061                 }
1062                 break;
1063
1064         case VIDEO_SELECT_SOURCE:
1065                 av7110->videostate.stream_source = (video_stream_source_t) arg;
1066                 break;
1067
1068         case VIDEO_SET_BLANK:
1069                 av7110->videostate.video_blank = (int) arg;
1070                 break;
1071
1072         case VIDEO_GET_STATUS:
1073                 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1074                 break;
1075
1076         case VIDEO_GET_EVENT:
1077                 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1078                 break;
1079
1080         case VIDEO_GET_SIZE:
1081                 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1082                 break;
1083
1084         case VIDEO_SET_DISPLAY_FORMAT:
1085         {
1086                 video_displayformat_t format = (video_displayformat_t) arg;
1087                 u16 val = 0;
1088
1089                 switch (format) {
1090                 case VIDEO_PAN_SCAN:
1091                         val = VID_PAN_SCAN_PREF;
1092                         break;
1093
1094                 case VIDEO_LETTER_BOX:
1095                         val = VID_VC_AND_PS_PREF;
1096                         break;
1097
1098                 case VIDEO_CENTER_CUT_OUT:
1099                         val = VID_CENTRE_CUT_PREF;
1100                         break;
1101
1102                 default:
1103                         ret = -EINVAL;
1104                 }
1105                 if (ret < 0)
1106                         break;
1107                 av7110->videostate.display_format = format;
1108                 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1109                                     1, (u16) val);
1110                 break;
1111         }
1112
1113         case VIDEO_SET_FORMAT:
1114                 if (arg > 1) {
1115                         ret = -EINVAL;
1116                         break;
1117                 }
1118                 av7110->display_ar = arg;
1119                 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1120                                     1, (u16) arg);
1121                 break;
1122
1123         case VIDEO_STILLPICTURE:
1124         {
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);
1131                 break;
1132         }
1133
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);
1139                 else
1140                         ret = vidcom(av7110, VIDEO_CMD_FFWD, arg);
1141                 if (!ret) {
1142                         av7110->trickmode = TRICK_FAST;
1143                         av7110->videostate.play_state = VIDEO_PLAYING;
1144                 }
1145                 break;
1146
1147         case VIDEO_SLOWMOTION:
1148                 if (av7110->playing&RP_VIDEO) {
1149                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1150                         if (!ret)
1151                                 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1152                 } else {
1153                         ret = vidcom(av7110, VIDEO_CMD_PLAY, 0);
1154                         if (!ret)
1155                                 ret = vidcom(av7110, VIDEO_CMD_STOP, 0);
1156                         if (!ret)
1157                                 ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1158                 }
1159                 if (!ret) {
1160                         av7110->trickmode = TRICK_SLOW;
1161                         av7110->videostate.play_state = VIDEO_PLAYING;
1162                 }
1163                 break;
1164
1165         case VIDEO_GET_CAPABILITIES:
1166                 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1167                         VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1168                 break;
1169
1170         case VIDEO_CLEAR_BUFFER:
1171                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1172                 av7110_ipack_reset(&av7110->ipack[1]);
1173
1174                 if (av7110->playing == RP_AV) {
1175                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1176                                             __Play, 2, AV_PES, 0);
1177                         if (ret)
1178                                 break;
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,
1184                                                     __Slow, 2, 0, 0);
1185                                 if (!ret)
1186                                         ret = vidcom(av7110, VIDEO_CMD_SLOW, arg);
1187                         }
1188                         if (av7110->trickmode == TRICK_FREEZE)
1189                                 ret = vidcom(av7110, VIDEO_CMD_STOP, 1);
1190                 }
1191                 break;
1192
1193         case VIDEO_SET_STREAMTYPE:
1194
1195                 break;
1196
1197         default:
1198                 ret = -ENOIOCTLCMD;
1199                 break;
1200         }
1201         return ret;
1202 }
1203
1204 static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1205                            unsigned int cmd, void *parg)
1206 {
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;
1210         int ret = 0;
1211
1212         dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1213
1214         if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1215             (cmd != AUDIO_GET_STATUS))
1216                 return -EPERM;
1217
1218         switch (cmd) {
1219         case AUDIO_STOP:
1220                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1221                         ret = av7110_av_stop(av7110, RP_AUDIO);
1222                 else
1223                         ret = audcom(av7110, AUDIO_CMD_MUTE);
1224                 if (!ret)
1225                         av7110->audiostate.play_state = AUDIO_STOPPED;
1226                 break;
1227
1228         case AUDIO_PLAY:
1229                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1230                         ret = av7110_av_start_play(av7110, RP_AUDIO);
1231                 if (!ret)
1232                         ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1233                 if (!ret)
1234                         av7110->audiostate.play_state = AUDIO_PLAYING;
1235                 break;
1236
1237         case AUDIO_PAUSE:
1238                 ret = audcom(av7110, AUDIO_CMD_MUTE);
1239                 if (!ret)
1240                         av7110->audiostate.play_state = AUDIO_PAUSED;
1241                 break;
1242
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);
1247                 }
1248                 break;
1249
1250         case AUDIO_SELECT_SOURCE:
1251                 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1252                 break;
1253
1254         case AUDIO_SET_MUTE:
1255         {
1256                 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1257                 if (!ret)
1258                         av7110->audiostate.mute_state = (int) arg;
1259                 break;
1260         }
1261
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);
1265                 break;
1266
1267         case AUDIO_SET_BYPASS_MODE:
1268                 if (FW_VERSION(av7110->arm_app) < 0x2621)
1269                         ret = -EINVAL;
1270                 av7110->audiostate.bypass_mode = (int)arg;
1271                 break;
1272
1273         case AUDIO_CHANNEL_SELECT:
1274                 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1275
1276                 switch(av7110->audiostate.channel_select) {
1277                 case AUDIO_STEREO:
1278                         ret = audcom(av7110, AUDIO_CMD_STEREO);
1279                         if (!ret) {
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);
1284                         }
1285                         break;
1286
1287                 case AUDIO_MONO_LEFT:
1288                         ret = audcom(av7110, AUDIO_CMD_MONO_L);
1289                         if (!ret) {
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);
1294                         }
1295                         break;
1296
1297                 case AUDIO_MONO_RIGHT:
1298                         ret = audcom(av7110, AUDIO_CMD_MONO_R);
1299                         if (!ret) {
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);
1304                         }
1305                         break;
1306
1307                 default:
1308                         ret = -EINVAL;
1309                         break;
1310                 }
1311                 break;
1312
1313         case AUDIO_GET_STATUS:
1314                 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1315                 break;
1316
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;
1320                 else
1321                         *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1322                                                 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1323                 break;
1324
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);
1331                 break;
1332         case AUDIO_SET_ID:
1333
1334                 break;
1335         case AUDIO_SET_MIXER:
1336         {
1337                 struct audio_mixer *amix = (struct audio_mixer *)parg;
1338
1339                 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1340                 break;
1341         }
1342         case AUDIO_SET_STREAMTYPE:
1343                 break;
1344         default:
1345                 ret = -ENOIOCTLCMD;
1346         }
1347         return ret;
1348 }
1349
1350
1351 static int dvb_video_open(struct inode *inode, struct file *file)
1352 {
1353         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1354         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1355         int err;
1356
1357         dprintk(2, "av7110:%p, \n", av7110);
1358
1359         if ((err = dvb_generic_open(inode, file)) < 0)
1360                 return err;
1361
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;
1368
1369                 /*  empty event queue */
1370                 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1371         }
1372
1373         return 0;
1374 }
1375
1376 static int dvb_video_release(struct inode *inode, struct file *file)
1377 {
1378         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1379         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1380
1381         dprintk(2, "av7110:%p, \n", av7110);
1382
1383         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1384                 av7110_av_stop(av7110, RP_VIDEO);
1385         }
1386
1387         return dvb_generic_release(inode, file);
1388 }
1389
1390 static int dvb_audio_open(struct inode *inode, struct file *file)
1391 {
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);
1395
1396         dprintk(2, "av7110:%p, \n", av7110);
1397
1398         if (err < 0)
1399                 return err;
1400         dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1401         av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1402         return 0;
1403 }
1404
1405 static int dvb_audio_release(struct inode *inode, struct file *file)
1406 {
1407         struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1408         struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
1409
1410         dprintk(2, "av7110:%p, \n", av7110);
1411
1412         av7110_av_stop(av7110, RP_AUDIO);
1413         return dvb_generic_release(inode, file);
1414 }
1415
1416
1417
1418 /******************************************************************************
1419  * driver registration
1420  ******************************************************************************/
1421
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,
1429 };
1430
1431 static struct dvb_device dvbdev_video = {
1432         .priv           = NULL,
1433         .users          = 6,
1434         .readers        = 5,    /* arbitrary */
1435         .writers        = 1,
1436         .fops           = &dvb_video_fops,
1437         .kernel_ioctl   = dvb_video_ioctl,
1438 };
1439
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,
1447 };
1448
1449 static struct dvb_device dvbdev_audio = {
1450         .priv           = NULL,
1451         .users          = 1,
1452         .writers        = 1,
1453         .fops           = &dvb_audio_fops,
1454         .kernel_ioctl   = dvb_audio_ioctl,
1455 };
1456
1457
1458 int av7110_av_register(struct av7110 *av7110)
1459 {
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;
1466
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;
1473
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));
1479
1480         dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1481                             &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1482
1483         dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1484                             &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1485
1486         return 0;
1487 }
1488
1489 void av7110_av_unregister(struct av7110 *av7110)
1490 {
1491         dvb_unregister_device(av7110->audio_dev);
1492         dvb_unregister_device(av7110->video_dev);
1493 }
1494
1495 int av7110_av_init(struct av7110 *av7110)
1496 {
1497         void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1498         int i, ret;
1499
1500         for (i = 0; i < 2; i++) {
1501                 struct ipack *ipack = av7110->ipack + i;
1502
1503                 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1504                 if (ret < 0) {
1505                         if (i)
1506                                 av7110_ipack_free(--ipack);
1507                         goto out;
1508                 }
1509                 ipack->data = av7110;
1510         }
1511
1512         dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1513         dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1514
1515         av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1516         av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1517 out:
1518         return ret;
1519 }
1520
1521 void av7110_av_exit(struct av7110 *av7110)
1522 {
1523         av7110_ipack_free(&av7110->ipack[0]);
1524         av7110_ipack_free(&av7110->ipack[1]);
1525 }