V4L/DVB (11761): dvb-ttpci: Fixed VIDEO_SLOWMOTION ioctl
[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 static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len);
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);
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, enum av7110_video_mode 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 enum av7110_video_mode sw2mode[16] = {
353         AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
354         AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
355         AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
356         AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
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         AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
361 };
362
363 static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
364 {
365         int i;
366         int hsize, vsize;
367         int sw;
368         u8 *p;
369         int ret = 0;
370
371         dprintk(2, "av7110:%p, \n", av7110);
372
373         if (av7110->sinfo)
374                 return 0;
375         for (i = 7; i < count - 10; i++) {
376                 p = buf + i;
377                 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
378                         continue;
379                 p += 4;
380                 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
381                 vsize = ((p[1] &0x0F) << 8) | (p[2]);
382                 sw = (p[3] & 0x0F);
383                 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
384                 if (!ret) {
385                         dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
386                         av7110->sinfo = 1;
387                 }
388                 break;
389         }
390         return ret;
391 }
392
393
394 /****************************************************************************
395  * I/O buffer management and control
396  ****************************************************************************/
397
398 static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
399                                          const u8 *buf, unsigned long count)
400 {
401         unsigned long todo = count;
402         int free;
403
404         while (todo > 0) {
405                 if (dvb_ringbuffer_free(rbuf) < 2048) {
406                         if (wait_event_interruptible(rbuf->queue,
407                                                      (dvb_ringbuffer_free(rbuf) >= 2048)))
408                                 return count - todo;
409                 }
410                 free = dvb_ringbuffer_free(rbuf);
411                 if (free > todo)
412                         free = todo;
413                 dvb_ringbuffer_write(rbuf, buf, free);
414                 todo -= free;
415                 buf += free;
416         }
417
418         return count - todo;
419 }
420
421 static void play_video_cb(u8 *buf, int count, void *priv)
422 {
423         struct av7110 *av7110 = (struct av7110 *) priv;
424         dprintk(2, "av7110:%p, \n", av7110);
425
426         if ((buf[3] & 0xe0) == 0xe0) {
427                 get_video_format(av7110, buf, count);
428                 aux_ring_buffer_write(&av7110->avout, buf, count);
429         } else
430                 aux_ring_buffer_write(&av7110->aout, buf, count);
431 }
432
433 static void play_audio_cb(u8 *buf, int count, void *priv)
434 {
435         struct av7110 *av7110 = (struct av7110 *) priv;
436         dprintk(2, "av7110:%p, \n", av7110);
437
438         aux_ring_buffer_write(&av7110->aout, buf, count);
439 }
440
441
442 #define FREE_COND_TS (dvb_ringbuffer_free(rb) >= 4096)
443
444 static ssize_t ts_play(struct av7110 *av7110, const char __user *buf,
445                        unsigned long count, int nonblock, int type)
446 {
447         struct dvb_ringbuffer *rb;
448         u8 *kb;
449         unsigned long todo = count;
450
451         dprintk(2, "%s: type %d cnt %lu\n", __func__, type, count);
452
453         rb = (type) ? &av7110->avout : &av7110->aout;
454         kb = av7110->kbuf[type];
455
456         if (!kb)
457                 return -ENOBUFS;
458
459         if (nonblock && !FREE_COND_TS)
460                 return -EWOULDBLOCK;
461
462         while (todo >= TS_SIZE) {
463                 if (!FREE_COND_TS) {
464                         if (nonblock)
465                                 return count - todo;
466                         if (wait_event_interruptible(rb->queue, FREE_COND_TS))
467                                 return count - todo;
468                 }
469                 if (copy_from_user(kb, buf, TS_SIZE))
470                         return -EFAULT;
471                 write_ts_to_decoder(av7110, type, kb, TS_SIZE);
472                 todo -= TS_SIZE;
473                 buf += TS_SIZE;
474         }
475
476         return count - todo;
477 }
478
479
480 #define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
481                    dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
482
483 static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
484                         unsigned long count, int nonblock, int type)
485 {
486         unsigned long todo = count, n;
487         dprintk(2, "av7110:%p, \n", av7110);
488
489         if (!av7110->kbuf[type])
490                 return -ENOBUFS;
491
492         if (nonblock && !FREE_COND)
493                 return -EWOULDBLOCK;
494
495         while (todo > 0) {
496                 if (!FREE_COND) {
497                         if (nonblock)
498                                 return count - todo;
499                         if (wait_event_interruptible(av7110->avout.queue,
500                                                      FREE_COND))
501                                 return count - todo;
502                 }
503                 n = todo;
504                 if (n > IPACKS * 2)
505                         n = IPACKS * 2;
506                 if (copy_from_user(av7110->kbuf[type], buf, n))
507                         return -EFAULT;
508                 av7110_ipack_instant_repack(av7110->kbuf[type], n,
509                                             &av7110->ipack[type]);
510                 todo -= n;
511                 buf += n;
512         }
513         return count - todo;
514 }
515
516 static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
517                         unsigned long count, int nonblock, int type)
518 {
519         unsigned long todo = count, n;
520         dprintk(2, "av7110:%p, \n", av7110);
521
522         if (!av7110->kbuf[type])
523                 return -ENOBUFS;
524
525         if (nonblock && !FREE_COND)
526                 return -EWOULDBLOCK;
527
528         while (todo > 0) {
529                 if (!FREE_COND) {
530                         if (nonblock)
531                                 return count - todo;
532                         if (wait_event_interruptible(av7110->avout.queue,
533                                                      FREE_COND))
534                                 return count - todo;
535                 }
536                 n = todo;
537                 if (n > IPACKS * 2)
538                         n = IPACKS * 2;
539                 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
540                 todo -= n;
541                 buf += n;
542         }
543         return count - todo;
544 }
545
546 static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
547                          unsigned long count, int nonblock, int type)
548 {
549         unsigned long todo = count, n;
550         dprintk(2, "av7110:%p, \n", av7110);
551
552         if (!av7110->kbuf[type])
553                 return -ENOBUFS;
554         if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
555                 return -EWOULDBLOCK;
556
557         while (todo > 0) {
558                 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
559                         if (nonblock)
560                                 return count - todo;
561                         if (wait_event_interruptible(av7110->aout.queue,
562                                         (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
563                                 return count-todo;
564                 }
565                 n = todo;
566                 if (n > IPACKS * 2)
567                         n = IPACKS * 2;
568                 if (copy_from_user(av7110->kbuf[type], buf, n))
569                         return -EFAULT;
570                 av7110_ipack_instant_repack(av7110->kbuf[type], n,
571                                             &av7110->ipack[type]);
572                 todo -= n;
573                 buf += n;
574         }
575         return count - todo;
576 }
577
578 void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
579 {
580         memset(p->pes, 0, TS_SIZE);
581         p->counter = 0;
582         p->pos = 0;
583         p->frags = 0;
584         if (feed)
585                 p->feed = feed;
586 }
587
588 static void clear_p2t(struct av7110_p2t *p)
589 {
590         memset(p->pes, 0, TS_SIZE);
591 //      p->counter = 0;
592         p->pos = 0;
593         p->frags = 0;
594 }
595
596
597 static int find_pes_header(u8 const *buf, long int length, int *frags)
598 {
599         int c = 0;
600         int found = 0;
601
602         *frags = 0;
603
604         while (c < length - 3 && !found) {
605                 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
606                     buf[c + 2] == 0x01) {
607                         switch ( buf[c + 3] ) {
608                         case PROG_STREAM_MAP:
609                         case PRIVATE_STREAM2:
610                         case PROG_STREAM_DIR:
611                         case ECM_STREAM     :
612                         case EMM_STREAM     :
613                         case PADDING_STREAM :
614                         case DSM_CC_STREAM  :
615                         case ISO13522_STREAM:
616                         case PRIVATE_STREAM1:
617                         case AUDIO_STREAM_S ... AUDIO_STREAM_E:
618                         case VIDEO_STREAM_S ... VIDEO_STREAM_E:
619                                 found = 1;
620                                 break;
621
622                         default:
623                                 c++;
624                                 break;
625                         }
626                 } else
627                         c++;
628         }
629         if (c == length - 3 && !found) {
630                 if (buf[length - 1] == 0x00)
631                         *frags = 1;
632                 if (buf[length - 2] == 0x00 &&
633                     buf[length - 1] == 0x00)
634                         *frags = 2;
635                 if (buf[length - 3] == 0x00 &&
636                     buf[length - 2] == 0x00 &&
637                     buf[length - 1] == 0x01)
638                         *frags = 3;
639                 return -1;
640         }
641
642         return c;
643 }
644
645 void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
646 {
647         int c, c2, l, add;
648         int check, rest;
649
650         c = 0;
651         c2 = 0;
652         if (p->frags){
653                 check = 0;
654                 switch(p->frags) {
655                 case 1:
656                         if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
657                                 check = 1;
658                                 c += 2;
659                         }
660                         break;
661                 case 2:
662                         if (buf[c] == 0x01) {
663                                 check = 1;
664                                 c++;
665                         }
666                         break;
667                 case 3:
668                         check = 1;
669                 }
670                 if (check) {
671                         switch (buf[c]) {
672                         case PROG_STREAM_MAP:
673                         case PRIVATE_STREAM2:
674                         case PROG_STREAM_DIR:
675                         case ECM_STREAM     :
676                         case EMM_STREAM     :
677                         case PADDING_STREAM :
678                         case DSM_CC_STREAM  :
679                         case ISO13522_STREAM:
680                         case PRIVATE_STREAM1:
681                         case AUDIO_STREAM_S ... AUDIO_STREAM_E:
682                         case VIDEO_STREAM_S ... VIDEO_STREAM_E:
683                                 p->pes[0] = 0x00;
684                                 p->pes[1] = 0x00;
685                                 p->pes[2] = 0x01;
686                                 p->pes[3] = buf[c];
687                                 p->pos = 4;
688                                 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
689                                 c += (TS_SIZE - 4) - p->pos;
690                                 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
691                                 clear_p2t(p);
692                                 break;
693
694                         default:
695                                 c = 0;
696                                 break;
697                         }
698                 }
699                 p->frags = 0;
700         }
701
702         if (p->pos) {
703                 c2 = find_pes_header(buf + c, length - c, &p->frags);
704                 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
705                         l = c2+c;
706                 else
707                         l = (TS_SIZE - 4) - p->pos;
708                 memcpy(p->pes + p->pos, buf, l);
709                 c += l;
710                 p->pos += l;
711                 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
712                 clear_p2t(p);
713         }
714
715         add = 0;
716         while (c < length) {
717                 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
718                 if (c2 >= 0) {
719                         c2 += c + add;
720                         if (c2 > c){
721                                 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
722                                 c = c2;
723                                 clear_p2t(p);
724                                 add = 0;
725                         } else
726                                 add = 1;
727                 } else {
728                         l = length - c;
729                         rest = l % (TS_SIZE - 4);
730                         l -= rest;
731                         p_to_t(buf + c, l, pid, &p->counter, p->feed);
732                         memcpy(p->pes, buf + c + l, rest);
733                         p->pos = rest;
734                         c = length;
735                 }
736         }
737 }
738
739
740 static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
741 {
742         int i;
743         int c = 0;
744         int fill;
745         u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
746
747         fill = (TS_SIZE - 4) - length;
748         if (pes_start)
749                 tshead[1] = 0x40;
750         if (fill)
751                 tshead[3] = 0x30;
752         tshead[1] |= (u8)((pid & 0x1F00) >> 8);
753         tshead[2] |= (u8)(pid & 0x00FF);
754         tshead[3] |= ((*counter)++ & 0x0F);
755         memcpy(buf, tshead, 4);
756         c += 4;
757
758         if (fill) {
759                 buf[4] = fill - 1;
760                 c++;
761                 if (fill > 1) {
762                         buf[5] = 0x00;
763                         c++;
764                 }
765                 for (i = 6; i < fill + 4; i++) {
766                         buf[i] = 0xFF;
767                         c++;
768                 }
769         }
770
771         return c;
772 }
773
774
775 static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
776                    struct dvb_demux_feed *feed)
777 {
778         int l, pes_start;
779         u8 obuf[TS_SIZE];
780         long c = 0;
781
782         pes_start = 0;
783         if (length > 3 &&
784              buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
785                 switch (buf[3]) {
786                         case PROG_STREAM_MAP:
787                         case PRIVATE_STREAM2:
788                         case PROG_STREAM_DIR:
789                         case ECM_STREAM     :
790                         case EMM_STREAM     :
791                         case PADDING_STREAM :
792                         case DSM_CC_STREAM  :
793                         case ISO13522_STREAM:
794                         case PRIVATE_STREAM1:
795                         case AUDIO_STREAM_S ... AUDIO_STREAM_E:
796                         case VIDEO_STREAM_S ... VIDEO_STREAM_E:
797                                 pes_start = 1;
798                                 break;
799
800                         default:
801                                 break;
802                 }
803
804         while (c < length) {
805                 memset(obuf, 0, TS_SIZE);
806                 if (length - c >= (TS_SIZE - 4)){
807                         l = write_ts_header2(pid, counter, pes_start,
808                                              obuf, (TS_SIZE - 4));
809                         memcpy(obuf + l, buf + c, TS_SIZE - l);
810                         c += TS_SIZE - l;
811                 } else {
812                         l = write_ts_header2(pid, counter, pes_start,
813                                              obuf, length - c);
814                         memcpy(obuf + l, buf + c, TS_SIZE - l);
815                         c = length;
816                 }
817                 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
818                 pes_start = 0;
819         }
820 }
821
822
823 static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len)
824 {
825         struct ipack *ipack = &av7110->ipack[type];
826
827         if (buf[1] & TRANS_ERROR) {
828                 av7110_ipack_reset(ipack);
829                 return -1;
830         }
831
832         if (!(buf[3] & PAYLOAD))
833                 return -1;
834
835         if (buf[1] & PAY_START)
836                 av7110_ipack_flush(ipack);
837
838         if (buf[3] & ADAPT_FIELD) {
839                 len -= buf[4] + 1;
840                 buf += buf[4] + 1;
841                 if (!len)
842                         return 0;
843         }
844
845         av7110_ipack_instant_repack(buf + 4, len - 4, ipack);
846         return 0;
847 }
848
849
850 int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
851 {
852         struct dvb_demux *demux = feed->demux;
853         struct av7110 *av7110 = (struct av7110 *) demux->priv;
854
855         dprintk(2, "av7110:%p, \n", av7110);
856
857         if (av7110->full_ts && demux->dmx.frontend->source != DMX_MEMORY_FE)
858                 return 0;
859
860         switch (feed->pes_type) {
861         case 0:
862                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
863                         return -EINVAL;
864                 break;
865         case 1:
866                 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
867                         return -EINVAL;
868                 break;
869         default:
870                 return -1;
871         }
872
873         return write_ts_to_decoder(av7110, feed->pes_type, buf, len);
874 }
875
876
877
878 /******************************************************************************
879  * Video MPEG decoder events
880  ******************************************************************************/
881 void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
882 {
883         struct dvb_video_events *events = &av7110->video_events;
884         int wp;
885
886         spin_lock_bh(&events->lock);
887
888         wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
889         if (wp == events->eventr) {
890                 events->overflow = 1;
891                 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
892         }
893
894         //FIXME: timestamp?
895         memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
896         events->eventw = wp;
897
898         spin_unlock_bh(&events->lock);
899
900         wake_up_interruptible(&events->wait_queue);
901 }
902
903
904 static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
905 {
906         struct dvb_video_events *events = &av7110->video_events;
907
908         if (events->overflow) {
909                 events->overflow = 0;
910                 return -EOVERFLOW;
911         }
912         if (events->eventw == events->eventr) {
913                 int ret;
914
915                 if (flags & O_NONBLOCK)
916                         return -EWOULDBLOCK;
917
918                 ret = wait_event_interruptible(events->wait_queue,
919                                                events->eventw != events->eventr);
920                 if (ret < 0)
921                         return ret;
922         }
923
924         spin_lock_bh(&events->lock);
925
926         memcpy(event, &events->events[events->eventr],
927                sizeof(struct video_event));
928         events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
929
930         spin_unlock_bh(&events->lock);
931
932         return 0;
933 }
934
935
936 /******************************************************************************
937  * DVB device file operations
938  ******************************************************************************/
939
940 static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
941 {
942         struct dvb_device *dvbdev = file->private_data;
943         struct av7110 *av7110 = dvbdev->priv;
944         unsigned int mask = 0;
945
946         dprintk(2, "av7110:%p, \n", av7110);
947
948         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
949                 poll_wait(file, &av7110->avout.queue, wait);
950
951         poll_wait(file, &av7110->video_events.wait_queue, wait);
952
953         if (av7110->video_events.eventw != av7110->video_events.eventr)
954                 mask = POLLPRI;
955
956         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
957                 if (av7110->playing) {
958                         if (FREE_COND)
959                                 mask |= (POLLOUT | POLLWRNORM);
960                         } else /* if not playing: may play if asked for */
961                                 mask |= (POLLOUT | POLLWRNORM);
962         }
963
964         return mask;
965 }
966
967 static ssize_t dvb_video_write(struct file *file, const char __user *buf,
968                                size_t count, loff_t *ppos)
969 {
970         struct dvb_device *dvbdev = file->private_data;
971         struct av7110 *av7110 = dvbdev->priv;
972         unsigned char c;
973
974         dprintk(2, "av7110:%p, \n", av7110);
975
976         if ((file->f_flags & O_ACCMODE) == O_RDONLY)
977                 return -EPERM;
978
979         if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
980                 return -EPERM;
981
982         if (get_user(c, buf))
983                 return -EFAULT;
984         if (c == 0x47 && count % TS_SIZE == 0)
985                 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
986         else
987                 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
988 }
989
990 static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
991 {
992         struct dvb_device *dvbdev = file->private_data;
993         struct av7110 *av7110 = dvbdev->priv;
994         unsigned int mask = 0;
995
996         dprintk(2, "av7110:%p, \n", av7110);
997
998         poll_wait(file, &av7110->aout.queue, wait);
999
1000         if (av7110->playing) {
1001                 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
1002                         mask |= (POLLOUT | POLLWRNORM);
1003         } else /* if not playing: may play if asked for */
1004                 mask = (POLLOUT | POLLWRNORM);
1005
1006         return mask;
1007 }
1008
1009 static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
1010                                size_t count, loff_t *ppos)
1011 {
1012         struct dvb_device *dvbdev = file->private_data;
1013         struct av7110 *av7110 = dvbdev->priv;
1014         unsigned char c;
1015
1016         dprintk(2, "av7110:%p, \n", av7110);
1017
1018         if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
1019                 printk(KERN_ERR "not audio source memory\n");
1020                 return -EPERM;
1021         }
1022
1023         if (get_user(c, buf))
1024                 return -EFAULT;
1025         if (c == 0x47 && count % TS_SIZE == 0)
1026                 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1027         else
1028                 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1029 }
1030
1031 static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1032
1033 #define MIN_IFRAME 400000
1034
1035 static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
1036 {
1037         unsigned i, n;
1038         int progressive = 0;
1039         int match = 0;
1040
1041         dprintk(2, "av7110:%p, \n", av7110);
1042
1043         if (!(av7110->playing & RP_VIDEO)) {
1044                 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
1045                         return -EBUSY;
1046         }
1047
1048         /* search in buf for instances of 00 00 01 b5 1? */
1049         for (i = 0; i < len; i++) {
1050                 unsigned char c;
1051                 if (get_user(c, buf + i))
1052                         return -EFAULT;
1053                 if (match == 5) {
1054                         progressive = c & 0x08;
1055                         match = 0;
1056                 }
1057                 if (c == 0x00) {
1058                         match = (match == 1 || match == 2) ? 2 : 1;
1059                         continue;
1060                 }
1061                 switch (match++) {
1062                 case 2: if (c == 0x01)
1063                                 continue;
1064                         break;
1065                 case 3: if (c == 0xb5)
1066                                 continue;
1067                         break;
1068                 case 4: if ((c & 0xf0) == 0x10)
1069                                 continue;
1070                         break;
1071                 }
1072                 match = 0;
1073         }
1074
1075         /* setting n always > 1, fixes problems when playing stillframes
1076            consisting of I- and P-Frames */
1077         n = MIN_IFRAME / len + 1;
1078
1079         /* FIXME: nonblock? */
1080         dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
1081
1082         for (i = 0; i < n; i++)
1083                 dvb_play(av7110, buf, len, 0, 1);
1084
1085         av7110_ipack_flush(&av7110->ipack[1]);
1086
1087         if (progressive)
1088                 return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1089         else
1090                 return 0;
1091 }
1092
1093
1094 static int dvb_video_ioctl(struct inode *inode, struct file *file,
1095                            unsigned int cmd, void *parg)
1096 {
1097         struct dvb_device *dvbdev = file->private_data;
1098         struct av7110 *av7110 = dvbdev->priv;
1099         unsigned long arg = (unsigned long) parg;
1100         int ret = 0;
1101
1102         dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1103
1104         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1105                 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1106                      cmd != VIDEO_GET_SIZE ) {
1107                         return -EPERM;
1108                 }
1109         }
1110
1111         switch (cmd) {
1112         case VIDEO_STOP:
1113                 av7110->videostate.play_state = VIDEO_STOPPED;
1114                 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
1115                         ret = av7110_av_stop(av7110, RP_VIDEO);
1116                 else
1117                         ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
1118                                av7110->videostate.video_blank ? 0 : 1);
1119                 if (!ret)
1120                         av7110->trickmode = TRICK_NONE;
1121                 break;
1122
1123         case VIDEO_PLAY:
1124                 av7110->trickmode = TRICK_NONE;
1125                 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1126                         av7110->videostate.play_state = VIDEO_PLAYING;
1127                         ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1128                         if (ret)
1129                                 break;
1130                 }
1131
1132                 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1133                         if (av7110->playing == RP_AV) {
1134                                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1135                                 if (ret)
1136                                         break;
1137                                 av7110->playing &= ~RP_VIDEO;
1138                         }
1139                         ret = av7110_av_start_play(av7110, RP_VIDEO);
1140                 }
1141                 if (!ret)
1142                         ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1143                 if (!ret)
1144                         av7110->videostate.play_state = VIDEO_PLAYING;
1145                 break;
1146
1147         case VIDEO_FREEZE:
1148                 av7110->videostate.play_state = VIDEO_FREEZED;
1149                 if (av7110->playing & RP_VIDEO)
1150                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
1151                 else
1152                         ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1153                 if (!ret)
1154                         av7110->trickmode = TRICK_FREEZE;
1155                 break;
1156
1157         case VIDEO_CONTINUE:
1158                 if (av7110->playing & RP_VIDEO)
1159                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1160                 if (!ret)
1161                         ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1162                 if (!ret) {
1163                         av7110->videostate.play_state = VIDEO_PLAYING;
1164                         av7110->trickmode = TRICK_NONE;
1165                 }
1166                 break;
1167
1168         case VIDEO_SELECT_SOURCE:
1169                 av7110->videostate.stream_source = (video_stream_source_t) arg;
1170                 break;
1171
1172         case VIDEO_SET_BLANK:
1173                 av7110->videostate.video_blank = (int) arg;
1174                 break;
1175
1176         case VIDEO_GET_STATUS:
1177                 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1178                 break;
1179
1180         case VIDEO_GET_EVENT:
1181                 ret = dvb_video_get_event(av7110, parg, file->f_flags);
1182                 break;
1183
1184         case VIDEO_GET_SIZE:
1185                 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1186                 break;
1187
1188         case VIDEO_SET_DISPLAY_FORMAT:
1189         {
1190                 video_displayformat_t format = (video_displayformat_t) arg;
1191
1192                 switch (format) {
1193                 case VIDEO_PAN_SCAN:
1194                         av7110->display_panscan = VID_PAN_SCAN_PREF;
1195                         break;
1196
1197                 case VIDEO_LETTER_BOX:
1198                         av7110->display_panscan = VID_VC_AND_PS_PREF;
1199                         break;
1200
1201                 case VIDEO_CENTER_CUT_OUT:
1202                         av7110->display_panscan = VID_CENTRE_CUT_PREF;
1203                         break;
1204
1205                 default:
1206                         ret = -EINVAL;
1207                 }
1208                 if (ret < 0)
1209                         break;
1210                 av7110->videostate.display_format = format;
1211                 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
1212                                     1, av7110->display_panscan);
1213                 break;
1214         }
1215
1216         case VIDEO_SET_FORMAT:
1217                 if (arg > 1) {
1218                         ret = -EINVAL;
1219                         break;
1220                 }
1221                 av7110->display_ar = arg;
1222                 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1223                                     1, (u16) arg);
1224                 break;
1225
1226         case VIDEO_STILLPICTURE:
1227         {
1228                 struct video_still_picture *pic =
1229                         (struct video_still_picture *) parg;
1230                 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1231                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1232                 ret = play_iframe(av7110, pic->iFrame, pic->size,
1233                                   file->f_flags & O_NONBLOCK);
1234                 break;
1235         }
1236
1237         case VIDEO_FAST_FORWARD:
1238                 //note: arg is ignored by firmware
1239                 if (av7110->playing & RP_VIDEO)
1240                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1241                                             __Scan_I, 2, AV_PES, 0);
1242                 else
1243                         ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
1244                 if (!ret) {
1245                         av7110->trickmode = TRICK_FAST;
1246                         av7110->videostate.play_state = VIDEO_PLAYING;
1247                 }
1248                 break;
1249
1250         case VIDEO_SLOWMOTION:
1251                 if (av7110->playing&RP_VIDEO) {
1252                         if (av7110->trickmode != TRICK_SLOW)
1253                                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
1254                         if (!ret)
1255                                 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1256                 } else {
1257                         ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
1258                         if (!ret)
1259                                 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
1260                         if (!ret)
1261                                 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1262                 }
1263                 if (!ret) {
1264                         av7110->trickmode = TRICK_SLOW;
1265                         av7110->videostate.play_state = VIDEO_PLAYING;
1266                 }
1267                 break;
1268
1269         case VIDEO_GET_CAPABILITIES:
1270                 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1271                         VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1272                 break;
1273
1274         case VIDEO_CLEAR_BUFFER:
1275                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1276                 av7110_ipack_reset(&av7110->ipack[1]);
1277
1278                 if (av7110->playing == RP_AV) {
1279                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1280                                             __Play, 2, AV_PES, 0);
1281                         if (ret)
1282                                 break;
1283                         if (av7110->trickmode == TRICK_FAST)
1284                                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1285                                                     __Scan_I, 2, AV_PES, 0);
1286                         if (av7110->trickmode == TRICK_SLOW) {
1287                                 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1288                                                     __Slow, 2, 0, 0);
1289                                 if (!ret)
1290                                         ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
1291                         }
1292                         if (av7110->trickmode == TRICK_FREEZE)
1293                                 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
1294                 }
1295                 break;
1296
1297         case VIDEO_SET_STREAMTYPE:
1298
1299                 break;
1300
1301         default:
1302                 ret = -ENOIOCTLCMD;
1303                 break;
1304         }
1305         return ret;
1306 }
1307
1308 static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1309                            unsigned int cmd, void *parg)
1310 {
1311         struct dvb_device *dvbdev = file->private_data;
1312         struct av7110 *av7110 = dvbdev->priv;
1313         unsigned long arg = (unsigned long) parg;
1314         int ret = 0;
1315
1316         dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
1317
1318         if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1319             (cmd != AUDIO_GET_STATUS))
1320                 return -EPERM;
1321
1322         switch (cmd) {
1323         case AUDIO_STOP:
1324                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1325                         ret = av7110_av_stop(av7110, RP_AUDIO);
1326                 else
1327                         ret = audcom(av7110, AUDIO_CMD_MUTE);
1328                 if (!ret)
1329                         av7110->audiostate.play_state = AUDIO_STOPPED;
1330                 break;
1331
1332         case AUDIO_PLAY:
1333                 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
1334                         ret = av7110_av_start_play(av7110, RP_AUDIO);
1335                 if (!ret)
1336                         ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1337                 if (!ret)
1338                         av7110->audiostate.play_state = AUDIO_PLAYING;
1339                 break;
1340
1341         case AUDIO_PAUSE:
1342                 ret = audcom(av7110, AUDIO_CMD_MUTE);
1343                 if (!ret)
1344                         av7110->audiostate.play_state = AUDIO_PAUSED;
1345                 break;
1346
1347         case AUDIO_CONTINUE:
1348                 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1349                         av7110->audiostate.play_state = AUDIO_PLAYING;
1350                         ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
1351                 }
1352                 break;
1353
1354         case AUDIO_SELECT_SOURCE:
1355                 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1356                 break;
1357
1358         case AUDIO_SET_MUTE:
1359         {
1360                 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1361                 if (!ret)
1362                         av7110->audiostate.mute_state = (int) arg;
1363                 break;
1364         }
1365
1366         case AUDIO_SET_AV_SYNC:
1367                 av7110->audiostate.AV_sync_state = (int) arg;
1368                 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
1369                 break;
1370
1371         case AUDIO_SET_BYPASS_MODE:
1372                 if (FW_VERSION(av7110->arm_app) < 0x2621)
1373                         ret = -EINVAL;
1374                 av7110->audiostate.bypass_mode = (int)arg;
1375                 break;
1376
1377         case AUDIO_CHANNEL_SELECT:
1378                 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
1379
1380                 switch(av7110->audiostate.channel_select) {
1381                 case AUDIO_STEREO:
1382                         ret = audcom(av7110, AUDIO_CMD_STEREO);
1383                         if (!ret) {
1384                                 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1385                                         i2c_writereg(av7110, 0x20, 0x02, 0x49);
1386                                 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1387                                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1388                         }
1389                         break;
1390
1391                 case AUDIO_MONO_LEFT:
1392                         ret = audcom(av7110, AUDIO_CMD_MONO_L);
1393                         if (!ret) {
1394                                 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1395                                         i2c_writereg(av7110, 0x20, 0x02, 0x4a);
1396                                 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1397                                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1398                         }
1399                         break;
1400
1401                 case AUDIO_MONO_RIGHT:
1402                         ret = audcom(av7110, AUDIO_CMD_MONO_R);
1403                         if (!ret) {
1404                                 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1405                                         i2c_writereg(av7110, 0x20, 0x02, 0x45);
1406                                 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1407                                         msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1408                         }
1409                         break;
1410
1411                 default:
1412                         ret = -EINVAL;
1413                         break;
1414                 }
1415                 break;
1416
1417         case AUDIO_GET_STATUS:
1418                 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1419                 break;
1420
1421         case AUDIO_GET_CAPABILITIES:
1422                 if (FW_VERSION(av7110->arm_app) < 0x2621)
1423                         *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1424                 else
1425                         *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1426                                                 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1427                 break;
1428
1429         case AUDIO_CLEAR_BUFFER:
1430                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1431                 av7110_ipack_reset(&av7110->ipack[0]);
1432                 if (av7110->playing == RP_AV)
1433                         ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
1434                                             __Play, 2, AV_PES, 0);
1435                 break;
1436         case AUDIO_SET_ID:
1437
1438                 break;
1439         case AUDIO_SET_MIXER:
1440         {
1441                 struct audio_mixer *amix = (struct audio_mixer *)parg;
1442
1443                 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
1444                 break;
1445         }
1446         case AUDIO_SET_STREAMTYPE:
1447                 break;
1448         default:
1449                 ret = -ENOIOCTLCMD;
1450         }
1451         return ret;
1452 }
1453
1454
1455 static int dvb_video_open(struct inode *inode, struct file *file)
1456 {
1457         struct dvb_device *dvbdev = file->private_data;
1458         struct av7110 *av7110 = dvbdev->priv;
1459         int err;
1460
1461         dprintk(2, "av7110:%p, \n", av7110);
1462
1463         if ((err = dvb_generic_open(inode, file)) < 0)
1464                 return err;
1465
1466         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1467                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1468                 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1469                 av7110->video_blank = 1;
1470                 av7110->audiostate.AV_sync_state = 1;
1471                 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1472
1473                 /*  empty event queue */
1474                 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1475         }
1476
1477         return 0;
1478 }
1479
1480 static int dvb_video_release(struct inode *inode, struct file *file)
1481 {
1482         struct dvb_device *dvbdev = file->private_data;
1483         struct av7110 *av7110 = dvbdev->priv;
1484
1485         dprintk(2, "av7110:%p, \n", av7110);
1486
1487         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1488                 av7110_av_stop(av7110, RP_VIDEO);
1489         }
1490
1491         return dvb_generic_release(inode, file);
1492 }
1493
1494 static int dvb_audio_open(struct inode *inode, struct file *file)
1495 {
1496         struct dvb_device *dvbdev = file->private_data;
1497         struct av7110 *av7110 = dvbdev->priv;
1498         int err = dvb_generic_open(inode, file);
1499
1500         dprintk(2, "av7110:%p, \n", av7110);
1501
1502         if (err < 0)
1503                 return err;
1504         dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1505         av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1506         return 0;
1507 }
1508
1509 static int dvb_audio_release(struct inode *inode, struct file *file)
1510 {
1511         struct dvb_device *dvbdev = file->private_data;
1512         struct av7110 *av7110 = dvbdev->priv;
1513
1514         dprintk(2, "av7110:%p, \n", av7110);
1515
1516         av7110_av_stop(av7110, RP_AUDIO);
1517         return dvb_generic_release(inode, file);
1518 }
1519
1520
1521
1522 /******************************************************************************
1523  * driver registration
1524  ******************************************************************************/
1525
1526 static const struct file_operations dvb_video_fops = {
1527         .owner          = THIS_MODULE,
1528         .write          = dvb_video_write,
1529         .ioctl          = dvb_generic_ioctl,
1530         .open           = dvb_video_open,
1531         .release        = dvb_video_release,
1532         .poll           = dvb_video_poll,
1533 };
1534
1535 static struct dvb_device dvbdev_video = {
1536         .priv           = NULL,
1537         .users          = 6,
1538         .readers        = 5,    /* arbitrary */
1539         .writers        = 1,
1540         .fops           = &dvb_video_fops,
1541         .kernel_ioctl   = dvb_video_ioctl,
1542 };
1543
1544 static const struct file_operations dvb_audio_fops = {
1545         .owner          = THIS_MODULE,
1546         .write          = dvb_audio_write,
1547         .ioctl          = dvb_generic_ioctl,
1548         .open           = dvb_audio_open,
1549         .release        = dvb_audio_release,
1550         .poll           = dvb_audio_poll,
1551 };
1552
1553 static struct dvb_device dvbdev_audio = {
1554         .priv           = NULL,
1555         .users          = 1,
1556         .writers        = 1,
1557         .fops           = &dvb_audio_fops,
1558         .kernel_ioctl   = dvb_audio_ioctl,
1559 };
1560
1561
1562 int av7110_av_register(struct av7110 *av7110)
1563 {
1564         av7110->audiostate.AV_sync_state = 0;
1565         av7110->audiostate.mute_state = 0;
1566         av7110->audiostate.play_state = AUDIO_STOPPED;
1567         av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1568         av7110->audiostate.channel_select = AUDIO_STEREO;
1569         av7110->audiostate.bypass_mode = 0;
1570
1571         av7110->videostate.video_blank = 0;
1572         av7110->videostate.play_state = VIDEO_STOPPED;
1573         av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1574         av7110->videostate.video_format = VIDEO_FORMAT_4_3;
1575         av7110->videostate.display_format = VIDEO_LETTER_BOX;
1576         av7110->display_ar = VIDEO_FORMAT_4_3;
1577         av7110->display_panscan = VID_VC_AND_PS_PREF;
1578
1579         init_waitqueue_head(&av7110->video_events.wait_queue);
1580         spin_lock_init(&av7110->video_events.lock);
1581         av7110->video_events.eventw = av7110->video_events.eventr = 0;
1582         av7110->video_events.overflow = 0;
1583         memset(&av7110->video_size, 0, sizeof (video_size_t));
1584
1585         dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
1586                             &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1587
1588         dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
1589                             &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1590
1591         return 0;
1592 }
1593
1594 void av7110_av_unregister(struct av7110 *av7110)
1595 {
1596         dvb_unregister_device(av7110->audio_dev);
1597         dvb_unregister_device(av7110->video_dev);
1598 }
1599
1600 int av7110_av_init(struct av7110 *av7110)
1601 {
1602         void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1603         int i, ret;
1604
1605         for (i = 0; i < 2; i++) {
1606                 struct ipack *ipack = av7110->ipack + i;
1607
1608                 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1609                 if (ret < 0) {
1610                         if (i)
1611                                 av7110_ipack_free(--ipack);
1612                         goto out;
1613                 }
1614                 ipack->data = av7110;
1615         }
1616
1617         dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1618         dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1619
1620         av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1621         av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1622 out:
1623         return ret;
1624 }
1625
1626 void av7110_av_exit(struct av7110 *av7110)
1627 {
1628         av7110_ipack_free(&av7110->ipack[0]);
1629         av7110_ipack_free(&av7110->ipack[1]);
1630 }