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