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