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