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